From 396f956d0d441a3eb6bde63060987812db58a0cc Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Mon, 27 May 2013 17:07:26 +0200 Subject: [PATCH] Make order ids pizzeria specific in XVSM implementation --- .../tuwien/sbc/valesriegler/common/OrderId.java | 15 +++++++++++++++ .../ac/tuwien/sbc/valesriegler/common/Util.java | 14 +------------- .../sbc/valesriegler/pizzeria/PizzeriaAgent.java | 1 + .../ac/tuwien/sbc/valesriegler/types/Order.java | 1 + .../valesriegler/xvsm/AbstractXVSMConnector.java | 1 + .../sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java | 7 +++++-- .../tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java | 14 +++++++++++--- 7 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/common/OrderId.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/OrderId.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/OrderId.java new file mode 100644 index 0000000..596ff40 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/OrderId.java @@ -0,0 +1,15 @@ +package at.ac.tuwien.sbc.valesriegler.common; + +import java.io.Serializable; + +public class OrderId implements Serializable { + private final Integer id; + + public OrderId(Integer id) { + this.id = id; + } + + public int getId() { + return Util.getIntSafe(id); + } +} 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 65ce4aa..c4fb3a4 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 @@ -35,6 +35,7 @@ public abstract class Util { public static final String FREE_TABLES = "freeTables"; public static final String IS_EATING = "isEating"; public static final String PAYMENT_DONE = "hasPaid"; + public static final String INFO = "info"; private static Random random = new Random(); @@ -44,19 +45,6 @@ public abstract class Util { public static final String JMS_CONNECTSTRING = "tcp://localhost:61616?jms.prefetchPolicy.all=1"; - public static ContainerReference getOrCreateNamedContainer(final String spaceUri, final String containerName, - final Capi capi, final List coordinators) throws MzsCoreException { - - ContainerReference container; - try { - container = capi.lookupContainer(containerName, URI.create(spaceUri), RequestTimeout.DEFAULT, null); - } catch (MzsCoreException e) { - container = capi.createContainer(containerName, URI.create(spaceUri), Container.UNBOUNDED, coordinators, null, - null); - } - return container; - } - public static String getId(int id) { return (id != 0 && id != -1) ? String.valueOf(id) : ""; } 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 cadbb01..0988a86 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 @@ -74,6 +74,7 @@ public class PizzeriaAgent { private void initXVSM() { xvsm = new PizzeriaAgentXVSM(); + xvsm.initializeOrderId(); xvsm.listenForFreeTables(); xvsm.listenForTakenOrders(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java index 853e84a..465a785 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java @@ -34,6 +34,7 @@ public class Order implements Serializable, HasId { } public Order(int groupId, List orderedPizzas) { + // TODO don't set the id here but let the waiter set it id = ++idNext; this.groupId = groupId; status = OrderStatus.NEW; 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 fc2d0c7..d05c675 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 @@ -50,6 +50,7 @@ public abstract class AbstractXVSMConnector { protected ContainerReference orderCompleteContainer; protected ContainerReference isEatingContainer; protected ContainerReference paymentDoneContainer; + protected ContainerReference infoContainer; protected Capi capi; protected NotificationManager notificationMgr; 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 7042b66..84b1d3d 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 @@ -8,6 +8,7 @@ import java.util.List; import javax.swing.SwingUtilities; +import at.ac.tuwien.sbc.valesriegler.common.OrderId; import org.mozartspaces.capi3.AnyCoordinator; import org.mozartspaces.capi3.FifoCoordinator; import org.mozartspaces.capi3.LindaCoordinator; @@ -42,6 +43,7 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { paymentDoneContainer = useContainer(Util.PAYMENT_DONE) ; orderTakenContainer = useContainer(Util.ORDER_TAKEN); assignTableContainer = useContainer(Util.ASSIGN_TABLE); + infoContainer = useContainer(Util.INFO); } public void listenForOccupiedTables() { @@ -266,7 +268,8 @@ public void listenForTakenOrders() { sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null); } - - + public void initializeOrderId() { + sendItemsToContainer(Arrays.asList(new OrderId(1)), infoContainer, 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 73be4ab..2caaab2 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 @@ -7,6 +7,7 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; +import at.ac.tuwien.sbc.valesriegler.common.OrderId; import org.mozartspaces.capi3.AnyCoordinator; import org.mozartspaces.capi3.FifoCoordinator; import org.mozartspaces.capi3.LindaCoordinator; @@ -49,6 +50,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST); paymentDoneContainer = useContainer(Util.PAYMENT_DONE); tableAssignedContainer = useContainer(Util.TABLE_ASSIGNED); + infoContainer = useContainer(Util.INFO); } public void listenForFreeTable() { @@ -203,10 +205,16 @@ public class WaiterXVSM extends AbstractXVSMConnector { Order order = groupData.getOrder(); order.setStatus(OrderStatus.ORDERED); - - // send the order as a whole to the space + /* 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), infoContainer, tx, RequestTimeout.INFINITE, "The Order id object could not be taken from the space!"); + final int id = orderId.getId(); + order.setId(id+100); + sendItemsToContainer(Arrays.asList(new OrderId(id+1)), infoContainer, RequestTimeout.DEFAULT, tx); + + // send the order as a whole to the space sendItemsToContainer(Arrays.asList(groupData), - orderTakenContainer, RequestTimeout.ZERO, tx); + orderTakenContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(order.getOrderedPizzas(), preparePizzasContainer, RequestTimeout.ZERO, tx); capi.commitTransaction(tx); -- 2.43.0