From a585d4a447ab2e6430a056093b7395f4e1ce4587 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Wed, 12 Jun 2013 14:15:02 +0200 Subject: [PATCH] GUI fix for Tables OutOfBoundsException [XVSM] Cook may prepare delivery pizza only when there is no open normal order --- runJMS-group.sh | 4 + runXVSM-spaces.sh | 3 +- runXVSM.sh | 14 +- .../tuwien/sbc/valesriegler/common/Util.java | 31 +- .../sbc/valesriegler/group/GroupAgent.java | 28 +- .../group/gui/DeliveryOverviewModel.java | 16 +- .../group/gui/GroupCreationHandler.java | 4 +- .../group/jms/JMSGroupConnector.java | 377 +++++++++--------- .../valesriegler/pizzeria/PizzeriaAgent.java | 2 +- .../pizzeria/gui/PizzeriaFrame.java | 5 +- .../gui/tablemodels/AbstractOrdersModel.java | 4 + .../gui/tablemodels/DeliveryOrdersModel.java | 60 +-- .../gui/tablemodels/GroupsOverviewModel.java | 20 - .../gui/tablemodels/OrdersOverviewModel.java | 12 +- .../sbc/valesriegler/types/PizzaOrder.java | 13 +- .../xvsm/AbstractXVSMConnector.java | 4 +- .../sbc/valesriegler/xvsm/CookXVSM.java | 73 ++-- .../valesriegler/xvsm/DeliveryGroupXVSM.java | 2 +- .../sbc/valesriegler/xvsm/DriverXVSM.java | 17 +- .../xvsm/EntityNotFoundByTemplate.java | 14 - .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 8 +- .../sbc/valesriegler/xvsm/GroupXVSM.java | 3 +- .../valesriegler/xvsm/PizzeriaAgentXVSM.java | 6 +- .../sbc/valesriegler/xvsm/WaiterXVSM.java | 21 +- .../xvsm/spacehelpers/SpaceListener.java | 5 +- .../xvsm/spacehelpers/SpaceListenerImpl.java | 3 +- .../SpaceListenerImplBuilder.java | 8 +- 27 files changed, 355 insertions(+), 402 deletions(-) create mode 100644 runJMS-group.sh diff --git a/runJMS-group.sh b/runJMS-group.sh new file mode 100644 index 0000000..4ff664b --- /dev/null +++ b/runJMS-group.sh @@ -0,0 +1,4 @@ +#!/bin/bash +mvn package + +mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.group.GroupAgent" -Dexec.args="JMS tcp://localhost:61610?jms.prefetchPolicy.all=1" & \ No newline at end of file diff --git a/runXVSM-spaces.sh b/runXVSM-spaces.sh index b7e6dc4..b39fc99 100755 --- a/runXVSM-spaces.sh +++ b/runXVSM-spaces.sh @@ -1,4 +1,5 @@ #!/bin/bash mvn exec:java -Dexec.mainClass="org.mozartspaces.core.Server" -Dexec.args=9876 & mvn exec:java -Dexec.mainClass="org.mozartspaces.core.Server" -Dexec.args=9875 & -mvn exec:java -Dexec.mainClass="org.mozartspaces.core.Server" -Dexec.args=9874 & \ No newline at end of file +mvn exec:java -Dexec.mainClass="org.mozartspaces.core.Server" -Dexec.args=9874 & +mvn exec:java -Dexec.mainClass="org.mozartspaces.core.Server" -Dexec.args=9877 & diff --git a/runXVSM.sh b/runXVSM.sh index 2916ed5..97aab33 100755 --- a/runXVSM.sh +++ b/runXVSM.sh @@ -1,15 +1,15 @@ #!/bin/bash mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="1 9875" & -mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="2 9875" & +#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="2 9875" & mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="1 9874" & -mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="2 9874" & +#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.cook.Cook" -Dexec.args="2 9874" & mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="1 9875" & -mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="2 9875" & +#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="2 9875" & mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="1 9874" & -mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="2 9874" & +#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter" -Dexec.args="2 9874" & mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.driver.Driver" -Dexec.args="1 9875" & -mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.driver.Driver" -Dexec.args="2 9875" & +#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.driver.Driver" -Dexec.args="2 9875" & mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.driver.Driver" -Dexec.args="1 9874" & -mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.driver.Driver" -Dexec.args="2 9874" & +#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.xvsm.driver.Driver" -Dexec.args="2 9874" & mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent" -Dexec.args="XVSM 9875" & -mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent" -Dexec.args="XVSM 9874" & +#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent" -Dexec.args="XVSM 9874" & 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 4f200e8..ff93d97 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java @@ -1,6 +1,7 @@ package at.ac.tuwien.sbc.valesriegler.common; import at.ac.tuwien.sbc.valesriegler.group.gui.GroupCreationDetailsRequest; +import at.ac.tuwien.sbc.valesriegler.types.HasOrder; import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.PizzaType; @@ -15,7 +16,28 @@ import java.util.*; import java.util.List; public abstract class Util { - private static final Logger log = LoggerFactory.getLogger(Util.class); + public static final Comparator HAS_ORDER_COMPARATOR = new Comparator() { + @Override + public int compare(HasOrder o1, HasOrder o2) { + final int o1Id = o1.getOrder().getId(); + final int o2Id = o2.getOrder().getId(); + if (o1Id == 0) { + if (o2Id > 0) + return 1; + } + if (o2Id == 0) { + if (o1Id > 0) + return 1; + } + if (o1Id < o2Id) + return -1; + else if (o1Id > o2Id) + return 1; + else + return 0; + } + }; + private static final Logger log = LoggerFactory.getLogger(Util.class); public static boolean useJMS = false; public static final String TABLE_ASSIGNED = "tables"; @@ -42,7 +64,7 @@ public abstract class Util { public static final String DELIVERY_PROGRESS = "deliveryInProgress"; public static final String DELIVERY_DONE = "deliveryDone"; - public static final boolean runSimulation = true; + public static final boolean runSimulation = false; public static final String NUMBER_DISPLAY_FORMAT = String.format("%%0%dd", 3); @@ -157,10 +179,13 @@ public abstract class Util { System.exit(1); } - public static Order createOrder(HasId group, GroupCreationDetailsRequest gc) { + public static Order createOrder(HasId group, GroupCreationDetailsRequest gc, boolean isDelivery) { List pizzaOrders = new ArrayList<>(); for (PizzaType pt : gc.pizzaTypes) { final PizzaOrder pizzaOrder = new PizzaOrder(pt); + if(isDelivery) { + pizzaOrder.setDeliveryPizza(true); + } pizzaOrders.add(pizzaOrder); } Order order = new Order(group, pizzaOrders); 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 af2418c..28e1b7a 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java @@ -1,20 +1,5 @@ package at.ac.tuwien.sbc.valesriegler.group; -import java.lang.management.ManagementFactory; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.atomic.AtomicLong; - -import javax.jms.JMSException; -import javax.swing.SwingUtilities; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel; import at.ac.tuwien.sbc.valesriegler.group.gui.GroupCreationDetailsRequest; @@ -29,6 +14,14 @@ import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.PizzaType; import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.jms.JMSException; +import javax.swing.*; +import java.lang.management.ManagementFactory; +import java.util.*; +import java.util.concurrent.atomic.AtomicLong; /** * The Main class of the Group component. @@ -81,7 +74,7 @@ public class GroupAgent { } groupAgent = new GroupAgent(); - if (!Util.runSimulation) { + if (true || !Util.runSimulation) { groupGui = new GroupGUI(); SwingUtilities.invokeLater(groupGui); } else if (Util.useJMS) { @@ -112,6 +105,7 @@ public class GroupAgent { log.warn("Starting benchmark in 20 sec"); Thread.sleep(20000); + startTime.set(new Date().getTime()); jmsnac.sendNACMsg(new BenchmarkStart()); Thread.sleep(60000); jmsnac.sendNACMsg(new BenchmarkStop()); @@ -150,7 +144,7 @@ public class GroupAgent { for (int i = 0; i < gc.numberOfGroups; i++) { DeliveryGroup group = new DeliveryGroup(); - final Order order = Util.createOrder(group, gc); + final Order order = Util.createOrder(group, gc, false); group.getDeliveryGroupData().setOrder(order); group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria); group.getDeliveryGroupData().setAddress(gc.address); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java index 562ab71..6790c7d 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java @@ -1,17 +1,16 @@ package at.ac.tuwien.sbc.valesriegler.group.gui; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import at.ac.tuwien.sbc.valesriegler.common.TableModel; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.DeliveryGroup; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; public class DeliveryOverviewModel extends TableModel { private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class); @@ -68,8 +67,9 @@ public class DeliveryOverviewModel extends TableModel { final DeliveryGroup deliveryGroup = items.get(group.getId()); if (deliveryGroup == null) { log.error("Delivery group not found: {}", group.getId()); - } else + } else { deliveryGroup.setDeliveryGroupData(group); + } } } fireTableDataChanged(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationHandler.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationHandler.java index 798771c..8aa5291 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 @@ -50,14 +50,14 @@ class GroupCreationHandler { for (int i = 0; i < gc.numberOfGroups; i++) { if(createDeliveryGroups) { DeliveryGroup group = new DeliveryGroup(); - final Order order = Util.createOrder(group, gc); + final Order order = Util.createOrder(group, gc, true); group.getDeliveryGroupData().setOrder(order); group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria); group.getDeliveryGroupData().setAddress(gc.address); newDeliveryGroups.add(group); } else { Group group = new Group(); - final Order order = Util.createOrder(group, gc); + final Order order = Util.createOrder(group, gc, false); group.getGroupData().setOrder(order); group.getGroupData().setSize(gc.size); group.getGroupData().setPizzeriaId(gc.pizzeria); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java index 29be12b..c2fe603 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java @@ -1,216 +1,197 @@ package at.ac.tuwien.sbc.valesriegler.group.jms; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; - -import javax.jms.Connection; -import javax.jms.DeliveryMode; -import javax.jms.JMSException; -import javax.jms.Message; -import javax.jms.MessageConsumer; -import javax.jms.MessageListener; -import javax.jms.MessageProducer; -import javax.jms.ObjectMessage; -import javax.jms.Session; - -import org.apache.activemq.ActiveMQConnectionFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; import at.ac.tuwien.sbc.valesriegler.cook.actions.DeliveryOrderInfo; import at.ac.tuwien.sbc.valesriegler.driver.actions.OrderDeliveredInfo; import at.ac.tuwien.sbc.valesriegler.group.AbstractGroupConnector; import at.ac.tuwien.sbc.valesriegler.group.Group; import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; -import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; -import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderResponse; -import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; -import at.ac.tuwien.sbc.valesriegler.group.actions.OrderResponse; -import at.ac.tuwien.sbc.valesriegler.group.actions.PayRequest; -import at.ac.tuwien.sbc.valesriegler.group.actions.PayResponse; -import at.ac.tuwien.sbc.valesriegler.group.actions.TableFree; -import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; -import at.ac.tuwien.sbc.valesriegler.group.actions.TableResponse; +import at.ac.tuwien.sbc.valesriegler.group.actions.*; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.waiter.actions.DeliverOrder; +import org.apache.activemq.ActiveMQConnectionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.jms.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; /** * This class handles the communication with other processes using JMS. - * + * * @author jan - * */ public class JMSGroupConnector extends AbstractGroupConnector implements MessageListener { - private static final Logger log = LoggerFactory.getLogger(JMSGroupConnector.class); - private static HashMap connectors = new HashMap(); - - public final String JMS_CONNECTSTRING; - private Connection connection; - - public JMSGroupConnector(String address) { - if (address == null) - throw new IllegalArgumentException("Address may not be null!"); - JMS_CONNECTSTRING = address; - init(); - } - - @Override - public void init() { - try { - connection = new ActiveMQConnectionFactory(JMS_CONNECTSTRING).createConnection(); - connection.start(); - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageConsumer consumer = session.createConsumer(session.createQueue("GroupConnector")); - consumer.setMessageListener(this); - } catch (JMSException e) { - log.error("EXCEPTION!", e); - } - } - - @Override - public void onMessage(Message msg) { - try { - msg.acknowledge(); - if (msg instanceof ObjectMessage) { - ObjectMessage objMsg = (ObjectMessage) msg; - Object obj = objMsg.getObject(); - - if (obj instanceof TableResponse) { - TableResponse tablerresponse = (TableResponse) obj; - log.debug("Received: " + tablerresponse); - Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(tablerresponse.getGroupdata().getId()); - g.assignTable(tablerresponse.getTable(), tablerresponse.getWaiterId()); - return; - } else if (obj instanceof OrderResponse) { - OrderResponse orderresponse = (OrderResponse) obj; - log.debug("Received: " + orderresponse); - Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(orderresponse.getGroupdata().getId()); - g.orderReceived(orderresponse.getWaiterId()); - return; - } else if (obj instanceof DeliverOrder) { - DeliverOrder deliverorder = (DeliverOrder) obj; - log.debug("Received: " + deliverorder); - Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(deliverorder.getGroupdata().getId()); - g.deliverPizzas(deliverorder.getGroupdata().getOrder(), deliverorder.getWaiterId()); - return; - } else if (obj instanceof PayResponse) { - PayResponse payresponse = (PayResponse) obj; - log.debug("Received: " + payresponse); - Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(payresponse.getGroupdata().getId()); - g.payForPizzas(payresponse.getWaiterId()); - return; - - } else if (obj instanceof DeliveryOrderResponse) { - DeliveryOrderResponse deliveryOrderResponse = (DeliveryOrderResponse) obj; - log.debug("Received: " + deliveryOrderResponse); - deliveryOrderResponse.getDeliveryGroupData().setWaiterIdOfOrder(deliveryOrderResponse.getWaiterId()); - ArrayList gd = new ArrayList(); - gd.add(deliveryOrderResponse.getDeliveryGroupData()); - GroupAgent.getInstance().getDeliveryModel().addDeliveries(gd); - return; - - } else if (obj instanceof DeliveryOrderInfo) { - DeliveryOrderInfo deliveryOrderInfo = (DeliveryOrderInfo) obj; - log.debug("Received: " + deliveryOrderInfo); - ArrayList gd = new ArrayList(); - gd.add(deliveryOrderInfo.getDeliveryGroupData()); - GroupAgent.getInstance().getDeliveryModel().addDeliveries(gd); - return; - - } else if (obj instanceof OrderDeliveredInfo) { - OrderDeliveredInfo orderDeliveredInfo = (OrderDeliveredInfo) obj; - log.debug("Received: " + orderDeliveredInfo); - orderDeliveredInfo.getDeliveryGroupData().setDriverId(orderDeliveredInfo.getDriverId()); - ArrayList gd = new ArrayList(); - gd.add(orderDeliveredInfo.getDeliveryGroupData()); - GroupAgent.getInstance().getDeliveryModel().addDeliveries(gd); - - if (GroupAgent.getInstance().getDeliveryModel().hasFinished()) { - log.warn("Finished after {} seconds", (new Date().getTime() - GroupAgent.startTime.get()) / 1000); - } - return; - } - - } - log.warn("Unknown messagetype received!"); - } catch (JMSException e) { - log.error("EXCEPTION!", e); - } - } - - @Override - public void send(AbstractAction request) { - try { - if (request instanceof TableRequest) { - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("WantToSitAtTable")); - wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - wantToSitAtTable.send(session.createObjectMessage(request)); - - MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); - informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - informPizzariaGui.send(session.createObjectMessage(request)); - return; - - } else if (request instanceof OrderRequest) { - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageProducer wantToOrder = session.createProducer(session.createQueue("WantToOrder")); - wantToOrder.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - wantToOrder.send(session.createObjectMessage(request)); - return; - - } else if (request instanceof PayRequest) { - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageProducer wantToPay = session.createProducer(session.createQueue("WantToPay")); - wantToPay.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - wantToPay.send(session.createObjectMessage(request)); - return; - - } else if (request instanceof TableFree) { - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("TablesFree")); - wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - wantToSitAtTable.send(session.createObjectMessage(request.getGroupdata().getTable())); - - MessageProducer wantToLeave = session.createProducer(session.createQueue("WantToLeave")); - wantToLeave.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - wantToLeave.send(session.createObjectMessage(request)); - - MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); - informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - informPizzariaGui.send(session.createObjectMessage(request)); - session.close(); - return; - - } else if (request instanceof DeliveryOrderRequest) { - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageProducer wantToPay = session.createProducer(session.createQueue("WantADelivery")); - wantToPay.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - wantToPay.send(session.createObjectMessage(request)); - - MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); - informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - informPizzariaGui.send(session.createObjectMessage(request)); - session.close(); - return; - } - } catch (JMSException e) { - log.error("EXCEPTION!", e); - } - } - - public static HashMap getConnectors() { - return connectors; - } - - public static JMSGroupConnector getConnector(String key) { - return connectors.get(key); - } - - public Connection getConnection() { - return connection; - } + private static final Logger log = LoggerFactory.getLogger(JMSGroupConnector.class); + private static HashMap connectors = new HashMap(); + + public final String JMS_CONNECTSTRING; + private Connection connection; + + public JMSGroupConnector(String address) { + if (address == null) + throw new IllegalArgumentException("Address may not be null!"); + JMS_CONNECTSTRING = address; + init(); + } + + @Override + public void init() { + try { + connection = new ActiveMQConnectionFactory(JMS_CONNECTSTRING).createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consumer = session.createConsumer(session.createQueue("GroupConnector")); + consumer.setMessageListener(this); + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + } + + @Override + public void onMessage(Message msg) { + try { + msg.acknowledge(); + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof TableResponse) { + TableResponse tablerresponse = (TableResponse) obj; + log.debug("Received: " + tablerresponse); + Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(tablerresponse.getGroupdata().getId()); + g.assignTable(tablerresponse.getTable(), tablerresponse.getWaiterId()); + return; + } else if (obj instanceof OrderResponse) { + OrderResponse orderresponse = (OrderResponse) obj; + log.debug("Received: " + orderresponse); + Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(orderresponse.getGroupdata().getId()); + g.orderReceived(orderresponse.getWaiterId()); + return; + } else if (obj instanceof DeliverOrder) { + DeliverOrder deliverorder = (DeliverOrder) obj; + log.debug("Received: " + deliverorder); + Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(deliverorder.getGroupdata().getId()); + g.deliverPizzas(deliverorder.getGroupdata().getOrder(), deliverorder.getWaiterId()); + return; + } else if (obj instanceof PayResponse) { + PayResponse payresponse = (PayResponse) obj; + log.debug("Received: " + payresponse); + Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(payresponse.getGroupdata().getId()); + g.payForPizzas(payresponse.getWaiterId()); + return; + + } else if (obj instanceof DeliveryOrderResponse) { + DeliveryOrderResponse deliveryOrderResponse = (DeliveryOrderResponse) obj; + log.debug("Received: " + deliveryOrderResponse); + deliveryOrderResponse.getDeliveryGroupData().setWaiterIdOfOrder(deliveryOrderResponse.getWaiterId()); + ArrayList gd = new ArrayList(); + gd.add(deliveryOrderResponse.getDeliveryGroupData()); + GroupAgent.getInstance().getDeliveryModel().addDeliveries(gd); + return; + + } else if (obj instanceof DeliveryOrderInfo) { + DeliveryOrderInfo deliveryOrderInfo = (DeliveryOrderInfo) obj; + log.debug("Received: " + deliveryOrderInfo); + ArrayList gd = new ArrayList(); + gd.add(deliveryOrderInfo.getDeliveryGroupData()); + GroupAgent.getInstance().getDeliveryModel().addDeliveries(gd); + return; + + } else if (obj instanceof OrderDeliveredInfo) { + OrderDeliveredInfo orderDeliveredInfo = (OrderDeliveredInfo) obj; + log.debug("Received: " + orderDeliveredInfo); + orderDeliveredInfo.getDeliveryGroupData().setDriverId(orderDeliveredInfo.getDriverId()); + ArrayList gd = new ArrayList(); + gd.add(orderDeliveredInfo.getDeliveryGroupData()); + GroupAgent.getInstance().getDeliveryModel().addDeliveries(gd); + + if (GroupAgent.getInstance().getDeliveryModel().hasFinished()) { + log.warn("Finished after {} seconds", (new Date().getTime() - GroupAgent.startTime.get()) / 1000); + } + return; + } + + } + log.warn("Unknown messagetype received!"); + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + } + + @Override + public void send(AbstractAction request) { + try { + if (request instanceof TableRequest) { + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("WantToSitAtTable")); + wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToSitAtTable.send(session.createObjectMessage(request)); + + MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); + informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzariaGui.send(session.createObjectMessage(request)); + return; + + } else if (request instanceof OrderRequest) { + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer wantToOrder = session.createProducer(session.createQueue("WantToOrder")); + wantToOrder.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToOrder.send(session.createObjectMessage(request)); + return; + + } else if (request instanceof PayRequest) { + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer wantToPay = session.createProducer(session.createQueue("WantToPay")); + wantToPay.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToPay.send(session.createObjectMessage(request)); + return; + + } else if (request instanceof TableFree) { + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("TablesFree")); + wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToSitAtTable.send(session.createObjectMessage(request.getGroupdata().getTable())); + + MessageProducer wantToLeave = session.createProducer(session.createQueue("WantToLeave")); + wantToLeave.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToLeave.send(session.createObjectMessage(request)); + + MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); + informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzariaGui.send(session.createObjectMessage(request)); + session.close(); + return; + + } else if (request instanceof DeliveryOrderRequest) { + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer wantToPay = session.createProducer(session.createQueue("WantADelivery")); + wantToPay.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToPay.send(session.createObjectMessage(request)); + + MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); + informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzariaGui.send(session.createObjectMessage(request)); + session.close(); + return; + } + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + } + + public static HashMap getConnectors() { + return connectors; + } + + public static JMSGroupConnector getConnector(String key) { + return connectors.get(key); + } + + public Connection getConnection() { + return connection; + } } 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 87a416c..e651ffb 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 @@ -53,7 +53,7 @@ public class PizzeriaAgent { case "XVSM" : pizzeriaAgent.createModels(); pizzeriaAgent.initXVSM(args[1]); - if (! Util.runSimulation) { + if (true || !Util.runSimulation) { pizzeriaAgent.initGUI(); } Util.useJMS = false; 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 0680842..49cb952 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 @@ -150,8 +150,7 @@ public class PizzeriaFrame extends JFrame { final Order currentOrder = pizzasOfDeliveryModel.getCurrentOrder(); if (currentOrder == null) return; - final int groupId = currentOrder.getGroupId(); - final DeliveryGroupData groupOfRow = deliveryOrdersModel.getGroupOfRow(currentOrder.getId()-1); + final DeliveryGroupData groupOfRow = deliveryOrdersModel.getHasOrderById(currentOrder.getGroupId()); pizzasOfDeliveryModel.setCurrentOrder(groupOfRow.getOrder()); deliveryDetailsModel.setCurrentDelivery(groupOfRow); @@ -214,7 +213,7 @@ public class PizzeriaFrame extends JFrame { final Order currentOrder = pizzasOfOrderModel.getCurrentOrder(); if (currentOrder == null) return; final int groupId = currentOrder.getGroupId(); - final GroupData groupOfRow = ordersModel.getGroupOfRow(currentOrder.getId()-1); + final GroupData groupOfRow = ordersModel.getHasOrderById(currentOrder.getGroupId()); pizzasOfOrderModel.setCurrentOrder(groupOfRow.getOrder()); waitersModel.setCurrentGroup(groupOfRow); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/AbstractOrdersModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/AbstractOrdersModel.java index fe60ee7..6f9359f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/AbstractOrdersModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/AbstractOrdersModel.java @@ -8,4 +8,8 @@ import org.slf4j.LoggerFactory; public abstract class AbstractOrdersModel extends TableModel { private static final Logger log = LoggerFactory.getLogger(AbstractOrdersModel.class); + + public T getHasOrderById(int id) { + return items.get(id); + } } 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 9335d60..6273902 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java @@ -1,15 +1,14 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - +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; -import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; -import at.ac.tuwien.sbc.valesriegler.types.Order; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; public class DeliveryOrdersModel extends AbstractOrdersModel { private static final Logger log = LoggerFactory.getLogger(DeliveryOrdersModel.class); @@ -32,28 +31,7 @@ public class DeliveryOrdersModel extends AbstractOrdersModel @Override public Object getValueAt(int rowIndex, int columnIndex) { List values = new ArrayList<>(items.values()); - // TODO: make sure that is necessary - Collections.sort(values, new Comparator() { - @Override - public int compare(DeliveryGroupData o1, DeliveryGroupData o2) { - final int o1Id = o1.getOrder().getId(); - final int o2Id = o2.getOrder().getId(); - if (o1Id == 0) { - if (o2Id > 0) - return 1; - } - if (o2Id == 0) { - if (o1Id > 0) - return 1; - } - if (o1Id < o2Id) - return -1; - else if (o1Id > o2Id) - return 1; - else - return 0; - } - }); + Collections.sort(values, Util.HAS_ORDER_COMPARATOR); DeliveryGroupData group = values.get(rowIndex); Order order = group.getOrder(); String wantedColumn = COLUMNS[columnIndex]; @@ -77,29 +55,7 @@ public class DeliveryOrdersModel extends AbstractOrdersModel public DeliveryGroupData getGroupOfRow(int rowIndex) { List values = new ArrayList<>(items.values()); - // TODO: make sure that is necessary - Collections.sort(values, new Comparator() { - @Override - public int compare(DeliveryGroupData o1, DeliveryGroupData o2) { - final int o1Id = o1.getOrder().getId(); - final int o2Id = o2.getOrder().getId(); - if (o1Id == 0) { - if (o2Id > 0) - return 1; - } - if (o2Id == 0) { - if (o1Id > 0) - return 1; - } - if (o1Id < o2Id) - return -1; - else if (o1Id > o2Id) - return 1; - else - return 0; - } - }); + Collections.sort(values, Util.HAS_ORDER_COMPARATOR); return values.get(rowIndex); - // return new ArrayList<>(items.values()).get(rowIndex); } } 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 142e14f..b754639 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 @@ -7,8 +7,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.List; public class GroupsOverviewModel extends TableModel { @@ -27,24 +25,6 @@ public class GroupsOverviewModel extends TableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { List values = new ArrayList<>(items.values()); - - // TODO: make sure that is necessary - Collections.sort(values, new Comparator() { - @Override - public int compare(GroupData o1, GroupData o2) { - final int o1Id = o1.getOrder().getId(); - final int o2Id = o2.getOrder().getId(); - if (o1Id == 0) { - if (o2Id > 0) return 1; - } - if (o2Id == 0) { - if (o1Id > 0) return 1; - } - if (o1Id < o2Id) return -1; - else if (o1Id > o2Id) return 1; - else return 0; - } - }); GroupData group = values.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch (wantedColumn) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java index 289ea43..9b36714 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java @@ -7,10 +7,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class OrdersOverviewModel extends AbstractOrdersModel { - private static final Logger log = LoggerFactory.getLogger(OrdersOverviewModel.class); + private static final Logger log = LoggerFactory.getLogger(OrdersOverviewModel.class); private static final String ID = "Order ID"; private static final String TABLE_ID = "Table ID"; @@ -19,10 +20,11 @@ public class OrdersOverviewModel extends AbstractOrdersModel { private static final String[] COLUMNS = new String[] { ID, TABLE_ID, GROUP_ID, STATUS }; - @Override + @Override public Object getValueAt(int rowIndex, int columnIndex) { List values = new ArrayList<>(items.values()); - GroupData group = values.get(rowIndex); + Collections.sort(values, Util.HAS_ORDER_COMPARATOR); + GroupData group = values.get(rowIndex); Order order = group.getOrder(); String wantedColumn = COLUMNS[columnIndex]; switch (wantedColumn) { @@ -40,7 +42,9 @@ public class OrdersOverviewModel extends AbstractOrdersModel { } public GroupData getGroupOfRow(int rowIndex) { - return new ArrayList<>(items.values()).get(rowIndex); + final List values = new ArrayList<>(items.values()); + Collections.sort(values, Util.HAS_ORDER_COMPARATOR); + return new ArrayList<>(values).get(rowIndex); } @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 8510199..d58ca02 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 @@ -1,10 +1,10 @@ package at.ac.tuwien.sbc.valesriegler.types; -import java.io.Serializable; - import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.common.Util; +import java.io.Serializable; + /** * Class denoting one Pizza in the future and its state. * @@ -19,6 +19,7 @@ public class PizzaOrder implements Serializable, HasId { protected PizzaType pizzaType; protected PizzaOrderStatus status; protected Integer cookId; + protected Boolean isDeliveryPizza; public PizzaOrder(PizzaType pizzaType) { id = ++nextID; @@ -37,10 +38,18 @@ public class PizzaOrder implements Serializable, HasId { return pizzaType; } + public Boolean getDeliveryPizza() { + return isDeliveryPizza; + } + public PizzaOrderStatus getStatus() { return status; } + public void setDeliveryPizza(Boolean deliveryPizza) { + isDeliveryPizza = deliveryPizza; + } + public int getId() { return id; } 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 783a56c..ffe5e7e 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 @@ -236,7 +236,7 @@ public abstract class AbstractXVSMConnector { URI.create(String.format(Util.SERVER_ADDR, port))); } - protected SpaceListenerImplBuilder getDefaultBuilder() { - return new SpaceListenerImplBuilder().setCapi(capi).setNotificationManager(notificationMgr); + protected SpaceListenerImplBuilder getDefaultBuilder(String name) { + return new SpaceListenerImplBuilder().setCapi(capi).setName(name).setNotificationManager(notificationMgr); } } \ 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 index eca7a70..5842b46 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 @@ -36,7 +36,7 @@ public class CookXVSM extends AbstractXVSMConnector { } public void listenForPizzas() { - getDefaultBuilder().setLookaround(true).enableCountAll().setCref(preparePizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { + getDefaultBuilder("").setLookaround(true).setCref(preparePizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) @@ -58,8 +58,7 @@ public class CookXVSM extends AbstractXVSMConnector { } // Require the lock for preparing the pizza - PizzaOrder order = takeMatchingEntity(new PizzaOrder(pizzaOrder.getId()), preparePizzasContainer, tx, RequestTimeout.DEFAULT, pizzaAlreadyCooked); - + 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, true); @@ -71,15 +70,14 @@ public class CookXVSM extends AbstractXVSMConnector { notifyCustomerPizzaDone(pizza, tx); capi.commitTransaction(tx); -// log.debug("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 (EntityNotFoundByTemplate e) { -// log.info("entitynotfound: {}", e.getMessage()); + log.info("entitynotfound: {}", e.getMessage()); } catch (Exception e) { -// log.info("outer cook"); -// log.info(e.getMessage()); -// e.printStackTrace(); + log.error("outer cook"); + log.error(e.getMessage()); } } @@ -89,7 +87,7 @@ public class CookXVSM extends AbstractXVSMConnector { } public void listenForDeliveryPizzas() { - getDefaultBuilder().setLookaround(true).setCref(prepareDeliveryPizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { + getDefaultBuilder("").setLookaround(true).setCref(prepareDeliveryPizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) @@ -105,47 +103,44 @@ public class CookXVSM extends AbstractXVSMConnector { String pizzaAlreadyCooked = String.format("Delivery Pizza with id %d has already been cooked by another cook", pizzaOrder.getId()); try { - if (!mayPrepareDeliveryPizza(pizzaOrder, tx)) { + if (!mayPrepareDeliveryPizza(tx)) { capi.rollbackTransaction(tx); continue; } // Require the lock for preparing the pizza - PizzaOrder order = takeMatchingEntity(new PizzaOrder(pizzaOrder.getId()), prepareDeliveryPizzasContainer, tx, RequestTimeout.DEFAULT, pizzaAlreadyCooked); + PizzaOrder order = takeMatchingEntity(new PizzaOrder(pizzaOrder.getId()), prepareDeliveryPizzasContainer, tx, RequestTimeout.ZERO, pizzaAlreadyCooked); // tell the space that you prepare the pizza -> without a transaction!! Pizza pizzaInProgress = Pizza.createPizzaFromPizzaOrder(order, cookId, true); pizzaInProgress.setStatus(PizzaOrderStatus.IN_PREPARATION); - if (! Util.runSimulation) { - notifyDeliveryPizzaInProgress(order, pizzaInProgress); - } + notifyDeliveryPizzaInProgress(order, pizzaInProgress); -// log.info("before creating!"); + log.debug("before creating!"); PizzaOrder pizza = createPizza(order); -// log.info("after creating!"); + log.debug("after creating!"); sendItemsToContainer(Arrays.asList(pizza), preparedDeliveryPizzasContainer, RequestTimeout.DEFAULT, tx); -// log.info("after prepareddeliverypizzascon"); + log.debug("after prepareddeliverypizzascon"); notifyDeliveryPizzaDone(pizza, tx); -// log.info("after notifydone"); + log.debug("after notifydone"); capi.commitTransaction(tx); - log.info("I have completed preparing a delivery pizza for order {}!", pizza.getOrderId()); + log.debug("I have completed preparing a delivery pizza for order {}!", pizza.getOrderId()); } catch (NullPointerException e) { // the strange nullpointer exception from the space } catch (EntityNotFoundByTemplate e) { -// log.info("entitynotfound: {}", e.getMessage()); + log.info("entitynotfound: {}", e.getMessage()); } catch (Exception e) { -// log.info("outer cook"); -// log.info(e.getMessage()); -// e.printStackTrace(); + log.error("outer cook"); + log.error(e.getMessage()); } } @@ -159,13 +154,13 @@ public class CookXVSM extends AbstractXVSMConnector { * in preparation or has been done! */ private boolean mayPrepareCustomerPizza(PizzaOrder pizzaOrder, TransactionReference tx) throws MzsCoreException { - List deliveryPizzas = readMatchingEntities(new PizzaOrder(), prepareDeliveryPizzasContainer, tx, RequestTimeout.INFINITE, "MAYPREPAREPIZZA: Cannot access prepareDeliveryPizzasContainer"); + List deliveryPizzas = readMatchingEntities(new PizzaOrder(), prepareDeliveryPizzasContainer, tx, RequestTimeout.DEFAULT, "MAYPREPAREPIZZA: Cannot access prepareDeliveryPizzasContainer"); if (!deliveryPizzas.isEmpty()) { final PizzaOrder template = new PizzaOrder(); template.setOrderId(pizzaOrder.getOrderId()); - final List pizzasAlreadyDone = readMatchingEntities(template, preparedPizzasContainer, tx, RequestTimeout.INFINITE, "MAYPREPAREPIZZA: Cannot access preparedPizzasContainer"); + final List pizzasAlreadyDone = readMatchingEntities(template, preparedPizzasContainer, tx, RequestTimeout.DEFAULT, "MAYPREPAREPIZZA: Cannot access preparedPizzasContainer"); if(! pizzasAlreadyDone.isEmpty()) return true; - final List pizzasOfOrderInProgress = readMatchingEntities(template, pizzaInProgressContainer, tx, RequestTimeout.INFINITE, "MAYPREPAREPIZZA:: Cannot access pizzaInProgressContainer"); + final List pizzasOfOrderInProgress = readMatchingEntities(template, pizzaInProgressContainer, tx, RequestTimeout.DEFAULT, "MAYPREPAREPIZZA:: Cannot access pizzaInProgressContainer"); if (! pizzasOfOrderInProgress.isEmpty()) return true; return false; @@ -174,8 +169,14 @@ public class CookXVSM extends AbstractXVSMConnector { } - private boolean mayPrepareDeliveryPizza(PizzaOrder pizzaOrder, TransactionReference tx) throws MzsCoreException { - // TODO: return false if a cook has already started cooking pizzas of an open order + private boolean mayPrepareDeliveryPizza(TransactionReference tx) throws MzsCoreException { + final PizzaOrder template = new PizzaOrder(); + template.setDeliveryPizza(false); + final List preparedNonDeliveryPizzas = readMatchingEntities(template, preparedPizzasContainer, tx, RequestTimeout.DEFAULT, "mayPrepareDeliveryPizza: cannot access preparedPizzasContainer"); + if(! preparedNonDeliveryPizzas.isEmpty()) return false; + final List nonDeliveryPizzasInProgress = readMatchingEntities(template, pizzaInProgressContainer, tx, RequestTimeout.DEFAULT, "mayPrepareDeliveryPizza: cannot access pizzaInProgressContainer"); + if (! nonDeliveryPizzasInProgress.isEmpty()) return false; + return true; } @@ -222,7 +223,7 @@ public class CookXVSM extends AbstractXVSMConnector { groupTemplate.setOrder(groupOrder); groupTemplate.setDeliveryStatus(null); final TransactionReference inPreparationTx = getDefaultTransaction(); - final DeliveryGroupData groupFromSpace = takeMatchingEntity(groupTemplate, pizzeriaDeliveryContainer, inPreparationTx, RequestTimeout.INFINITE, "Cannot take the delivery order from pizzeriaDeliveryContainer"); + final DeliveryGroupData groupFromSpace = takeMatchingEntity(groupTemplate, pizzeriaDeliveryContainer, inPreparationTx, RequestTimeout.DEFAULT, "Cannot take the delivery order from pizzeriaDeliveryContainer"); final List orderedPizzas = groupFromSpace.getOrder().getOrderedPizzas(); for (PizzaOrder orderedPizza : orderedPizzas) { if (orderedPizza.getId() == pizzaInProgress.getId()) { @@ -232,20 +233,20 @@ public class CookXVSM extends AbstractXVSMConnector { } sendItemsToContainer(Arrays.asList(groupFromSpace), pizzeriaDeliveryContainer, RequestTimeout.DEFAULT, inPreparationTx); sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, inPreparationTx); -// log.info("before inprogress comit!"); + log.debug("before inprogress comit!"); capi.commitTransaction(inPreparationTx); } private void notifyCustomerPizzaInProgress(PizzaOrder order, Pizza pizzaInProgress) { try { - 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()); final GroupData groupTemplate = new GroupData(); final Order groupOrder = new Order(); groupOrder.setId(order.getOrderId()); groupTemplate.setOrder(groupOrder); groupTemplate.setState(null); final TransactionReference inPreparationTx = getDefaultTransaction(); - final GroupData groupFromSpace = takeMatchingEntity(groupTemplate, pizzeriaGroupContainer, inPreparationTx, RequestTimeout.INFINITE, "Cannot take the order from pizzeriaGroupContainer"); + final GroupData groupFromSpace = takeMatchingEntity(groupTemplate, pizzeriaGroupContainer, inPreparationTx, 100, "Cannot take the order from pizzeriaGroupContainer"); final List orderedPizzas = groupFromSpace.getOrder().getOrderedPizzas(); for (PizzaOrder orderedPizza : orderedPizzas) { if (orderedPizza.getId() == pizzaInProgress.getId()) { @@ -253,12 +254,12 @@ public class CookXVSM extends AbstractXVSMConnector { orderedPizza.setCookId(pizzaInProgress.getCookId()); } } - sendItemsToContainer(Arrays.asList(groupFromSpace), pizzeriaGroupContainer, RequestTimeout.DEFAULT, inPreparationTx); - sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, inPreparationTx); + sendItemsToContainer(Arrays.asList(groupFromSpace), pizzeriaGroupContainer, RequestTimeout.ZERO, inPreparationTx); + sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.ZERO, inPreparationTx); capi.commitTransaction(inPreparationTx); } catch (Exception e) { -// log.error("This error has occurred: {}", e.getMessage()); -// e.printStackTrace(); + log.error("This error has occurred: {}", e.getMessage()); + e.printStackTrace(); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DeliveryGroupXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DeliveryGroupXVSM.java index 6ffedd7..4e400d4 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DeliveryGroupXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DeliveryGroupXVSM.java @@ -35,7 +35,7 @@ public class DeliveryGroupXVSM extends AbstractXVSMConnector { } public void waitForMyOrder() { - getDefaultBuilder().setCref(myContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder("").setCref(myContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { 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 49196e0..58aa409 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 @@ -39,7 +39,7 @@ public class DriverXVSM extends AbstractXVSMConnector { public void listenForPreparedDeliveryOrders() { - getDefaultBuilder().setCref(deliverDeliveryOrderContainer).setLookaround(true).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForPreparedDeliveryOrders").setCref(deliverDeliveryOrderContainer).setLookaround(true).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { @@ -63,14 +63,14 @@ public class DriverXVSM extends AbstractXVSMConnector { try { // Get lock for delivering this delivery order - takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.DEFAULT, error); - final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, MzsConstants.RequestTimeout.INFINITE, "Cannot get the delivery order!"); + takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.ZERO, error); + final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, 2000, "Cannot get the delivery order!"); group.setDriverId(driverId); group.setDeliveryStatus(DeliveryStatus.IN_PROGRESS); final List groups = Arrays.asList(group); -// sendItemsToContainer(groups, -// pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null); + sendItemsToContainer(groups, + pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null); if (!Util.runSimulation) { Thread.sleep(3000); @@ -90,12 +90,12 @@ public class DriverXVSM extends AbstractXVSMConnector { if (success) { group.setDeliveryStatus(DeliveryStatus.DELIVERED); if (!Util.runSimulation) { - sendItemsToContainer(Arrays.asList(new Bill(1000, group.getId())), cref, MzsConstants.RequestTimeout.DEFAULT, null); + sendItemsToContainer(Arrays.asList(new Bill(1000, group.getId())), cref, MzsConstants.RequestTimeout.ZERO, null); } } else { group.setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED); } - sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.DEFAULT, tx); + sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, tx); capi.commitTransaction(tx); @@ -108,7 +108,7 @@ public class DriverXVSM extends AbstractXVSMConnector { } public void listenForPreparedDeliveryPizzas() { - getDefaultBuilder().setCref(preparedDeliveryPizzasContainer).setLookaround(true).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForPreparedDeliveryPizzas").setCref(preparedDeliveryPizzasContainer).setLookaround(true).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) @@ -148,7 +148,6 @@ public class DriverXVSM extends AbstractXVSMConnector { tx); capi.commitTransaction(tx); - handleDeliveries(groupsWithCompleteOrder); } else { log.info("Not yet all pizzas prepared! Order with id " + orderId + " has " + numberOfPizzas diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/EntityNotFoundByTemplate.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/EntityNotFoundByTemplate.java index 3c3884c..7c371d7 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/EntityNotFoundByTemplate.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/EntityNotFoundByTemplate.java @@ -8,18 +8,4 @@ public class EntityNotFoundByTemplate extends RuntimeException { public EntityNotFoundByTemplate(String message) { super(message); } - - public EntityNotFoundByTemplate(Throwable cause) { - super(cause); - } - - public EntityNotFoundByTemplate(String message, Throwable cause) { - super(message, cause); - } - - public EntityNotFoundByTemplate(String message, Throwable cause, - boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } - } 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 17eddeb..48fe3ca 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 @@ -30,7 +30,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } public void listenForNewPizzerias() { - getDefaultBuilder().setCref(groupAgentInfoContainer).setLookaround(true).setSpaceAction(new SpaceAction() { + getDefaultBuilder("groupAgentInfoContainer").setCref(groupAgentInfoContainer).setLookaround(false).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { @@ -66,7 +66,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } private void listenToDeliveryUpdates(int port) { - getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.PIZZERIA_DELIVERY, port)).setSpaceAction(new SpaceAction() { + getDefaultBuilder("").setCref(useContainerOfSpaceWithPort(Util.PIZZERIA_DELIVERY, port)).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); @@ -92,7 +92,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } private void listenToGroupUpdates(int port) { - getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.PIZZERIA_GROUP, port)).setSpaceAction(new SpaceAction() { + getDefaultBuilder("").setCref(useContainerOfSpaceWithPort(Util.PIZZERIA_GROUP, port)).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); @@ -147,7 +147,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { startTime.set(new Date().getTime()); Thread.sleep(60000); } catch (InterruptedException e) { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + e.printStackTrace(); } log.info("ATTENTION: It's all over now!"); 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 62207dc..aa713b1 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 @@ -29,7 +29,7 @@ public class GroupXVSM extends AbstractXVSMConnector { } public void waitForMyOrder() { - getDefaultBuilder().setCref(orderDeliveredContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder("").setCref(orderDeliveredContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); @@ -71,6 +71,7 @@ public class GroupXVSM extends AbstractXVSMConnector { } catch (Exception e) { log.error("hab mich verschluckt!"); + e.printStackTrace(); } } 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 461db2d..4abcd3d 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 @@ -40,7 +40,7 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { } public void listenForTablesUpdates() { - getDefaultBuilder().setCref(pizzeriaTableContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForTablesUpdates").setCref(pizzeriaTableContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { @@ -63,7 +63,7 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { } public void listenForGroupUpdates() { - getDefaultBuilder().setCref(pizzeriaGroupContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForGroupUpdates").setCref(pizzeriaGroupContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); @@ -83,7 +83,7 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { } public void listenForDeliveryUpdates() { - getDefaultBuilder().setCref(pizzeriaDeliveryContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForDeliveryUpdates").setCref(pizzeriaDeliveryContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); 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 87c88d7..d1cd241 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 @@ -4,7 +4,6 @@ import at.ac.tuwien.sbc.valesriegler.common.OrderId; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.*; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; -import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceListener; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.MzsCoreException; import org.mozartspaces.core.TransactionReference; @@ -13,6 +12,7 @@ import org.slf4j.LoggerFactory; import java.io.Serializable; import java.util.Arrays; +import java.util.Collections; import java.util.List; public class WaiterXVSM extends AbstractXVSMConnector { @@ -46,12 +46,12 @@ public class WaiterXVSM extends AbstractXVSMConnector { } public void listenForPhoneOrders() { - getDefaultBuilder().setCref(phoneCallsContainer).setLookaround(true).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForPhoneOrders").setCref(phoneCallsContainer).setLookaround(true).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List phoneOrders = castEntries(entries); -// if(inNotification.get()) Collections.shuffle(phoneOrders); + if(inNotification.get()) Collections.shuffle(phoneOrders); for (DeliveryGroupData phoneOrder : phoneOrders) { final int id = phoneOrder.getId(); @@ -60,10 +60,10 @@ public class WaiterXVSM extends AbstractXVSMConnector { final TransactionReference tx = getDefaultTransaction(); try { - // Get the the delete lock so that only one waiter can answer this particular phone call + // Get the delete lock so that only one waiter can answer this particular phone call takeMatchingEntity(template, phoneCallsContainer, tx, RequestTimeout.DEFAULT, errorMsg); - final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, RequestTimeout.INFINITE, errorMsg); + final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, RequestTimeout.DEFAULT, errorMsg); group.setWaiterIdOfOrder(WaiterXVSM.this.waiterId); final Order order = group.getOrder(); group.setDeliveryStatus(DeliveryStatus.ORDERED); @@ -82,6 +82,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { log.info("Waiter has taken a phone delivery call!"); } catch (Exception e) { + e.printStackTrace(); } } @@ -90,7 +91,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { } public void listenForFreeTable() { - SpaceListener listener = getDefaultBuilder().setCref(freeTablesContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForFreeTable").setCref(freeTablesContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { @@ -129,7 +130,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { public void listenForNewGuests() { - SpaceListener listener = getDefaultBuilder().setLookaround(true).setCref(assignTableContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForNewGuests").setLookaround(true).setCref(assignTableContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) @@ -172,7 +173,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { } public void listenForPaymentRequest() { - SpaceListener paymentListener = getDefaultBuilder().setCref(paymentRequestContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForPayment").setLookaround(true).setCref(paymentRequestContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) @@ -218,7 +219,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { } public void listenForOrderRequests() { - getDefaultBuilder().setCref(takeOrderContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForOrderRequest").setLookaround(true).setCref(takeOrderContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) @@ -278,7 +279,7 @@ 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! */ - getDefaultBuilder().setLookaround(true).setCref(preparedPizzasContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder("listenForPreparedPizzas").setLookaround(true).setCref(preparedPizzasContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/spacehelpers/SpaceListener.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/spacehelpers/SpaceListener.java index 9e2686b..02eb6c6 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/spacehelpers/SpaceListener.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/spacehelpers/SpaceListener.java @@ -34,8 +34,9 @@ public abstract class SpaceListener implements NotificationListener { protected boolean lookAround = true; protected long timeout = 3000; protected SpaceAction spaceAction; + public String name; - @Override + @Override public void entryOperationFinished(Notification arg0, Operation arg1, List entries) { synchronized (AbstractXVSMConnector.lockObject) { @@ -75,10 +76,10 @@ public abstract class SpaceListener implements NotificationListener { List entries = getEntries(); if(entries.size() != 0) { + log.info("Start '{}' task", name); log.info("{} entries in timer", entries.size()); spaceAction.inNotification.set(false); spaceAction.onEntriesWritten(entries); - } else { } } catch (Exception e) { log.info(e.getMessage()); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/spacehelpers/SpaceListenerImpl.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/spacehelpers/SpaceListenerImpl.java index 519684c..f45ce4a 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/spacehelpers/SpaceListenerImpl.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/spacehelpers/SpaceListenerImpl.java @@ -24,7 +24,7 @@ public class SpaceListenerImpl extends SpaceListener { private final boolean countAll; - public SpaceListenerImpl(Capi capi, ContainerReference cref, int timeout, SpaceAction spaceAction, NotificationManager notificationManager, boolean lookaround, boolean countAll) { + public SpaceListenerImpl(Capi capi, ContainerReference cref, int timeout, SpaceAction spaceAction, NotificationManager notificationManager, boolean lookaround, boolean countAll, String name) { this.capi = capi; this.cref = cref; this.timeout = timeout; @@ -32,6 +32,7 @@ public class SpaceListenerImpl extends SpaceListener { this.notificationManager = notificationManager; this.lookAround = lookaround; this.countAll = countAll; + this.name = name; } @Override diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/spacehelpers/SpaceListenerImplBuilder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/spacehelpers/SpaceListenerImplBuilder.java index 78a1e77..2a54663 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/spacehelpers/SpaceListenerImplBuilder.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/spacehelpers/SpaceListenerImplBuilder.java @@ -14,6 +14,7 @@ public class SpaceListenerImplBuilder { private boolean lookaround = false; private NotificationManager notificationManager; private boolean countAll = false; + private String name; public SpaceListenerImplBuilder setCapi(Capi capi) { this.capi = capi; @@ -51,7 +52,7 @@ public class SpaceListenerImplBuilder { } public SpaceListenerImpl createSpaceListenerImpl() { - final SpaceListenerImpl spaceListener = new SpaceListenerImpl(capi, cref, timeout, spaceAction, notificationManager, lookaround, countAll); + final SpaceListenerImpl spaceListener = new SpaceListenerImpl(capi, cref, timeout, spaceAction, notificationManager, lookaround, countAll, name); spaceListener.startHandlingAbsenceOfNotifications(); try { notificationManager.createNotification(cref, spaceListener, Operation.WRITE); @@ -60,4 +61,9 @@ public class SpaceListenerImplBuilder { } return spaceListener; } + + public SpaceListenerImplBuilder setName(String name) { + this.name = name; + return this; + } } \ No newline at end of file -- 2.43.0