From 05a31109a4c72d7e9d6d27b9a075a37e8d64d639 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Fri, 7 Jun 2013 00:28:57 +0200 Subject: [PATCH] [XVSM] Crucial container communication changes --- .../tuwien/sbc/valesriegler/common/Util.java | 7 +- .../valesriegler/pizzeria/PizzeriaAgent.java | 28 ++-- .../gui/tablemodels/GroupsOverviewModel.java | 20 +-- .../tuwien/sbc/valesriegler/types/Order.java | 14 +- .../tuwien/sbc/valesriegler/types/Table.java | 19 ++- .../xvsm/AbstractXVSMConnector.java | 26 +++- .../sbc/valesriegler/xvsm/CookXVSM.java | 24 +++- .../sbc/valesriegler/xvsm/DriverXVSM.java | 2 +- .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 19 ++- .../sbc/valesriegler/xvsm/GroupXVSM.java | 65 ++++++---- .../valesriegler/xvsm/PizzeriaAgentXVSM.java | 97 ++++++++++++-- .../sbc/valesriegler/xvsm/WaiterXVSM.java | 122 +++++++++++------- 12 files changed, 317 insertions(+), 126 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 2c04677..5a5f6f7 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 @@ -23,7 +23,7 @@ public abstract class Util { public static final String DELIVERY_ORDER_TAKEN = "deliverOrderTaken"; public static final String DELIVER_PIZZAS = "deliverPizzas"; public static final String DELIVER_DELIVERY_PIZZAS = "deliverDeliveryPizzas"; - public static final String DELIVER_DELIVERY_ORDER = "deliverDeliveryOrder"; + public static final String DELIVER_DELIVERY_ORDER = "deliverDeliveryOrderContainer"; public static final String PREPARE_PIZZAS = "preparePizzas"; public static final String PREPARE_DELIVERY_PIZZAS = "prepareDeliveryPizzas"; public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress"; @@ -34,6 +34,9 @@ public abstract class Util { public static final String PIZZERIA_INFO = "pizzeriaInfo"; public static final String PHONE_CALLS = "phoneCalls"; public static final String GROUP_AGENT_INFO = "groupAgentInfo"; + public static final String PIZZERIA_GROUP = "pizzeriaGroupContainer"; + public static final String PIZZERIA_TABLE = "pizzeriaTableContainer"; + public static final String PIZZERIA_DELIVERY = "pizzeriaDeliveryContainer"; private static Random random = new Random(); @@ -104,7 +107,7 @@ public abstract class Util { wrapperPanel.setBorder(new TitledBorder(title)); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); wrapperPanel.add(scrollPane, c); - + table.setAutoCreateRowSorter(true); return table; } 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 ac114f1..06435c5 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,19 +1,17 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria; -import java.util.List; - -import javax.swing.SwingUtilities; - -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.*; -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.*; 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. @@ -84,15 +82,17 @@ public class PizzeriaAgent { xvsm.initializeOrderId(); xvsm.notifyGroupAgent(); - xvsm.listenForFreeTables(); - xvsm.listenForTakenOrders(); +// xvsm.listenForFreeTables(); +// xvsm.listenForTakenOrders(); xvsm.listenForTakenDeliveryOrders(); - xvsm.listenForWaitingGroups(); +// xvsm.listenForWaitingGroups(); xvsm.listenForPizzasInPreparation(); - xvsm.listenForDeliveredOrders(); +// xvsm.listenForDeliveredOrders(); xvsm.listenForPreparedPizzas(); - xvsm.listenForPayment(); - xvsm.listenForOccupiedTables(); + xvsm.listenForTables(); + xvsm.listenForGroups(); +// xvsm.listenForPayment(); +// xvsm.listenForOccupiedTables(); } private void initGUI() { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java index 34f1c2d..1a27ddc 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java @@ -1,15 +1,16 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - import at.ac.tuwien.sbc.valesriegler.common.TableModel; import at.ac.tuwien.sbc.valesriegler.types.GroupData; -import at.ac.tuwien.sbc.valesriegler.types.GroupState; import at.ac.tuwien.sbc.valesriegler.types.Table; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; public class GroupsOverviewModel extends TableModel { + private static final Logger log = LoggerFactory.getLogger(GroupsOverviewModel.class); private static final String ID = "ID"; private static final String SIZE = "Size"; @@ -44,9 +45,12 @@ public class GroupsOverviewModel extends TableModel { } public void removeGroupsFromTables(List tables) { - for (Table table : tables) { - final int groupId = table.getGroupId(); - items.remove(groupId); + synchronized (items) { + for (Table table : tables) { + final int groupId = table.getGroupId(); + log.info("222222222222222 Add group {} to a table", groupId); + items.remove(groupId); + } } fireTableDataChanged(); } 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 4d2de24..215e666 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 @@ -1,15 +1,13 @@ package at.ac.tuwien.sbc.valesriegler.types; +import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.common.Util; +import org.mozartspaces.capi3.Queryable; + import java.io.Serializable; import java.util.ArrayList; import java.util.List; -import org.mozartspaces.capi3.Queryable; - -import at.ac.tuwien.sbc.valesriegler.common.HasId; -import at.ac.tuwien.sbc.valesriegler.common.Util; -import at.ac.tuwien.sbc.valesriegler.group.Group; - /** * Object denotes a Group's interest in pizzas and the state of the pizzas * themselves. @@ -51,6 +49,10 @@ public class Order implements Serializable, HasId { cookedPizzas = new ArrayList(); } + public void addCookedPizza(Pizza pizza) { + cookedPizzas.add(pizza); + } + public List getCookedPizzas() { return cookedPizzas; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java index d398f18..813e122 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java @@ -1,10 +1,9 @@ package at.ac.tuwien.sbc.valesriegler.types; -import java.io.Serializable; - +import at.ac.tuwien.sbc.valesriegler.common.HasId; import org.mozartspaces.capi3.Queryable; -import at.ac.tuwien.sbc.valesriegler.common.HasId; +import java.io.Serializable; /** * A table is a thing, where people can put things at. Possibly a laptop, to @@ -20,6 +19,11 @@ public class Table implements Serializable, HasId { private Integer groupId = -1; + /** + * is needed for spaces linda selection + */ + private Boolean isFree = true; + public Table(Integer id) { this.id = id; } @@ -39,8 +43,17 @@ public class Table implements Serializable, HasId { public void setGroupId(int groupId) { this.groupId = groupId; + if(groupId != -1) { + isFree = false; + } else { + isFree = true; + } } + public void setFree(Boolean free) { + isFree = free; + } + public boolean isFree() { if (groupId == -1) return true; 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 09cebbd..5b9f376 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 @@ -37,12 +37,15 @@ public abstract class AbstractXVSMConnector { protected ContainerReference paymentRequestContainer; protected ContainerReference freeTablesContainer; protected ContainerReference pizzaInProgressContainer; - protected ContainerReference orderCompleteContainer; + protected ContainerReference orderDeliveredContainer; protected ContainerReference paymentDoneContainer; protected ContainerReference pizzeriaInfoContainer; protected ContainerReference phoneCallsContainer; protected ContainerReference groupAgentInfoContainer; - protected ContainerReference deliverDeliveryOrder; + protected ContainerReference deliverDeliveryOrderContainer; + protected ContainerReference pizzeriaGroupContainer; + protected ContainerReference pizzeriaDeliveryContainer; + protected ContainerReference pizzeriaTableContainer; protected Capi capi; protected NotificationManager notificationMgr; @@ -132,6 +135,25 @@ public abstract class AbstractXVSMConnector { } } + protected T takeMatchingEntityIfItExists( + T entity, ContainerReference ref, TransactionReference tx, long timeout) + throws MzsCoreException { + try { + LindaSelector sel = LindaCoordinator.newSelector(entity, 1); + + ArrayList entities = capi.take(ref, sel, timeout, tx); + + return (T) CapiUtil.getSingleEntry(entities); + } catch (CountNotMetException e) { + } catch (MzsTimeoutException e) { + capi.rollbackTransaction(tx); + + throw new EntityNotFoundByTemplate(); + } + + return null; + } + /** * Searches for all entities matching the given template object by linda selection and takes them. */ 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 25cf111..acaae9c 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 @@ -1,9 +1,7 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; import at.ac.tuwien.sbc.valesriegler.common.Util; -import at.ac.tuwien.sbc.valesriegler.types.Pizza; -import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; -import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; +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; @@ -35,6 +33,7 @@ public class CookXVSM extends AbstractXVSMConnector { prepareDeliveryPizzasContainer = useContainer(Util.PREPARE_DELIVERY_PIZZAS); pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS); preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); + pizzeriaGroupContainer = useContainer(Util.PIZZERIA_GROUP); } public void listenForPizzas() { @@ -68,16 +67,29 @@ public class CookXVSM extends AbstractXVSMConnector { pizzaInProgress.setStatus(PizzaOrderStatus.IN_PREPARATION); - log.info("I say that I now prepare a pizza for order {}", pizzaInProgress.getOrderId()); + log.debug("I say that I now prepare a pizza for order {}", pizzaInProgress.getOrderId()); sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, null); PizzaOrder pizza = createPizza(order); final ContainerReference container = preparedPizzasContainer; sendItemsToContainer(Arrays.asList(pizza), container, RequestTimeout.DEFAULT, tx); - + final GroupData group = new GroupData(); + final Order groupOrder = new Order(); + groupOrder.setId(pizza.getOrderId()); + group.setOrder(groupOrder); + group.setState(GroupState.ORDERED); + GroupData groupData = takeMatchingEntity(group, pizzeriaGroupContainer, tx, RequestTimeout.DEFAULT, "Cannot take the group from pizzeriaGroupContainer"); + 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), pizzeriaGroupContainer, RequestTimeout.DEFAULT, tx); capi.commitTransaction(tx); - log.info("I have completed preparing a pizza for order {}!", pizza.getOrderId()); + log.debug("I have completed preparing a pizza for order {}!", pizza.getOrderId()); } catch (NullPointerException e) { // the strange nullpointer exception from the space } catch (Exception e) { 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 89deb62..4a2c38e 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 @@ -17,7 +17,7 @@ public class DriverXVSM extends AbstractXVSMConnector { this.preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); this.deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); - this.deliverDeliveryOrder = useContainer(Util.DELIVER_DELIVERY_ORDER); + this.deliverDeliveryOrderContainer = useContainer(Util.DELIVER_DELIVERY_ORDER); } 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 cd8cb49..b5abdeb 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 @@ -8,15 +8,21 @@ import at.ac.tuwien.sbc.valesriegler.types.Table; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceListener; 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 javax.swing.*; import java.io.Serializable; +import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.Set; +import static java.lang.String.format; +import static java.lang.String.valueOf; + public class GroupAgentXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(GroupAgentXVSM.class); @@ -128,7 +134,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { final List pizzeriaRegistrations = castEntries(entries); final List pizzeriaIds = new ArrayList<>(); for (PizzeriaRegistration registration : pizzeriaRegistrations) { - final String pizzeriaId = String.valueOf(registration.pizzeriaSpacePort); + final String pizzeriaId = valueOf(registration.pizzeriaSpacePort); if (!pizzeriaIdentifiers.contains(pizzeriaId)) { pizzeriaIds.add(pizzeriaId); listenToPizzeria(pizzeriaId); @@ -157,7 +163,16 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } public void sendNewGroupsToSpace(List groupData, int pizzeriaSpacePort) { - sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.ASSIGN_TABLE, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); + try { + TransactionReference tx = capi.createTransaction( + Util.SPACE_TRANSACTION_TIMEOUT, + URI.create(format(Util.SERVER_ADDR, pizzeriaSpacePort))); + sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.ASSIGN_TABLE, pizzeriaSpacePort), RequestTimeout.DEFAULT, tx); + sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PIZZERIA_GROUP, pizzeriaSpacePort), RequestTimeout.DEFAULT, tx); + capi.commitTransaction(tx); + } catch (MzsCoreException e) { + Util.handleSpaceErrorAndTerminate(e); + } } public void sendNewDeliveriesToSpace(List groupData, int pizzeriaSpacePort) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java index f8c9842..945b95d 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java @@ -1,20 +1,21 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; -import java.io.Serializable; -import java.util.Arrays; -import java.util.List; -import java.util.Random; - +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 org.mozartspaces.core.MzsConstants.RequestTimeout; +import org.mozartspaces.core.TransactionReference; import org.mozartspaces.notifications.Notification; import org.mozartspaces.notifications.NotificationListener; import org.mozartspaces.notifications.Operation; import org.slf4j.Logger; 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.Order; +import java.io.Serializable; +import java.util.Arrays; +import java.util.List; +import java.util.Random; public class GroupXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(GroupXVSM.class); @@ -28,7 +29,8 @@ public class GroupXVSM extends AbstractXVSMConnector { this.groupId = groupId; paymentRequestContainer = useContainerOfSpaceWithPort(Util.PAYMENT_REQUEST, port) ; - orderCompleteContainer = useContainerOfSpaceWithPort(Util.ORDER_COMPLETE, port) ; + orderDeliveredContainer = useContainerOfSpaceWithPort(Util.ORDER_COMPLETE, port) ; + pizzeriaGroupContainer = useContainerOfSpaceWithPort(Util.PIZZERIA_GROUP, port); } public void waitForMyOrder() { @@ -46,27 +48,40 @@ public class GroupXVSM extends AbstractXVSMConnector { } }; try { - notificationMgr.createNotification(orderCompleteContainer, deliveredOrders, Operation.WRITE); + notificationMgr.createNotification(orderDeliveredContainer, deliveredOrders, Operation.WRITE); } catch (Exception e) { Util.handleSpaceErrorAndTerminate(e); } } private void eatAndThenPay() { - int timeToEat = Util.getRandom(3, 5); - log.info("I eat {} seconds now...", timeToEat); - - GroupData groupData = new GroupData(); - groupData.setId(groupId); - - try { - Thread.sleep(timeToEat * 1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - sendItemsToContainer(Arrays.asList(groupData), paymentRequestContainer, RequestTimeout.DEFAULT, null); - log.info("I sent my payment request to the space! GroupId: {}", groupId); - } + try { + int timeToEat = Util.getRandom(3, 5); + log.info("I eat {} seconds now...", timeToEat); + + try { + Thread.sleep(timeToEat * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + GroupData groupData = new GroupData(); + groupData.setId(groupId); + groupData.setState(GroupState.EATING); + final TransactionReference tx = getDefaultTransaction(); + + final GroupData group = takeMatchingEntity(groupData, pizzeriaGroupContainer, tx, RequestTimeout.DEFAULT, "Group does not exist!"); + + group.setState(GroupState.PAY); + final List groupList = Arrays.asList(group); + sendItemsToContainer(groupList, paymentRequestContainer, RequestTimeout.DEFAULT, tx); + sendItemsToContainer(groupList, pizzeriaGroupContainer, RequestTimeout.DEFAULT, tx); + capi.commitTransaction(tx); + log.info("I sent my payment request to the space! GroupId: {}", groupId); + + } catch (Exception e) { + log.error("hab mich verschluckt!"); + } + } } 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 93ded78..c100f3c 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 @@ -1,12 +1,8 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; -import java.io.Serializable; -import java.util.Arrays; -import java.util.List; - -import javax.swing.SwingUtilities; - import at.ac.tuwien.sbc.valesriegler.common.OrderId; +import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; 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; @@ -14,8 +10,11 @@ import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.common.Util; -import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; +import javax.swing.*; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class PizzeriaAgentXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(PizzeriaAgentXVSM.class); @@ -25,13 +24,15 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { tableAssignedContainer = useContainer(Util.TABLE_ASSIGNED) ; freeTablesContainer = useContainer(Util.FREE_TABLES); pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS) ; - orderCompleteContainer = useContainer(Util.ORDER_COMPLETE) ; + orderDeliveredContainer = useContainer(Util.ORDER_COMPLETE) ; preparedPizzasContainer = useContainer(Util.DELIVER_PIZZAS); paymentDoneContainer = useContainer(Util.PAYMENT_DONE) ; orderTakenContainer = useContainer(Util.ORDER_TAKEN); assignTableContainer = useContainer(Util.ASSIGN_TABLE); pizzeriaInfoContainer = useContainer(Util.PIZZERIA_INFO); deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); + pizzeriaTableContainer = useContainer(Util.PIZZERIA_TABLE); + pizzeriaGroupContainer = useContainer(Util.PIZZERIA_GROUP); } public void listenForOccupiedTables() { @@ -53,8 +54,79 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { } }).createSpaceListenerImpl(); } - - public void listenForFreeTables() { + + public void listenForTables() { + SpaceListener tablesListener = getDefaultBuilder().setCref(pizzeriaTableContainer).setSpaceAction(new SpaceAction() { + @Override + public void onEntriesWritten(List entries) throws Exception { + log.info("listenForTables!!!!!"); + + final List
tables = castEntries(entries); + + final ArrayList
occupiedTables = new ArrayList<>(); + for (Table table : tables) { + if(! table.isFree()) occupiedTables.add(table); + } + log.info("11111111111111111111 Number of occupied Tables: {}", occupiedTables.size()); + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + PizzeriaAgent.getInstance().getTablesModel().addItems(tables); + PizzeriaAgent.getInstance().getGroupModel().removeGroupsFromTables(occupiedTables); + } + }); + } + }).createSpaceListenerImpl(); + } + + public void listenForGroups() { + + 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); + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + PizzeriaAgent.getInstance().getGroupModel().addItems(waitingGroups); + PizzeriaAgent.getInstance().getOrdersModel().addItems(orderedGroups); + } + }); + + } + }).createSpaceListenerImpl(); + } + + private List getOrderedGroups(List groups) { + final ArrayList orderedGroups = new ArrayList<>(); + for (GroupData group : groups) { + if(group.getState() != GroupState.WAITING) { + orderedGroups.add(group); + } + } + return orderedGroups; + } + + private List getWaitingGroups(List groups) { + final ArrayList waitingGroups = new ArrayList<>(); + for (GroupData group : groups) { + if (group.getState() == GroupState.WAITING) { + waitingGroups.add(group); + } else { + log.info("Fucking groupstate is {}", group.getState()); + } + + } + return waitingGroups; + } + + public void listenForFreeTables() { SpaceListener tablesListener = getDefaultBuilder().setCref(freeTablesContainer).setSpaceAction(new SpaceAction() { @Override @@ -159,7 +231,7 @@ public void listenForTakenOrders() { } public void listenForDeliveredOrders() { - SpaceListener deliveredOrders = getDefaultBuilder().setCref(orderCompleteContainer).setSpaceAction(new SpaceAction() { + SpaceListener deliveredOrders = getDefaultBuilder().setCref(orderDeliveredContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); @@ -223,6 +295,7 @@ public void listenForTakenOrders() { public void sendFreeTablesToContainer(List
tables) { sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null); + sendItemsToContainer(tables, pizzeriaTableContainer, RequestTimeout.DEFAULT, null); } public void initializeOrderId() { 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 b358690..38ee3c1 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 @@ -31,7 +31,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS); prepareDeliveryPizzasContainer = useContainer(Util.PREPARE_DELIVERY_PIZZAS); - orderCompleteContainer = useContainer(Util.ORDER_COMPLETE); + orderDeliveredContainer = useContainer(Util.ORDER_COMPLETE); preparedPizzasContainer = useContainer(Util.DELIVER_PIZZAS); paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST); paymentDoneContainer = useContainer(Util.PAYMENT_DONE); @@ -39,7 +39,9 @@ public class WaiterXVSM extends AbstractXVSMConnector { pizzeriaInfoContainer = useContainer(Util.PIZZERIA_INFO); phoneCallsContainer = useContainer(Util.PHONE_CALLS); preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS); - deliverDeliveryOrder = useContainer(Util.DELIVER_DELIVERY_ORDER); + deliverDeliveryOrderContainer = useContainer(Util.DELIVER_DELIVERY_ORDER); + pizzeriaGroupContainer = useContainer(Util.PIZZERIA_GROUP); + pizzeriaTableContainer = useContainer(Util.PIZZERIA_TABLE); } public void listenForPhoneOrders() { @@ -80,21 +82,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { }).createSpaceListenerImpl(); } - 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 = getDefaultBuilder().setCref(freeTablesContainer).setSpaceAction(new SpaceAction() { @@ -116,12 +103,13 @@ public class WaiterXVSM extends AbstractXVSMConnector { Table lockedFreeTable = takeMatchingEntity(tableTemplate, freeTablesContainer, tx, RequestTimeout.DEFAULT, String.format("There was no free table found with id %d", id)); - + takeMatchingEntityIfItExists(tableTemplate, pizzeriaTableContainer, tx, RequestTimeout.DEFAULT); GroupData groupTemplate = new GroupData(); GroupData lockedGroup = takeMatchingEntity(groupTemplate, assignTableContainer, tx, RequestTimeout.DEFAULT, "There is no group waiting for a table at the moment"); + takeMatchingEntityIfItExists(lockedGroup, pizzeriaGroupContainer, tx, RequestTimeout.DEFAULT); assignGroupToTable(lockedGroup, lockedFreeTable, tx); } catch (Exception e) { @@ -132,8 +120,9 @@ public class WaiterXVSM extends AbstractXVSMConnector { }).createSpaceListenerImpl(); } + public void listenForNewGuests() { - SpaceListener listener = getDefaultBuilder().setCref(assignTableContainer).setSpaceAction(new SpaceAction() { + SpaceListener listener = getDefaultBuilder().setLookaround(true).setCref(assignTableContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) @@ -151,15 +140,20 @@ public class WaiterXVSM extends AbstractXVSMConnector { String groupNotFound = String.format("Group with id %d was already assigned a table by another waiter!", group.getId()); try { + final GroupData templateGroup = new GroupData(group.getId()); GroupData lockedGroup = takeMatchingEntity( - new GroupData(group.getId()), + templateGroup, assignTableContainer, tx, RequestTimeout.DEFAULT, groupNotFound); + takeMatchingEntityIfItExists(templateGroup, pizzeriaGroupContainer, tx, RequestTimeout.DEFAULT); // 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, + final Table freeTable = new Table(null); + freeTable.setFree(true); + Table lockedFreeTable = takeMatchingEntity(freeTable, freeTablesContainer, tx, RequestTimeout.DEFAULT, noFreeTable); + takeMatchingEntityIfItExists(lockedFreeTable, pizzeriaTableContainer, tx, RequestTimeout.DEFAULT); assignGroupToTable(lockedGroup, lockedFreeTable, tx); } catch (Exception e) { @@ -182,6 +176,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { for (GroupData groupData : groups) { TransactionReference tx = getDefaultTransaction(); GroupData entity = new GroupData(groupData.getId()); + entity.setState(GroupState.PAY); // Acquire the lock so that another waiter can't do the same // thing! @@ -190,15 +185,25 @@ public class WaiterXVSM extends AbstractXVSMConnector { 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); + GroupData group = takeMatchingEntity(entity, pizzeriaGroupContainer, tx, RequestTimeout.DEFAULT, "Cannot get paying group!"); + group.setPayingWaiter(waiterId); + group.setState(GroupState.GONE); + + final Table tableTemplate = new Table(null); + tableTemplate.setGroupId(group.getId()); + final Table table = takeMatchingEntity(tableTemplate, pizzeriaTableContainer, tx, RequestTimeout.DEFAULT, "Table was not found!"); + table.setGroupId(-1); + final List
tables = Arrays.asList(table); + sendItemsToContainer(tables, pizzeriaTableContainer, RequestTimeout.DEFAULT, tx); + sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, tx); + + final List groupsPayed = Arrays.asList(group); + sendItemsToContainer(groupsPayed, paymentDoneContainer, RequestTimeout.DEFAULT, tx); + sendItemsToContainer(groupsPayed, pizzeriaGroupContainer, RequestTimeout.DEFAULT, tx); capi.commitTransaction(tx); } catch (Exception e) { -// log.info(e.getMessage()); + log.info(e.getMessage()); } } } @@ -226,10 +231,11 @@ public class WaiterXVSM extends AbstractXVSMConnector { "The order for group %d was already taken by an other waiter!", groupData.getId()); takeMatchingEntity(entity, takeOrderContainer, tx, RequestTimeout.DEFAULT, orderTakenByOtherWaiter); + GroupData group = takeMatchingEntityIfItExists(entity, pizzeriaGroupContainer, tx, RequestTimeout.DEFAULT); - groupData.setOrderWaiter(waiterId); - groupData.setState(GroupState.ORDERED); - Order order = groupData.getOrder(); + group.setOrderWaiter(waiterId); + group.setState(GroupState.ORDERED); + Order order = group.getOrder(); order.setStatus(OrderStatus.ORDERED); /* get the id of the last order of the pizzeria and set the order accordingly and @@ -237,15 +243,17 @@ public class WaiterXVSM extends AbstractXVSMConnector { updatePizzeriaOrderNumber(order, tx); // send the order as a whole to the space - final List groupsWhoHaveOrdered = Arrays.asList(groupData); + final List groupsWhoHaveOrdered = Arrays.asList(group); sendItemsToContainer(groupsWhoHaveOrdered, orderTakenContainer, RequestTimeout.ZERO, tx); + sendItemsToContainer(groupsWhoHaveOrdered, + pizzeriaGroupContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(order.getOrderedPizzas(), preparePizzasContainer, RequestTimeout.ZERO, tx); capi.commitTransaction(tx); log.info("Waiter has taken order from group {}", - groupData.getId()); + group.getId()); } catch (Exception e) { // log.info(e.getMessage()); } @@ -263,11 +271,12 @@ public class WaiterXVSM extends AbstractXVSMConnector { * number of those pizzas with the number of necessary pizzas. If all * pizzas of an order are complete he then delivers them! */ - SpaceListener preparedPizzasListener = getDefaultBuilder().setCref(preparedPizzasContainer).setSpaceAction(new SpaceAction() { + SpaceListener preparedPizzasListener = getDefaultBuilder().setLookaround(true).setCref(preparedPizzasContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { + log.info("NEW PIZZAS ARRived"); List pizzas = castEntries(entries); @@ -281,12 +290,14 @@ public class WaiterXVSM extends AbstractXVSMConnector { try { GroupData entity = new GroupData(); - entity.setState(null); + entity.setState(GroupState.ORDERED); entity.setOrder(order); - GroupData groupData = takeMatchingEntity(entity, + takeMatchingEntity(entity, orderTakenContainer, tx, RequestTimeout.DEFAULT, "Another waiter just checks if the order is complete"); + GroupData groupData = takeMatchingEntityIfItExists(entity, + pizzeriaGroupContainer, tx, RequestTimeout.DEFAULT); int groupId = groupData.getId(); int numberOfPizzas = groupData.getOrder().getNumberOfPizzas(); @@ -298,16 +309,19 @@ public class WaiterXVSM extends AbstractXVSMConnector { RequestTimeout.DEFAULT, "Cannot take the pizzas from the preparedPizzasContainer"); + final List groupsWithCompleteOrder = Arrays.asList(groupData); 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); + 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); sendItemsToContainer(groupsWithCompleteOrder, - orderCompleteContainer, RequestTimeout.DEFAULT, + orderDeliveredContainer, RequestTimeout.DEFAULT, + tx); + sendItemsToContainer(groupsWithCompleteOrder, + pizzeriaGroupContainer, RequestTimeout.DEFAULT, tx); capi.commitTransaction(tx); @@ -328,7 +342,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { }).createSpaceListenerImpl(); } - public void listenForPreparedDeliveryPizzas() { /** * A waiter gets informed when a new pizza is complete. He takes the @@ -380,7 +393,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { group.setOrder(completeOrder); final List groupsWithCompleteOrder = Arrays.asList(group); sendItemsToContainer(groupsWithCompleteOrder, - deliverDeliveryOrder, RequestTimeout.DEFAULT, + deliverDeliveryOrderContainer, RequestTimeout.DEFAULT, tx); capi.commitTransaction(tx); @@ -401,6 +414,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { }).createSpaceListenerImpl(); } + private void assignGroupToTable(GroupData lockedGroup, Table lockedFreeTable, TransactionReference tx) throws MzsCoreException { @@ -413,10 +427,14 @@ public class WaiterXVSM extends AbstractXVSMConnector { lockedGroup.setTableWaiter(waiterId); final List
freeTables = Arrays.asList(lockedFreeTable); + sendItemsToContainer(freeTables, + pizzeriaTableContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(freeTables, tableAssignedContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(Arrays.asList(lockedGroup), takeOrderContainer, RequestTimeout.ZERO, tx); + sendItemsToContainer(Arrays.asList(lockedGroup), pizzeriaGroupContainer, + RequestTimeout.ZERO, tx); try { capi.commitTransaction(tx); @@ -428,4 +446,18 @@ public class WaiterXVSM extends AbstractXVSMConnector { log.info(e.getMessage()); } } + + 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); + } } -- 2.43.0