From c45cbde1cc5c36a4baf0d218e516df4d9ab31e65 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Fri, 31 May 2013 21:43:13 +0200 Subject: [PATCH] Some UI Fixes in PizzeriaGUI inter-table communication. [XVSM] Create delivery groups. Let the waiter answer the phone and take the order to the kitchen. --- .../tuwien/sbc/valesriegler/common/Util.java | 5 + .../jms/messageListeners/OrdersToCook.java | 2 +- .../sbc/valesriegler/group/DeliveryGroup.java | 2 +- .../sbc/valesriegler/group/GroupAgent.java | 26 +- .../group/gui/GroupCreationHandler.java | 14 +- .../group/gui/GroupCreationPanel.java | 14 +- .../valesriegler/pizzeria/PizzeriaAgent.java | 1 + .../pizzeria/gui/PizzeriaFrame.java | 22 +- .../gui/tablemodels/DeliveryOrdersModel.java | 12 +- .../valesriegler/types/DeliveryGroupData.java | 10 +- .../sbc/valesriegler/types/GroupData.java | 3 +- .../sbc/valesriegler/types/HasOrder.java | 7 + .../tuwien/sbc/valesriegler/types/Pizza.java | 17 +- .../sbc/valesriegler/types/PizzaOrder.java | 16 +- .../xvsm/AbstractXVSMConnector.java | 6 +- .../sbc/valesriegler/xvsm/CookXVSM.java | 16 +- .../sbc/valesriegler/xvsm/DriverXVSM.java | 92 +++ .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 16 +- .../valesriegler/xvsm/PizzeriaAgentXVSM.java | 74 ++- .../sbc/valesriegler/xvsm/WaiterXVSM.java | 618 ++++++++++-------- .../sbc/valesriegler/xvsm/waiter/Waiter.java | 2 + 21 files changed, 614 insertions(+), 361 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/types/HasOrder.java 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 753bb25..77a8692 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 @@ -28,8 +28,11 @@ public abstract class Util { 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 PREPARE_PIZZAS = "preparePizzas"; public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress"; public static final String ORDER_COMPLETE = "orderComplete"; @@ -40,6 +43,7 @@ public abstract class Util { 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"; private static Random random = new Random(); @@ -48,6 +52,7 @@ public abstract class Util { public static final String SERVER_ADDR = "xvsm://localhost:%d"; public static final int GROUP_AGENT_PORT = 9876; + public static final int DELIVERY_CUSTOMERS_PORT = 9877; public static final String JMS_CONNECTSTRING = "tcp://localhost:61616?jms.prefetchPolicy.all=1"; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java index b9a2dd3..0bdb17e 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java @@ -68,7 +68,7 @@ public class OrdersToCook implements MessageListener { for (PizzaOrder po : orderrequest.getGroupdata().getOrder().getOrderedPizzas()) { Thread.sleep(po.getPizzaType().duration * 1000); po.setStatus(PizzaOrderStatus.DONE); - Pizza p = Pizza.createPizzaFromPizzaOrder(po, cook.getId()); + Pizza p = Pizza.createPizzaFromPizzaOrder(po, cook.getId(), false); orderrequest.getGroupdata().getOrder().getCookedPizzas().add(p); } orderrequest.getGroupdata().getOrder().setStatus(OrderStatus.DELIVERY_PENDING); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java index 36d5ea9..3e61504 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java @@ -8,7 +8,7 @@ import org.slf4j.LoggerFactory; public class DeliveryGroup implements Runnable, HasId { private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class); - private static int idNext = 0; // TODO: set to 0 after debugging! + private static int idNext = 0; private DeliveryGroupData deliveryGroupData; 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 31a84a1..34f4b09 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 @@ -4,6 +4,7 @@ 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; 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; @@ -59,6 +60,7 @@ public class GroupAgent { } public GroupAgent() { + deliveryOverviewModel = new DeliveryOverviewModel(); groupModel = new GroupOverviewModel(); if (Util.useJMS) { this.groupconn = new JMSGroupConnector(); @@ -112,7 +114,7 @@ public class GroupAgent { } xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort); - log.info("New Groups were sent to the space"); + log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort); // start the space group in a new thread for (GroupData group : groupData) { @@ -124,6 +126,26 @@ public class GroupAgent { public void onDeliveryGroupsCreated(List newDeliveryGroups) { - //To change body of created methods use File | Settings | File Templates. + if (!Util.useJMS) { + List groupData = new ArrayList<>(); + for (DeliveryGroup group : newDeliveryGroups) { + groupData.add(group.getDeliveryGroupData()); + } + final String pizzeriaId = groupData.get(0).getPizzeriaId(); + + int pizzeriaSpacePort = 0; + try { + pizzeriaSpacePort = Integer.parseInt(pizzeriaId); + } catch (NumberFormatException e) { + log.error("Pizzeria Identifier should be an integer in the XVSM version!"); + } + + xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort); + + log.info("New delivery groups were sent to the pizzeria space of port {}", 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 19f4c5e..1d0501a 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 @@ -19,13 +19,13 @@ import java.util.List; * group creation request should be issued */ class GroupCreationHandler { - private final boolean isDeliveryGroup; + private final boolean createDeliveryGroups; private JPanel creationPanel; private JPanel chooseGroupSizePanel; private JPanel groupCreationPanel; - public GroupCreationHandler(JPanel creationPanel, JPanel chooseGroupSizePanel, JPanel groupCreationPanel, boolean isDeliveryGroup) { - this.isDeliveryGroup = isDeliveryGroup; + public GroupCreationHandler(JPanel creationPanel, JPanel chooseGroupSizePanel, JPanel groupCreationPanel, boolean createDeliveryGroups) { + this.createDeliveryGroups = createDeliveryGroups; this.creationPanel = creationPanel; this.chooseGroupSizePanel = chooseGroupSizePanel; this.groupCreationPanel = groupCreationPanel; @@ -48,7 +48,7 @@ class GroupCreationHandler { List newNormalGroups = new ArrayList<>(); for (int i = 0; i < gc.numberOfGroups; i++) { - if(isDeliveryGroup) { + if(createDeliveryGroups) { DeliveryGroup group = new DeliveryGroup(); final Order order = createOrder(group, gc); group.getDeliveryGroupData().setOrder(order); @@ -63,7 +63,7 @@ class GroupCreationHandler { newNormalGroups.add(group); } } - if(isDeliveryGroup) { + if(createDeliveryGroups) { DeliveryOverviewModel deliveryOverviewModel = GroupAgent.getInstance().getDeliveryModel(); deliveryOverviewModel.addItems(newDeliveryGroups); GroupAgent.getInstance().onDeliveryGroupsCreated(newDeliveryGroups); @@ -77,7 +77,9 @@ class GroupCreationHandler { private Order createOrder(HasId group, GroupCreationDetailsRequest gc) { List pizzaOrders = new ArrayList<>(); for (PizzaType pt : gc.pizzaTypes) { - pizzaOrders.add(new PizzaOrder(pt)); + final PizzaOrder pizzaOrder = new PizzaOrder(pt); + pizzaOrder.setDeliveryPizza(createDeliveryGroups); + pizzaOrders.add(pizzaOrder); } Order order = new Order(group, pizzaOrders); order.setNumberOfPizzas(order.getOrderedPizzas().size()); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java index fcf056c..9073877 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java @@ -9,10 +9,10 @@ import java.awt.event.ActionListener; public class GroupCreationPanel extends JPanel { - private boolean withAddress; + private boolean createDeliveryGroups; - public GroupCreationPanel(final boolean withAddress) { - this.withAddress = withAddress; + public GroupCreationPanel(final boolean createDeliveryGroups) { + this.createDeliveryGroups = createDeliveryGroups; final JPanel chooseGroupSizePanel = new JPanel(); JLabel creationLabel = new JLabel("How many members should the group have?"); @@ -26,10 +26,10 @@ public class GroupCreationPanel extends JPanel { @Override public void actionPerformed(ActionEvent e) { int numberMembers = (int) spinner.getValue(); - GroupCreationPanel.this.withAddress = withAddress; - final GroupCreationDetailsPanel groupCreationDetailsPanel = new GroupCreationDetailsPanel(numberMembers, GroupCreationPanel.this.withAddress); + GroupCreationPanel.this.createDeliveryGroups = createDeliveryGroups; + final GroupCreationDetailsPanel groupCreationDetailsPanel = new GroupCreationDetailsPanel(numberMembers, GroupCreationPanel.this.createDeliveryGroups); GroupCreationHandler groupCreationHandler = new GroupCreationHandler(GroupCreationPanel.this, chooseGroupSizePanel, - groupCreationDetailsPanel, GroupCreationPanel.this.withAddress); + groupCreationDetailsPanel, GroupCreationPanel.this.createDeliveryGroups); groupCreationDetailsPanel.setCreateAndCancelHandler(groupCreationHandler); groupCreationHandler.showGroupCreationDetailPanel(); @@ -39,7 +39,7 @@ public class GroupCreationPanel extends JPanel { GridLayout creationPanelLayout = new GridLayout(3, 1); chooseGroupSizePanel.setLayout(creationPanelLayout); - final String title = withAddress ? "Create Delivery Groups" : "Create Normal Groups"; + final String title = createDeliveryGroups ? "Create Delivery Groups" : "Create Normal Groups"; this.setBorder(new TitledBorder(title)); this.add(chooseGroupSizePanel); 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 58551db..ac114f1 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 @@ -86,6 +86,7 @@ public class PizzeriaAgent { xvsm.listenForFreeTables(); xvsm.listenForTakenOrders(); + xvsm.listenForTakenDeliveryOrders(); xvsm.listenForWaitingGroups(); xvsm.listenForPizzasInPreparation(); xvsm.listenForDeliveredOrders(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java index 8ef5880..309770a 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java @@ -19,6 +19,8 @@ import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; +import at.ac.tuwien.sbc.valesriegler.types.Order; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -130,11 +132,14 @@ public class PizzeriaFrame extends JFrame { int minIndex = lsm.getMinSelectionIndex(); if (minIndex < 0) return; - GroupData currentGroup = PizzeriaAgent.getInstance().getOrdersModel().getGroupOfRow(minIndex); - detailsPanel.setBorder(new TitledBorder(String.format(WAITER_IDS_CAPTION, currentGroup.getOrder().getId()))); - pizzasPanel.setBorder(new TitledBorder(String.format(PIZZAS_CAPTION, currentGroup.getOrder().getId()))); - PizzeriaAgent.getInstance().getWaitersModel().setCurrentGroup(currentGroup); - PizzeriaAgent.getInstance().getPizzasOfOrderModel().setCurrentOrder(currentGroup.getOrder()); + DeliveryGroupData currentGroup = PizzeriaAgent.getInstance().getDeliveryOrdersModel().getGroupOfRow(minIndex); + final Order order = currentGroup.getOrder(); + detailsPanel.setBorder(new TitledBorder(String.format(WAITER_IDS_CAPTION, order.getId()))); + pizzasPanel.setBorder(new TitledBorder(String.format(PIZZAS_CAPTION, order.getId()))); + PizzeriaAgent.getInstance().getDeliveryDetailsModel().setCurrentDelivery(currentGroup); + PizzeriaAgent.getInstance().getPizzasOfDeliveryModel().setCurrentOrder(order); + + log.info("THIS IS THE ORDER: {}", order.toString()); detailsPanel.setVisible(true); pizzasPanel.setVisible(true); @@ -150,6 +155,13 @@ public class PizzeriaFrame extends JFrame { PizzeriaAgent.getInstance().getWaitersModel().fireTableDataChanged(); } }); + PizzeriaAgent.getInstance().getDeliveryOrdersModel().addTableModelListener(new TableModelListener() { + @Override + public void tableChanged(TableModelEvent e) { + PizzeriaAgent.getInstance().getPizzasOfDeliveryModel().fireTableDataChanged(); + PizzeriaAgent.getInstance().getDeliveryDetailsModel().fireTableDataChanged(); + } + }); detailWrapper.add(detailsPanel); detailWrapper.add(pizzasPanel); 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 fedbe7d..07bf7a3 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 @@ -2,6 +2,7 @@ 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; @@ -13,12 +14,13 @@ public class DeliveryOrdersModel extends TableModel { private static final Logger log = LoggerFactory.getLogger(DeliveryOrdersModel.class); private static final String ID = "Delivery ID"; + private static final String ORDER_ID = "Order ID"; private static final String DELIVERY_ADDRESS = "Address"; private static final String STATUS = "Status"; private static final String LOAD_BALANCER_ID = "Moved by"; private static final String ORIGINAL_PIZZERIA = "Moved from"; - private static final String[] COLUMNS = new String[] { ID, DELIVERY_ADDRESS, STATUS, LOAD_BALANCER_ID, ORIGINAL_PIZZERIA }; + private static final String[] COLUMNS = new String[] { ID, ORDER_ID, DELIVERY_ADDRESS, STATUS, LOAD_BALANCER_ID, ORIGINAL_PIZZERIA }; @Override protected String[] getColumns() { @@ -33,6 +35,8 @@ public class DeliveryOrdersModel extends TableModel { String wantedColumn = COLUMNS[columnIndex]; switch (wantedColumn) { case ID: + return group.getId(); + case ORDER_ID: return order.getId(); case DELIVERY_ADDRESS: return group.getAddress(); @@ -46,4 +50,10 @@ public class DeliveryOrdersModel extends TableModel { throw new RuntimeException(UNHANDLEDCOLUMN); } } + + public DeliveryGroupData getGroupOfRow(int rowIndex) { + synchronized (items) { + return new ArrayList<>(items.values()).get(rowIndex); + } + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryGroupData.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryGroupData.java index 1ad794b..9187c62 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryGroupData.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryGroupData.java @@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory; import java.io.Serializable; @Queryable(autoindex = true) -public class DeliveryGroupData implements Serializable, HasId { +public class DeliveryGroupData implements Serializable, HasOrder { private static final Logger log = LoggerFactory.getLogger(GroupData.class); private Integer id; @@ -31,6 +31,10 @@ public class DeliveryGroupData implements Serializable, HasId { this.id = id; } + public DeliveryGroupData() { + //To change body of created methods use File | Settings | File Templates. + } + public void setId(Integer id) { this.id = id; } @@ -51,8 +55,8 @@ public class DeliveryGroupData implements Serializable, HasId { return originalPizzeriaId; } - public Integer getDriverId() { - return driverId; + public int getDriverId() { + return Util.getIntSafe(driverId); } public void setDriverId(Integer driverId) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java index 094da95..9f80b5f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java @@ -19,7 +19,7 @@ import at.ac.tuwien.sbc.valesriegler.common.Util; * */ @Queryable(autoindex = true) -public class GroupData implements Serializable, HasId { +public class GroupData implements Serializable, HasOrder { private static final Logger log = LoggerFactory.getLogger(GroupData.class); private Integer id; @@ -51,6 +51,7 @@ public class GroupData implements Serializable, HasId { return Util.getIntSafe(id); } + @Override public Order getOrder() { return order; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/HasOrder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/HasOrder.java new file mode 100644 index 0000000..27231c1 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/HasOrder.java @@ -0,0 +1,7 @@ +package at.ac.tuwien.sbc.valesriegler.types; + +import at.ac.tuwien.sbc.valesriegler.common.HasId; + +public interface HasOrder extends HasId { + Order getOrder(); +} 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 ef30948..63f745e 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 @@ -25,24 +25,21 @@ public class Pizza extends PizzaOrder implements Serializable { */ private Integer idOfOrder; - // private Person consumer; - - private Pizza(int id, PizzaType type, int cookId, int orderId) { - super(id); - super.setCookId(cookId); + private Pizza(int id, PizzaType type, int cookId, int orderId, boolean isDeliveryPizza) { this.id = id; - pizzaType = type; + this.pizzaType = type; this.cookId = cookId; - idOfOrder = orderId; - status = PizzaOrderStatus.DONE; + this.idOfOrder = orderId; + this.isDeliveryPizza = isDeliveryPizza; + this.status = PizzaOrderStatus.DONE; } public Pizza() { super(); } - public static Pizza createPizzaFromPizzaOrder(PizzaOrder pizzaorder, int cookId) { - return new Pizza(pizzaorder.getId(), pizzaorder.getPizzaType(), cookId, pizzaorder.getOrderId()); + public static Pizza createPizzaFromPizzaOrder(PizzaOrder pizzaorder, int cookId, boolean isDeliveryPizza) { + return new Pizza(pizzaorder.getId(), pizzaorder.getPizzaType(), cookId, pizzaorder.getOrderId(), isDeliveryPizza); } @Override 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 4a1929e..09cb0ff 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 @@ -19,20 +19,30 @@ public class PizzaOrder implements Serializable, HasId { protected PizzaType pizzaType; protected PizzaOrderStatus status; protected Integer cookId; - + + protected boolean isDeliveryPizza; + public PizzaOrder(PizzaType pizzaType) { id = ++nextID; this.pizzaType = pizzaType; status = PizzaOrderStatus.NEW; } - + public PizzaOrder(int id) { - this.id = id; + this.id = id; } public PizzaOrder() { } + public void setDeliveryPizza(boolean deliveryPizza) { + isDeliveryPizza = deliveryPizza; + } + + public boolean isDeliveryPizza() { + return isDeliveryPizza; + } + 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 4e0b861..8c05433 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 @@ -33,9 +33,11 @@ public abstract class AbstractXVSMConnector { protected ContainerReference assignTableContainer; protected ContainerReference takeOrderContainer; protected ContainerReference orderTakenContainer; + protected ContainerReference deliveryOrderTakenContainer; protected ContainerReference groupAgentOrderTakenContainer; protected ContainerReference preparePizzasContainer; protected ContainerReference deliverPizzasContainer; + protected ContainerReference deliverDeliveryPizzasContainer; protected ContainerReference paymentRequestContainer; protected ContainerReference groupAgentPaymentRequestContainer; protected ContainerReference freeTablesContainer; @@ -45,10 +47,12 @@ public abstract class AbstractXVSMConnector { protected ContainerReference paymentDoneContainer; protected ContainerReference groupAgentPaymentDoneContainer; protected ContainerReference pizzeriaInfoContainer; + protected ContainerReference phoneCallsContainer; protected ContainerReference groupAgentInfoContainer; + protected ContainerReference groupAgentDeliveryOrderTakenContainer; protected Capi capi; - protected NotificationManager notificationMgr; + protected NotificationManager notificationMgr; protected int port; public AbstractXVSMConnector(int port) { 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 525cea5..8fbb713 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 @@ -7,6 +7,7 @@ import java.util.Collections; import java.util.List; 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.TransactionReference; @@ -31,6 +32,7 @@ public class CookXVSM extends AbstractXVSMConnector { deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS); preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS); pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS) ; + deliverDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); } public void listenForPizzas() { @@ -45,8 +47,9 @@ public class CookXVSM extends AbstractXVSMConnector { if(inNotification.get()) Collections.shuffle(pizzas); for (PizzaOrder pizzaOrder : pizzas) { - - TransactionReference tx = capi.createTransaction(9000, URI.create(String.format(Util.SERVER_ADDR, port))); + + final boolean deliveryPizza = pizzaOrder.isDeliveryPizza(); + 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 { @@ -54,7 +57,7 @@ public class CookXVSM extends AbstractXVSMConnector { 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); + Pizza pizzaInProgress = Pizza.createPizzaFromPizzaOrder(order, cookId, deliveryPizza); pizzaInProgress.setStatus(PizzaOrderStatus.IN_PREPARATION); @@ -62,8 +65,9 @@ public class CookXVSM extends AbstractXVSMConnector { sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, null); PizzaOrder pizza = createPizza(order); - - sendItemsToContainer(Arrays.asList(pizza), deliverPizzasContainer, RequestTimeout.DEFAULT, tx); + + final ContainerReference container = deliveryPizza ? deliverDeliveryPizzasContainer : deliverPizzasContainer; + sendItemsToContainer(Arrays.asList(pizza), container, RequestTimeout.DEFAULT, tx); capi.commitTransaction(tx); log.info("I have completed preparing a pizza for order {}!", pizza.getOrderId()); @@ -91,7 +95,7 @@ public class CookXVSM extends AbstractXVSMConnector { long duration = order.getPizzaType().duration; Thread.sleep(duration * 1000); - PizzaOrder pizza = Pizza.createPizzaFromPizzaOrder(order, cookId); + PizzaOrder pizza = Pizza.createPizzaFromPizzaOrder(order, cookId, false); pizza.setStatus(PizzaOrderStatus.DONE); return pizza; } 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 69a6393..8adc7f9 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 @@ -1,9 +1,20 @@ 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; +import org.mozartspaces.core.TransactionReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.Serializable; +import java.util.Arrays; +import java.util.List; + public class DriverXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(DriverXVSM.class); @@ -14,5 +25,86 @@ public class DriverXVSM extends AbstractXVSMConnector { this.driverId = id; + this.deliverDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); + + } + + public void listenForPreparedPizzas() { + /** + * 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, deliverDeliveryPizzasContainer) { + + @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(); + final boolean isDeliveryPizza = pizza.isDeliveryPizza(); + + try { + DeliveryGroupData entity = new DeliveryGroupData(); + entity.setDeliveryStatus(null); + entity.setOrder(order); + + DeliveryGroupData groupData = takeMatchingEntity(entity, + orderTakenContainer, tx, MzsConstants.RequestTimeout.DEFAULT, + "Another driver just checks if the delivery order is complete"); + int groupId = groupData.getId(); + int numberOfPizzas = order.getNumberOfPizzas(); + + Pizza pizzaTemplate = new Pizza(); + pizzaTemplate.setOrderId(orderId); + + List pizzasOfOrder = takeMatchingEntities( + pizzaTemplate, deliverPizzasContainer, tx, + MzsConstants.RequestTimeout.DEFAULT, + "Cannot take the pizzas from the deliverDeliveryPizzasContainer"); + + if (pizzasOfOrder.size() == numberOfPizzas) { + DeliveryGroupData group = new DeliveryGroupData(); + group.setDriverId(driverId); + Order completeOrder = new Order(); + completeOrder.setId(orderId); + completeOrder.setGroupId(groupId); + group.setOrder(completeOrder); + final List groupsWithCompleteOrder = Arrays.asList(group); + sendItemsToContainer(groupsWithCompleteOrder, + orderCompleteContainer, MzsConstants.RequestTimeout.DEFAULT, + tx); + sendItemsToContainer(groupsWithCompleteOrder, + groupAgentOrderCompleteContainer, MzsConstants.RequestTimeout.DEFAULT, + null); + + 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, deliverDeliveryPizzasContainer); } } 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 9087f2f..c22520e 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 @@ -2,9 +2,13 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupData; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.Table; import org.mozartspaces.core.MzsConstants.RequestTimeout; +import org.mozartspaces.core.MzsCoreException; +import org.mozartspaces.core.TransactionReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -123,10 +127,6 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { createNotification(tableAssignmentListener, groupAgentOrderTakenContainer); } - public void sendNewGroupsToSpace(List groupData, int pizzeriaSpacePort) { - sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.ASSIGN_TABLE, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); - } - public void listenForNewPizzerias() { SpaceListener newPizzeriaListener = new SpaceListenerImpl(capi, groupAgentInfoContainer, true) { @@ -150,4 +150,12 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { createNotification(newPizzeriaListener, groupAgentInfoContainer); } + + public void sendNewGroupsToSpace(List groupData, int pizzeriaSpacePort) { + sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.ASSIGN_TABLE, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); + } + + public void sendNewDeliveriesToSpace(List groupData, int pizzeriaSpacePort) { + sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PHONE_CALLS, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); + } } 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 8addaa5..01c7a64 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 @@ -7,6 +7,7 @@ import java.util.List; 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.notifications.Notification; import org.mozartspaces.notifications.NotificationListener; @@ -16,10 +17,6 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; -import at.ac.tuwien.sbc.valesriegler.types.GroupData; -import at.ac.tuwien.sbc.valesriegler.types.Pizza; -import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; -import at.ac.tuwien.sbc.valesriegler.types.Table; public class PizzeriaAgentXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(PizzeriaAgentXVSM.class); @@ -35,7 +32,8 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { orderTakenContainer = useContainer(Util.ORDER_TAKEN); assignTableContainer = useContainer(Util.ASSIGN_TABLE); pizzeriaInfoContainer = useContainer(Util.PIZZERIA_INFO); - } + deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); + } public void listenForOccupiedTables() { NotificationListener tablesListener = new NotificationListener() { @@ -112,26 +110,48 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { handleSpaceErrorAndTerminate(e); } } - + + public void listenForTakenDeliveryOrders() { + NotificationListener orderTakenListener = new NotificationListener() { + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + final List groups = castEntries(entries); + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + PizzeriaAgent.getInstance().getDeliveryOrdersModel().addItems(groups); + } + }); + + } + }; + try { + notificationMgr.createNotification(deliveryOrderTakenContainer, orderTakenListener, Operation.WRITE); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); + } + } + public void listenForTakenOrders() { - + NotificationListener orderTakenListener = new NotificationListener() { @Override public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { 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)); } }); - + } }; try { @@ -146,19 +166,19 @@ public void listenForTakenOrders() { @Override public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { final List pizzas = castEntries(entries); - + log.info("{} pizzas in progress changes!", pizzas.size()); for (PizzaOrder pizza : pizzas) { log.info(pizza.toString()); } - + SwingUtilities.invokeLater(new Runnable() { @Override public void run() { PizzeriaAgent.getInstance().getOrdersModel().updatePizzasInPreparation(pizzas); } }); - + } }; try { @@ -173,20 +193,20 @@ public void listenForTakenOrders() { @Override public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { 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); } }); - + } }; try { @@ -200,22 +220,22 @@ public void listenForTakenOrders() { NotificationListener preparedPizzasListener = new NotificationListener() { @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); - + SwingUtilities.invokeLater(new Runnable() { @Override public void run() { PizzeriaAgent.getInstance().getOrdersModel().updateStatusOfPizzasDone(pizza); } }); - + } }; - + try { notificationMgr.createNotification(deliverPizzasContainer, preparedPizzasListener, Operation.WRITE); log.info("Created deliverPizzasContainer notification for the pizzeria"); @@ -229,22 +249,22 @@ public void listenForTakenOrders() { @Override public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { final List groups = castEntries(entries); - + final GroupData group = groups.get(0); log.info("Group {} is gone", group.getId()); - + 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); } }); - + Table table = new Table(tableId); - + sendItemsToContainer(Arrays.asList(table), freeTablesContainer, RequestTimeout.DEFAULT, null); } }; @@ -255,11 +275,11 @@ public void listenForTakenOrders() { } } + public void sendFreeTablesToContainer(List tables) { sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null); } - public void initializeOrderId() { sendItemsToContainer(Arrays.asList(new OrderId(0)), pizzeriaInfoContainer, 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 815383e..27369af 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 @@ -1,276 +1,328 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; -import java.io.Serializable; -import java.util.Arrays; -import java.util.List; - import at.ac.tuwien.sbc.valesriegler.common.OrderId; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.*; +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 java.io.Serializable; +import java.util.Arrays; +import java.util.List; public class WaiterXVSM extends AbstractXVSMConnector { - private static final Logger log = LoggerFactory.getLogger(WaiterXVSM.class); - private final int waiterId; - - public WaiterXVSM(int waiterId, int port) { - super(port); - - this.waiterId = waiterId; - - freeTablesContainer = useContainer(Util.FREE_TABLES); - assignTableContainer = useContainer(Util.ASSIGN_TABLE); - takeOrderContainer = useContainer(Util.TAKE_ORDER); - orderTakenContainer = useContainer(Util.ORDER_TAKEN); - groupAgentOrderTakenContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_ORDER_TAKEN, Util.GROUP_AGENT_PORT); - preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS); - orderCompleteContainer = useContainer(Util.ORDER_COMPLETE); - groupAgentOrderCompleteContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_ORDER_COMPLETE, Util.GROUP_AGENT_PORT); - deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS); - paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST); - groupAgentPaymentRequestContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_PAYMENT_REQUEST, Util.GROUP_AGENT_PORT); - paymentDoneContainer = useContainer(Util.PAYMENT_DONE); - groupAgentPaymentDoneContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_PAYMENT_DONE, Util.GROUP_AGENT_PORT); - tableAssignedContainer = useContainer(Util.TABLE_ASSIGNED); - groupAgentTableAssignedContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_TABLE_ASSIGNED, Util.GROUP_AGENT_PORT); - pizzeriaInfoContainer = useContainer(Util.PIZZERIA_INFO); - } - - public void listenForFreeTable() { - SpaceListener listener = new SpaceListenerImpl(capi, freeTablesContainer, false) { - - @Override - void onEntriesWritten(List entries) throws Exception { - - List
tables = castEntries(entries); - - for (Table table : tables) { - - TransactionReference tx = getDefaultTransaction(); - - // Acquire a lock for the free table in the - // FreeTableContainer - int id = table.getId(); - - Table tableTemplate = new Table(id); - try { - Table lockedFreeTable = takeMatchingEntity(tableTemplate, - freeTablesContainer, tx, RequestTimeout.DEFAULT, - String.format("There was no free table found with id %d", id)); - - - GroupData groupTemplate = new GroupData(); - GroupData lockedGroup = takeMatchingEntity(groupTemplate, - assignTableContainer, tx, RequestTimeout.DEFAULT, - "There is no group waiting for a table at the moment"); - - assignGroupToTable(lockedGroup, lockedFreeTable, tx); - } catch (Exception e) { + private static final Logger log = LoggerFactory.getLogger(WaiterXVSM.class); + private final int waiterId; + + public WaiterXVSM(int waiterId, int port) { + super(port); + + this.waiterId = waiterId; + + freeTablesContainer = useContainer(Util.FREE_TABLES); + assignTableContainer = useContainer(Util.ASSIGN_TABLE); + takeOrderContainer = useContainer(Util.TAKE_ORDER); + orderTakenContainer = useContainer(Util.ORDER_TAKEN); + deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); + groupAgentOrderTakenContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_ORDER_TAKEN, Util.GROUP_AGENT_PORT); + preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS); + orderCompleteContainer = useContainer(Util.ORDER_COMPLETE); + groupAgentOrderCompleteContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_ORDER_COMPLETE, Util.GROUP_AGENT_PORT); + deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS); + paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST); + groupAgentPaymentRequestContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_PAYMENT_REQUEST, Util.GROUP_AGENT_PORT); + paymentDoneContainer = useContainer(Util.PAYMENT_DONE); + groupAgentPaymentDoneContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_PAYMENT_DONE, Util.GROUP_AGENT_PORT); + tableAssignedContainer = useContainer(Util.TABLE_ASSIGNED); + groupAgentTableAssignedContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_TABLE_ASSIGNED, Util.GROUP_AGENT_PORT); + pizzeriaInfoContainer = useContainer(Util.PIZZERIA_INFO); + phoneCallsContainer = useContainer(Util.PHONE_CALLS); + groupAgentDeliveryOrderTakenContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_DELIVERY_ORDER_TAKEN, Util.GROUP_AGENT_PORT); + } + + public void listenForPhoneOrders() { + SpaceListener phoneListener = new SpaceListenerImpl(capi, phoneCallsContainer, true) { + @Override + void onEntriesWritten(List entries) throws Exception { + final List phoneOrders = castEntries(entries); + + 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 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); + + updatePizzeriaOrderNumber(order, tx); + + + // send the order as a whole to the space + final List groupsWhoHaveOrdered = Arrays.asList(groupFromSpace); + sendItemsToContainer(groupsWhoHaveOrdered, + deliveryOrderTakenContainer, RequestTimeout.ZERO, tx); + sendItemsToContainer(order.getOrderedPizzas(), + preparePizzasContainer, RequestTimeout.ZERO, tx); + sendItemsToContainer(groupsWhoHaveOrdered, + groupAgentDeliveryOrderTakenContainer, RequestTimeout.ZERO, null); + capi.commitTransaction(tx); + + log.info("Waite has taken a phone delivery call!"); + } catch (Exception e) { + } + + + } + + } + }; + createNotification(phoneListener, phoneCallsContainer); + } + + private void updatePizzeriaOrderNumber(Order order, TransactionReference tx) throws MzsCoreException { + /* get the id of the last order of the pizzeria and set the order accordingly and + update the space */ + final OrderId orderId = takeMatchingEntity(new OrderId(null), pizzeriaInfoContainer, tx, RequestTimeout.INFINITE, "The Order id object could not be taken from the space!"); + final int id = orderId.getId(); + final int nextId = id + 1; + order.setId(nextId); + final List orderedPizzas = order.getOrderedPizzas(); + for (PizzaOrder orderedPizza : orderedPizzas) { + orderedPizza.setOrderId(nextId); + } + sendItemsToContainer(Arrays.asList(new OrderId(nextId)), pizzeriaInfoContainer, RequestTimeout.DEFAULT, tx); + } + + + public void listenForFreeTable() { + SpaceListener listener = new SpaceListenerImpl(capi, freeTablesContainer, false) { + + @Override + void onEntriesWritten(List entries) throws Exception { + + List
tables = castEntries(entries); + + for (Table table : tables) { + + TransactionReference tx = getDefaultTransaction(); + + // Acquire a lock for the free table in the + // FreeTableContainer + int id = table.getId(); + + Table tableTemplate = new Table(id); + try { + Table lockedFreeTable = takeMatchingEntity(tableTemplate, + freeTablesContainer, tx, RequestTimeout.DEFAULT, + String.format("There was no free table found with id %d", id)); + + + GroupData groupTemplate = new GroupData(); + GroupData lockedGroup = takeMatchingEntity(groupTemplate, + assignTableContainer, tx, RequestTimeout.DEFAULT, + "There is no group waiting for a table at the moment"); + + assignGroupToTable(lockedGroup, lockedFreeTable, tx); + } catch (Exception e) { // log.info(e.getMessage()); - } - } - } - }; - - createNotification(listener, freeTablesContainer); - } - - - - public void listenForNewGuests() { - SpaceListener listener = new SpaceListenerImpl(capi, assignTableContainer) { - - @Override - void onEntriesWritten(List entries) - throws Exception { - log.info("New guest groups have arrived"); - - List groups = castEntries(entries); - - for (GroupData group : groups) { - - TransactionReference tx = getDefaultTransaction(); - - // Acquire a lock for the group in the - // AssignTableContainer - String groupNotFound = String.format("Group with id %d was already assigned a table by another waiter!", group.getId()); - - try { - GroupData lockedGroup = takeMatchingEntity( - new GroupData(group.getId()), - assignTableContainer, tx, - RequestTimeout.DEFAULT, groupNotFound); - // Acquire a lock for one free table in the - // TablesContainer - String noFreeTable = String.format("No free table for group with id %d could be found", group.getId()); - Table lockedFreeTable = takeMatchingEntity(new Table(null), freeTablesContainer, tx, RequestTimeout.DEFAULT, - noFreeTable); - - assignGroupToTable(lockedGroup, lockedFreeTable, tx); - } catch (Exception e) { + } + } + } + }; + + createNotification(listener, freeTablesContainer); + } + + public void listenForNewGuests() { + SpaceListener listener = new SpaceListenerImpl(capi, assignTableContainer) { + + @Override + void onEntriesWritten(List entries) + throws Exception { + log.info("New guest groups have arrived"); + + List groups = castEntries(entries); + + for (GroupData group : groups) { + + TransactionReference tx = getDefaultTransaction(); + + // Acquire a lock for the group in the + // AssignTableContainer + String groupNotFound = String.format("Group with id %d was already assigned a table by another waiter!", group.getId()); + + try { + GroupData lockedGroup = takeMatchingEntity( + new GroupData(group.getId()), + assignTableContainer, tx, + RequestTimeout.DEFAULT, groupNotFound); + // Acquire a lock for one free table in the + // TablesContainer + String noFreeTable = String.format("No free table for group with id %d could be found", group.getId()); + Table lockedFreeTable = takeMatchingEntity(new Table(null), freeTablesContainer, tx, RequestTimeout.DEFAULT, + noFreeTable); + + assignGroupToTable(lockedGroup, lockedFreeTable, tx); + } catch (Exception e) { // log.info(e.getMessage()); - } - } - } - }; - - createNotification(listener, assignTableContainer); - } + } + } + } + }; + + createNotification(listener, assignTableContainer); + } public void listenForPaymentRequest() { - SpaceListener paymentListener = new SpaceListenerImpl(capi, paymentRequestContainer) { - - @Override - void onEntriesWritten(List entries) - throws Exception { - - List groups = castEntries(entries); - - for (GroupData groupData : groups) { - TransactionReference tx = getDefaultTransaction(); - GroupData entity = new GroupData(groupData.getId()); - - // Acquire the lock so that another waiter can't do the same - // thing! - String paymentRequestTakenByOtherWaiter = String.format( - "The payment request for group %d was already taken by an other waiter!", - groupData.getId()); - try { - takeMatchingEntity(entity, paymentRequestContainer, tx, RequestTimeout.DEFAULT, paymentRequestTakenByOtherWaiter); - - groupData.setPayingWaiter(waiterId); + SpaceListener paymentListener = new SpaceListenerImpl(capi, paymentRequestContainer) { + + @Override + void onEntriesWritten(List entries) + throws Exception { + + List groups = castEntries(entries); + + for (GroupData groupData : groups) { + TransactionReference tx = getDefaultTransaction(); + GroupData entity = new GroupData(groupData.getId()); + + // Acquire the lock so that another waiter can't do the same + // thing! + String paymentRequestTakenByOtherWaiter = String.format( + "The payment request for group %d was already taken by an other waiter!", + groupData.getId()); + try { + takeMatchingEntity(entity, paymentRequestContainer, tx, RequestTimeout.DEFAULT, paymentRequestTakenByOtherWaiter); + + groupData.setPayingWaiter(waiterId); final List groupsPayed = Arrays.asList(groupData); sendItemsToContainer(groupsPayed, paymentDoneContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(groupsPayed, groupAgentPaymentDoneContainer, RequestTimeout.ZERO, null); - capi.commitTransaction(tx); - } catch (Exception e) { + capi.commitTransaction(tx); + } catch (Exception e) { // log.info(e.getMessage()); - } - } - } - }; - - createNotification(paymentListener, paymentRequestContainer); - } - - public void listenForOrderRequests() { - SpaceListener ordersListener = new SpaceListenerImpl(capi, takeOrderContainer) { - - @Override - void onEntriesWritten(List entries) - throws Exception { - - List groups = castEntries(entries); - - for (GroupData groupData : groups) { - - TransactionReference tx = getDefaultTransaction(); - GroupData entity = new GroupData(groupData.getId()); - entity.setState(GroupState.SITTING); - - try { - // Acquire the lock so that another waiter can't do the same thing! - 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); - - groupData.setOrderWaiter(waiterId); - groupData.setState(GroupState.ORDERED); - Order order = groupData.getOrder(); - order.setStatus(OrderStatus.ORDERED); + } + } + } + }; + + createNotification(paymentListener, paymentRequestContainer); + } + + public void listenForOrderRequests() { + SpaceListener ordersListener = new SpaceListenerImpl(capi, takeOrderContainer) { + + @Override + void onEntriesWritten(List entries) + throws Exception { + + List groups = castEntries(entries); + + for (GroupData groupData : groups) { + + TransactionReference tx = getDefaultTransaction(); + GroupData entity = new GroupData(groupData.getId()); + entity.setState(GroupState.SITTING); + + try { + // Acquire the lock so that another waiter can't do the same thing! + 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); + + groupData.setOrderWaiter(waiterId); + groupData.setState(GroupState.ORDERED); + Order order = groupData.getOrder(); + order.setStatus(OrderStatus.ORDERED); /* get the id of the last order of the pizzeria and set the order accordingly and update the space */ - final OrderId orderId = takeMatchingEntity(new OrderId(null), pizzeriaInfoContainer, tx, RequestTimeout.INFINITE, "The Order id object could not be taken from the space!"); - final int id = orderId.getId(); - final int nextId = id+1; - order.setId(nextId); - final List orderedPizzas = order.getOrderedPizzas(); - for (PizzaOrder orderedPizza : orderedPizzas) { - orderedPizza.setOrderId(nextId); - } - sendItemsToContainer(Arrays.asList(new OrderId(nextId)), pizzeriaInfoContainer, RequestTimeout.DEFAULT, tx); + updatePizzeriaOrderNumber(order, tx); // send the order as a whole to the space final List groupsWhoHaveOrdered = Arrays.asList(groupData); sendItemsToContainer(groupsWhoHaveOrdered, orderTakenContainer, RequestTimeout.ZERO, tx); - sendItemsToContainer(order.getOrderedPizzas(), - preparePizzasContainer, RequestTimeout.ZERO, tx); + sendItemsToContainer(order.getOrderedPizzas(), + preparePizzasContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(groupsWhoHaveOrdered, groupAgentOrderTakenContainer, RequestTimeout.ZERO, null); - capi.commitTransaction(tx); + capi.commitTransaction(tx); - log.info("Waiter has taken order from group {}", - groupData.getId()); - } catch (Exception e) { + log.info("Waiter has taken order from group {}", + groupData.getId()); + } catch (Exception e) { // log.info(e.getMessage()); - } - } - } - }; - - createNotification(ordersListener, takeOrderContainer); - } - - public void listenForPreparedPizzas() { - /** - * 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, deliverPizzasContainer) { - - @Override - void onEntriesWritten(List entries) - throws Exception { - - List pizzas = castEntries(entries); - - for (Pizza pizza : pizzas) { - int orderId = pizza.getOrderId(); - - TransactionReference tx = getDefaultTransaction(); - - try { - GroupData entity = new GroupData(); - entity.setState(null); - Order order = new Order(); - order.setId(orderId); - entity.setOrder(order); - - GroupData groupData = takeMatchingEntity(entity, - orderTakenContainer, tx, RequestTimeout.DEFAULT, - "Another waiter just checks if the order is complete"); - int groupId = groupData.getId(); - int numberOfPizzas = groupData.getOrder().getNumberOfPizzas(); - - Pizza pizzaTemplate = new Pizza(); - pizzaTemplate.setOrderId(orderId); - - List pizzasOfOrder = takeMatchingEntities( - pizzaTemplate, deliverPizzasContainer, tx, - RequestTimeout.DEFAULT, - "Cannot take the pizzas from the deliverPizzasContainer"); - - if (pizzasOfOrder.size() == numberOfPizzas) { - GroupData group = new GroupData(); - group.setServingWaiter(waiterId); - Order completeOrder = new Order(); - completeOrder.setId(orderId); - completeOrder.setGroupId(groupId); - group.setOrder(completeOrder); + } + } + } + }; + + createNotification(ordersListener, takeOrderContainer); + } + + public void listenForPreparedPizzas() { + /** + * 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, deliverPizzasContainer) { + + @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(); + final boolean isDeliveryPizza = pizza.isDeliveryPizza(); + + try { + GroupData entity = new GroupData(); + entity.setState(null); + entity.setOrder(order); + + GroupData groupData = takeMatchingEntity(entity, + orderTakenContainer, tx, RequestTimeout.DEFAULT, + "Another waiter just checks if the order is complete"); + int groupId = groupData.getId(); + int numberOfPizzas = groupData.getOrder().getNumberOfPizzas(); + + Pizza pizzaTemplate = new Pizza(); + pizzaTemplate.setOrderId(orderId); + + List pizzasOfOrder = takeMatchingEntities( + pizzaTemplate, deliverPizzasContainer, tx, + RequestTimeout.DEFAULT, + "Cannot take the pizzas from the deliverPizzasContainer"); + + if (pizzasOfOrder.size() == numberOfPizzas) { + GroupData group = new GroupData(); + group.setServingWaiter(waiterId); + Order completeOrder = new Order(); + completeOrder.setId(orderId); + completeOrder.setGroupId(groupId); + group.setOrder(completeOrder); final List groupsWithCompleteOrder = Arrays.asList(group); sendItemsToContainer(groupsWithCompleteOrder, orderCompleteContainer, RequestTimeout.DEFAULT, @@ -278,54 +330,54 @@ public class WaiterXVSM extends AbstractXVSMConnector { sendItemsToContainer(groupsWithCompleteOrder, groupAgentOrderCompleteContainer, RequestTimeout.DEFAULT, null); - 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, deliverPizzasContainer); - } - - private void assignGroupToTable(GroupData lockedGroup, - Table lockedFreeTable, TransactionReference tx) - throws MzsCoreException { - // The new group sits down at the table - lockedFreeTable.setGroupId(lockedGroup.getId()); - - // The new group now wants to order - lockedGroup.setState(GroupState.SITTING); - lockedGroup.setTable(lockedFreeTable); - lockedGroup.setTableWaiter(waiterId); + + 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, deliverPizzasContainer); + } + + private void assignGroupToTable(GroupData lockedGroup, + Table lockedFreeTable, TransactionReference tx) + throws MzsCoreException { + // The new group sits down at the table + lockedFreeTable.setGroupId(lockedGroup.getId()); + + // The new group now wants to order + lockedGroup.setState(GroupState.SITTING); + lockedGroup.setTable(lockedFreeTable); + lockedGroup.setTableWaiter(waiterId); final List
freeTables = Arrays.asList(lockedFreeTable); sendItemsToContainer(freeTables, - tableAssignedContainer, RequestTimeout.ZERO, tx); - sendItemsToContainer(Arrays.asList(lockedGroup), takeOrderContainer, - RequestTimeout.ZERO, tx); + tableAssignedContainer, RequestTimeout.ZERO, tx); + sendItemsToContainer(Arrays.asList(lockedGroup), takeOrderContainer, + RequestTimeout.ZERO, tx); sendItemsToContainer(freeTables, groupAgentTableAssignedContainer, RequestTimeout.ZERO, null); - try { - capi.commitTransaction(tx); - log.info("Assigned table to group with groupId {}", - lockedGroup.getId()); - } catch (Exception e) { - log.info("Assigning a table to group with groupId {} failed", - lockedGroup.getId()); - log.info(e.getMessage()); - } - } - + try { + capi.commitTransaction(tx); + log.info("Assigned table to group with groupId {}", + lockedGroup.getId()); + } catch (Exception e) { + log.info("Assigning a table to group with groupId {} failed", + lockedGroup.getId()); + log.info(e.getMessage()); + } + } } 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 9a553b6..337bff7 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 @@ -44,6 +44,8 @@ public class Waiter implements Serializable { // when tables get free the waiter should have a look if there are waiting guests and assign the new table to them xvsm.listenForFreeTable(); + + xvsm.listenForPhoneOrders(); } -- 2.43.0