From 4a60c5550b77c32ebdfc8bb30cbe617c5ee82563 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Fri, 7 Jun 2013 14:02:46 +0200 Subject: [PATCH] [XVSM] Delivery in progress, Delivery done updates in UIs, Delivery logic --- .../tuwien/sbc/valesriegler/common/Util.java | 35 +++--- .../sbc/valesriegler/group/GroupAgent.java | 18 +-- .../group/gui/DeliveryOverviewModel.java | 22 ++++ .../group/gui/GroupCreationHandler.java | 1 + .../group/gui/GroupOverviewModel.java | 13 +- .../valesriegler/pizzeria/PizzeriaAgent.java | 28 ++--- .../gui/tablemodels/AbstractOrdersModel.java | 1 - .../gui/tablemodels/DeliveryOrdersModel.java | 20 +++- .../gui/tablemodels/OrdersOverviewModel.java | 21 ++-- .../valesriegler/types/DeliveryStatus.java | 2 +- .../xvsm/AbstractXVSMConnector.java | 5 +- .../sbc/valesriegler/xvsm/CookXVSM.java | 35 ++++-- .../sbc/valesriegler/xvsm/DriverXVSM.java | 74 ++++++++++++ .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 51 +++++++- .../valesriegler/xvsm/PizzeriaAgentXVSM.java | 111 +++++++----------- .../sbc/valesriegler/xvsm/WaiterXVSM.java | 38 +++--- .../sbc/valesriegler/xvsm/driver/Driver.java | 2 +- 17 files changed, 305 insertions(+), 172 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 33ac9e2..a853cd2 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 @@ -1,31 +1,20 @@ package at.ac.tuwien.sbc.valesriegler.common; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; -import java.net.URI; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.Set; - -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JTable; -import javax.swing.ScrollPaneConstants; -import javax.swing.border.TitledBorder; - +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; +import at.ac.tuwien.sbc.valesriegler.types.PizzaType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; -import at.ac.tuwien.sbc.valesriegler.types.PizzaType; +import javax.swing.*; +import javax.swing.border.TitledBorder; +import java.awt.*; +import java.net.URI; +import java.util.*; +import java.util.List; public abstract class Util { private static final Logger log = LoggerFactory.getLogger(Util.class); - public static boolean useJMS = true; + public static boolean useJMS = false; public static final String TABLE_ASSIGNED = "tables"; public static final String ASSIGN_TABLE = "assignTable"; @@ -48,6 +37,12 @@ public abstract class Util { public static final String PIZZERIA_GROUP = "pizzeriaGroupContainer"; public static final String PIZZERIA_TABLE = "pizzeriaTableContainer"; public static final String PIZZERIA_DELIVERY = "pizzeriaDeliveryContainer"; + public static final String DELIVERY_PROGRESS = "deliveryInProgress"; + public static final String DELIVERY_DONE = "deliveryDone"; + public static final String BILL_PAID = "billPaid"; + + + public static final String NUMBER_DISPLAY_FORMAT = String.format("%%0%dd", 3); private static Random random = new Random(); 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 8b57589..30ac826 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 @@ -1,16 +1,5 @@ package at.ac.tuwien.sbc.valesriegler.group; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.swing.SwingUtilities; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel; import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel; @@ -18,6 +7,11 @@ import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.*; +import java.util.*; /** * The Main class of the Group component. @@ -51,7 +45,7 @@ public class GroupAgent { // throw new IllegalArgumentException(USAGE); } - String mw = "JMS";// args[0]; + String mw = "XVSM";// args[0]; log.info("Middleware: " + mw); if ("JMS".equalsIgnoreCase(mw)) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java index 95431a9..1f99e75 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java @@ -4,6 +4,8 @@ package at.ac.tuwien.sbc.valesriegler.group.gui; import at.ac.tuwien.sbc.valesriegler.common.TableModel; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.DeliveryGroup; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,4 +51,24 @@ public class DeliveryOverviewModel extends TableModel { throw new RuntimeException(UNHANDLEDCOLUMN); } } + + public void setOrdersTaken(List groups) { + for (DeliveryGroupData group : groups) { + changeStateOfGroup(group.getId(), DeliveryStatus.ORDERED); + } + + fireTableDataChanged(); + } + + private void changeStateOfGroup(int groupId, DeliveryStatus state) { + items.get(groupId).getDeliveryGroupData().setDeliveryStatus(state); + } + + public void setOrdersDelivered(List groups) { + for (DeliveryGroupData group : groups) { + changeStateOfGroup(group.getId(), group.getDeliveryStatus()); + } + + fireTableDataChanged(); + } } 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 02e08dd..4267186 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 @@ -55,6 +55,7 @@ class GroupCreationHandler { final Order order = createOrder(group, gc); group.getDeliveryGroupData().setOrder(order); group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria); + group.getDeliveryGroupData().setAddress(gc.address); newDeliveryGroups.add(group); } else { Group group = new Group(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java index e750ef4..b64abb7 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java @@ -1,17 +1,16 @@ package at.ac.tuwien.sbc.valesriegler.group.gui; -import java.util.ArrayList; -import java.util.List; - -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.group.Group; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupState; import at.ac.tuwien.sbc.valesriegler.types.Table; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; /** * Might not the nicest way to store global data, but this is an elemental @@ -53,7 +52,7 @@ public class GroupOverviewModel extends TableModel { switch (wantedColumn) { case ID: int groupId = group.getGroupData().getId(); - return groupId; + return String.format(Util.NUMBER_DISPLAY_FORMAT, groupId); case SIZE: int groupSize = group.getGroupData().getSize(); return groupSize; 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 4f9687c..ef04134 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 @@ -1,25 +1,17 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria; -import java.util.List; - -import javax.swing.SwingUtilities; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.pizzeria.actions.TableNew; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzeriaFrame; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.DeliveryDetailsModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.DeliveryOrdersModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.GroupsOverviewModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.OrdersOverviewModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.PizzasOfOrderModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.TablesOverviewModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.WaitersOfOrderModel; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.*; import at.ac.tuwien.sbc.valesriegler.pizzeria.jms.JMSPizzeriaConnector; import at.ac.tuwien.sbc.valesriegler.types.Table; import at.ac.tuwien.sbc.valesriegler.xvsm.PizzeriaAgentXVSM; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.*; +import java.util.List; /** * The Main class of the Pizzeria compoment. @@ -88,13 +80,15 @@ public class PizzeriaAgent { // xvsm.listenForFreeTables(); // xvsm.listenForTakenOrders(); - xvsm.listenForTakenDeliveryOrders(); + xvsm.listenForDeliveryUpdates(); // xvsm.listenForWaitingGroups(); xvsm.listenForPizzasInPreparation(); // xvsm.listenForDeliveredOrders(); xvsm.listenForPreparedPizzas(); - xvsm.listenForTables(); - xvsm.listenForGroups(); + xvsm.listenForPreparedDeliveryPizzas(); + xvsm.listenForTablesUpdates(); + xvsm.listenForGroupUpdates(); +// xvsm.listenForDeliveriesInProgress(); // xvsm.listenForPayment(); // xvsm.listenForOccupiedTables(); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/AbstractOrdersModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/AbstractOrdersModel.java index a26f78c..134bf5d 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/AbstractOrdersModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/AbstractOrdersModel.java @@ -56,7 +56,6 @@ public abstract class AbstractOrdersModel extends TableModel synchronized (items) { for (Pizza pizza : pizzas) { int orderId = pizza.getOrderId(); - log.info("pizza was done: {}", pizza.toString()); Order order = getOrderById(orderId); if(order == null) { return; 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 3c2f3a3..ff7e840 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 @@ -1,8 +1,6 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; -import at.ac.tuwien.sbc.valesriegler.common.TableModel; 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 org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,4 +54,22 @@ public class DeliveryOrdersModel extends AbstractOrdersModel return new ArrayList<>(items.values()).get(rowIndex); } } + +// public void updateInProgress(List groupsInProgress) { +// synchronized (items) { +// for (DeliveryGroupData group : groupsInProgress) { +// Order order = group.getOrder(); +// List groups = new ArrayList(items.values()); +// for (DeliveryGroupData 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/pizzeria/gui/tablemodels/OrdersOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java index 7ebf765..7ec570e 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,16 +1,15 @@ 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 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 org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.common.TableModel; -import at.ac.tuwien.sbc.valesriegler.common.Util; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class OrdersOverviewModel extends AbstractOrdersModel { private static final Logger log = LoggerFactory.getLogger(OrdersOverviewModel.class); @@ -30,11 +29,11 @@ public class OrdersOverviewModel extends AbstractOrdersModel { String wantedColumn = COLUMNS[columnIndex]; switch (wantedColumn) { case ID: - return order.getId(); + return String.format(Util.NUMBER_DISPLAY_FORMAT, order.getId()); case TABLE_ID: - return group.getTable().getId(); + return String.format(Util.NUMBER_DISPLAY_FORMAT, group.getTable().getId()); case GROUP_ID: - return order.getGroupId(); + return String.format(Util.NUMBER_DISPLAY_FORMAT, order.getGroupId()); case STATUS: return order.getStatus(); default: diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryStatus.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryStatus.java index 5348feb..1d7225a 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryStatus.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryStatus.java @@ -4,5 +4,5 @@ package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; public enum DeliveryStatus implements Serializable { - ORDERED, IS_DELIVERED, DELIVERED, PAID, DELIVERY_FAILED, START; + ORDERED, IN_PROGRESS, IS_DELIVERED, DELIVERED, PAID, DELIVERY_FAILED, START; } 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 5b9f376..270c73f 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 @@ -46,6 +46,9 @@ public abstract class AbstractXVSMConnector { protected ContainerReference pizzeriaGroupContainer; protected ContainerReference pizzeriaDeliveryContainer; protected ContainerReference pizzeriaTableContainer; + protected ContainerReference deliveryInProgress; + protected ContainerReference deliveryDone; + protected ContainerReference billPaid; protected Capi capi; protected NotificationManager notificationMgr; @@ -89,7 +92,7 @@ public abstract class AbstractXVSMConnector { Util.handleSpaceErrorAndTerminate(e); } - throw new RuntimeException("Could not Create container " + containerName); + throw new RuntimeException("Could not lookup or create container " + containerName); } protected List createCoordinators(Coordinator... coordinator) { 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 acaae9c..cc712c8 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 @@ -4,7 +4,6 @@ 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 at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceListener; -import org.mozartspaces.core.ContainerReference; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.MzsCoreException; import org.mozartspaces.core.TransactionReference; @@ -34,10 +33,11 @@ public class CookXVSM extends AbstractXVSMConnector { pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS); preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); pizzeriaGroupContainer = useContainer(Util.PIZZERIA_GROUP); + pizzeriaDeliveryContainer = useContainer(Util.PIZZERIA_DELIVERY); } public void listenForPizzas() { - SpaceListener pizzasListener = getDefaultBuilder().setCref(preparePizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { + SpaceListener pizzasListener = getDefaultBuilder().setLookaround(true).setCref(preparePizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) @@ -72,9 +72,8 @@ public class CookXVSM extends AbstractXVSMConnector { PizzaOrder pizza = createPizza(order); - final ContainerReference container = preparedPizzasContainer; - sendItemsToContainer(Arrays.asList(pizza), container, RequestTimeout.DEFAULT, tx); - final GroupData group = new GroupData(); + sendItemsToContainer(Arrays.asList(pizza), preparedPizzasContainer, RequestTimeout.DEFAULT, tx); + final GroupData group = new GroupData(); final Order groupOrder = new Order(); groupOrder.setId(pizza.getOrderId()); group.setOrder(groupOrder); @@ -107,19 +106,21 @@ public class CookXVSM extends AbstractXVSMConnector { * in preparation! */ private boolean mayPreparePizza(PizzaOrder pizzaOrder, TransactionReference tx) throws MzsCoreException { - List deliveryPizzas = readMatchingEntities(new PizzaOrder(), prepareDeliveryPizzasContainer, tx, RequestTimeout.ZERO, "BBBBBB"); + List deliveryPizzas = readMatchingEntities(new PizzaOrder(), prepareDeliveryPizzasContainer, tx, RequestTimeout.ZERO, "MAYPREPAREPIZZA: Cannot access prepareDeliveryPizzasContainer"); if (!deliveryPizzas.isEmpty()) { final PizzaOrder template = new PizzaOrder(); template.setOrderId(pizzaOrder.getOrderId()); - final List pizzasOfOrderInProgress = readMatchingEntities(template, pizzaInProgressContainer, tx, RequestTimeout.ZERO, "ASDF"); + final List pizzasOfOrderInProgress = readMatchingEntities(template, pizzaInProgressContainer, tx, RequestTimeout.ZERO, "MAYPREPAREPIZZA:: Cannot access pizzaInProgressContainer"); if (pizzasOfOrderInProgress.isEmpty()) return false; + final List pizzasAlreadyDone = readMatchingEntities(template, preparedPizzasContainer, tx, RequestTimeout.ZERO, "MAYPREPAREPIZZA: Cannot access preparedPizzasContainer"); + if(pizzasAlreadyDone.isEmpty()) return false; } return true; } public void listenForDeliveryPizzas() { - SpaceListener pizzasListener = getDefaultBuilder().setCref(prepareDeliveryPizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { + SpaceListener pizzasListener = getDefaultBuilder().setLookaround(true).setCref(prepareDeliveryPizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) @@ -148,8 +149,22 @@ public class CookXVSM extends AbstractXVSMConnector { PizzaOrder pizza = createPizza(order); - final ContainerReference container = preparedDeliveryPizzasContainer; - sendItemsToContainer(Arrays.asList(pizza), container, RequestTimeout.DEFAULT, tx); + sendItemsToContainer(Arrays.asList(pizza), preparedDeliveryPizzasContainer, RequestTimeout.DEFAULT, tx); + + final DeliveryGroupData group = new DeliveryGroupData(); + final Order groupOrder = new Order(); + groupOrder.setId(pizza.getOrderId()); + group.setOrder(groupOrder); + group.setDeliveryStatus(null); + DeliveryGroupData groupData = takeMatchingEntity(group, pizzeriaDeliveryContainer, tx, RequestTimeout.DEFAULT, "Cannot take the delivery order from pizzeriaDeliveryContainer"); + final List orderedPizzas = groupData.getOrder().getOrderedPizzas(); + for (PizzaOrder orderedPizza : orderedPizzas) { + if (orderedPizza.getId() == pizza.getId()) { + orderedPizza.setStatus(PizzaOrderStatus.DONE); + orderedPizza.setCookId(pizza.getCookId()); + } + } + sendItemsToContainer(Arrays.asList(groupData), pizzeriaDeliveryContainer, RequestTimeout.DEFAULT, tx); capi.commitTransaction(tx); log.info("I have completed preparing a delivery pizza for order {}!", pizza.getOrderId()); 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 4a2c38e..302d0a3 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 @@ -2,9 +2,21 @@ 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.DeliveryStatus; +import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; +import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceListener; +import org.mozartspaces.core.ContainerReference; +import org.mozartspaces.core.MzsConstants; +import org.mozartspaces.core.TransactionReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.Serializable; +import java.net.URI; +import java.util.Arrays; +import java.util.List; + public class DriverXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(DriverXVSM.class); @@ -18,7 +30,69 @@ public class DriverXVSM extends AbstractXVSMConnector { this.preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); this.deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); this.deliverDeliveryOrderContainer = useContainer(Util.DELIVER_DELIVERY_ORDER); + pizzeriaDeliveryContainer = useContainer(Util.PIZZERIA_DELIVERY); + deliveryDone = useContainer(Util.DELIVERY_DONE); + deliveryInProgress = useContainer(Util.DELIVERY_PROGRESS); } + public void listenForPreparedDeliveryOrders() { + SpaceListener orderListener = getDefaultBuilder().setCref(deliverDeliveryOrderContainer).setLookaround(true).setSpaceAction(new SpaceAction() { + + @Override + public void onEntriesWritten(List entries) throws Exception { + final List deliveries = castEntries(entries); + + for (DeliveryGroupData delivery : deliveries) { + DeliveryGroupData template = new DeliveryGroupData(delivery.getId()); + template.setDeliveryStatus(DeliveryStatus.ORDERED); + + final TransactionReference tx = capi.createTransaction( + 7000, + URI.create(String.format(Util.SERVER_ADDR, port))); + final String error = "Another driver wants to deliver this order!"; + + try { + // Get lock for delivering this delivery order + takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.DEFAULT, error); + final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, MzsConstants.RequestTimeout.DEFAULT, "Cannot get the delivery order!"); + group.setDriverId(driverId); + group.setDeliveryStatus(DeliveryStatus.IN_PROGRESS); + + final List groups = Arrays.asList(group); + sendItemsToContainer(groups, + deliveryInProgress, MzsConstants.RequestTimeout.ZERO, null); + sendItemsToContainer(groups, + pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null); + + Thread.sleep(3000); + + final String address = String.format(Util.SERVER_ADDR, Util.DELIVERY_CUSTOMERS_PORT); + ContainerReference cref; + try { + cref = capi.lookupContainer(group.getAddress(), URI.create(address), MzsConstants.RequestTimeout.ZERO, null); + log.debug("Looked up container {} successfully!", address); + + group.setDeliveryStatus(DeliveryStatus.DELIVERED); + + } catch (Exception ex) { + log.info("Could not find container {}", address); + + group.setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED); + } + sendItemsToContainer(groups, deliveryDone, MzsConstants.RequestTimeout.DEFAULT, tx); + sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.DEFAULT, tx); + capi.commitTransaction(tx); + + + } catch (Exception e) { + log.info("DRiverXVSM exception"); + log.info(e.getMessage()); + e.printStackTrace(); + } + } + + } + }).createSpaceListenerImpl(); + } } 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 b5abdeb..8230aa7 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 @@ -38,7 +38,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { @Override public void onEntriesWritten(List entries) throws Exception { GroupData groupData = getSingleGroup(entries); - final int groupId = groupData.getOrder().getId(); + final int groupId = groupData.getId(); SwingUtilities.invokeLater(new Runnable() { @Override @@ -123,6 +123,42 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { }).createSpaceListenerImpl(); } + public void listenForDeliveryOrdersTaken(int port) { + getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.DELIVERY_ORDER_TAKEN, port)).setSpaceAction(new SpaceAction() { + @Override + public void onEntriesWritten(List entries) throws Exception { + final List groups = castEntries(entries); + + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + GroupAgent.getInstance().getDeliveryModel().setOrdersTaken(groups); + } + }); + } + }).createSpaceListenerImpl(); + } + + private void listenForDeliveredDeliveryOrders(int port) { + getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.DELIVERY_DONE, port)).setSpaceAction(new SpaceAction() { + @Override + public void onEntriesWritten(List entries) throws Exception { + final List groups = castEntries(entries); + + log.info("DElivered orders!"); + + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + GroupAgent.getInstance().getDeliveryModel().setOrdersDelivered(groups); + } + }); + } + }).createSpaceListenerImpl(); + } + public void listenForNewPizzerias() { SpaceListener newPizzeriaListener = getDefaultBuilder().setCref(groupAgentInfoContainer).setLookaround(true).setSpaceAction(new SpaceAction() { @@ -160,6 +196,8 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { listenForPaymentRequest(port); listenForTableAssigned(port); listenForOrdersTaken(port); + listenForDeliveryOrdersTaken(port); + listenForDeliveredDeliveryOrders(port); } public void sendNewGroupsToSpace(List groupData, int pizzeriaSpacePort) { @@ -176,6 +214,15 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } public void sendNewDeliveriesToSpace(List groupData, int pizzeriaSpacePort) { - sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PHONE_CALLS, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); + try { + TransactionReference tx = capi.createTransaction( + Util.SPACE_TRANSACTION_TIMEOUT, + URI.create(format(Util.SERVER_ADDR, pizzeriaSpacePort))); + sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PHONE_CALLS, pizzeriaSpacePort), RequestTimeout.DEFAULT, tx); + sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PIZZERIA_DELIVERY, pizzeriaSpacePort), RequestTimeout.DEFAULT, tx); + capi.commitTransaction(tx); + } catch (MzsCoreException e) { + Util.handleSpaceErrorAndTerminate(e); + } } } 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 c100f3c..947b870 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 @@ -33,6 +33,9 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); pizzeriaTableContainer = useContainer(Util.PIZZERIA_TABLE); pizzeriaGroupContainer = useContainer(Util.PIZZERIA_GROUP); + preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); + pizzeriaDeliveryContainer = useContainer(Util.PIZZERIA_DELIVERY); + } public void listenForOccupiedTables() { @@ -55,7 +58,7 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { }).createSpaceListenerImpl(); } - public void listenForTables() { + public void listenForTablesUpdates() { SpaceListener tablesListener = getDefaultBuilder().setCref(pizzeriaTableContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { @@ -80,13 +83,11 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { }).createSpaceListenerImpl(); } - public void listenForGroups() { + public void listenForGroupUpdates() { SpaceListener groupsListener = getDefaultBuilder().setCref(pizzeriaGroupContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { - log.info("listenForGroups!!!!!"); - final List groups = castEntries(entries); final List waitingGroups = getWaitingGroups(groups); final List orderedGroups = getOrderedGroups(groups); @@ -126,53 +127,8 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { return waitingGroups; } - public void listenForFreeTables() { - SpaceListener tablesListener = getDefaultBuilder().setCref(freeTablesContainer).setSpaceAction(new SpaceAction() { - - @Override - public void onEntriesWritten(List entries) throws Exception { - log.info("A table has become free"); - - 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); - } - }); - } - }).createSpaceListenerImpl(); - } - - public void listenForWaitingGroups() { - - SpaceListener groupsListener = getDefaultBuilder().setCref(assignTableContainer).setSpaceAction(new SpaceAction() { - @Override - public void onEntriesWritten(List entries) throws Exception { - final List groups = castEntries(entries); - - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getGroupModel().addItems(groups); - } - }); - - } - }).createSpaceListenerImpl(); - } - - public void listenForTakenDeliveryOrders() { - SpaceListener orderTakenListener = getDefaultBuilder().setCref(deliveryOrderTakenContainer).setSpaceAction(new SpaceAction() { + public void listenForDeliveryUpdates() {//deliveryOrderTakenContainer + SpaceListener orderTakenListener = getDefaultBuilder().setCref(pizzeriaDeliveryContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); @@ -188,23 +144,24 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { }).createSpaceListenerImpl(); } -public void listenForTakenOrders() { - - SpaceListener orderTakenListener = getDefaultBuilder().setCref(orderTakenContainer).setSpaceAction(new SpaceAction() { - @Override - public void onEntriesWritten(List entries) throws Exception { - final List groups = castEntries(entries); - - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getOrdersModel().addItems(groups); - } - }); - } - }).createSpaceListenerImpl(); - } +// public void listenForDeliveriesInProgress() { +// SpaceListener pizzasInProgress = getDefaultBuilder().setCref(deliveryInProgress).setSpaceAction(new SpaceAction() { +// +// @Override +// public void onEntriesWritten(List entries) throws Exception { +// final List groups = castEntries(entries); +// +// SwingUtilities.invokeLater(new Runnable() { +// @Override +// public void run() { +// PizzeriaAgent.getInstance().getDeliveryOrdersModel().addItems(groups); +// } +// }); +// +// } +// }).createSpaceListenerImpl(); +// } public void listenForPizzasInPreparation() { SpaceListener pizzasInProgress = getDefaultBuilder().setCref(pizzaInProgressContainer).setSpaceAction(new SpaceAction() { @@ -258,7 +215,6 @@ public void listenForTakenOrders() { @Override public void run() { PizzeriaAgent.getInstance().getOrdersModel().updateStatusOfPizzasDone(pizzas); - PizzeriaAgent.getInstance().getDeliveryOrdersModel().updateStatusOfPizzasDone(pizzas); } }); @@ -266,6 +222,24 @@ public void listenForTakenOrders() { }).createSpaceListenerImpl(); } + public void listenForPreparedDeliveryPizzas() { + SpaceListener preparedPizzasListener = getDefaultBuilder().setCref(preparedDeliveryPizzasContainer).setSpaceAction(new SpaceAction() { + @Override + public void onEntriesWritten(List entries) throws Exception { + final List pizzas = castEntries(entries); + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + PizzeriaAgent.getInstance().getDeliveryOrdersModel().updateStatusOfPizzasDone(pizzas); + } + }); + + } + }).createSpaceListenerImpl(); + } + + public void listenForPayment() { SpaceListener isGoneListener = getDefaultBuilder().setCref(paymentDoneContainer).setSpaceAction(new SpaceAction() { @Override @@ -292,7 +266,6 @@ public void listenForTakenOrders() { }).createSpaceListenerImpl(); } - public void sendFreeTablesToContainer(List
tables) { sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null); sendItemsToContainer(tables, pizzeriaTableContainer, RequestTimeout.DEFAULT, null); 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 38ee3c1..e957d93 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 @@ -42,6 +42,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { deliverDeliveryOrderContainer = useContainer(Util.DELIVER_DELIVERY_ORDER); pizzeriaGroupContainer = useContainer(Util.PIZZERIA_GROUP); pizzeriaTableContainer = useContainer(Util.PIZZERIA_TABLE); + pizzeriaDeliveryContainer = useContainer(Util.PIZZERIA_DELIVERY); } public void listenForPhoneOrders() { @@ -53,22 +54,26 @@ public class WaiterXVSM extends AbstractXVSMConnector { for (DeliveryGroupData phoneOrder : phoneOrders) { final int id = phoneOrder.getId(); final DeliveryGroupData template = new DeliveryGroupData(id); - final String errorMsg = String.format("There was phone call with id %d", id); + final String errorMsg = String.format("There was phone call with id %d. Another waiter already responded!", id); final TransactionReference tx = getDefaultTransaction(); try { - final DeliveryGroupData groupFromSpace = takeMatchingEntity(template, phoneCallsContainer, tx, RequestTimeout.DEFAULT, errorMsg); - groupFromSpace.setWaiterIdOfOrder(WaiterXVSM.this.waiterId); - final Order order = groupFromSpace.getOrder(); - groupFromSpace.setDeliveryStatus(DeliveryStatus.ORDERED); + // Get the the delete lock so that only one waiter can answer this particular phone call + takeMatchingEntity(template, phoneCallsContainer, tx, RequestTimeout.DEFAULT, errorMsg); - updatePizzeriaOrderNumber(order, tx); + final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, RequestTimeout.DEFAULT, errorMsg); + group.setWaiterIdOfOrder(WaiterXVSM.this.waiterId); + final Order order = group.getOrder(); + group.setDeliveryStatus(DeliveryStatus.ORDERED); + updatePizzeriaOrderNumber(order, tx); // send the order as a whole to the space - final List groupsWhoHaveOrdered = Arrays.asList(groupFromSpace); + final List groupsWhoHaveOrdered = Arrays.asList(group); sendItemsToContainer(groupsWhoHaveOrdered, deliveryOrderTakenContainer, RequestTimeout.ZERO, tx); + sendItemsToContainer(groupsWhoHaveOrdered, + pizzeriaDeliveryContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(order.getOrderedPizzas(), prepareDeliveryPizzasContainer, RequestTimeout.ZERO, tx); capi.commitTransaction(tx); @@ -286,7 +291,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { order.setId(orderId); TransactionReference tx = getDefaultTransaction(); - final boolean isDeliveryPizza = pizza.isDeliveryPizza(); try { GroupData entity = new GroupData(); @@ -311,9 +315,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { final List groupsWithCompleteOrder = Arrays.asList(groupData); if (pizzasOfOrder.size() == numberOfPizzas) { - for (PizzaOrder pizza1 : groupData.getOrder().getOrderedPizzas()) { - log.info("STAtus of pizza: {}", pizza1.getStatus()); - } groupData.setServingWaiter(waiterId); groupData.setState(GroupState.EATING); groupData.getOrder().setStatus(OrderStatus.DELIVERED); @@ -371,9 +372,12 @@ public class WaiterXVSM extends AbstractXVSMConnector { entity.setDeliveryStatus(null); entity.setOrder(order); - DeliveryGroupData groupData = takeMatchingEntity(entity, + takeMatchingEntity(entity, deliveryOrderTakenContainer, tx, RequestTimeout.DEFAULT, "Another driver just checks if the delivery order is complete"); + DeliveryGroupData groupData = takeMatchingEntity(entity, + pizzeriaDeliveryContainer, tx, RequestTimeout.DEFAULT, + "Waiter cannot take the delivery order from Space!"); int groupId = groupData.getId(); int numberOfPizzas = groupData.getOrder().getNumberOfPizzas(); @@ -386,15 +390,13 @@ public class WaiterXVSM extends AbstractXVSMConnector { "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); + final List groupsWithCompleteOrder = Arrays.asList(groupData); sendItemsToContainer(groupsWithCompleteOrder, deliverDeliveryOrderContainer, RequestTimeout.DEFAULT, tx); + sendItemsToContainer(groupsWithCompleteOrder, + pizzeriaDeliveryContainer, RequestTimeout.DEFAULT, + tx); capi.commitTransaction(tx); } else { 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 c58a300..37de548 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,7 +24,7 @@ public class Driver { private void start() { xvsm = new DriverXVSM(id, port); -// xvsm.listenForPreparedPizzas(); + xvsm.listenForPreparedDeliveryOrders(); } public Driver(int id, int port) { -- 2.43.0