From a0835a2d22acdec0eaa531cd67cd0df80e323b99 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Wed, 12 Jun 2013 18:44:26 +0200 Subject: [PATCH] [XVSM] Some fixes for making sure that Cook only prepares normal pizzas when there are no deliveries... --- erkenntnisse | 4 + runXVSM.sh | 14 +- .../tuwien/sbc/valesriegler/common/Util.java | 7 +- .../sbc/valesriegler/group/GroupAgent.java | 4 +- .../valesriegler/pizzeria/PizzeriaAgent.java | 2 +- .../tuwien/sbc/valesriegler/types/Pizza.java | 8 +- .../sbc/valesriegler/types/PizzaOrder.java | 13 +- .../xvsm/AbstractXVSMConnector.java | 16 +- .../sbc/valesriegler/xvsm/CookXVSM.java | 194 +++++++++++------- .../valesriegler/xvsm/DeliveryGroupXVSM.java | 2 +- .../sbc/valesriegler/xvsm/DriverXVSM.java | 5 +- .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 6 +- .../sbc/valesriegler/xvsm/GroupXVSM.java | 7 +- .../sbc/valesriegler/xvsm/WaiterXVSM.java | 27 ++- 14 files changed, 191 insertions(+), 118 deletions(-) create mode 100644 erkenntnisse diff --git a/erkenntnisse b/erkenntnisse new file mode 100644 index 0000000..a3fa1f5 --- /dev/null +++ b/erkenntnisse @@ -0,0 +1,4 @@ +XVSM: +Ableitung scheisse: Pizza<->PizzaOrder wegen Template-matching +DELETE-LOCK -> Congestion +keine primitiven datentypen für template-matching \ No newline at end of file diff --git a/runXVSM.sh b/runXVSM.sh index 97aab33..2916ed5 100755 --- a/runXVSM.sh +++ b/runXVSM.sh @@ -1,15 +1,15 @@ #!/bin/bash mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="1 9875" & -#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="2 9875" & +mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="2 9875" & mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="1 9874" & -#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="2 9874" & +mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="2 9874" & mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="1 9875" & -#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="2 9875" & +mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="2 9875" & mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="1 9874" & -#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="2 9874" & +mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="2 9874" & mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.driver.Driver" -Dexec.args="1 9875" & -#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.driver.Driver" -Dexec.args="2 9875" & +mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.driver.Driver" -Dexec.args="2 9875" & mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.driver.Driver" -Dexec.args="1 9874" & -#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.driver.Driver" -Dexec.args="2 9874" & +mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.driver.Driver" -Dexec.args="2 9874" & mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent" -Dexec.args="XVSM 9875" & -#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent" -Dexec.args="XVSM 9874" & +mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent" -Dexec.args="XVSM 9874" & diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java index ff93d97..1ba0576 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java @@ -64,7 +64,7 @@ public abstract class Util { public static final String DELIVERY_PROGRESS = "deliveryInProgress"; public static final String DELIVERY_DONE = "deliveryDone"; - public static final boolean runSimulation = false; + public static final boolean runSimulation = true; public static final String NUMBER_DISPLAY_FORMAT = String.format("%%0%dd", 3); @@ -137,7 +137,6 @@ public abstract class Util { wrapperPanel.setBorder(new TitledBorder(title)); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); wrapperPanel.add(scrollPane, c); - // table.setAutoCreateRowSorter(true); return table; } @@ -183,9 +182,7 @@ public abstract class Util { List pizzaOrders = new ArrayList<>(); for (PizzaType pt : gc.pizzaTypes) { final PizzaOrder pizzaOrder = new PizzaOrder(pt); - if(isDelivery) { - pizzaOrder.setDeliveryPizza(true); - } + pizzaOrder.setDeliveryPizza(isDelivery); pizzaOrders.add(pizzaOrder); } Order order = new Order(group, pizzaOrders); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java index 28e1b7a..67f1778 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java @@ -74,7 +74,7 @@ public class GroupAgent { } groupAgent = new GroupAgent(); - if (true || !Util.runSimulation) { + if (!Util.runSimulation) { groupGui = new GroupGUI(); SwingUtilities.invokeLater(groupGui); } else if (Util.useJMS) { @@ -144,7 +144,7 @@ public class GroupAgent { for (int i = 0; i < gc.numberOfGroups; i++) { DeliveryGroup group = new DeliveryGroup(); - final Order order = Util.createOrder(group, gc, false); + final Order order = Util.createOrder(group, gc, true); group.getDeliveryGroupData().setOrder(order); group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria); group.getDeliveryGroupData().setAddress(gc.address); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java index e651ffb..abf6917 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java @@ -53,7 +53,7 @@ public class PizzeriaAgent { case "XVSM" : pizzeriaAgent.createModels(); pizzeriaAgent.initXVSM(args[1]); - if (true || !Util.runSimulation) { + if (!Util.runSimulation) { pizzeriaAgent.initGUI(); } Util.useJMS = false; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java index ffc7a47..1c8f729 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java @@ -1,10 +1,10 @@ package at.ac.tuwien.sbc.valesriegler.types; -import java.io.Serializable; - import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; +import java.io.Serializable; + /** * this is a PIZZA! omnomnomnom * @@ -13,7 +13,7 @@ import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; */ public class Pizza extends PizzaOrder implements Serializable { public JMSWaiter deliveryAgent; - protected boolean isDeliveryPizza; + protected Boolean isDeliveryPizza; /** * Yes that's funny! the pizza has a field idOfOrder although PizzaOrder @@ -68,7 +68,7 @@ public class Pizza extends PizzaOrder implements Serializable { } public void setDeliveryPizza(boolean deliveryPizza) { - isDeliveryPizza = deliveryPizza; + this.isDeliveryPizza = deliveryPizza; } public boolean isDeliveryPizza() { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java index d58ca02..96cf070 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java @@ -11,7 +11,7 @@ import java.io.Serializable; * @author jan * */ -public class PizzaOrder implements Serializable, HasId { +public class PizzaOrder implements Serializable, HasId, Comparable { private static int nextID = 0; protected Integer id; @@ -38,7 +38,7 @@ public class PizzaOrder implements Serializable, HasId { return pizzaType; } - public Boolean getDeliveryPizza() { + public boolean getDeliveryPizza() { return isDeliveryPizza; } @@ -51,7 +51,7 @@ public class PizzaOrder implements Serializable, HasId { } public int getId() { - return id; + return Util.getIntSafe(id); } public int getCookId() { @@ -90,4 +90,11 @@ public class PizzaOrder implements Serializable, HasId { + pizzaType + ", status=" + status + "]"; } + @Override + public int compareTo(PizzaOrder o) { + final int id2 = o.orderId; + if (orderId < id2) return -1; + else if (orderId > id2) return 1; + return 0; + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java index ffe5e7e..97ccc18 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java @@ -179,10 +179,10 @@ public abstract class AbstractXVSMConnector { * Searches for all entities matching the given template object by linda selection and reads them. */ protected List readMatchingEntities( - T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg) + T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg, int count) throws MzsCoreException { try { - LindaSelector sel = LindaCoordinator.newSelector(entity, MzsConstants.Selecting.COUNT_MAX); + LindaSelector sel = LindaCoordinator.newSelector(entity, count); ArrayList entities = capi.read(ref, sel, timeout, tx); @@ -197,6 +197,13 @@ public abstract class AbstractXVSMConnector { throw new EntityNotFoundByTemplate(errorMsg); } } + + protected List readMatchingEntity( + T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg) + throws MzsCoreException { + return readMatchingEntities(entity, ref, tx, timeout, errorMsg, 1); + } + protected List castEntries(List entries) { List newList = new ArrayList(); if (entries.size() == 0) return newList; @@ -236,6 +243,11 @@ public abstract class AbstractXVSMConnector { URI.create(String.format(Util.SERVER_ADDR, port))); } + protected SpaceListenerImplBuilder getDefaultBuilder() { + return new SpaceListenerImplBuilder().setCapi(capi).setNotificationManager(notificationMgr); + } + + protected SpaceListenerImplBuilder getDefaultBuilder(String name) { return new SpaceListenerImplBuilder().setCapi(capi).setName(name).setNotificationManager(notificationMgr); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java index 5842b46..13c6da7 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java @@ -3,8 +3,11 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.*; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; +import org.mozartspaces.capi3.EntryLockedException; +import org.mozartspaces.core.MzsConstants; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.MzsCoreException; +import org.mozartspaces.core.RequestContext; import org.mozartspaces.core.TransactionReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,7 +22,6 @@ public class CookXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(CookXVSM.class); private int cookId; - public CookXVSM(int id, int port) { super(port); @@ -36,58 +38,67 @@ public class CookXVSM extends AbstractXVSMConnector { } public void listenForPizzas() { - getDefaultBuilder("").setLookaround(true).setCref(preparePizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForNormalPizzas").setLookaround(true).setCref(preparePizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { - @Override - public void onEntriesWritten(List entries) - throws Exception { + @Override + public void onEntriesWritten(List entries) + throws Exception { - List pizzas = castEntries(entries); + List pizzas = castEntries(entries); - if (inNotification.get()) Collections.shuffle(pizzas); + sortOrShufflePizzas(pizzas, inNotification.get()); - for (PizzaOrder pizzaOrder : pizzas) { + for (PizzaOrder pizzaOrder : pizzas) { - TransactionReference tx = capi.createTransaction(9000, URI.create(String.format(Util.SERVER_ADDR, port))); - String pizzaAlreadyCooked = String.format("Normal pizza with id %d has already been cooked by another cook", pizzaOrder.getId()); + TransactionReference tx = capi.createTransaction(9000, URI.create(String.format(Util.SERVER_ADDR, port))); + String pizzaAlreadyCooked = String.format("Normal pizza with id %d has already been cooked by another cook", pizzaOrder.getId()); - try { - if (!mayPrepareCustomerPizza(pizzaOrder, tx)) { - capi.rollbackTransaction(tx); - continue; - } + try { + if (!mayPrepareCustomerPizza(pizzaOrder, tx)) { + capi.rollbackTransaction(tx); + continue; + } - // Require the lock for preparing the pizza - PizzaOrder order = takeMatchingEntity(new PizzaOrder(pizzaOrder.getId()), preparePizzasContainer, tx, RequestTimeout.ZERO, pizzaAlreadyCooked); + // Require the lock for preparing the pizza + PizzaOrder order = takeMatchingEntity(new PizzaOrder(pizzaOrder.getId()), preparePizzasContainer, tx, RequestTimeout.ZERO, pizzaAlreadyCooked); - // tell the space that you prepare the pizza -> without a transaction!! - Pizza pizzaInProgress = Pizza.createPizzaFromPizzaOrder(order, cookId, true); - pizzaInProgress.setStatus(PizzaOrderStatus.IN_PREPARATION); + // tell the space that you prepare the pizza -> without a transaction!! + Pizza pizzaInProgress = Pizza.createPizzaFromPizzaOrder(order, cookId, true); + pizzaInProgress.setStatus(PizzaOrderStatus.IN_PREPARATION); - notifyCustomerPizzaInProgress(order, pizzaInProgress); + notifyCustomerPizzaInProgress(order, pizzaInProgress); - PizzaOrder pizza = createPizza(order); + PizzaOrder pizza = createPizza(order); - notifyCustomerPizzaDone(pizza, tx); - capi.commitTransaction(tx); - log.debug("I have completed preparing a pizza for order {}!", pizza.getOrderId()); - } catch (NullPointerException e) { - // the strange nullpointer exception from the space - } catch (EntityNotFoundByTemplate e) { - log.info("entitynotfound: {}", e.getMessage()); - } catch (Exception e) { - log.error("outer cook"); - log.error(e.getMessage()); - } + notifyCustomerPizzaDone(pizza, tx); + capi.commitTransaction(tx); + log.debug("I have completed preparing a pizza for order {}!", pizza.getOrderId()); + } catch (NullPointerException e) { + // the strange nullpointer exception from the space + } catch (EntityNotFoundByTemplate e) { + log.info("entitynotfound: {}", e.getMessage()); + } catch (EntryLockedException e) { + capi.rollbackTransaction(tx); + } catch (Exception e) { + log.error("outer cook"); + log.error(e.getMessage()); + } - } - } - }).createSpaceListenerImpl(); + } + } + }).createSpaceListenerImpl(); } + private void sortOrShufflePizzas(List pizzas, boolean notification) { + if (notification) Collections.shuffle(pizzas); + else { + Collections.sort(pizzas); + } + } + public void listenForDeliveryPizzas() { - getDefaultBuilder("").setLookaround(true).setCref(prepareDeliveryPizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForDeliveryPizzas").setLookaround(true).setCref(prepareDeliveryPizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) @@ -95,15 +106,16 @@ public class CookXVSM extends AbstractXVSMConnector { List pizzas = castEntries(entries); - if (inNotification.get()) Collections.shuffle(pizzas); + sortOrShufflePizzas(pizzas, inNotification.get()); for (PizzaOrder pizzaOrder : pizzas) { + final RequestContext requestContext = new RequestContext(); TransactionReference tx = capi.createTransaction(12000, URI.create(String.format(Util.SERVER_ADDR, port))); String pizzaAlreadyCooked = String.format("Delivery Pizza with id %d has already been cooked by another cook", pizzaOrder.getId()); try { - if (!mayPrepareDeliveryPizza(tx)) { + if (!mayPrepareDeliveryPizza(tx, pizzaOrder)) { capi.rollbackTransaction(tx); continue; } @@ -123,7 +135,6 @@ public class CookXVSM extends AbstractXVSMConnector { log.debug("after creating!"); - sendItemsToContainer(Arrays.asList(pizza), preparedDeliveryPizzasContainer, RequestTimeout.DEFAULT, tx); log.debug("after prepareddeliverypizzascon"); @@ -137,7 +148,8 @@ public class CookXVSM extends AbstractXVSMConnector { // the strange nullpointer exception from the space } catch (EntityNotFoundByTemplate e) { log.info("entitynotfound: {}", e.getMessage()); - + } catch (EntryLockedException e) { + capi.rollbackTransaction(tx); } catch (Exception e) { log.error("outer cook"); log.error(e.getMessage()); @@ -154,30 +166,46 @@ public class CookXVSM extends AbstractXVSMConnector { * in preparation or has been done! */ private boolean mayPrepareCustomerPizza(PizzaOrder pizzaOrder, TransactionReference tx) throws MzsCoreException { - List deliveryPizzas = readMatchingEntities(new PizzaOrder(), prepareDeliveryPizzasContainer, tx, RequestTimeout.DEFAULT, "MAYPREPAREPIZZA: Cannot access prepareDeliveryPizzasContainer"); + boolean mayPrepare = false; + + List deliveryPizzas = readMatchingEntities(new PizzaOrder(), prepareDeliveryPizzasContainer, tx, RequestTimeout.DEFAULT, "mayPrepareCustomerPizza: Cannot access prepareDeliveryPizzasContainer", MzsConstants.Selecting.COUNT_MAX); if (!deliveryPizzas.isEmpty()) { - final PizzaOrder template = new PizzaOrder(); - template.setOrderId(pizzaOrder.getOrderId()); - final List pizzasAlreadyDone = readMatchingEntities(template, preparedPizzasContainer, tx, RequestTimeout.DEFAULT, "MAYPREPAREPIZZA: Cannot access preparedPizzasContainer"); - if(! pizzasAlreadyDone.isEmpty()) return true; - final List pizzasOfOrderInProgress = readMatchingEntities(template, pizzaInProgressContainer, tx, RequestTimeout.DEFAULT, "MAYPREPAREPIZZA:: Cannot access pizzaInProgressContainer"); - if (! pizzasOfOrderInProgress.isEmpty()) return true; - - return false; + Pizza pizza = new Pizza(); + pizza.setIdOfOrder(pizzaOrder.getOrderId()); + final List pizzasAlreadyDone = readMatchingEntities(pizza, preparedPizzasContainer, tx, RequestTimeout.DEFAULT, "mayPrepareCustomerPizza: Cannot access preparedPizzasContainer", MzsConstants.Selecting.COUNT_MAX); + if(! pizzasAlreadyDone.isEmpty()) mayPrepare = true; + else { + final List pizzasOfOrderInProgress = readMatchingEntities(pizza, pizzaInProgressContainer, tx, RequestTimeout.DEFAULT, "mayPrepareCustomerPizza: Cannot access pizzaInProgressContainer", MzsConstants.Selecting.COUNT_MAX); + if (! pizzasOfOrderInProgress.isEmpty()) mayPrepare = true; + else mayPrepare = false; + } } - else return true; + else mayPrepare = true; + log.warn("Cook may prepare Customer pizza: {} for order {}", mayPrepare, pizzaOrder.getOrderId()); + return mayPrepare; } - private boolean mayPrepareDeliveryPizza(TransactionReference tx) throws MzsCoreException { - final PizzaOrder template = new PizzaOrder(); - template.setDeliveryPizza(false); - final List preparedNonDeliveryPizzas = readMatchingEntities(template, preparedPizzasContainer, tx, RequestTimeout.DEFAULT, "mayPrepareDeliveryPizza: cannot access preparedPizzasContainer"); - if(! preparedNonDeliveryPizzas.isEmpty()) return false; - final List nonDeliveryPizzasInProgress = readMatchingEntities(template, pizzaInProgressContainer, tx, RequestTimeout.DEFAULT, "mayPrepareDeliveryPizza: cannot access pizzaInProgressContainer"); - if (! nonDeliveryPizzasInProgress.isEmpty()) return false; - - return true; + /** + * A cook may prepare a delivery pizza if no customer group order is open + */ + private boolean mayPrepareDeliveryPizza(TransactionReference tx, PizzaOrder pizzaOrder) throws MzsCoreException { + boolean mayPrepare = false; + + Pizza pizza = new Pizza(); + pizza.setDeliveryPizza(false); + final List preparedNonDeliveryPizzas = readMatchingEntities(pizza, preparedPizzasContainer, tx, RequestTimeout.DEFAULT, "mayPrepareDeliveryPizza: cannot access preparedPizzasContainer", MzsConstants.Selecting.COUNT_MAX); + if(! preparedNonDeliveryPizzas.isEmpty()) { + log.warn("PreparedNonDeliveryPizzas NOT empty!"); + mayPrepare = false; + } + else { + final List nonDeliveryPizzasInProgress = readMatchingEntities(pizza, pizzaInProgressContainer, tx, RequestTimeout.DEFAULT, "mayPrepareDeliveryPizza: cannot access pizzaInProgressContainer", MzsConstants.Selecting.COUNT_MAX); + if (! nonDeliveryPizzasInProgress.isEmpty()) mayPrepare = false; + else mayPrepare = true; + } + log.warn("Cook may prepare Delivery pizza: {} for order {}", mayPrepare, pizzaOrder.getOrderId()); + return mayPrepare; } private void notifyCustomerPizzaDone(PizzaOrder pizza, TransactionReference tx) throws MzsCoreException { @@ -199,6 +227,7 @@ public class CookXVSM extends AbstractXVSMConnector { } private void notifyDeliveryPizzaDone(PizzaOrder pizza, TransactionReference tx) throws MzsCoreException { + sendItemsToContainer(Arrays.asList(pizza), preparedDeliveryPizzasContainer, RequestTimeout.DEFAULT, tx); final DeliveryGroupData group = new DeliveryGroupData(); final Order groupOrder = new Order(); groupOrder.setId(pizza.getOrderId()); @@ -216,30 +245,36 @@ public class CookXVSM extends AbstractXVSMConnector { } private void notifyDeliveryPizzaInProgress(PizzaOrder order, Pizza pizzaInProgress) throws MzsCoreException { - log.info("I say that I now prepare a delivery pizza for order {}", pizzaInProgress.getOrderId()); - final DeliveryGroupData groupTemplate = new DeliveryGroupData(); - final Order groupOrder = new Order(); - groupOrder.setId(order.getOrderId()); - groupTemplate.setOrder(groupOrder); - groupTemplate.setDeliveryStatus(null); - final TransactionReference inPreparationTx = getDefaultTransaction(); - final DeliveryGroupData groupFromSpace = takeMatchingEntity(groupTemplate, pizzeriaDeliveryContainer, inPreparationTx, RequestTimeout.DEFAULT, "Cannot take the delivery order from pizzeriaDeliveryContainer"); - final List orderedPizzas = groupFromSpace.getOrder().getOrderedPizzas(); - for (PizzaOrder orderedPizza : orderedPizzas) { - if (orderedPizza.getId() == pizzaInProgress.getId()) { - orderedPizza.setStatus(PizzaOrderStatus.IN_PREPARATION); - orderedPizza.setCookId(pizzaInProgress.getCookId()); + try { + log.info("I say that I now prepare a delivery pizza for order {}", pizzaInProgress.getOrderId()); + final DeliveryGroupData groupTemplate = new DeliveryGroupData(); + final Order groupOrder = new Order(); + groupOrder.setId(order.getOrderId()); + groupTemplate.setOrder(groupOrder); + groupTemplate.setDeliveryStatus(null); + final TransactionReference inPreparationTx = getDefaultTransaction(); + final DeliveryGroupData groupFromSpace = takeMatchingEntity(groupTemplate, pizzeriaDeliveryContainer, inPreparationTx, RequestTimeout.DEFAULT, "Cannot take the delivery order from pizzeriaDeliveryContainer"); + final List orderedPizzas = groupFromSpace.getOrder().getOrderedPizzas(); + for (PizzaOrder orderedPizza : orderedPizzas) { + if (orderedPizza.getId() == pizzaInProgress.getId()) { + orderedPizza.setStatus(PizzaOrderStatus.IN_PREPARATION); + orderedPizza.setCookId(pizzaInProgress.getCookId()); + } } + sendItemsToContainer(Arrays.asList(groupFromSpace), pizzeriaDeliveryContainer, RequestTimeout.DEFAULT, inPreparationTx); + sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, inPreparationTx); + log.debug("before inprogress comit!"); + capi.commitTransaction(inPreparationTx); + } catch(NullPointerException e) { + } catch (Exception e) { + log.error("notifyDeliveryPizzaInProgress: {}", e.getMessage()); + e.printStackTrace(); } - sendItemsToContainer(Arrays.asList(groupFromSpace), pizzeriaDeliveryContainer, RequestTimeout.DEFAULT, inPreparationTx); - sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, inPreparationTx); - log.debug("before inprogress comit!"); - capi.commitTransaction(inPreparationTx); } private void notifyCustomerPizzaInProgress(PizzaOrder order, Pizza pizzaInProgress) { try { - log.debug("I say that I now prepare a pizza for order {}", pizzaInProgress.getOrderId()); + log.info("I say that I now prepare a pizza for order {}", pizzaInProgress.getOrderId()); final GroupData groupTemplate = new GroupData(); final Order groupOrder = new Order(); groupOrder.setId(order.getOrderId()); @@ -257,8 +292,9 @@ public class CookXVSM extends AbstractXVSMConnector { sendItemsToContainer(Arrays.asList(groupFromSpace), pizzeriaGroupContainer, RequestTimeout.ZERO, inPreparationTx); sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.ZERO, inPreparationTx); capi.commitTransaction(inPreparationTx); + } catch(NullPointerException e) { } catch (Exception e) { - log.error("This error has occurred: {}", e.getMessage()); + log.error("notifyCustomerPizzaInProgress: {}", e.getMessage()); e.printStackTrace(); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DeliveryGroupXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DeliveryGroupXVSM.java index 4e400d4..6ffedd7 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DeliveryGroupXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DeliveryGroupXVSM.java @@ -35,7 +35,7 @@ public class DeliveryGroupXVSM extends AbstractXVSMConnector { } public void waitForMyOrder() { - getDefaultBuilder("").setCref(myContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(myContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java index 58aa409..5f6a23d 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java @@ -8,6 +8,7 @@ import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus; import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.Pizza; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; +import org.mozartspaces.capi3.EntryLockedException; import org.mozartspaces.core.ContainerReference; import org.mozartspaces.core.MzsConstants; import org.mozartspaces.core.MzsCoreException; @@ -64,7 +65,7 @@ public class DriverXVSM extends AbstractXVSMConnector { try { // Get lock for delivering this delivery order takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.ZERO, error); - final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, 2000, "Cannot get the delivery order!"); + final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, MzsConstants.RequestTimeout.DEFAULT, "Cannot get the delivery order!"); group.setDriverId(driverId); group.setDeliveryStatus(DeliveryStatus.IN_PROGRESS); @@ -99,6 +100,8 @@ public class DriverXVSM extends AbstractXVSMConnector { capi.commitTransaction(tx); + } catch(EntryLockedException e) { + capi.rollbackTransaction(tx); } catch (Exception e) { log.info("DRiverXVSM exception"); log.info(e.getMessage()); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java index 48fe3ca..841eb1a 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java @@ -30,7 +30,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } public void listenForNewPizzerias() { - getDefaultBuilder("groupAgentInfoContainer").setCref(groupAgentInfoContainer).setLookaround(false).setSpaceAction(new SpaceAction() { + getDefaultBuilder("groupAgentInfoContainer").setCref(groupAgentInfoContainer).setLookaround(true).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { @@ -66,7 +66,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } private void listenToDeliveryUpdates(int port) { - getDefaultBuilder("").setCref(useContainerOfSpaceWithPort(Util.PIZZERIA_DELIVERY, port)).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.PIZZERIA_DELIVERY, port)).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); @@ -92,7 +92,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } private void listenToGroupUpdates(int port) { - getDefaultBuilder("").setCref(useContainerOfSpaceWithPort(Util.PIZZERIA_GROUP, port)).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.PIZZERIA_GROUP, port)).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java index aa713b1..ba4e7b9 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java @@ -29,7 +29,7 @@ public class GroupXVSM extends AbstractXVSMConnector { } public void waitForMyOrder() { - getDefaultBuilder("").setCref(orderDeliveredContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(orderDeliveredContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); @@ -69,9 +69,10 @@ public class GroupXVSM extends AbstractXVSMConnector { notificationMgr.shutdown(); + } catch(NullPointerException e) { + // npe from the space } catch (Exception e) { - log.error("hab mich verschluckt!"); - e.printStackTrace(); + log.error("Customer group fail, groupId: {}", groupId); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java index d1cd241..80eb481 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java @@ -4,6 +4,7 @@ import at.ac.tuwien.sbc.valesriegler.common.OrderId; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.*; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; +import org.mozartspaces.capi3.EntryLockedException; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.MzsCoreException; import org.mozartspaces.core.TransactionReference; @@ -61,7 +62,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { final TransactionReference tx = getDefaultTransaction(); try { // Get the delete lock so that only one waiter can answer this particular phone call - takeMatchingEntity(template, phoneCallsContainer, tx, RequestTimeout.DEFAULT, errorMsg); + takeMatchingEntity(template, phoneCallsContainer, tx, RequestTimeout.ZERO, errorMsg); final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, RequestTimeout.DEFAULT, errorMsg); group.setWaiterIdOfOrder(WaiterXVSM.this.waiterId); @@ -81,6 +82,11 @@ public class WaiterXVSM extends AbstractXVSMConnector { capi.commitTransaction(tx); log.info("Waiter has taken a phone delivery call!"); + } catch (EntityNotFoundByTemplate e) { + log.debug("entitynotfound: {}", e.getMessage()); + } catch(EntryLockedException e) { + capi.rollbackTransaction(tx); + }catch(NullPointerException e) { } catch (Exception e) { e.printStackTrace(); } @@ -109,7 +115,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { Table tableTemplate = new Table(id); try { Table lockedFreeTable = takeMatchingEntity(tableTemplate, - freeTablesContainer, tx, RequestTimeout.DEFAULT, + freeTablesContainer, tx, RequestTimeout.ZERO, String.format("There was no free table found with id %d", id)); takeMatchingEntityIfItExists(tableTemplate, pizzeriaTableContainer, tx, RequestTimeout.DEFAULT); @@ -120,6 +126,8 @@ public class WaiterXVSM extends AbstractXVSMConnector { takeMatchingEntityIfItExists(lockedGroup, pizzeriaGroupContainer, tx, RequestTimeout.DEFAULT); assignGroupToTable(lockedGroup, lockedFreeTable, tx); + } catch(EntryLockedException e) { + capi.rollbackTransaction(tx); } catch (Exception e) { // log.info(e.getMessage()); } @@ -152,7 +160,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { GroupData lockedGroup = takeMatchingEntity( templateGroup, assignTableContainer, tx, - RequestTimeout.DEFAULT, groupNotFound); + RequestTimeout.ZERO, groupNotFound); takeMatchingEntityIfItExists(templateGroup, pizzeriaGroupContainer, tx, RequestTimeout.DEFAULT); // Acquire a lock for one free table in the // TablesContainer @@ -164,6 +172,8 @@ public class WaiterXVSM extends AbstractXVSMConnector { takeMatchingEntityIfItExists(lockedFreeTable, pizzeriaTableContainer, tx, RequestTimeout.DEFAULT); assignGroupToTable(lockedGroup, lockedFreeTable, tx); + } catch(EntryLockedException e) { + capi.rollbackTransaction(tx); } catch (Exception e) { // log.info(e.getMessage()); } @@ -192,7 +202,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { "The payment request for group %d was already taken by an other waiter!", groupData.getId()); try { - takeMatchingEntity(entity, paymentRequestContainer, tx, RequestTimeout.DEFAULT, paymentRequestTakenByOtherWaiter); + takeMatchingEntity(entity, paymentRequestContainer, tx, RequestTimeout.ZERO, paymentRequestTakenByOtherWaiter); GroupData group = takeMatchingEntity(entity, pizzeriaGroupContainer, tx, RequestTimeout.DEFAULT, "Cannot get paying group!"); group.setPayingWaiter(waiterId); group.setState(GroupState.GONE); @@ -210,6 +220,8 @@ public class WaiterXVSM extends AbstractXVSMConnector { sendItemsToContainer(groupsPayed, pizzeriaGroupContainer, RequestTimeout.DEFAULT, tx); capi.commitTransaction(tx); + } catch(EntryLockedException e) { + capi.rollbackTransaction(tx); } catch (Exception e) { log.info(e.getMessage()); } @@ -238,7 +250,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { String orderTakenByOtherWaiter = String.format( "The order for group %d was already taken by an other waiter!", groupData.getId()); - takeMatchingEntity(entity, takeOrderContainer, tx, RequestTimeout.DEFAULT, orderTakenByOtherWaiter); + takeMatchingEntity(entity, takeOrderContainer, tx, RequestTimeout.ZERO, orderTakenByOtherWaiter); GroupData group = takeMatchingEntityIfItExists(entity, pizzeriaGroupContainer, tx, RequestTimeout.INFINITE); group.setOrderWaiter(waiterId); @@ -262,6 +274,8 @@ public class WaiterXVSM extends AbstractXVSMConnector { log.info("Waiter has taken order from group {}", group.getId()); + } catch(EntryLockedException e) { + capi.rollbackTransaction(tx); } catch (Exception e) { // log.info(e.getMessage()); } @@ -284,7 +298,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { @Override public void onEntriesWritten(List entries) throws Exception { - log.info("NEW PIZZAS ARRived"); List pizzas = castEntries(entries); @@ -329,7 +342,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { capi.commitTransaction(tx); } else { - log.info("Not yet all pizzas prepared! Order with id " + log.debug("Not yet all pizzas prepared! Order with id " + orderId + " has " + numberOfPizzas + " pizzas, but only " + pizzasOfOrder.size() + " pizzas are ready by now!"); -- 2.43.0