From a953597d2f4722d712a5f0f2c0f28442ffb59070 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Sun, 9 Jun 2013 17:11:53 +0200 Subject: [PATCH] [XVSM] Some simulation support. --- .../tuwien/sbc/valesriegler/common/Util.java | 16 +- .../sbc/valesriegler/group/GroupAgent.java | 275 ++++++++++-------- .../group/gui/GroupCreationHandler.java | 21 +- .../valesriegler/pizzeria/PizzeriaAgent.java | 12 +- .../pizzeria/gui/PizzeriaFrame.java | 8 +- .../gui/tablemodels/DeliveryOrdersModel.java | 4 +- .../gui/tablemodels/OrdersOverviewModel.java | 2 +- .../sbc/valesriegler/xvsm/CookXVSM.java | 22 +- .../valesriegler/xvsm/DeliveryGroupXVSM.java | 2 + .../sbc/valesriegler/xvsm/DriverXVSM.java | 4 +- .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 46 ++- .../sbc/valesriegler/xvsm/GroupXVSM.java | 2 + .../xvsm/spacehelpers/SpaceListener.java | 21 +- 13 files changed, 266 insertions(+), 169 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 b801774..4aa24c6 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,5 +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.Order; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.PizzaType; import org.slf4j.Logger; @@ -39,8 +41,8 @@ public abstract class Util { public static final String PIZZERIA_DELIVERY = "pizzeriaDeliveryContainer"; public static final String DELIVERY_PROGRESS = "deliveryInProgress"; public static final String DELIVERY_DONE = "deliveryDone"; - public static final String BILL_PAID = "billPaid"; + public static final boolean runSimulation = false; public static final String NUMBER_DISPLAY_FORMAT = String.format("%%0%dd", 3); @@ -154,4 +156,16 @@ public abstract class Util { e.printStackTrace(); System.exit(1); } + + public static Order createOrder(HasId group, GroupCreationDetailsRequest gc) { + List pizzaOrders = new ArrayList<>(); + for (PizzaType pt : gc.pizzaTypes) { + final PizzaOrder pizzaOrder = new PizzaOrder(pt); + pizzaOrders.add(pizzaOrder); + } + Order order = new Order(group, pizzaOrders); + order.setNumberOfPizzas(order.getOrderedPizzas().size()); + + return order; + } } 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 4f16733..9fd88a7 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 @@ -2,10 +2,13 @@ package at.ac.tuwien.sbc.valesriegler.group; 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; import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel; import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupData; +import at.ac.tuwien.sbc.valesriegler.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; @@ -17,132 +20,160 @@ import java.util.*; * The Main class of the Group component. *

* Start the communication and the group GUI: - * + * * @author Gregor Riegler * @author jan */ public class GroupAgent { - private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">"; - - private static final Logger log = LoggerFactory.getLogger(GroupAgent.class); - private GroupOverviewModel groupModel; - private AbstractGroupConnector groupconn; - private DeliveryOverviewModel deliveryOverviewModel; - private GroupAgentXVSM xvsm; - - /* - * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be - * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for - * selecting for which pizzeria the customer groups are created - * In case of XVSM the identifier string is a port number - */ - private Set pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet()); - - public static GroupAgent groupAgent; - - public static void main(String[] args) { - if (args.length != 1) { - // throw new IllegalArgumentException(USAGE); - } - - String mw = "XVSM";// args[0]; - log.info("Middleware: " + mw); - - if ("JMS".equalsIgnoreCase(mw)) { - Util.useJMS = true; - } else if ("XVSM".equalsIgnoreCase(mw)) { - Util.useJMS = false; - } else { - throw new IllegalArgumentException(USAGE); - } - - groupAgent = new GroupAgent(); - - SwingUtilities.invokeLater(new GroupGUI()); - } - - public GroupAgent() { - deliveryOverviewModel = new DeliveryOverviewModel(); - groupModel = new GroupOverviewModel(); - if (Util.useJMS) { - groupconn = new JMSGroupConnector(); - groupconn.init(); - } else { - xvsm = new GroupAgentXVSM(); - - xvsm.listenForNewPizzerias(); - } - } - - public static GroupAgent getInstance() { - return groupAgent; - } - - public AbstractGroupConnector getGroupcomm() { - return groupconn; - } - - public Set getPizzeriaIdentifiers() { - return pizzeriaIdentifiers; - } - - public GroupOverviewModel getGroupModel() { - return groupModel; - } - - public DeliveryOverviewModel getDeliveryModel() { - return deliveryOverviewModel; - } - - public void onGroupsCreated(List newGroups) { - if (!Util.useJMS) { - List groupData = new ArrayList<>(); - - for (Group group : newGroups) { - groupData.add(group.getGroupData()); - } - final String pizzeriaId = groupData.get(0).getPizzeriaId(); - - int pizzeriaSpacePort = 0; - try { - pizzeriaSpacePort = Integer.parseInt(pizzeriaId); - } catch (NumberFormatException e) { - log.error("Pizzeria Identifier should be an integer in the XVSM version!"); - } - xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort); - - log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort); - - // start the space group in a new thread - for (GroupData group : groupData) { - new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start(); - } - } - - } - - public void onDeliveryGroupsCreated(List newDeliveryGroups) { - if (!Util.useJMS) { - List groupData = new ArrayList<>(); - for (DeliveryGroup group : newDeliveryGroups) { - groupData.add(group.getDeliveryGroupData()); - } - final String pizzeriaId = groupData.get(0).getPizzeriaId(); - - int pizzeriaSpacePort = 0; - try { - pizzeriaSpacePort = Integer.parseInt(pizzeriaId); - } catch (NumberFormatException e) { - log.error("Pizzeria Identifier should be an integer in the XVSM version!"); - } - - xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort); - - log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort); - - for (DeliveryGroupData deliveryGroupData : groupData) { - new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort, deliveryGroupData.getAddress())).start(); + private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">"; + + private static final Logger log = LoggerFactory.getLogger(GroupAgent.class); + private GroupOverviewModel groupModel; + private AbstractGroupConnector groupconn; + private DeliveryOverviewModel deliveryOverviewModel; + private GroupAgentXVSM xvsm; + + /* + * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be + * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for + * selecting for which pizzeria the customer groups are created + * In case of XVSM the identifier string is a port number + */ + private Set pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet()); + + public static GroupAgent groupAgent; + + public static void main(String[] args) { + if (args.length != 1) { + // throw new IllegalArgumentException(USAGE); + } + + String mw = "JMS";// args[0]; + log.info("Middleware: " + mw); + + if ("JMS".equalsIgnoreCase(mw)) { + Util.useJMS = true; + } else if ("XVSM".equalsIgnoreCase(mw)) { + Util.useJMS = false; + } else { + throw new IllegalArgumentException(USAGE); + } + + groupAgent = new GroupAgent(); + + if (! Util.runSimulation) { + SwingUtilities.invokeLater(new GroupGUI()); + } + } + + public GroupAgent() { + deliveryOverviewModel = new DeliveryOverviewModel(); + groupModel = new GroupOverviewModel(); + if (Util.useJMS) { + groupconn = new JMSGroupConnector(); + groupconn.init(); + } else { + + xvsm = new GroupAgentXVSM(); + + xvsm.listenForNewPizzerias(); + + if (Util.runSimulation) { + xvsm.listenToPizzeria("9875"); + xvsm.listenToPizzeria("9874"); + xvsm.runSimulation(); + } + } + } + + public List createGroups(List pizzaTypes1, String pizzeria, int number) { + List newDeliveryGroups = new ArrayList<>(); + final String fakeAddress = "address"; + final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress, pizzeria); + + for (int i = 0; i < gc.numberOfGroups; i++) { + DeliveryGroup group = new DeliveryGroup(); + final Order order = Util.createOrder(group, gc); + group.getDeliveryGroupData().setOrder(order); + group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria); + group.getDeliveryGroupData().setAddress(gc.address); + newDeliveryGroups.add(group); + } + + return newDeliveryGroups; + } + + public static GroupAgent getInstance() { + return groupAgent; + } + + public AbstractGroupConnector getGroupcomm() { + return groupconn; + } + + public Set getPizzeriaIdentifiers() { + return pizzeriaIdentifiers; + } + + public GroupOverviewModel getGroupModel() { + return groupModel; + } + + public DeliveryOverviewModel getDeliveryModel() { + return deliveryOverviewModel; + } + + public void onGroupsCreated(List newGroups) { + if (!Util.useJMS) { + List groupData = new ArrayList<>(); + + for (Group group : newGroups) { + groupData.add(group.getGroupData()); + } + final String pizzeriaId = groupData.get(0).getPizzeriaId(); + + int pizzeriaSpacePort = 0; + try { + pizzeriaSpacePort = Integer.parseInt(pizzeriaId); + } catch (NumberFormatException e) { + log.error("Pizzeria Identifier should be an integer in the XVSM version!"); + } + xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort); + + log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort); + + // start the space group in a new thread + for (GroupData group : groupData) { + new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start(); + } + } + + } + + public void onDeliveryGroupsCreated(List newDeliveryGroups) { + if (!Util.useJMS) { + List groupData = new ArrayList<>(); + for (DeliveryGroup group : newDeliveryGroups) { + groupData.add(group.getDeliveryGroupData()); + } + final String pizzeriaId = groupData.get(0).getPizzeriaId(); + + int pizzeriaSpacePort = 0; + try { + pizzeriaSpacePort = Integer.parseInt(pizzeriaId); + } catch (NumberFormatException e) { + log.error("Pizzeria Identifier should be an integer in the XVSM version!"); + } + + xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort); + + log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort); + + if (!Util.runSimulation) { + for (DeliveryGroupData deliveryGroupData : groupData) { + new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort, deliveryGroupData.getAddress())).start(); + } } } - } + } } 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 4267186..798771c 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 @@ -1,12 +1,10 @@ package at.ac.tuwien.sbc.valesriegler.group.gui; -import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.DeliveryGroup; import at.ac.tuwien.sbc.valesriegler.group.Group; import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; import at.ac.tuwien.sbc.valesriegler.types.Order; -import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; -import at.ac.tuwien.sbc.valesriegler.types.PizzaType; import org.slf4j.LoggerFactory; import javax.swing.*; @@ -52,14 +50,14 @@ class GroupCreationHandler { for (int i = 0; i < gc.numberOfGroups; i++) { if(createDeliveryGroups) { DeliveryGroup group = new DeliveryGroup(); - final Order order = createOrder(group, gc); + final Order order = Util.createOrder(group, gc); 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 = createOrder(group, gc); + final Order order = Util.createOrder(group, gc); group.getGroupData().setOrder(order); group.getGroupData().setSize(gc.size); group.getGroupData().setPizzeriaId(gc.pizzeria); @@ -77,19 +75,6 @@ class GroupCreationHandler { } } - private Order createOrder(HasId group, GroupCreationDetailsRequest gc) { - List pizzaOrders = new ArrayList<>(); - for (PizzaType pt : gc.pizzaTypes) { - final PizzaOrder pizzaOrder = new PizzaOrder(pt); -// pizzaOrder.setDeliveryPizza(createDeliveryGroups); - pizzaOrders.add(pizzaOrder); - } - Order order = new Order(group, pizzaOrders); - order.setNumberOfPizzas(order.getOrderedPizzas().size()); - - return order; - } - public void handleCancel() { chooseGroupSizePanel.setVisible(true); creationPanel.remove(groupCreationPanel); 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 0569bf9..1d4347f 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 @@ -50,15 +50,19 @@ public class PizzeriaAgent { case "XVSM" : pizzeriaAgent.createModels(); pizzeriaAgent.initXVSM(args[1]); - pizzeriaAgent.initGUI(); - Util.useJMS = false; + if (! Util.runSimulation) { + pizzeriaAgent.initGUI(); + } + Util.useJMS = false; break; case "JMS" : pizzeriaAgent.createModels(); pizzeriaAgent.jmspc = new JMSPizzeriaConnector(args[1]); pizzeriaAgent.jmspc.init(); - pizzeriaAgent.initGUI(); - Util.useJMS = true; + if (! Util.runSimulation) { + pizzeriaAgent.initGUI(); + } + Util.useJMS = true; break; default : throw new IllegalArgumentException(USAGE); 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 0bf2460..54bbf23 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 @@ -126,7 +126,7 @@ public class PizzeriaFrame extends JFrame { int minIndex = lsm.getMinSelectionIndex(); if (minIndex < 0) return; - DeliveryGroupData currentGroup = deliveryOrdersModel.getGroupOfRow(minIndex + 1); + DeliveryGroupData currentGroup = deliveryOrdersModel.getGroupOfRow(minIndex); final Order order = currentGroup.getOrder(); detailsPanel.setBorder(new TitledBorder(String.format(WAITER_IDS_CAPTION, order.getId()))); pizzasPanel.setBorder(new TitledBorder(String.format(PIZZAS_CAPTION, order.getId()))); @@ -147,7 +147,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(groupId); + final DeliveryGroupData groupOfRow = deliveryOrdersModel.getGroupOfRow(currentOrder.getId()-1); pizzasOfDeliveryModel.setCurrentOrder(groupOfRow.getOrder()); deliveryDetailsModel.setCurrentDelivery(groupOfRow); @@ -187,7 +187,7 @@ public class PizzeriaFrame extends JFrame { int minIndex = lsm.getMinSelectionIndex(); if (minIndex < 0) return; - GroupData currentGroup = PizzeriaAgent.getInstance().getOrdersModel().getGroupOfRow(minIndex + 1); + GroupData currentGroup = PizzeriaAgent.getInstance().getOrdersModel().getGroupOfRow(minIndex); waitersPanel.setBorder(new TitledBorder(String.format(WAITER_IDS_CAPTION, currentGroup.getOrder().getId()))); pizzasPanel.setBorder(new TitledBorder(String.format(PIZZAS_CAPTION, currentGroup.getOrder().getId()))); PizzeriaAgent.getInstance().getWaitersModel().setCurrentGroup(currentGroup); @@ -210,7 +210,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(groupId); + final GroupData groupOfRow = ordersModel.getGroupOfRow(currentOrder.getId()-1); pizzasOfOrderModel.setCurrentOrder(groupOfRow.getOrder()); waitersModel.setCurrentGroup(groupOfRow); 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 ff9dce6..c6bed84 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 @@ -18,7 +18,7 @@ public class DeliveryOrdersModel extends AbstractOrdersModel private static final String LOAD_BALANCER_ID = "Moved by"; private static final String ORIGINAL_PIZZERIA = "Moved from"; - private static final String[] COLUMNS = new String[] { ID, ORDER_ID, DELIVERY_ADDRESS, STATUS, LOAD_BALANCER_ID, ORIGINAL_PIZZERIA }; + private static final String[] COLUMNS = new String[] { ORDER_ID, ID, DELIVERY_ADDRESS, STATUS, LOAD_BALANCER_ID, ORIGINAL_PIZZERIA }; @Override protected String[] getColumns() { @@ -50,6 +50,6 @@ public class DeliveryOrdersModel extends AbstractOrdersModel } public DeliveryGroupData getGroupOfRow(int rowIndex) { - return items.get(rowIndex); + return new ArrayList<>(items.values()).get(rowIndex); } } 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 bbb4863..289ea43 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 @@ -40,7 +40,7 @@ public class OrdersOverviewModel extends AbstractOrdersModel { } public GroupData getGroupOfRow(int rowIndex) { - return items.get(rowIndex); + return new ArrayList<>(items.values()).get(rowIndex); } @Override 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 cfbfe6a..9a70174 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 @@ -76,7 +76,7 @@ public class CookXVSM extends AbstractXVSMConnector { } catch (NullPointerException e) { // the strange nullpointer exception from the space } catch (EntityNotFoundByTemplate e) { - + log.info("entitynotfound: {}", e.getMessage()); } catch (Exception e) { log.info("outer cook"); log.info(e.getMessage()); @@ -98,7 +98,7 @@ public class CookXVSM extends AbstractXVSMConnector { List pizzas = castEntries(entries); - if (inNotification.get()) Collections.shuffle(pizzas); + if (inNotification.get() || Util.runSimulation) Collections.shuffle(pizzas); for (PizzaOrder pizzaOrder : pizzas) { @@ -118,19 +118,30 @@ public class CookXVSM extends AbstractXVSMConnector { Pizza pizzaInProgress = Pizza.createPizzaFromPizzaOrder(order, cookId, true); pizzaInProgress.setStatus(PizzaOrderStatus.IN_PREPARATION); - notifyDeliveryPizzaInProgress(order, pizzaInProgress); + if (! Util.runSimulation) { + notifyDeliveryPizzaInProgress(order, pizzaInProgress); + } + +// log.info("before creating!"); PizzaOrder pizza = createPizza(order); +// log.info("after creating!"); + sendItemsToContainer(Arrays.asList(pizza), preparedDeliveryPizzasContainer, RequestTimeout.DEFAULT, tx); +// log.info("after prepareddeliverypizzascon"); + notifyDeliveryPizzaDone(pizza, tx); +// log.info("after notifydone"); + capi.commitTransaction(tx); log.info("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()); } catch (Exception e) { log.info("outer cook"); @@ -222,6 +233,7 @@ 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!"); capi.commitTransaction(inPreparationTx); } @@ -253,7 +265,9 @@ public class CookXVSM extends AbstractXVSMConnector { private PizzaOrder createPizza(PizzaOrder order) throws InterruptedException { long duration = order.getPizzaType().duration; - Thread.sleep(duration * 1000); + if (! Util.runSimulation) { + Thread.sleep(duration * 1000); + } PizzaOrder pizza = Pizza.createPizzaFromPizzaOrder(order, cookId, false); pizza.setStatus(PizzaOrderStatus.DONE); 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 5af3541..6ffedd7 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 @@ -53,6 +53,8 @@ public class DeliveryGroupXVSM extends AbstractXVSMConnector { group.setDeliveryStatus(DeliveryStatus.PAID); sendItemsToContainer(Arrays.asList(group), pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.DEFAULT, tx); capi.commitTransaction(tx); + + notificationMgr.shutdown(); } } } 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 c51927a..6d8a8c0 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 @@ -66,7 +66,9 @@ public class DriverXVSM extends AbstractXVSMConnector { sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null); - Thread.sleep(3000); + if (! Util.runSimulation) { + Thread.sleep(3000); + } final String address = String.format(Util.SERVER_ADDR, Util.DELIVERY_CUSTOMERS_PORT); ContainerReference cref = null; 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 066ae33..de1fc00 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 @@ -1,9 +1,12 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.group.DeliveryGroup; import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; +import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupData; +import at.ac.tuwien.sbc.valesriegler.types.PizzaType; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.slf4j.Logger; @@ -12,6 +15,7 @@ import org.slf4j.LoggerFactory; import javax.swing.*; import java.io.Serializable; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Set; @@ -55,7 +59,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } - private void listenToPizzeria(String pizzeriaId) { + public void listenToPizzeria(String pizzeriaId) { final int port = Integer.parseInt(pizzeriaId); listenToGroupUpdates(port); listenToDeliveryUpdates(port); @@ -104,4 +108,44 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PIZZERIA_DELIVERY, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PHONE_CALLS, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); } + + public void runSimulation() { + new Thread(new Runnable() { + @Override + public void run() { + List newDeliveryGroups = new ArrayList<>(); + List pizzaTypes1 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA, PizzaType.MARGHERITA); + List pizzaTypes2 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA); + List pizzaTypes3 = Arrays.asList(PizzaType.SALAMI, PizzaType.MARGHERITA); + + final String pizzeria1 = "9875"; + final String pizzeria2 = "9874"; + List groups1 = GroupAgent.getInstance().createGroups(pizzaTypes1, pizzeria1, 4); + List groups2 = GroupAgent.getInstance().createGroups(pizzaTypes2, pizzeria2, 3); + List groups3 = GroupAgent.getInstance().createGroups(pizzaTypes3, pizzeria2, 2); + + final DeliveryOverviewModel deliveryModel = GroupAgent.getInstance().getDeliveryModel(); + deliveryModel.addItems(groups1); + deliveryModel.addItems(groups2); + deliveryModel.addItems(groups3); + + GroupAgent.getInstance().onDeliveryGroupsCreated(groups2); + GroupAgent.getInstance().onDeliveryGroupsCreated(groups3); + GroupAgent.getInstance().onDeliveryGroupsCreated(groups1); + + log.info("ATTENTION: Now let's wait 60 seconds!"); + + try { + Thread.sleep(60000); + } catch (InterruptedException e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + + log.info("ATTENTION: It's all over now!"); + notificationMgr.shutdown(); + +// System.exit(1); + } + }).start(); + } } 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 4a1b771..62207dc 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 @@ -67,6 +67,8 @@ public class GroupXVSM extends AbstractXVSMConnector { capi.commitTransaction(tx); log.info("I sent my payment request to the space! GroupId: {}", groupId); + notificationMgr.shutdown(); + } catch (Exception e) { log.error("hab mich verschluckt!"); } 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 f85e000..9e2686b 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 @@ -1,11 +1,6 @@ package at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers; -import java.io.Serializable; -import java.util.Date; -import java.util.List; -import java.util.Timer; -import java.util.TimerTask; - +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.xvsm.AbstractXVSMConnector; import org.mozartspaces.notifications.Notification; import org.mozartspaces.notifications.NotificationListener; @@ -13,6 +8,12 @@ import org.mozartspaces.notifications.Operation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.Serializable; +import java.util.Date; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; + /** * * The SpaceListener provides the {@link NotificationListener} interface. @@ -40,7 +41,7 @@ public abstract class SpaceListener implements NotificationListener { synchronized (AbstractXVSMConnector.lockObject) { AbstractXVSMConnector.timeOflastOperation.set(new Date().getTime()); try { - log.info("I am running a notification now!"); +// log.info("I am running a notification now!"); spaceAction.inNotification.set(true); spaceAction.onEntriesWritten(entries); } catch (Exception e) { @@ -53,7 +54,7 @@ public abstract class SpaceListener implements NotificationListener { abstract List getEntries() throws Exception; public void startHandlingAbsenceOfNotifications() { - if(!lookAround) return; + if(!lookAround || Util.runSimulation) return; AbstractXVSMConnector.timeOflastOperation.set(new Date().getTime() + 3500); Timer timer = new Timer(); @@ -71,15 +72,13 @@ public abstract class SpaceListener implements NotificationListener { synchronized(AbstractXVSMConnector.lockObject) { try { AbstractXVSMConnector.timeOflastOperation.set(new Date().getTime()); - log.info("Timer task running"); - + List entries = getEntries(); if(entries.size() != 0) { log.info("{} entries in timer", entries.size()); spaceAction.inNotification.set(false); spaceAction.onEntriesWritten(entries); } else { - log.info("No entries in timer!"); } } catch (Exception e) { log.info(e.getMessage()); -- 2.43.0