From 14b65f2b6de6f7e234e0076d8f57355b03e13a44 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Thu, 9 May 2013 15:32:15 +0200 Subject: [PATCH] Space cook listens for pizza requests --- README.md | 5 +- .../tuwien/sbc/valesriegler/common/Util.java | 1 + .../valesriegler/group/gui/GroupFrame.java | 3 + .../sbc/valesriegler/types/PizzaOrder.java | 14 +++- .../xvsm/AbstractXVSMConnector.java | 12 ++- .../sbc/valesriegler/xvsm/CookXVSM.java | 84 +++++++++++++++++++ ...iterXVSMConnector.java => WaiterXVSM.java} | 51 ++++++----- .../sbc/valesriegler/xvsm/cook/Cook.java | 15 +--- .../sbc/valesriegler/xvsm/waiter/Waiter.java | 6 +- 9 files changed, 147 insertions(+), 44 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java rename src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/{WaiterXVSMConnector.java => WaiterXVSM.java} (84%) diff --git a/README.md b/README.md index c26b67b..32a7e85 100644 --- a/README.md +++ b/README.md @@ -19,4 +19,7 @@ Space Based Computing SS 2013 mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent" -Dexec.args="XVSM" -Dmozartspaces.configurationFile="mozartspaces-client.xml" ### How to start a Space Waiter with Id 1 - mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="1" -Dmozartspaces.configurationFile="mozartspaces-client.xml" \ No newline at end of file + mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="1" -Dmozartspaces.configurationFile="mozartspaces-client.xml" + +### How to start a Space Cook with Id 1 + mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="1" -Dmozartspaces.configurationFile="mozartspaces-client.xml" \ No newline at end of file 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 1aa3581..948d845 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 @@ -20,6 +20,7 @@ public abstract class Util { public static final String TAKE_ORDER = "takeOrder"; public static final String ORDER = "order"; public static final String DELIVER_PIZZAS = "deliverPizzas"; + public static final String PREPARE_PIZZAS = "preparePizzas"; public static final String PAYMENT = "payment"; public static final String FREE_TABLES = "freeTables"; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java index 9393982..48ee7b7 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java @@ -151,6 +151,9 @@ public class GroupFrame extends JFrame { pizzaOrders.add(new PizzaOrder(pt)); } Order order = new Order(group, pizzaOrders); + for (PizzaOrder pizzaOrder : order.getOrderedPizzas()) { + pizzaOrder.setOrderId(order.getId()); + } group.getGroupData().setOrder(order); newGroups.add(group); 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 6c3b768..8c68dde 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 @@ -14,6 +14,7 @@ public class PizzaOrder implements Serializable, HasId { private static int nextID = 0; private Integer id; + private Integer orderId; private PizzaType pizzaType; private PizzaOrderStatus status; private Integer cookId; @@ -52,12 +53,23 @@ public class PizzaOrder implements Serializable, HasId { return cookId; } + public Integer getOrderId() { + return orderId; + } + + public void setOrderId(Integer orderId) { + this.orderId = orderId; + } + public void setCookId(Integer cookId) { this.cookId = cookId; } @Override public String toString() { - return "PizzaOrder [id=" + id + ", pizzaType=" + pizzaType + ", status=" + status + "]"; + return "PizzaOrder [id=" + id + ", orderId=" + orderId + ", pizzaType=" + + pizzaType + ", status=" + status + ", cookId=" + cookId + "]"; } + + } 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 52494ab..27886b1 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 @@ -36,6 +36,7 @@ public abstract class AbstractXVSMConnector { protected ContainerReference assignTableContainer; protected ContainerReference takeOrderContainer; protected ContainerReference ordersContainer; + protected ContainerReference preparePizzasContainer; protected ContainerReference deliverPizzasContainer; protected ContainerReference paymentContainer; protected ContainerReference freeTablesContainer; @@ -60,12 +61,16 @@ public abstract class AbstractXVSMConnector { public void useTablesContainer() { tablesContainer = useContainer(Util.TABLES_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ; } + + public void usePreparePizzasContainer() { + preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ; + } public void useTakeOrderContainer() { takeOrderContainer = useContainer(Util.TAKE_ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ; } - public void useorderContainer() { + public void useOrdersContainer() { ordersContainer = useContainer(Util.ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ; } @@ -80,6 +85,10 @@ public abstract class AbstractXVSMConnector { public void useFreeTablesContainer() { freeTablesContainer = useContainer(Util.FREE_TABLES, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))); } + + public void useDeliverPizzasContainer() { + deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))); + } private ContainerReference useContainer(String containerName, List coordinators) { try { @@ -160,5 +169,4 @@ public abstract class AbstractXVSMConnector { sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null); sendItemsToContainer(newGroups, assignTableContainer, RequestTimeout.DEFAULT, null); } - } \ No newline at end of file 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 new file mode 100644 index 0000000..c9c6a3f --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java @@ -0,0 +1,84 @@ +package at.ac.tuwien.sbc.valesriegler.xvsm; + +import java.io.Serializable; +import java.util.List; + +import org.mozartspaces.notifications.Notification; +import org.mozartspaces.notifications.NotificationListener; +import org.mozartspaces.notifications.Operation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; + +public class CookXVSM extends AbstractXVSMConnector { + private static final Logger log = LoggerFactory.getLogger(CookXVSM.class); + + private int cookId; + + public CookXVSM(int id) { + super(); + + this.cookId = id; + useGroupsContainer(); + useOrdersContainer(); + useDeliverPizzasContainer(); + usePreparePizzasContainer(); + } + + public void listenForPizzas() { + NotificationListener pizzasListener = new NotificationListener() { + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + + log.info("New Pizzas to prepare!"); + + List pizzas = castEntries(entries); + for (PizzaOrder pizzaOrder : pizzas) { + log.info(pizzaOrder.toString()); + } +// +// List groups = castEntries(entries); +// Collections.shuffle(groups); +// +// for (GroupData group : groups) { +// try { +// TransactionReference tx = capi.createTransaction(1500, URI.create(Util.SERVER_ADDR)); +// +// try { +// // 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()); +// +// GroupData lockedGroup = takeEntityByTemplateFromContainer(new GroupData(group.getId()), assignTableContainer, tx, 1000, groupNotFound); +// // Acquire a lock for one free table in the TablesContainer +// Table lockedFreeTable = takeEntityByTemplateFromContainer(new Table(null), freeTablesContainer, tx, RequestTimeout.DEFAULT, String.format("No free table for group with id %d could be found", group.getId())); +// +// assignGroupToTable(lockedGroup, lockedFreeTable, tx); +// +// } catch (IllegalArgumentException e) { +// log.info("IllegalArgumentException"); +// e.printStackTrace(); +// } catch (EntityNotFoundByTemplate e) { +// log.info(e.getMessage()); +// } catch (Exception e) { +// log.error("AN INNER EXCEPTION"); +// e.printStackTrace(); +// } +// } catch (TransactionException e) { +// log.info("An unimportant TransactionException has occurred"); +// } catch (Exception e) { +// log.error("OUTER EXCEPTION"); +// } +// +// } + } + }; + try { + notificationMgr.createNotification(preparePizzasContainer, pizzasListener, Operation.WRITE); + log.info("Created pizzasContainer notification for a cook"); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); + } + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSMConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java similarity index 84% rename from src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSMConnector.java rename to src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java index 17cbbf6..69cb378 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSMConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java @@ -22,14 +22,16 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupState; +import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.Table; -public class WaiterXVSMConnector extends AbstractXVSMConnector { - private static final Logger log = LoggerFactory.getLogger(WaiterXVSMConnector.class); +public class WaiterXVSM extends AbstractXVSMConnector { + private static final Logger log = LoggerFactory.getLogger(WaiterXVSM.class); private final int waiterId; - public WaiterXVSMConnector(int waiterId) { + public WaiterXVSM(int waiterId) { super(); this.waiterId = waiterId; @@ -38,7 +40,8 @@ public class WaiterXVSMConnector extends AbstractXVSMConnector { useAssignTableContainer(); useTablesContainer(); useTakeOrderContainer(); - useorderContainer(); + useOrdersContainer(); + usePreparePizzasContainer(); useGroupsContainer(); } @@ -93,10 +96,8 @@ public class WaiterXVSMConnector extends AbstractXVSMConnector { try { notificationMgr.createNotification(freeTablesContainer, freeTableListener, Operation.WRITE); log.info("Created freeTablesContainer notification for a waiter"); - } catch (MzsCoreException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); } } @@ -146,10 +147,8 @@ public class WaiterXVSMConnector extends AbstractXVSMConnector { try { notificationMgr.createNotification(assignTableContainer, newGroupsListener, Operation.WRITE); log.info("Created assingTableContainer notification for a waiter"); - } catch (MzsCoreException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); } } @@ -165,23 +164,25 @@ public class WaiterXVSMConnector extends AbstractXVSMConnector { GroupData groupData = groups.get(0); try { - TransactionReference tx = capi.createTransaction(1500, URI.create(Util.SERVER_ADDR)); - GroupData entity = new GroupData(groupData.getId()); - entity.setState(GroupState.ORDER_PENDING); - takeEntityByTemplateFromContainer(entity, takeOrderContainer, tx, RequestTimeout.DEFAULT, String.format("The order for group %d was already taken by an other waiter!", groupData.getId())); + TransactionReference tx = capi.createTransaction(1500, URI.create(Util.SERVER_ADDR)); + GroupData entity = new GroupData(groupData.getId()); + entity.setState(GroupState.ORDER_PENDING); + takeEntityByTemplateFromContainer(entity, takeOrderContainer, tx, RequestTimeout.DEFAULT, String.format("The order for group %d was already taken by an other waiter!", groupData.getId())); log.info("Will now write taken order from group {} to space", groupData.getId()); groupData.setOrderWaiter(waiterId); groupData.setState(GroupState.ORDERED); - groupData.getOrder().setStatus(OrderStatus.ORDERED); - sendItemsToContainer(Arrays.asList(groupData), ordersContainer, RequestTimeout.ZERO, tx); - sendItemsToContainer(Arrays.asList(groupData), groupsContainer, RequestTimeout.ZERO, tx); - capi.commitTransaction(tx); + Order order = groupData.getOrder(); + order.setStatus(OrderStatus.ORDERED); + + sendItemsToContainer(Arrays.asList(groupData), ordersContainer, RequestTimeout.ZERO, tx); + sendItemsToContainer(Arrays.asList(groupData), groupsContainer, RequestTimeout.ZERO, tx); + sendItemsToContainer(order.getOrderedPizzas(), preparePizzasContainer, RequestTimeout.ZERO, tx); + capi.commitTransaction(tx); - log.info("Waiter has taken order from group {}", groupData.getId()); - + log.info("Waiter has taken order from group {}", groupData.getId()); } catch (MzsCoreException e) { log.info("ERROR in listenForOrders"); e.printStackTrace(); @@ -192,10 +193,8 @@ public class WaiterXVSMConnector extends AbstractXVSMConnector { try { notificationMgr.createNotification(takeOrderContainer, ordersListener, Operation.WRITE); log.info("Created takeOrderContainer notification for a waiter"); - } catch (MzsCoreException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java index 75e7264..79e7d26 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java @@ -4,6 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.xvsm.AbstractXVSMConnector; +import at.ac.tuwien.sbc.valesriegler.xvsm.CookXVSM; import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector; public class Cook { @@ -11,7 +12,7 @@ public class Cook { private static final Logger log = LoggerFactory.getLogger(Cook.class); private int id; - private AbstractXVSMConnector xvsm; + private CookXVSM xvsm; public static void main(String[] args) { if(args.length != 1) { @@ -31,17 +32,9 @@ public class Cook { } private void start() { - initSpaceCommunication(); + xvsm = new CookXVSM(id); - - - } - - private void initSpaceCommunication() { - xvsm = new XVSMConnector(); - xvsm.initSpaceCommunication(); - - log.info("Space Connection established!"); + xvsm.listenForPizzas(); } 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 090c2b2..7029664 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 @@ -16,7 +16,7 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.Table; -import at.ac.tuwien.sbc.valesriegler.xvsm.WaiterXVSMConnector; +import at.ac.tuwien.sbc.valesriegler.xvsm.WaiterXVSM; import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector; /** @@ -30,7 +30,7 @@ public class Waiter implements Serializable { private static final Logger log = LoggerFactory.getLogger(Waiter.class); private int id; - private WaiterXVSMConnector xvsm; + private WaiterXVSM xvsm; public static void main(String[] args) { if (args.length != 1) { @@ -50,7 +50,7 @@ public class Waiter implements Serializable { } private void start() { - xvsm = new WaiterXVSMConnector(id); + xvsm = new WaiterXVSM(id); xvsm.listenForOrders(); -- 2.43.0