From a326897bc200941fc998f75d0f5e935c25dddd78 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Thu, 6 Jun 2013 14:13:05 +0200 Subject: [PATCH] [XVSM] Create initial Pizzeria Recovery support. --- .../tuwien/sbc/valesriegler/common/Util.java | 8 +- .../sbc/valesriegler/group/SpaceGroup.java | 1 - .../group/gui/GroupCreationHandler.java | 4 +- .../gui/tablemodels/DeliveryOrdersModel.java | 2 +- .../gui/tablemodels/GroupsOverviewModel.java | 8 + .../gui/tablemodels/OrdersOverviewModel.java | 100 ++----- .../tuwien/sbc/valesriegler/types/Pizza.java | 14 +- .../sbc/valesriegler/types/PizzaOrder.java | 14 +- .../xvsm/AbstractXVSMConnector.java | 32 ++- .../sbc/valesriegler/xvsm/CookXVSM.java | 189 ++++++++++---- .../sbc/valesriegler/xvsm/DriverXVSM.java | 25 +- .../valesriegler/xvsm/PizzeriaAgentXVSM.java | 243 +++++++----------- .../sbc/valesriegler/xvsm/WaiterXVSM.java | 92 ++++++- .../sbc/valesriegler/xvsm/cook/Cook.java | 1 + .../sbc/valesriegler/xvsm/driver/Driver.java | 1 + .../sbc/valesriegler/xvsm/waiter/Waiter.java | 2 + 16 files changed, 414 insertions(+), 322 deletions(-) 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 77a8692..1cecd4d 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 @@ -24,24 +24,20 @@ public abstract class Util { public static boolean useJMS = true; public static final String TABLE_ASSIGNED = "tables"; - public static final String GROUP_AGENT_TABLE_ASSIGNED = "groupAgentTables"; public static final String ASSIGN_TABLE = "assignTable"; public static final String TAKE_ORDER = "takeOrder"; public static final String ORDER_TAKEN = "order"; public static final String DELIVERY_ORDER_TAKEN = "deliverOrderTaken"; - public static final String GROUP_AGENT_ORDER_TAKEN = "groupAgentOrder"; - public static final String GROUP_AGENT_DELIVERY_ORDER_TAKEN = "groupAgentDeliveryOrder"; public static final String DELIVER_PIZZAS = "deliverPizzas"; public static final String DELIVER_DELIVERY_PIZZAS = "deliverDeliveryPizzas"; + public static final String DELIVER_DELIVERY_ORDER = "deliverDeliveryOrder"; public static final String PREPARE_PIZZAS = "preparePizzas"; + public static final String PREPARE_DELIVERY_PIZZAS = "prepareDeliveryPizzas"; public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress"; public static final String ORDER_COMPLETE = "orderComplete"; - public static final String GROUP_AGENT_ORDER_COMPLETE = "groupAgentOrderComplete"; public static final String PAYMENT_REQUEST = "payment"; - public static final String GROUP_AGENT_PAYMENT_REQUEST = "groupAgentPayment"; public static final String FREE_TABLES = "freeTables"; public static final String PAYMENT_DONE = "hasPaid"; - public static final String GROUP_AGENT_PAYMENT_DONE = "groupAgentHasPaid"; public static final String PIZZERIA_INFO = "pizzeriaInfo"; public static final String PHONE_CALLS = "phoneCalls"; public static final String GROUP_AGENT_INFO = "groupAgentInfo"; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/SpaceGroup.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/SpaceGroup.java index 00a2fc5..73bd921 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/SpaceGroup.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/SpaceGroup.java @@ -6,7 +6,6 @@ public class SpaceGroup implements Runnable { private GroupXVSM xvsm; public SpaceGroup(int groupId, int pizzeriaSpacePort) { - // TODO add space port of pizzeria! xvsm = new GroupXVSM(groupId, pizzeriaSpacePort); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationHandler.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationHandler.java index 1d0501a..02e08dd 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationHandler.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationHandler.java @@ -7,6 +7,7 @@ import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.PizzaType; +import org.slf4j.LoggerFactory; import javax.swing.*; import java.util.ArrayList; @@ -19,6 +20,7 @@ import java.util.List; * group creation request should be issued */ class GroupCreationHandler { + private static final org.slf4j.Logger log = LoggerFactory.getLogger(GroupCreationHandler.class); private final boolean createDeliveryGroups; private JPanel creationPanel; private JPanel chooseGroupSizePanel; @@ -78,7 +80,7 @@ class GroupCreationHandler { List pizzaOrders = new ArrayList<>(); for (PizzaType pt : gc.pizzaTypes) { final PizzaOrder pizzaOrder = new PizzaOrder(pt); - pizzaOrder.setDeliveryPizza(createDeliveryGroups); +// pizzaOrder.setDeliveryPizza(createDeliveryGroups); pizzaOrders.add(pizzaOrder); } Order order = new Order(group, pizzaOrders); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java index 07bf7a3..3c2f3a3 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java @@ -10,7 +10,7 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; -public class DeliveryOrdersModel extends TableModel { +public class DeliveryOrdersModel extends AbstractOrdersModel { private static final Logger log = LoggerFactory.getLogger(DeliveryOrdersModel.class); private static final String ID = "Delivery ID"; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java index 594ed32..34f1c2d 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java @@ -7,6 +7,7 @@ import java.util.List; import at.ac.tuwien.sbc.valesriegler.common.TableModel; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupState; +import at.ac.tuwien.sbc.valesriegler.types.Table; public class GroupsOverviewModel extends TableModel { private static final String ID = "ID"; @@ -42,4 +43,11 @@ public class GroupsOverviewModel extends TableModel { fireTableDataChanged(); } + public void removeGroupsFromTables(List tables) { + for (Table table : tables) { + final int groupId = table.getGroupId(); + items.remove(groupId); + } + fireTableDataChanged(); + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java index 5f88643..7ebf765 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java @@ -1,22 +1,18 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; +import at.ac.tuwien.sbc.valesriegler.types.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.TableModel; import at.ac.tuwien.sbc.valesriegler.common.Util; -import at.ac.tuwien.sbc.valesriegler.types.GroupData; -import at.ac.tuwien.sbc.valesriegler.types.Order; -import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; -import at.ac.tuwien.sbc.valesriegler.types.Pizza; -import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; -import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; -public class OrdersOverviewModel extends TableModel { +public class OrdersOverviewModel extends AbstractOrdersModel { private static final Logger log = LoggerFactory.getLogger(OrdersOverviewModel.class); private static final String ID = "Order ID"; @@ -65,77 +61,11 @@ public class OrdersOverviewModel extends TableModel { fireTableDataChanged(); } - /** - * Traverse the GroupDatas and search for an order of which the orderId - * matches the orderId of one of the pizzas. Then set the status and the - * cookId of the respective pizza. - */ - public void updatePizzasInPreparation(List pizzas) { - synchronized (items) { - for (Pizza pizza : pizzas) { - List groups = new ArrayList(items.values()); - for (GroupData groupData : groups) { - Order order = groupData.getOrder(); - if (order.getId() == pizza.getOrderId()) { - - List orderedPizzas = order.getOrderedPizzas(); - Map orderedPizzasById = Util.intoMapById(orderedPizzas); - PizzaOrder pizzaOrder = orderedPizzasById.get(pizza.getId()); - if (pizzaOrder == null) { - throw new RuntimeException("A pizza which has never been ordered is in preparation... damn!"); - } - pizzaOrder.setStatus(PizzaOrderStatus.IN_PREPARATION); - pizzaOrder.setCookId(pizza.getCookId()); - } - } - } - } - - fireTableDataChanged(); - } - - public void updateStatusOfOrder(GroupData group) { - synchronized (items) { - Order order = group.getOrder(); - List groups = new ArrayList(items.values()); - for (GroupData groupData : groups) { - Order orderOfGroup = groupData.getOrder(); - if (order.getId() == orderOfGroup.getId()) { - orderOfGroup.setStatus(OrderStatus.DELIVERED); - groupData.setServingWaiter(group.getServingWaiter()); - } - } - } - - fireTableDataChanged(); + public void updateStatusOfOrder(GroupData group) { + updateStatusOfOrders(Arrays.asList(group)); } - public void updateStatusOfPizzasDone(Pizza pizza) { - int orderId = pizza.getOrderId(); - synchronized (items) { - Order order = getOrderById(orderId); - List orderedPizzas = order.getOrderedPizzas(); - for (PizzaOrder pizzaOrder : orderedPizzas) { - if (pizzaOrder.getId() == pizza.getId()) { - pizzaOrder.setStatus(PizzaOrderStatus.DONE); - } - } - } - - fireTableDataChanged(); - } - - private Order getOrderById(int orderId) { - List groups = new ArrayList(items.values()); - for (GroupData groupData : groups) { - if (groupData.getOrder().getId() == orderId) { - return groupData.getOrder(); - } - } - throw new RuntimeException(String.format("Order with orderId %d not found!", orderId)); - } - - public void updatePaymentWaiter(GroupData group) { + public void updatePaymentWaiter(GroupData group) { synchronized (items) { List groups = new ArrayList(items.values()); for (GroupData groupData : groups) { @@ -146,4 +76,22 @@ public class OrdersOverviewModel extends TableModel { } fireTableDataChanged(); } + + public void updateStatusOfOrders(List updatedGroups) { + synchronized (items) { + for (GroupData group : updatedGroups) { + Order order = group.getOrder(); + List groups = new ArrayList(items.values()); + for (GroupData groupData : groups) { + Order orderOfGroup = groupData.getOrder(); + if (order.getId() == orderOfGroup.getId()) { + orderOfGroup.setStatus(OrderStatus.DELIVERED); + groupData.setServingWaiter(group.getServingWaiter()); + } + } + } + } + + fireTableDataChanged(); + } } 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 63f745e..ffc7a47 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 @@ -13,8 +13,9 @@ import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; */ public class Pizza extends PizzaOrder implements Serializable { public JMSWaiter deliveryAgent; + protected boolean isDeliveryPizza; - /** + /** * Yes that's funny! the pizza has a field idOfOrder although PizzaOrder * already has a field orderId! The reason: The space linda selection is a * little bit limited. when I look for a template pizza with orderId set the @@ -51,12 +52,12 @@ public class Pizza extends PizzaOrder implements Serializable { @Override public int getOrderId() { return getIdOfOrder(); - }; + } @Override public void setOrderId(Integer orderId) { idOfOrder = orderId; - }; + } public int getIdOfOrder() { return Util.getIntSafe(idOfOrder); @@ -66,4 +67,11 @@ public class Pizza extends PizzaOrder implements Serializable { this.idOfOrder = idOfOrder; } + public void setDeliveryPizza(boolean deliveryPizza) { + isDeliveryPizza = deliveryPizza; + } + + public boolean isDeliveryPizza() { + return 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 09cb0ff..8510199 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 @@ -20,9 +20,7 @@ public class PizzaOrder implements Serializable, HasId { protected PizzaOrderStatus status; protected Integer cookId; - protected boolean isDeliveryPizza; - - public PizzaOrder(PizzaType pizzaType) { + public PizzaOrder(PizzaType pizzaType) { id = ++nextID; this.pizzaType = pizzaType; status = PizzaOrderStatus.NEW; @@ -35,15 +33,7 @@ public class PizzaOrder implements Serializable, HasId { public PizzaOrder() { } - public void setDeliveryPizza(boolean deliveryPizza) { - isDeliveryPizza = deliveryPizza; - } - - public boolean isDeliveryPizza() { - return isDeliveryPizza; - } - - public PizzaType getPizzaType() { + public PizzaType getPizzaType() { return pizzaType; } 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 e4bb0a7..2e3fc81 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 @@ -34,8 +34,9 @@ public abstract class AbstractXVSMConnector { protected ContainerReference orderTakenContainer; protected ContainerReference deliveryOrderTakenContainer; protected ContainerReference preparePizzasContainer; - protected ContainerReference deliverPizzasContainer; - protected ContainerReference deliverDeliveryPizzasContainer; + protected ContainerReference prepareDeliveryPizzasContainer; + protected ContainerReference preparedPizzasContainer; + protected ContainerReference preparedDeliveryPizzasContainer; protected ContainerReference paymentRequestContainer; protected ContainerReference freeTablesContainer; protected ContainerReference pizzaInProgressContainer; @@ -44,8 +45,9 @@ public abstract class AbstractXVSMConnector { protected ContainerReference pizzeriaInfoContainer; protected ContainerReference phoneCallsContainer; protected ContainerReference groupAgentInfoContainer; - protected Capi capi; + protected ContainerReference deliverDeliveryOrder; + protected Capi capi; protected NotificationManager notificationMgr; protected int port; @@ -142,7 +144,7 @@ public abstract class AbstractXVSMConnector { } /** - * Searches for all entities matching the given template object by linda selection. + * Searches for all entities matching the given template object by linda selection and takes them. */ protected List takeMatchingEntities( T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg) @@ -164,6 +166,28 @@ 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) + throws MzsCoreException { + try { + LindaSelector sel = LindaCoordinator.newSelector(entity, MzsConstants.Selecting.COUNT_MAX); + + ArrayList entities = capi.read(ref, sel, timeout, tx); + + return (List) entities; + } catch (CountNotMetException e) { + capi.rollbackTransaction(tx); + + throw new EntityNotFoundByTemplate(errorMsg); + } catch (MzsTimeoutException e) { + capi.rollbackTransaction(tx); + + throw new EntityNotFoundByTemplate(errorMsg); + } + } protected List castEntries(List entries) { List newList = new ArrayList(); if (entries.size() == 0) return newList; 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 8fbb713..149a14c 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 @@ -6,18 +6,17 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import at.ac.tuwien.sbc.valesriegler.types.*; import org.mozartspaces.capi3.FifoCoordinator; import org.mozartspaces.core.ContainerReference; import org.mozartspaces.core.MzsConstants; import org.mozartspaces.core.MzsConstants.RequestTimeout; +import org.mozartspaces.core.MzsCoreException; import org.mozartspaces.core.TransactionReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.Util; -import at.ac.tuwien.sbc.valesriegler.types.Pizza; -import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; -import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; public class CookXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(CookXVSM.class); @@ -28,68 +27,146 @@ public class CookXVSM extends AbstractXVSMConnector { super(port); this.cookId = id; - orderTakenContainer = useContainer(Util.ORDER_TAKEN) ; - deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS); + orderTakenContainer = useContainer(Util.ORDER_TAKEN); + deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); + preparedPizzasContainer = useContainer(Util.DELIVER_PIZZAS); preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS); + prepareDeliveryPizzasContainer = useContainer(Util.PREPARE_DELIVERY_PIZZAS); pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS) ; - deliverDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); + preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); } - public void listenForPizzas() { - SpaceListener pizzasListener = new SpaceListenerImpl(capi, preparePizzasContainer, 15000) { - - @Override - void onEntriesWritten(List entries) - throws Exception { - - List pizzas = castEntries(entries); - - if(inNotification.get()) Collections.shuffle(pizzas); - - for (PizzaOrder pizzaOrder : pizzas) { - - final boolean deliveryPizza = pizzaOrder.isDeliveryPizza(); + public void listenForPizzas() { + SpaceListener pizzasListener = new SpaceListenerImpl(capi, preparePizzasContainer, 15000) { + + @Override + void onEntriesWritten(List entries) + throws Exception { + + List pizzas = castEntries(entries); + + if(inNotification.get()) Collections.shuffle(pizzas); + + for (PizzaOrder pizzaOrder : pizzas) { + TransactionReference tx = capi.createTransaction(9000, URI.create(String.format(Util.SERVER_ADDR, port))); - String pizzaAlreadyCooked = String.format("Pizza with id %d has already been cooked by another cook", pizzaOrder.getId()); - - try { - // 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, deliveryPizza); - pizzaInProgress.setStatus(PizzaOrderStatus.IN_PREPARATION); - - - log.info("I say that I now prepare a pizza for order {}", pizzaInProgress.getOrderId()); - sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, null); - - PizzaOrder pizza = createPizza(order); - - final ContainerReference container = deliveryPizza ? deliverDeliveryPizzasContainer : deliverPizzasContainer; + String pizzaAlreadyCooked = String.format("Pizza with id %d has already been cooked by another cook", pizzaOrder.getId()); + + try { + if (! mayPreparePizza(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); + + + // tell the space that you prepare the pizza -> without a transaction!! + Pizza pizzaInProgress = Pizza.createPizzaFromPizzaOrder(order, cookId, true); + pizzaInProgress.setStatus(PizzaOrderStatus.IN_PREPARATION); + + + log.info("I say that I now prepare a pizza for order {}", pizzaInProgress.getOrderId()); + sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, null); + + PizzaOrder pizza = createPizza(order); + + final ContainerReference container = preparedPizzasContainer; sendItemsToContainer(Arrays.asList(pizza), container, RequestTimeout.DEFAULT, tx); - - capi.commitTransaction(tx); - log.info("I have completed preparing a pizza for order {}!", pizza.getOrderId()); - } catch (NullPointerException e) { - // the strange nullpointer exception from the space - } catch (Exception e) { - log.info(e.getMessage()); - } - - } + + capi.commitTransaction(tx); + log.info("I have completed preparing a pizza for order {}!", pizza.getOrderId()); + } catch (NullPointerException e) { + // the strange nullpointer exception from the space + } catch (Exception e) { + log.info(e.getMessage()); + } + + } + } + + @Override + List getEntries() throws Exception { + return capi.read(preparePizzasContainer, FifoCoordinator + .newSelector(MzsConstants.Selecting.COUNT_ALL), + RequestTimeout.DEFAULT, null); } - - @Override - List getEntries() throws Exception { - return capi.read(preparePizzasContainer, FifoCoordinator - .newSelector(MzsConstants.Selecting.COUNT_ALL), - RequestTimeout.DEFAULT, null); - } }; createNotification(pizzasListener, preparePizzasContainer); - - } + + } + + /** + * A cook may prepare a pizza if there is no current delivery order or if at least a pizza of the same order is + * in preparation! + */ + private boolean mayPreparePizza(PizzaOrder pizzaOrder, TransactionReference tx) throws MzsCoreException { + List deliveryPizzas = readMatchingEntities(new PizzaOrder(), prepareDeliveryPizzasContainer, tx, RequestTimeout.ZERO, "BBBBBB"); + if (! deliveryPizzas.isEmpty()) { + final PizzaOrder template = new PizzaOrder(); + template.setOrderId(pizzaOrder.getOrderId()); + final List pizzasOfOrderInProgress = readMatchingEntities(template, pizzaInProgressContainer, tx, RequestTimeout.ZERO, "ASDF"); + if(pizzasOfOrderInProgress.isEmpty()) return false; + } + return true; + } + + + public void listenForDeliveryPizzas() { + SpaceListener pizzasListener = new SpaceListenerImpl(capi, prepareDeliveryPizzasContainer, 15000) { + + @Override + void onEntriesWritten(List entries) + throws Exception { + + List pizzas = castEntries(entries); + + if(inNotification.get()) Collections.shuffle(pizzas); + + for (PizzaOrder pizzaOrder : pizzas) { + + TransactionReference tx = capi.createTransaction(9000, URI.create(String.format(Util.SERVER_ADDR, port))); + String pizzaAlreadyCooked = String.format("Pizza with id %d has already been cooked by another cook", pizzaOrder.getId()); + + try { + // Require the lock for preparing the pizza + PizzaOrder order = takeMatchingEntity(new PizzaOrder(pizzaOrder.getId()), prepareDeliveryPizzasContainer, 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); + + + log.info("I say that I now prepare a pizza for order {}", pizzaInProgress.getOrderId()); + sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, null); + + PizzaOrder pizza = createPizza(order); + + final ContainerReference container = preparedDeliveryPizzasContainer; + sendItemsToContainer(Arrays.asList(pizza), container, RequestTimeout.DEFAULT, tx); + + capi.commitTransaction(tx); + log.info("I have completed preparing a delivery pizza for order {}!", pizza.getOrderId()); + } catch (NullPointerException e) { + // the strange nullpointer exception from the space + } catch (Exception e) { + log.info(e.getMessage()); + } + + } + } + + @Override + List getEntries() throws Exception { + return capi.read(prepareDeliveryPizzasContainer, FifoCoordinator + .newSelector(MzsConstants.Selecting.COUNT_ALL), + RequestTimeout.DEFAULT, null); + } + }; + createNotification(pizzasListener, prepareDeliveryPizzasContainer); + + } private PizzaOrder createPizza(PizzaOrder order) throws InterruptedException { long duration = order.getPizzaType().duration; 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 00bb47a..7289d62 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 @@ -3,7 +3,6 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; -import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.Pizza; import org.mozartspaces.core.MzsConstants; @@ -25,25 +24,28 @@ public class DriverXVSM extends AbstractXVSMConnector { this.driverId = id; - this.deliverDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); - + this.preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); + this.deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); + this.deliverDeliveryOrder = useContainer(Util.DELIVER_DELIVERY_ORDER); } public void listenForPreparedPizzas() { /** - * A waiter gets informed when a new pizza is complete. He takes the + * A driver gets informed when a new pizza is complete. He takes the * orderId of the pizza and looks up the corresponding order from which * he gets the number of necessary pizzas of the order. He then tries to * fetch all pizzas with the corresponding orderId and compares the * number of those pizzas with the number of necessary pizzas. If all * pizzas of an order are complete he then delivers them! */ - SpaceListener preparedPizzasListener = new SpaceListenerImpl(capi, deliverDeliveryPizzasContainer) { + SpaceListener preparedPizzasListener = new SpaceListenerImpl(capi, preparedDeliveryPizzasContainer, true) { @Override void onEntriesWritten(List entries) throws Exception { + log.info("HELLLOOOOO"); + List pizzas = castEntries(entries); for (Pizza pizza : pizzas) { @@ -52,7 +54,6 @@ public class DriverXVSM extends AbstractXVSMConnector { order.setId(orderId); TransactionReference tx = getDefaultTransaction(); - final boolean isDeliveryPizza = pizza.isDeliveryPizza(); try { DeliveryGroupData entity = new DeliveryGroupData(); @@ -60,18 +61,18 @@ public class DriverXVSM extends AbstractXVSMConnector { entity.setOrder(order); DeliveryGroupData groupData = takeMatchingEntity(entity, - orderTakenContainer, tx, MzsConstants.RequestTimeout.DEFAULT, + deliveryOrderTakenContainer, tx, MzsConstants.RequestTimeout.DEFAULT, "Another driver just checks if the delivery order is complete"); int groupId = groupData.getId(); - int numberOfPizzas = order.getNumberOfPizzas(); + int numberOfPizzas = groupData.getOrder().getNumberOfPizzas(); Pizza pizzaTemplate = new Pizza(); pizzaTemplate.setOrderId(orderId); List pizzasOfOrder = takeMatchingEntities( - pizzaTemplate, deliverPizzasContainer, tx, + pizzaTemplate, preparedDeliveryPizzasContainer, tx, MzsConstants.RequestTimeout.DEFAULT, - "Cannot take the pizzas from the deliverDeliveryPizzasContainer"); + "Cannot take the pizzas from the preparedDeliveryPizzasContainer"); if (pizzasOfOrder.size() == numberOfPizzas) { DeliveryGroupData group = new DeliveryGroupData(); @@ -82,7 +83,7 @@ public class DriverXVSM extends AbstractXVSMConnector { group.setOrder(completeOrder); final List groupsWithCompleteOrder = Arrays.asList(group); sendItemsToContainer(groupsWithCompleteOrder, - orderCompleteContainer, MzsConstants.RequestTimeout.DEFAULT, + deliverDeliveryOrder, MzsConstants.RequestTimeout.DEFAULT, tx); capi.commitTransaction(tx); @@ -102,6 +103,6 @@ public class DriverXVSM extends AbstractXVSMConnector { } }; - createNotification(preparedPizzasListener, deliverDeliveryPizzasContainer); + createNotification(preparedPizzasListener, preparedDeliveryPizzasContainer); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java index 01c7a64..200ee46 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java @@ -9,6 +9,7 @@ import javax.swing.SwingUtilities; import at.ac.tuwien.sbc.valesriegler.common.OrderId; import at.ac.tuwien.sbc.valesriegler.types.*; import org.mozartspaces.core.MzsConstants.RequestTimeout; +import org.mozartspaces.core.TransactionReference; import org.mozartspaces.notifications.Notification; import org.mozartspaces.notifications.NotificationListener; import org.mozartspaces.notifications.Operation; @@ -27,7 +28,7 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { freeTablesContainer = useContainer(Util.FREE_TABLES); pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS) ; orderCompleteContainer = useContainer(Util.ORDER_COMPLETE) ; - deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS); + preparedPizzasContainer = useContainer(Util.DELIVER_PIZZAS); paymentDoneContainer = useContainer(Util.PAYMENT_DONE) ; orderTakenContainer = useContainer(Util.ORDER_TAKEN); assignTableContainer = useContainer(Util.ASSIGN_TABLE); @@ -36,85 +37,77 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { } public void listenForOccupiedTables() { - NotificationListener tablesListener = new NotificationListener() { + SpaceListener tablesListener = new SpaceListenerImpl(capi, tableAssignedContainer, false) { @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + void onEntriesWritten(List entries) throws Exception { log.info("A new group was assigned to a table!"); - - final List
tables = castEntries(entries); - - if(tables.size() != 1) { - throw new RuntimeException("Only one table can get free at once!"); - } - final Table table = tables.get(0); - + + final List
tables = castEntries(entries); + log.info("occupied tables: " + tables.toString()); + SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getTablesModel().addItems(Arrays.asList(table)); - PizzeriaAgent.getInstance().getGroupModel().removeGroup(table.getGroupId()); - } - }); + @Override + public void run() { + PizzeriaAgent.getInstance().getTablesModel().addItems(tables); + PizzeriaAgent.getInstance().getGroupModel().removeGroupsFromTables(tables); + } + }); } }; - try { - notificationMgr.createNotification(tableAssignedContainer, tablesListener, Operation.WRITE); - log.info("Created tableAssigned notification for pizzeria!"); - } catch (Exception e) { - handleSpaceErrorAndTerminate(e); - } + createNotification(tablesListener, tableAssignedContainer); } public void listenForFreeTables() { - NotificationListener tablesListener = new NotificationListener() { + SpaceListener tablesListener = new SpaceListenerImpl(capi, freeTablesContainer, false) { + @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + void onEntriesWritten(List entries) throws Exception { log.info("A table has become free"); - - final List
tables = castEntries(entries); - + + final List
tables = castEntries(entries); + + log.info("freetables: " + tables.toString()); +// final TransactionReference tx = getDefaultTransaction(); +// for (Table table : tables) { +// log.info("remove assigned table {}", table.getId()); +// takeMatchingEntities(new Table(table.getId()), tableAssignedContainer, tx, RequestTimeout.DEFAULT, "Could not remove assigned tables"); +// } +// capi.commitTransaction(tx); + SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getTablesModel().addItems(tables); - } - }); + @Override + public void run() { + PizzeriaAgent.getInstance().getTablesModel().addItems(tables); + } + }); } }; - try { - notificationMgr.createNotification(freeTablesContainer, tablesListener, Operation.WRITE); - } catch (Exception e) { - handleSpaceErrorAndTerminate(e); - } + createNotification(tablesListener, freeTablesContainer); } public void listenForWaitingGroups() { - NotificationListener groupsListener = new NotificationListener() { + SpaceListener groupsListener = new SpaceListenerImpl(capi, assignTableContainer, false) { @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); - + SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getGroupModel().addItems(groups); - } - }); - + @Override + public void run() { + PizzeriaAgent.getInstance().getGroupModel().addItems(groups); + } + }); + } }; - try { - notificationMgr.createNotification(assignTableContainer, groupsListener, Operation.WRITE); - } catch (Exception e) { - handleSpaceErrorAndTerminate(e); - } + createNotification(groupsListener, assignTableContainer); } public void listenForTakenDeliveryOrders() { - NotificationListener orderTakenListener = new NotificationListener() { + SpaceListener orderTakenListener = new SpaceListenerImpl(capi, deliveryOrderTakenContainer, false) { @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); SwingUtilities.invokeLater(new Runnable() { @@ -126,128 +119,96 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { } }; - try { - notificationMgr.createNotification(deliveryOrderTakenContainer, orderTakenListener, Operation.WRITE); - } catch (Exception e) { - handleSpaceErrorAndTerminate(e); - } + createNotification(orderTakenListener, deliveryOrderTakenContainer); } public void listenForTakenOrders() { - NotificationListener orderTakenListener = new NotificationListener() { + SpaceListener orderTakenListener = new SpaceListenerImpl(capi, orderTakenContainer, false) { @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); - if(groups.size() != 1) { - throw new RuntimeException("Only one order per table!"); - } - final GroupData group = groups.get(0); - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getOrdersModel().addItems(Arrays.asList(group)); - } - }); + @Override + public void run() { + PizzeriaAgent.getInstance().getOrdersModel().addItems(groups); + } + }); } }; - try { - notificationMgr.createNotification(orderTakenContainer, orderTakenListener, Operation.WRITE); - } catch (Exception e) { - handleSpaceErrorAndTerminate(e); - } + createNotification(orderTakenListener, orderTakenContainer); } public void listenForPizzasInPreparation() { - NotificationListener pizzasInProgress = new NotificationListener() { + SpaceListener pizzasInProgress = new SpaceListenerImpl(capi, pizzaInProgressContainer, false) { + @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + void onEntriesWritten(List entries) throws Exception { final List pizzas = castEntries(entries); log.info("{} pizzas in progress changes!", pizzas.size()); for (PizzaOrder pizza : pizzas) { - log.info(pizza.toString()); - } + log.info(pizza.toString()); + } SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getOrdersModel().updatePizzasInPreparation(pizzas); - } - }); + @Override + public void run() { + PizzeriaAgent.getInstance().getOrdersModel().updatePizzasInPreparation(pizzas); + PizzeriaAgent.getInstance().getDeliveryOrdersModel().updatePizzasInPreparation(pizzas); + } + }); } }; - try { - notificationMgr.createNotification(pizzaInProgressContainer, pizzasInProgress, Operation.WRITE); - } catch (Exception e) { - handleSpaceErrorAndTerminate(e); - } + createNotification(pizzasInProgress, pizzaInProgressContainer); } public void listenForDeliveredOrders() { - NotificationListener deliveredOrders = new NotificationListener() { + SpaceListener deliveredOrders = new SpaceListenerImpl(capi, orderCompleteContainer, false) { @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); log.info("{} order was delivered!", groups.size()); - if(groups.size() != 1) { - throw new RuntimeException("A waiter should only deliver one order at once!"); - } - final GroupData group = groups.get(0); SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getOrdersModel().updateStatusOfOrder(group); - } - }); - + @Override + public void run() { + PizzeriaAgent.getInstance().getOrdersModel().updateStatusOfOrders(groups); + } + }); } }; - try { - notificationMgr.createNotification(orderCompleteContainer, deliveredOrders, Operation.WRITE); - } catch (Exception e) { - handleSpaceErrorAndTerminate(e); - } + createNotification(deliveredOrders, orderCompleteContainer); } public void listenForPreparedPizzas() { - NotificationListener preparedPizzasListener = new NotificationListener() { + SpaceListener preparedPizzasListener = new SpaceListenerImpl(capi, preparedPizzasContainer, false) { @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { - - List pizzas = castEntries(entries); - if(pizzas.size() != 1) throw new RuntimeException("Multiple pizzas in one notification?! That should not happen! A cook can only prepare one at the same time!"); - - final Pizza pizza = pizzas.get(0); + void onEntriesWritten(List entries) throws Exception { + final List pizzas = castEntries(entries); - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getOrdersModel().updateStatusOfPizzasDone(pizza); - } - }); + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + PizzeriaAgent.getInstance().getOrdersModel().updateStatusOfPizzasDone(pizzas); + PizzeriaAgent.getInstance().getDeliveryOrdersModel().updateStatusOfPizzasDone(pizzas); + } + }); } - }; - - try { - notificationMgr.createNotification(deliverPizzasContainer, preparedPizzasListener, Operation.WRITE); - log.info("Created deliverPizzasContainer notification for the pizzeria"); - } catch (Exception e) { - handleSpaceErrorAndTerminate(e); - } + }; + createNotification(preparedPizzasListener, preparedPizzasContainer); + } public void listenForPayment() { - NotificationListener isGoneListener = new NotificationListener() { + SpaceListener isGoneListener = new SpaceListenerImpl(capi, paymentDoneContainer, false) { @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); final GroupData group = groups.get(0); @@ -256,23 +217,19 @@ public void listenForTakenOrders() { final int tableId = PizzeriaAgent.getInstance().getTablesModel().getTableIdOfGroup(group); SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getOrdersModel().updatePaymentWaiter(group); - PizzeriaAgent.getInstance().getTablesModel().makeTableFree(tableId); - } - }); + @Override + public void run() { + PizzeriaAgent.getInstance().getOrdersModel().updatePaymentWaiter(group); + PizzeriaAgent.getInstance().getTablesModel().makeTableFree(tableId); + } + }); - Table table = new Table(tableId); + Table table = new Table(tableId); - sendItemsToContainer(Arrays.asList(table), freeTablesContainer, RequestTimeout.DEFAULT, null); + sendItemsToContainer(Arrays.asList(table), freeTablesContainer, RequestTimeout.DEFAULT, null); } }; - try { - notificationMgr.createNotification(paymentDoneContainer, isGoneListener, Operation.WRITE); - } catch (Exception e) { - handleSpaceErrorAndTerminate(e); - } + createNotification(isGoneListener, paymentDoneContainer); } 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 7184884..b46115b 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 @@ -29,13 +29,16 @@ public class WaiterXVSM extends AbstractXVSMConnector { orderTakenContainer = useContainer(Util.ORDER_TAKEN); deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS); + prepareDeliveryPizzasContainer = useContainer(Util.PREPARE_DELIVERY_PIZZAS); orderCompleteContainer = useContainer(Util.ORDER_COMPLETE); - deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS); + preparedPizzasContainer = useContainer(Util.DELIVER_PIZZAS); paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST); paymentDoneContainer = useContainer(Util.PAYMENT_DONE); tableAssignedContainer = useContainer(Util.TABLE_ASSIGNED); pizzeriaInfoContainer = useContainer(Util.PIZZERIA_INFO); phoneCallsContainer = useContainer(Util.PHONE_CALLS); + preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); + deliverDeliveryOrder = useContainer(Util.DELIVER_DELIVERY_ORDER); } public void listenForPhoneOrders() { @@ -64,10 +67,10 @@ public class WaiterXVSM extends AbstractXVSMConnector { sendItemsToContainer(groupsWhoHaveOrdered, deliveryOrderTakenContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(order.getOrderedPizzas(), - preparePizzasContainer, RequestTimeout.ZERO, tx); + prepareDeliveryPizzasContainer, RequestTimeout.ZERO, tx); capi.commitTransaction(tx); - log.info("Waite has taken a phone delivery call!"); + log.info("Waiter has taken a phone delivery call!"); } catch (Exception e) { } @@ -270,7 +273,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { * number of those pizzas with the number of necessary pizzas. If all * pizzas of an order are complete he then delivers them! */ - SpaceListener preparedPizzasListener = new SpaceListenerImpl(capi, deliverPizzasContainer) { + SpaceListener preparedPizzasListener = new SpaceListenerImpl(capi, preparedPizzasContainer) { @Override void onEntriesWritten(List entries) @@ -301,9 +304,9 @@ public class WaiterXVSM extends AbstractXVSMConnector { pizzaTemplate.setOrderId(orderId); List pizzasOfOrder = takeMatchingEntities( - pizzaTemplate, deliverPizzasContainer, tx, + pizzaTemplate, preparedPizzasContainer, tx, RequestTimeout.DEFAULT, - "Cannot take the pizzas from the deliverPizzasContainer"); + "Cannot take the pizzas from the preparedPizzasContainer"); if (pizzasOfOrder.size() == numberOfPizzas) { GroupData group = new GroupData(); @@ -334,7 +337,82 @@ public class WaiterXVSM extends AbstractXVSMConnector { } }; - createNotification(preparedPizzasListener, deliverPizzasContainer); + createNotification(preparedPizzasListener, preparedPizzasContainer); + } + + + public void listenForPreparedDeliveryPizzas() { + /** + * A waiter gets informed when a new pizza is complete. He takes the + * orderId of the pizza and looks up the corresponding order from which + * he gets the number of necessary pizzas of the order. He then tries to + * fetch all pizzas with the corresponding orderId and compares the + * number of those pizzas with the number of necessary pizzas. If all + * pizzas of an order are complete he then delivers them! + */ + SpaceListener preparedPizzasListener = new SpaceListenerImpl(capi, preparedDeliveryPizzasContainer, true) { + + @Override + void onEntriesWritten(List entries) + throws Exception { + + List pizzas = castEntries(entries); + + for (Pizza pizza : pizzas) { + int orderId = pizza.getOrderId(); + Order order = new Order(); + order.setId(orderId); + + TransactionReference tx = getDefaultTransaction(); + + try { + DeliveryGroupData entity = new DeliveryGroupData(); + entity.setDeliveryStatus(null); + entity.setOrder(order); + + DeliveryGroupData groupData = takeMatchingEntity(entity, + deliveryOrderTakenContainer, tx, MzsConstants.RequestTimeout.DEFAULT, + "Another driver just checks if the delivery order is complete"); + int groupId = groupData.getId(); + int numberOfPizzas = groupData.getOrder().getNumberOfPizzas(); + + Pizza pizzaTemplate = new Pizza(); + pizzaTemplate.setOrderId(orderId); + + List pizzasOfOrder = takeMatchingEntities( + pizzaTemplate, preparedDeliveryPizzasContainer, tx, + MzsConstants.RequestTimeout.DEFAULT, + "Cannot take the pizzas from the preparedDeliveryPizzasContainer"); + + if (pizzasOfOrder.size() == numberOfPizzas) { + DeliveryGroupData group = new DeliveryGroupData(); + Order completeOrder = new Order(); + completeOrder.setId(orderId); + completeOrder.setGroupId(groupId); + group.setOrder(completeOrder); + final List groupsWithCompleteOrder = Arrays.asList(group); + sendItemsToContainer(groupsWithCompleteOrder, + deliverDeliveryOrder, MzsConstants.RequestTimeout.DEFAULT, + tx); + + capi.commitTransaction(tx); + } else { + log.info("Not yet all pizzas prepared! Order with id " + + orderId + " has " + numberOfPizzas + + " pizzas, but only " + pizzasOfOrder.size() + + " pizzas are ready by now!"); + capi.rollbackTransaction(tx); + } + } catch (NullPointerException e) { + + } catch (Exception e) { + capi.rollbackTransaction(tx); + } + } + } + }; + + createNotification(preparedPizzasListener, preparedDeliveryPizzasContainer); } private void assignGroupToTable(GroupData lockedGroup, diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java index ba647ef..3c46775 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java @@ -25,6 +25,7 @@ public class Cook { private void start() { xvsm = new CookXVSM(id, pizzeriaPort); xvsm.listenForPizzas(); + xvsm.listenForDeliveryPizzas(); } public Cook(int id, int pizzeriaPort) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/driver/Driver.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/driver/Driver.java index 3ce9980..33e6cd5 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/driver/Driver.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/driver/Driver.java @@ -24,6 +24,7 @@ public class Driver { private void start() { xvsm = new DriverXVSM(id, port); + xvsm.listenForPreparedPizzas(); } public Driver(int id, int port) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java index 337bff7..d75b750 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java @@ -46,6 +46,8 @@ public class Waiter implements Serializable { xvsm.listenForFreeTable(); xvsm.listenForPhoneOrders(); + + xvsm.listenForPreparedDeliveryPizzas(); } -- 2.43.0