From 240ea88e8d8fcbb74818bb9b679c7ac417573669 Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 10 Jun 2013 00:52:55 +0200 Subject: [PATCH 01/16] [JMS] merged GroupAgent and added Util.runSimulation check. --- .../sbc/valesriegler/group/GroupAgent.java | 329 +++++++++--------- .../group/jms/GroupJMSNACMsgListener.java | 5 +- 2 files changed, 175 insertions(+), 159 deletions(-) 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 9fd88a7..a6998d4 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,179 +1,192 @@ package at.ac.tuwien.sbc.valesriegler.group; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +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; import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel; -import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; +import at.ac.tuwien.sbc.valesriegler.group.jms.GroupJMSNACMsgListener; +import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC; 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; - -import javax.swing.*; -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 = "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(); - } - } - } - } + 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 DeliveryOverviewModel deliveryOverviewModel; + private GroupAgentXVSM xvsm; + static private JMSNAC jmsnac; + static private GroupGUI groupGui; + /** + * 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) { + groupGui = new GroupGUI(); + SwingUtilities.invokeLater(groupGui); + } + } + + public GroupAgent() { + deliveryOverviewModel = new DeliveryOverviewModel(); + groupModel = new GroupOverviewModel(); + if (Util.useJMS) { + jmsnac = new JMSNAC(new GroupJMSNACMsgListener()); + } 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 Set getPizzeriaIdentifiers() { + return pizzeriaIdentifiers; + } + + public GroupOverviewModel getGroupModel() { + return groupModel; + } + + public static GroupGUI getGroupGui() { + return groupGui; + } + + public static JMSNAC getJmsnac() { + return jmsnac; + } + + 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/jms/GroupJMSNACMsgListener.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java index ae63458..52fcb22 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java @@ -7,6 +7,7 @@ import javax.jms.ObjectMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; import at.ac.tuwien.sbc.valesriegler.jms.nac.AbstractJMSNACMsgListener; import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.AddressInfoRequest; @@ -36,7 +37,9 @@ public class GroupJMSNACMsgListener extends AbstractJMSNACMsgListener implements if (obj instanceof AddressInfoResponse) { AddressInfoResponse response = (AddressInfoResponse) obj; GroupAgent.getInstance().getPizzeriaIdentifiers().add(response.getAddress()); - GroupAgent.getGroupGui().getFrame().repaint(); + if (!Util.runSimulation) { + GroupAgent.getGroupGui().getFrame().repaint(); + } JMSGroupConnector.getConnectors().put(response.getAddress(), new JMSGroupConnector(response.getAddress())); } else { log.warn("Received unknown Object: " + obj); -- 2.43.0 From 6f89c96e88a8f437edc361b7c303a37f2683322e Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 10 Jun 2013 01:04:13 +0200 Subject: [PATCH 02/16] [JMS] debugging random behaviour --- .../sbc/valesriegler/group/GroupAgent.java | 8 ++-- .../sbc/valesriegler/group/GroupGUI.java | 5 +++ .../valesriegler/group/gui/GroupFrame.java | 37 ++++++++++--------- .../group/jms/GroupJMSNACMsgListener.java | 10 ++++- .../jms/nac/AbstractJMSNACMsgListener.java | 8 +++- 5 files changed, 43 insertions(+), 25 deletions(-) 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 a6998d4..ae4b9ca 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 @@ -52,10 +52,10 @@ public class GroupAgent { public static void main(String[] args) { if (args.length != 1) { - // throw new IllegalArgumentException(USAGE); + throw new IllegalArgumentException(USAGE); } - String mw = "JMS";// args[0]; + String mw = args[0]; log.info("Middleware: " + mw); if ("JMS".equalsIgnoreCase(mw)) { @@ -67,7 +67,6 @@ public class GroupAgent { } groupAgent = new GroupAgent(); - if (!Util.runSimulation) { groupGui = new GroupGUI(); SwingUtilities.invokeLater(groupGui); @@ -78,7 +77,8 @@ public class GroupAgent { deliveryOverviewModel = new DeliveryOverviewModel(); groupModel = new GroupOverviewModel(); if (Util.useJMS) { - jmsnac = new JMSNAC(new GroupJMSNACMsgListener()); + GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener(); + jmsnac = new JMSNAC(tmp); } else { xvsm = new GroupAgentXVSM(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupGUI.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupGUI.java index 4ee4ec3..dae7a14 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupGUI.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupGUI.java @@ -1,11 +1,16 @@ package at.ac.tuwien.sbc.valesriegler.group; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import at.ac.tuwien.sbc.valesriegler.group.gui.GroupFrame; public class GroupGUI implements Runnable { + private static final Logger log = LoggerFactory.getLogger(GroupAgent.class); private GroupFrame frame; @Override public void run() { + log.debug("Starting GroupGUI."); frame = new GroupFrame(GroupAgent.getInstance().getGroupModel(), GroupAgent.getInstance().getDeliveryModel()); frame.pack(); frame.setVisible(true); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java index 34d5b0f..a32f12f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java @@ -5,10 +5,11 @@ import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JPanel; -import at.ac.tuwien.sbc.valesriegler.common.Util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.common.Util; + /** * Base Frame of the Group UI * @@ -18,40 +19,40 @@ import org.slf4j.LoggerFactory; @SuppressWarnings("serial") public class GroupFrame extends JFrame { private static final Logger log = LoggerFactory.getLogger(GroupFrame.class); - + public GroupFrame(GroupOverviewModel groupModel, DeliveryOverviewModel deliveryModel) { super("Groups"); + log.debug("GroupFrame()"); JPanel wrapper = new JPanel(); GridLayout wrapperLayout = new GridLayout(2, 2); wrapper.setLayout(wrapperLayout); // Create Wizard-like panel for the creation of groups - wrapper.add(new GroupCreationPanel(false)); + wrapper.add(new GroupCreationPanel(false)); - // Create Wizard-like panel for the creation of delivery groups - wrapper.add(new GroupCreationPanel(true)); + // Create Wizard-like panel for the creation of delivery groups + wrapper.add(new GroupCreationPanel(true)); // Create the panel for the group overview table initGroupOverview(wrapper, groupModel); - // Create the panel for the deliveries overview table - initDeliveriesOverview(wrapper, deliveryModel); + // Create the panel for the deliveries overview table + initDeliveriesOverview(wrapper, deliveryModel); - setContentPane(wrapper); + setContentPane(wrapper); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } - - private void initGroupOverview(JPanel wrapper, GroupOverviewModel groupModel) { - JPanel tablePanel = new JPanel(); - Util.createTableInTitledPanel(tablePanel, groupModel, "Group Overview"); - wrapper.add(tablePanel); + private void initGroupOverview(JPanel wrapper, GroupOverviewModel groupModel) { + JPanel tablePanel = new JPanel(); + Util.createTableInTitledPanel(tablePanel, groupModel, "Group Overview"); + wrapper.add(tablePanel); } - private void initDeliveriesOverview(JPanel wrapper, DeliveryOverviewModel deliveryModel) { - JPanel tablePanel = new JPanel(); - Util.createTableInTitledPanel(tablePanel, deliveryModel, "Deliveries Overview"); - wrapper.add(tablePanel); - } + private void initDeliveriesOverview(JPanel wrapper, DeliveryOverviewModel deliveryModel) { + JPanel tablePanel = new JPanel(); + Util.createTableInTitledPanel(tablePanel, deliveryModel, "Deliveries Overview"); + wrapper.add(tablePanel); + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java index 52fcb22..af51779 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java @@ -24,7 +24,7 @@ public class GroupJMSNACMsgListener extends AbstractJMSNACMsgListener implements public GroupJMSNACMsgListener() { Thread th = new Thread(this); th.setDaemon(true); - th.run(); + th.start(); } @Override @@ -56,7 +56,15 @@ public class GroupJMSNACMsgListener extends AbstractJMSNACMsgListener implements while (true) { try { getJmsnac().sendNACMsg(new AddressInfoRequest()); + } catch (Exception e) { + log.warn("EXCEPTION!", e); + } + try { Thread.sleep(10000); + } catch (InterruptedException e) { + log.warn("EXCEPTION!", e); + } + try { GroupAgent.getInstance().getPizzeriaIdentifiers().clear(); } catch (Exception e) { log.warn("EXCEPTION!", e); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/AbstractJMSNACMsgListener.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/AbstractJMSNACMsgListener.java index 4a90191..3a3ba3e 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/AbstractJMSNACMsgListener.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/AbstractJMSNACMsgListener.java @@ -2,6 +2,9 @@ package at.ac.tuwien.sbc.valesriegler.jms.nac; import javax.jms.MessageListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Abstract "Naming and Control"-msgListener. * @@ -9,12 +12,13 @@ import javax.jms.MessageListener; * */ public abstract class AbstractJMSNACMsgListener implements MessageListener { - // private static final Logger log = LoggerFactory.getLogger(AbstractJMSNACMsgListener.class); + private static final Logger log = LoggerFactory.getLogger(AbstractJMSNACMsgListener.class); private JMSNAC jmsnac = null; public AbstractJMSNACMsgListener() { } - protected void setJmsnac(JMSNAC jmsnac) { + public void setJmsnac(JMSNAC jmsnac) { + log.info("setJmsnac():" + jmsnac); this.jmsnac = jmsnac; } public JMSNAC getJmsnac() { -- 2.43.0 From aeaa4f885d6b73e3f05be6cbaf9fc64532d84324 Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 10 Jun 2013 02:01:41 +0200 Subject: [PATCH 03/16] [JMS] OLD workflow now works with dynamic pizzerias. --- .../tuwien/sbc/valesriegler/group/Group.java | 1 + .../group/jms/GroupJMSNACMsgListener.java | 17 ++++-------- .../jms/PizzeriaJMSNACMsgListener.java | 2 ++ .../tuwien/sbc/valesriegler/types/Order.java | 26 +++++++++++-------- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java index 4ab8fdf..13b2b83 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java @@ -49,6 +49,7 @@ public class Group implements Runnable, HasId { JMSGroupConnector.getConnector(groupData.getPizzeriaId()).send(new TableRequest(groupData)); break; case SITTING : + groupData.getOrder().genId(); JMSGroupConnector.getConnector(groupData.getPizzeriaId()).send(new OrderRequest(groupData)); order(); break; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java index af51779..5485fbe 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java @@ -33,8 +33,9 @@ public class GroupJMSNACMsgListener extends AbstractJMSNACMsgListener implements if (msg instanceof ObjectMessage) { ObjectMessage objMsg = (ObjectMessage) msg; Object obj = objMsg.getObject(); - - if (obj instanceof AddressInfoResponse) { + if (obj instanceof AddressInfoRequest) { + // DO NOTHING. + } else if (obj instanceof AddressInfoResponse) { AddressInfoResponse response = (AddressInfoResponse) obj; GroupAgent.getInstance().getPizzeriaIdentifiers().add(response.getAddress()); if (!Util.runSimulation) { @@ -55,17 +56,9 @@ public class GroupJMSNACMsgListener extends AbstractJMSNACMsgListener implements public void run() { while (true) { try { - getJmsnac().sendNACMsg(new AddressInfoRequest()); - } catch (Exception e) { - log.warn("EXCEPTION!", e); - } - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - log.warn("EXCEPTION!", e); - } - try { + Thread.sleep(15000); GroupAgent.getInstance().getPizzeriaIdentifiers().clear(); + getJmsnac().sendNACMsg(new AddressInfoRequest()); } catch (Exception e) { log.warn("EXCEPTION!", e); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/PizzeriaJMSNACMsgListener.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/PizzeriaJMSNACMsgListener.java index 3c2deb0..80271c1 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/PizzeriaJMSNACMsgListener.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/PizzeriaJMSNACMsgListener.java @@ -30,6 +30,8 @@ public class PizzeriaJMSNACMsgListener extends AbstractJMSNACMsgListener { if (obj instanceof AddressInfoRequest) { AddressInfoResponse air = new AddressInfoResponse(PizzeriaAgent.getInstance().getJmspc().getCONNECTSTRING()); PizzeriaAgent.getInstance().getJmsnac().sendNACMsg(air); + } else if (obj instanceof AddressInfoResponse) { + // DO NOTHING. } else { log.warn("Received unknown Object: " + obj); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java index 215e666..687d84b 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java @@ -1,13 +1,14 @@ package at.ac.tuwien.sbc.valesriegler.types; -import at.ac.tuwien.sbc.valesriegler.common.HasId; -import at.ac.tuwien.sbc.valesriegler.common.Util; -import org.mozartspaces.capi3.Queryable; - import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import org.mozartspaces.capi3.Queryable; + +import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.common.Util; + /** * Object denotes a Group's interest in pizzas and the state of the pizzas * themselves. @@ -32,8 +33,8 @@ public class Order implements Serializable, HasId { } public Order(int groupId, List orderedPizzas) { - // TODO don't set the id here but let the waiter set it - id = ++idNext; + // TODO don't set the id here but let the waiter set it + // genId(); this.groupId = groupId; status = OrderStatus.NEW; this.orderedPizzas = orderedPizzas; @@ -41,17 +42,17 @@ public class Order implements Serializable, HasId { } public Order(HasId group, List orderedPizzas) { - // TODO don't set the id here but let the waiter set it - id = ++idNext; + // TODO don't set the id here but let the waiter set it + // genId(); groupId = group.getId(); status = OrderStatus.NEW; this.orderedPizzas = orderedPizzas; cookedPizzas = new ArrayList(); } - public void addCookedPizza(Pizza pizza) { - cookedPizzas.add(pizza); - } + public void addCookedPizza(Pizza pizza) { + cookedPizzas.add(pizza); + } public List getCookedPizzas() { return cookedPizzas; @@ -72,6 +73,9 @@ public class Order implements Serializable, HasId { public void setId(Integer id) { this.id = id; } + public void genId() { + id = ++idNext; + } @Override public int getId() { -- 2.43.0 From 5f52e3235f587d003c54151b1e3a3e18ff9688e7 Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 10 Jun 2013 02:39:10 +0200 Subject: [PATCH 04/16] [JMS] NEW workflow outlined. implementation tomorrow. --- .../common/AbstractDeliveryAction.java | 27 +++++++ .../cook/actions/DeliveryOrderInfo.java | 31 ++++++++ .../DeliveryOrdersToCook.java | 64 +++++++++++++++ .../driver/actions/OrderDeliveredInfo.java | 24 ++++++ .../messageListeners/PendingDeliveries.java | 64 +++++++++++++++ .../sbc/valesriegler/group/DeliveryGroup.java | 78 +++++++++++++------ .../group/actions/DeliveryOrderRequest.java | 24 ++++++ .../group/jms/JMSGroupConnector.java | 8 +- .../jms/messageListeners/WantADelivery.java | 38 ++------- 9 files changed, 299 insertions(+), 59 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/common/AbstractDeliveryAction.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/PendingDeliveries.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderRequest.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/AbstractDeliveryAction.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/AbstractDeliveryAction.java new file mode 100644 index 0000000..0764975 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/AbstractDeliveryAction.java @@ -0,0 +1,27 @@ +package at.ac.tuwien.sbc.valesriegler.common; + +import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; + +/** + * Abstract delivery action that can be performed in the DS. In almost all cases the + * DeliveryGroupData is relevant, it may be null in the few corner cases. + * + * @author jan + * + */ +public abstract class AbstractDeliveryAction extends AbstractAction { + private final DeliveryGroupData groupData; + + public AbstractDeliveryAction(DeliveryGroupData groupData) { + super(null); + this.groupData = groupData; + } + + public DeliveryGroupData getGroupData() { + return groupData; + } + + public DeliveryGroupData getDeliveryGroupData() { + return groupData; + } +} \ No newline at end of file diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java new file mode 100644 index 0000000..71e75e6 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java @@ -0,0 +1,31 @@ +package at.ac.tuwien.sbc.valesriegler.cook.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; +import at.ac.tuwien.sbc.valesriegler.types.GroupData; + +/** + * response to the group's interest in pizza. + * + * @author jan + * + */ +public class DeliveryOrderInfo extends AbstractAction implements Serializable { + private final int cookId; + + public DeliveryOrderInfo(GroupData groupdata, int cookId) { + super(groupdata); + this.cookId = cookId; + } + + public int getCookId() { + return cookId; + } + + @Override + public String toString() { + return "OrderInfo [cookId=" + cookId + "]"; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java new file mode 100644 index 0000000..049add1 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java @@ -0,0 +1,64 @@ +package at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.ObjectMessage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.cook.jms.JMSCook; +import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; + +/** + * Cook the requested pizza. + * + * @author jan + * + */ +public class DeliveryOrdersToCook implements MessageListener { + private static final Logger log = LoggerFactory.getLogger(DeliveryOrdersToCook.class); + private final JMSCook cook; + + public DeliveryOrdersToCook(JMSCook cook) { + this.cook = cook; + } + + @Override + public void onMessage(Message msg) { + try { + synchronized (cook) { + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof DeliveryOrderRequest) { + DeliveryOrderRequest dor = (DeliveryOrderRequest) obj; + log.debug("Received: " + dor); + + // generate random delay + Thread.sleep((long) (Math.random() * 10000)); + + // TODO: + + } else { + log.warn("Received unknown Object: " + obj); + } + } else { + log.warn("Received unknown Message: " + msg); + } + msg.acknowledge(); + } + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } catch (InterruptedException e) { + log.error("EXCEPTION!", e); + } + } + + @Override + public String toString() { + return "CookRequestedPizza [cook=" + cook + "]"; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java new file mode 100644 index 0000000..539ec5e --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java @@ -0,0 +1,24 @@ +package at.ac.tuwien.sbc.valesriegler.driver.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; +import at.ac.tuwien.sbc.valesriegler.types.GroupData; + +/** + * response to the group's interest in pizza. + * + * @author jan + * + */ +public class OrderDeliveredInfo extends AbstractAction implements Serializable { + private final int cookId; + + public OrderDeliveredInfo(GroupData groupdata, int cookId) { + super(groupdata); + this.cookId = cookId; + } + + // TODO + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/PendingDeliveries.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/PendingDeliveries.java new file mode 100644 index 0000000..75f0308 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/PendingDeliveries.java @@ -0,0 +1,64 @@ +package at.ac.tuwien.sbc.valesriegler.driver.jms.messageListeners; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.ObjectMessage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.driver.jms.JMSDriver; +import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; + +/** + * + * + * @author jan + * + */ +public class PendingDeliveries implements MessageListener { + private static final Logger log = LoggerFactory.getLogger(PendingDeliveries.class); + private final JMSDriver driver; + + public PendingDeliveries(JMSDriver driver) { + this.driver = driver; + } + + @Override + public void onMessage(Message msg) { + try { + synchronized (driver) { + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof DeliveryOrderRequest) { + DeliveryOrderRequest dor = (DeliveryOrderRequest) obj; + log.debug("Received: " + dor); + + // generate random delay + Thread.sleep((long) (Math.random() * 10000)); + + // TODO: + + } else { + log.warn("Received unknown Object: " + obj); + } + } else { + log.warn("Received unknown Message: " + msg); + } + msg.acknowledge(); + } + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } catch (InterruptedException e) { + log.error("EXCEPTION!", e); + } + } + + @Override + public String toString() { + return "PendingDeliveries [driver=" + driver + "]"; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java index 2b78a76..9832e2f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java @@ -1,40 +1,68 @@ package at.ac.tuwien.sbc.valesriegler.group; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; +import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class DeliveryGroup implements Runnable, HasId { - private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class); - private static int idNext = 0; + private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class); + private static int idNext = 0; + + private DeliveryGroupData deliveryGroupData; - private DeliveryGroupData deliveryGroupData; + public DeliveryGroup() { + deliveryGroupData = new DeliveryGroupData(++idNext); + } - public DeliveryGroup() { - this.deliveryGroupData = new DeliveryGroupData(++idNext); - } + public DeliveryGroup(int id) { + deliveryGroupData = new DeliveryGroupData(id); + } - public DeliveryGroup(int id) { - this.deliveryGroupData = new DeliveryGroupData(id); - } + public DeliveryGroupData getDeliveryGroupData() { + return deliveryGroupData; + } - public DeliveryGroupData getDeliveryGroupData() { - return deliveryGroupData; - } + @Override + public int getId() { + return deliveryGroupData.getId(); + } - @Override - public int getId() { - return deliveryGroupData.getId(); - } + public void setDeliveryGroupData(DeliveryGroupData deliveryGroupData) { + this.deliveryGroupData = deliveryGroupData; + } - public void setDeliveryGroupData(DeliveryGroupData deliveryGroupData) { - this.deliveryGroupData = deliveryGroupData; - } + @Override + public void run() { + log.debug("Thread started for:" + this); + try { + Thread.sleep((long) (Math.random() * 10000)); + switch (deliveryGroupData.getDeliveryStatus()) { + case START : + JMSGroupConnector conn = JMSGroupConnector.getConnector(deliveryGroupData.getPizzeriaId()); + conn.send(new DeliveryOrderRequest(deliveryGroupData)); + break; + case ORDERED : + break; + case IN_PROGRESS :// TODO + break; + case DELIVERED : + break; + case PAID : + break; + case IS_DELIVERED : + break; + case DELIVERY_FAILED : + break; + default : + break; + } - @Override - public void run() { - //@jan you can use that like in at.ac.tuwien.sbc.valesriegler.group.Group if you need to... - } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderRequest.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderRequest.java new file mode 100644 index 0000000..5779166 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderRequest.java @@ -0,0 +1,24 @@ +package at.ac.tuwien.sbc.valesriegler.group.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.common.AbstractDeliveryAction; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; + +/** + * This is the phonecall to a pizzeria. + * + * @author jan + * + */ +public class DeliveryOrderRequest extends AbstractDeliveryAction implements Serializable { + public DeliveryOrderRequest(DeliveryGroupData deliveryGroupData) { + super(deliveryGroupData); + } + + @Override + public String toString() { + return "DeliveryOrderRequest [getDeliveryGroupData()=" + getDeliveryGroupData() + "]"; + } + +} 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 360b8b1..82dff0c 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 @@ -20,6 +20,7 @@ import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; 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.OrderRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderResponse; import at.ac.tuwien.sbc.valesriegler.group.actions.PayRequest; @@ -147,15 +148,20 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message informPizzariaGui.send(session.createObjectMessage(request)); session.close(); return; + + } else if (request instanceof DeliveryOrderRequest) { + // TODO: + return; } } catch (JMSException e) { log.error("EXCEPTION!", e); } } - public static HashMap getConnectors() { + public static HashMap getConnectors() { return connectors; } + public static JMSGroupConnector getConnector(String key) { return connectors.get(key); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java index 92ae50d..86ffdd0 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java @@ -1,20 +1,14 @@ package at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners; -import javax.jms.Connection; -import javax.jms.DeliveryMode; import javax.jms.JMSException; import javax.jms.Message; 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.group.actions.OrderRequest; -import at.ac.tuwien.sbc.valesriegler.group.actions.OrderResponse; +import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; /** @@ -40,37 +34,15 @@ public class WantADelivery implements MessageListener { ObjectMessage objMsg = (ObjectMessage) msg; Object obj = objMsg.getObject(); - if (obj instanceof OrderRequest) { - OrderRequest orderrequest = (OrderRequest) obj; - log.debug("Received: " + orderrequest); + if (obj instanceof DeliveryOrderRequest) { + DeliveryOrderRequest dor = (DeliveryOrderRequest) obj; + log.debug("Received: " + dor); // generate random delay Thread.sleep((long) (Math.random() * 10000)); - ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(waiter.getCONNECTSTRING()); - Connection connection = connectionFactory.createConnection(); - connection.start(); - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - OrderResponse or = new OrderResponse(orderrequest.getGroupdata(), waiter.getId()); + // TODO - // Make cooks do their work - MessageProducer prodOP = session.createProducer(session.createQueue("OrdersToCook")); - prodOP.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - // TODO: hack - orderrequest.getGroupdata().setOrderWaiter(waiter.getId()); - prodOP.send(session.createObjectMessage(orderrequest)); - - // inform Group + Pizzeria - MessageProducer informGroup = session.createProducer(session.createQueue("GroupConnector")); - informGroup.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - informGroup.send(session.createObjectMessage(or)); - - MessageProducer informPizzeria = session.createProducer(session.createQueue("PizzeriaConnector")); - informPizzeria.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - informPizzeria.send(session.createObjectMessage(or)); - - session.close(); - connection.close(); } else { log.warn("Received unknown Object: " + obj); } -- 2.43.0 From 72f00af84cfe27f3743964d5954344a08fefe860 Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 10 Jun 2013 13:59:47 +0200 Subject: [PATCH 05/16] [JMS] Send DeliveryGroups to the waiter --- .../sbc/valesriegler/group/jms/JMSGroupConnector.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 82dff0c..c382ba3 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 @@ -150,7 +150,15 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message return; } else if (request instanceof DeliveryOrderRequest) { - // TODO: + 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) { -- 2.43.0 From db21f25017e82a40096d5c486a92c0843edd3fd8 Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 10 Jun 2013 14:28:12 +0200 Subject: [PATCH 06/16] [JMS] NEW Workflow: cook now gets the order. --- .../group/actions/DeliveryOrderResponse.java | 31 +++++++++++++++++++ .../valesriegler/waiter/jms/JMSWaiter.java | 6 ++++ .../jms/messageListeners/WantADelivery.java | 28 ++++++++++++++++- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderResponse.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderResponse.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderResponse.java new file mode 100644 index 0000000..ea0e830 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderResponse.java @@ -0,0 +1,31 @@ +package at.ac.tuwien.sbc.valesriegler.group.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.common.AbstractDeliveryAction; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; + +/** + * This is the phonecall to a pizzeria. + * + * @author jan + * + */ +public class DeliveryOrderResponse extends AbstractDeliveryAction implements Serializable { + private final int waiterId; + + public DeliveryOrderResponse(DeliveryGroupData deliveryGroupData, int waiterId) { + super(deliveryGroupData); + this.waiterId = waiterId; + } + + @Override + public String toString() { + return "DeliveryOrderResponse [getDeliveryGroupData()=" + getDeliveryGroupData() + "]"; + } + + public int getWaiterId() { + return waiterId; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/JMSWaiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/JMSWaiter.java index ea22a61..8ba3124 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/JMSWaiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/JMSWaiter.java @@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.CookedOrders; +import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantADelivery; import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToOrder; import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToPay; import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToSitAtTable; @@ -57,6 +58,11 @@ public class JMSWaiter implements HasId { MessageConsumer consWantToPay = sessWantToPay.createConsumer(sessWantToPay.createQueue("WantToPay")); consWantToPay.setMessageListener(new WantToPay(this)); + Session sessWantADelivery = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consWantADelivery = sessWantADelivery.createConsumer(sessWantADelivery + .createQueue("WantADelivery")); + consWantADelivery.setMessageListener(new WantADelivery(this)); + } catch (Exception e) { log.error("Caught: ", e); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java index 86ffdd0..e9d0f0d 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java @@ -1,14 +1,20 @@ package at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners; +import javax.jms.Connection; +import javax.jms.DeliveryMode; import javax.jms.JMSException; import javax.jms.Message; 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.group.actions.DeliveryOrderRequest; +import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderResponse; import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; /** @@ -41,7 +47,27 @@ public class WantADelivery implements MessageListener { // generate random delay Thread.sleep((long) (Math.random() * 10000)); - // TODO + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(waiter.getCONNECTSTRING()); + Connection connection = connectionFactory.createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + DeliveryOrderResponse or = new DeliveryOrderResponse(dor.getDeliveryGroupData(), waiter.getId()); + + // Make cooks do their work + MessageProducer prodOP = session.createProducer(session.createQueue("DeliveryOrdersToCook")); + prodOP.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + // TODO: hack + dor.getDeliveryGroupData().setWaiterIdOfOrder(waiter.getId()); + prodOP.send(session.createObjectMessage(dor)); + + // inform Group + Pizzeria + MessageProducer informGroup = session.createProducer(session.createQueue("GroupConnector")); + informGroup.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informGroup.send(session.createObjectMessage(or)); + + MessageProducer informPizzeria = session.createProducer(session.createQueue("PizzeriaConnector")); + informPizzeria.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzeria.send(session.createObjectMessage(or)); } else { log.warn("Received unknown Object: " + obj); -- 2.43.0 From 23f19205ba56c7c4f14fe6a7f797e9359c2cd124 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 10 Jun 2013 15:53:05 +0200 Subject: [PATCH 07/16] [JMS] new WF works now (hopefully) --- .../tuwien/sbc/valesriegler/common/Util.java | 2 + .../cook/actions/DeliveryOrderInfo.java | 10 +- .../sbc/valesriegler/cook/jms/JMSCook.java | 12 ++- .../DeliveryOrdersToCook.java | 49 +++++++++- .../jms/messageListeners/OrdersToCook.java | 4 +- .../valesriegler/driver/jms/JMSDriver.java | 14 +-- .../CookedDeliveryOrders.java | 95 +++++++++++++++++++ .../messageListeners/PendingDeliveries.java | 64 ------------- 8 files changed, 167 insertions(+), 83 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java delete mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/PendingDeliveries.java 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 671dc09..3db298a 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 @@ -66,6 +66,8 @@ public abstract class Util { public static final int GROUP_AGENT_PORT = 9876; public static final int DELIVERY_CUSTOMERS_PORT = 9877; + public static final String JMS_DELIVERY_DESTINATION = "tcp://localhost:61611?jms.prefetchPolicy.all=1"; + public static String getId(int id) { return (id != 0 && id != -1) ? String.valueOf(id) : ""; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java index 71e75e6..0abcc11 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java @@ -2,8 +2,8 @@ package at.ac.tuwien.sbc.valesriegler.cook.actions; import java.io.Serializable; -import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; -import at.ac.tuwien.sbc.valesriegler.types.GroupData; +import at.ac.tuwien.sbc.valesriegler.common.AbstractDeliveryAction; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; /** * response to the group's interest in pizza. @@ -11,11 +11,11 @@ import at.ac.tuwien.sbc.valesriegler.types.GroupData; * @author jan * */ -public class DeliveryOrderInfo extends AbstractAction implements Serializable { +public class DeliveryOrderInfo extends AbstractDeliveryAction implements Serializable { private final int cookId; - public DeliveryOrderInfo(GroupData groupdata, int cookId) { - super(groupdata); + public DeliveryOrderInfo(DeliveryGroupData deliveryGroupData, int cookId) { + super(deliveryGroupData); this.cookId = cookId; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java index c2378da..79026eb 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java @@ -10,6 +10,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.DeliveryOrdersToCook; import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.OrdersToCook; /** @@ -34,9 +35,14 @@ public class JMSCook implements HasId { Connection connection = connectionFactory.createConnection(); connection.start(); - Session sessWantToSit = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); - MessageConsumer consWantToSit = sessWantToSit.createConsumer(sessWantToSit.createQueue("OrdersToCook")); - consWantToSit.setMessageListener(new OrdersToCook(this)); + Session sessOrdersToCook = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); + MessageConsumer consOrdersToCook = sessOrdersToCook.createConsumer(sessOrdersToCook.createQueue("OrdersToCook")); + consOrdersToCook.setMessageListener(new OrdersToCook(this)); + + Session sessDeliveryOrdersToCook = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); + MessageConsumer consDeliveryOrdersToCook = sessDeliveryOrdersToCook.createConsumer(sessDeliveryOrdersToCook + .createQueue("DeliveryOrdersToCook")); + consDeliveryOrdersToCook.setMessageListener(new DeliveryOrdersToCook(this)); } catch (JMSException e) { log.error("EXCEPTION!", e); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java index 049add1..973b778 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java @@ -1,15 +1,25 @@ package at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners; +import javax.jms.Connection; +import javax.jms.DeliveryMode; import javax.jms.JMSException; import javax.jms.Message; 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.cook.actions.DeliveryOrderInfo; import at.ac.tuwien.sbc.valesriegler.cook.jms.JMSCook; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; +import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; +import at.ac.tuwien.sbc.valesriegler.types.Pizza; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; /** * Cook the requested pizza. @@ -37,11 +47,44 @@ public class DeliveryOrdersToCook implements MessageListener { DeliveryOrderRequest dor = (DeliveryOrderRequest) obj; log.debug("Received: " + dor); - // generate random delay - Thread.sleep((long) (Math.random() * 10000)); + for (PizzaOrder pizzaorder : dor.getDeliveryGroupData().getOrder().getOrderedPizzas()) { + pizzaorder.setStatus(PizzaOrderStatus.IN_PREPARATION); + } - // TODO: + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(cook.getCONNECTSTRING()); + Connection connection = connectionFactory.createConnection(); + connection.start(); + // inform pizzeria + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer prod = session.createProducer(session.createQueue("PizzeriaConnector")); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + DeliveryOrderInfo doi = new DeliveryOrderInfo(dor.getDeliveryGroupData(), cook.getId()); + prod.send(session.createObjectMessage(doi)); + session.close(); + + // generate delay + for (PizzaOrder po : dor.getDeliveryGroupData().getOrder().getOrderedPizzas()) { + Thread.sleep(po.getPizzaType().duration * 1000); + po.setStatus(PizzaOrderStatus.DONE); + Pizza p = Pizza.createPizzaFromPizzaOrder(po, cook.getId(), false); + dor.getGroupdata().getOrder().getCookedPizzas().add(p); + } + dor.getGroupdata().getOrder().setStatus(OrderStatus.DELIVERY_PENDING); + + // let pizzas be delivered. + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + prod = session.createProducer(session.createQueue("CookedDeliveryOrders")); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + prod.send(session.createObjectMessage(doi)); + + // inform pizeria + prod = session.createProducer(session.createQueue("PizzeriaConnector")); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + // WTF: oi = new OrderInfo(orderrequest.getGroupdata(), cook.getId()); + prod.send(session.createObjectMessage(doi)); + session.close(); + connection.close(); } else { log.warn("Received unknown Object: " + obj); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java index 5c61c7b..62b6875 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java @@ -63,7 +63,7 @@ public class OrdersToCook implements MessageListener { prod.send(session.createObjectMessage(oi)); session.close(); - // generate random delay + // generate delay for (PizzaOrder po : orderrequest.getGroupdata().getOrder().getOrderedPizzas()) { Thread.sleep(po.getPizzaType().duration * 1000); po.setStatus(PizzaOrderStatus.DONE); @@ -81,7 +81,7 @@ public class OrdersToCook implements MessageListener { // inform pizeria prod = session.createProducer(session.createQueue("PizzeriaConnector")); prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - oi = new OrderInfo(orderrequest.getGroupdata(), cook.getId()); + // WTF: oi = new OrderInfo(orderrequest.getGroupdata(), cook.getId()); prod.send(session.createObjectMessage(oi)); session.close(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/JMSDriver.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/JMSDriver.java index 2aac4e7..06318be 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/JMSDriver.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/JMSDriver.java @@ -2,12 +2,15 @@ package at.ac.tuwien.sbc.valesriegler.driver.jms; import javax.jms.Connection; import javax.jms.JMSException; +import javax.jms.MessageConsumer; +import javax.jms.Session; import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.driver.jms.messageListeners.CookedDeliveryOrders; /** * JMSDriver registers all the Listeners. @@ -27,7 +30,7 @@ public class JMSDriver implements HasId { public JMSDriver(String jmsURL, int id) { CONNECTSTRING = jmsURL; this.id = id; - log.info("I AM A JMSCook WITH ID {}", this.id); + log.info("I AM A JMSDriver WITH ID {}", this.id); try { // Connecting to the Broker and to the output queue @@ -35,11 +38,10 @@ public class JMSDriver implements HasId { Connection connection = connectionFactory.createConnection(); connection.start(); - // Session sessWantToSit = connection.createSession(false, - // Session.CLIENT_ACKNOWLEDGE); - // MessageConsumer consWantToSit = - // sessWantToSit.createConsumer(sessWantToSit.createQueue("OrdersToCook")); - // consWantToSit.setMessageListener(new OrdersToCook(this)); + Session sessCookedDeliveryOrders = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); + MessageConsumer consCookedDeliveryOrders = sessCookedDeliveryOrders.createConsumer(sessCookedDeliveryOrders + .createQueue("OrdersToCook")); + consCookedDeliveryOrders.setMessageListener(new CookedDeliveryOrders(this)); } catch (JMSException e) { log.error("EXCEPTION!", e); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java new file mode 100644 index 0000000..4d2d889 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java @@ -0,0 +1,95 @@ +package at.ac.tuwien.sbc.valesriegler.driver.jms.messageListeners; + +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; +import javax.jms.Message; +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.Util; +import at.ac.tuwien.sbc.valesriegler.cook.actions.DeliveryOrderInfo; +import at.ac.tuwien.sbc.valesriegler.driver.jms.JMSDriver; +import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderResponse; + +/** + * + * + * @author jan + * + */ +public class CookedDeliveryOrders implements MessageListener { + private static final Logger log = LoggerFactory.getLogger(CookedDeliveryOrders.class); + private final JMSDriver driver; + + public CookedDeliveryOrders(JMSDriver driver) { + this.driver = driver; + } + + @Override + public void onMessage(Message msg) { + try { + synchronized (driver) { + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof DeliveryOrderResponse) { + DeliveryOrderResponse dor = (DeliveryOrderResponse) obj; + log.debug("Received: " + dor); + + // generate random delay + Thread.sleep((long) (Math.random() * 10000)); + String addr = dor.getDeliveryGroupData().getAddress(); + + // Deliver to destination broker + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Util.JMS_DELIVERY_DESTINATION); + Connection connection = connectionFactory.createConnection(); + connection.start(); + + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer prod = session.createProducer(session.createQueue(addr)); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + DeliveryOrderInfo doi = new DeliveryOrderInfo(dor.getDeliveryGroupData(), driver.getId()); + prod.send(session.createObjectMessage(doi)); + session.close(); + connection.close(); + + // Inform pizzeria + connectionFactory = new ActiveMQConnectionFactory(driver.getCONNECTSTRING()); + connection = connectionFactory.createConnection(); + connection.start(); + + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + prod = session.createProducer(session.createQueue("PizzeriaConnector")); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + prod.send(session.createObjectMessage(doi)); + session.close(); + connection.close(); + + } else { + log.warn("Received unknown Object: " + obj); + } + } else { + log.warn("Received unknown Message: " + msg); + } + msg.acknowledge(); + } + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } catch (InterruptedException e) { + log.error("EXCEPTION!", e); + } + } + + @Override + public String toString() { + return "PendingDeliveries [driver=" + driver + "]"; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/PendingDeliveries.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/PendingDeliveries.java deleted file mode 100644 index 75f0308..0000000 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/PendingDeliveries.java +++ /dev/null @@ -1,64 +0,0 @@ -package at.ac.tuwien.sbc.valesriegler.driver.jms.messageListeners; - -import javax.jms.JMSException; -import javax.jms.Message; -import javax.jms.MessageListener; -import javax.jms.ObjectMessage; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import at.ac.tuwien.sbc.valesriegler.driver.jms.JMSDriver; -import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; - -/** - * - * - * @author jan - * - */ -public class PendingDeliveries implements MessageListener { - private static final Logger log = LoggerFactory.getLogger(PendingDeliveries.class); - private final JMSDriver driver; - - public PendingDeliveries(JMSDriver driver) { - this.driver = driver; - } - - @Override - public void onMessage(Message msg) { - try { - synchronized (driver) { - if (msg instanceof ObjectMessage) { - ObjectMessage objMsg = (ObjectMessage) msg; - Object obj = objMsg.getObject(); - - if (obj instanceof DeliveryOrderRequest) { - DeliveryOrderRequest dor = (DeliveryOrderRequest) obj; - log.debug("Received: " + dor); - - // generate random delay - Thread.sleep((long) (Math.random() * 10000)); - - // TODO: - - } else { - log.warn("Received unknown Object: " + obj); - } - } else { - log.warn("Received unknown Message: " + msg); - } - msg.acknowledge(); - } - } catch (JMSException e) { - log.error("EXCEPTION!", e); - } catch (InterruptedException e) { - log.error("EXCEPTION!", e); - } - } - - @Override - public String toString() { - return "PendingDeliveries [driver=" + driver + "]"; - } -} -- 2.43.0 From 9df2d71d7d092d6dae74418f843181668b367425 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 10 Jun 2013 16:13:13 +0200 Subject: [PATCH 08/16] [JMS] GG should be able to launch deliveryorders now. --- runJMS.sh | 2 +- .../sbc/valesriegler/group/DeliveryGroup.java | 27 +- .../group/gui/DeliveryOverviewModel.java | 233 +++++++++--------- .../valesriegler/types/DeliveryStatus.java | 3 +- 4 files changed, 141 insertions(+), 124 deletions(-) diff --git a/runJMS.sh b/runJMS.sh index 9294948..fad31d5 100755 --- a/runJMS.sh +++ b/runJMS.sh @@ -16,7 +16,7 @@ mvn exec:java -Dexec.mainClass="org.apache.activemq.console.Main" -Dexec.args="s # wait for broakers to start -sleep 120 +sleep 60 echo "*** continuing launch process ***" echo "*** continuing launch process ***" echo "*** continuing launch process ***" diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java index 9832e2f..38aa521 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java @@ -7,6 +7,7 @@ import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus; public class DeliveryGroup implements Runnable, HasId { private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class); @@ -41,23 +42,23 @@ public class DeliveryGroup implements Runnable, HasId { try { Thread.sleep((long) (Math.random() * 10000)); switch (deliveryGroupData.getDeliveryStatus()) { - case START : + case START : // should not happen. + break; + case ORDER_PENDING : JMSGroupConnector conn = JMSGroupConnector.getConnector(deliveryGroupData.getPizzeriaId()); conn.send(new DeliveryOrderRequest(deliveryGroupData)); break; - case ORDERED : - break; - case IN_PROGRESS :// TODO + case IN_PROGRESS : // do nothing break; - case DELIVERED : + case DELIVERED : // do nothing break; - case PAID : + case PAID : // do nothing break; - case IS_DELIVERED : + case IS_DELIVERED : // do nothing break; - case DELIVERY_FAILED : + case DELIVERY_FAILED : // do nothing break; - default : + default : // do nothing break; } @@ -65,4 +66,12 @@ public class DeliveryGroup implements Runnable, HasId { e.printStackTrace(); } } + + // lets order at the pizzaria. + public void orderSomeFood() { + log.debug("orderSomeFood(): " + this); + deliveryGroupData.setDeliveryStatus(DeliveryStatus.ORDER_PENDING); + GroupAgent.getInstance().getGroupModel().fireTableDataChanged(); + new Thread(this).start(); + } } 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 53613e0..e91b45f 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,123 +1,132 @@ 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); - - private static final String STATE = "State"; - private static final String PIZZAS = "Pizzas"; - private static final String ADDRESS = "Address"; - private static final String ID = "ID"; - private static final String PIZZERIA = "Pizzeria"; - private static final String[] COLUMNS = new String[]{ID, ADDRESS, PIZZAS, STATE, PIZZERIA}; - - - @Override - protected String[] getColumns() { - return COLUMNS; - } - - @Override - public Object getValueAt(int rowIndex, int columnIndex) { - List values = new ArrayList<>(items.values()); - DeliveryGroup group = values.get(rowIndex); - String wantedColumn = COLUMNS[columnIndex]; - switch (wantedColumn) { - case ID: - int groupId = group.getId(); - return groupId; - case ADDRESS: - return group.getDeliveryGroupData().getAddress(); - case PIZZAS: - if (group.getDeliveryGroupData().getOrder() == null) - return "none"; - return Util.pizzaDisplay(group.getDeliveryGroupData().getOrder().getOrderedPizzas()); - case STATE: - return group.getDeliveryGroupData().getDeliveryStatus(); - case PIZZERIA: - return group.getDeliveryGroupData().getPizzeriaId(); - default: - throw new RuntimeException(UNHANDLEDCOLUMN); - } - } - - public void setOrdersTaken(List groups) { - for (DeliveryGroupData group : groups) { - changeStateOfGroup(group.getId(), DeliveryStatus.ORDERED); - } - - fireTableDataChanged(); - } - - private void changeStateOfGroup(int groupId, DeliveryStatus state) { - items.get(groupId).getDeliveryGroupData().setDeliveryStatus(state); - } - - public void setOrdersDelivered(List groups) { - for (DeliveryGroupData group : groups) { - changeStateOfGroup(group.getId(), group.getDeliveryStatus()); - } - - fireTableDataChanged(); - } - - public void addDeliveries(List groups) { - synchronized (items) { - for (DeliveryGroupData group : groups) { - final DeliveryGroup deliveryGroup = items.get(group.getId()); - if(deliveryGroup==null) { - log.error("Delivery group not found!!!"); - } - deliveryGroup.setDeliveryGroupData(group); - } - } - fireTableDataChanged(); - } - - public void createStatistics() { - int size; - int finished; - synchronized (items) { - final Collection values = items.values(); - size = values.size(); - finished = 0; - for (DeliveryGroup group : values) { - final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); - if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { - finished++; - } - } - } - log.info("{} deliveries were ordered", size); - log.info("{} were finished", finished); - } - - public boolean hasFinished() { - int size; - int finished; - synchronized (items) { - final Collection values = items.values(); - size = values.size(); - finished = 0; - for (DeliveryGroup group : values) { - final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); - if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { - finished++; - } - } - } - return size == finished; - } + private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class); + + private static final String STATE = "State"; + private static final String PIZZAS = "Pizzas"; + private static final String ADDRESS = "Address"; + private static final String ID = "ID"; + private static final String PIZZERIA = "Pizzeria"; + private static final String[] COLUMNS = new String[]{ID, ADDRESS, PIZZAS, STATE, PIZZERIA}; + + @Override + protected String[] getColumns() { + return COLUMNS; + } + + @Override + public void addItems(List newItems) { + log.info("addItems()"); + super.addItems(newItems); + for (DeliveryGroup g : newItems) { + if (Util.useJMS) + g.orderSomeFood(); + } + } + + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + List values = new ArrayList<>(items.values()); + DeliveryGroup group = values.get(rowIndex); + String wantedColumn = COLUMNS[columnIndex]; + switch (wantedColumn) { + case ID : + int groupId = group.getId(); + return groupId; + case ADDRESS : + return group.getDeliveryGroupData().getAddress(); + case PIZZAS : + if (group.getDeliveryGroupData().getOrder() == null) + return "none"; + return Util.pizzaDisplay(group.getDeliveryGroupData().getOrder().getOrderedPizzas()); + case STATE : + return group.getDeliveryGroupData().getDeliveryStatus(); + case PIZZERIA : + return group.getDeliveryGroupData().getPizzeriaId(); + default : + throw new RuntimeException(UNHANDLEDCOLUMN); + } + } + + public void setOrdersTaken(List groups) { + for (DeliveryGroupData group : groups) { + changeStateOfGroup(group.getId(), DeliveryStatus.ORDERED); + } + + fireTableDataChanged(); + } + + private void changeStateOfGroup(int groupId, DeliveryStatus state) { + items.get(groupId).getDeliveryGroupData().setDeliveryStatus(state); + } + + public void setOrdersDelivered(List groups) { + for (DeliveryGroupData group : groups) { + changeStateOfGroup(group.getId(), group.getDeliveryStatus()); + } + + fireTableDataChanged(); + } + + public void addDeliveries(List groups) { + synchronized (items) { + for (DeliveryGroupData group : groups) { + final DeliveryGroup deliveryGroup = items.get(group.getId()); + if (deliveryGroup == null) { + log.error("Delivery group not found!!!"); + } + deliveryGroup.setDeliveryGroupData(group); + } + } + fireTableDataChanged(); + } + + public void createStatistics() { + int size; + int finished; + synchronized (items) { + final Collection values = items.values(); + size = values.size(); + finished = 0; + for (DeliveryGroup group : values) { + final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); + if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { + finished++; + } + } + } + log.info("{} deliveries were ordered", size); + log.info("{} were finished", finished); + } + + public boolean hasFinished() { + int size; + int finished; + synchronized (items) { + final Collection values = items.values(); + size = values.size(); + finished = 0; + for (DeliveryGroup group : values) { + final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); + if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { + finished++; + } + } + } + return size == finished; + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryStatus.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryStatus.java index 1d7225a..b0d3495 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryStatus.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryStatus.java @@ -1,8 +1,7 @@ package at.ac.tuwien.sbc.valesriegler.types; - import java.io.Serializable; public enum DeliveryStatus implements Serializable { - ORDERED, IN_PROGRESS, IS_DELIVERED, DELIVERED, PAID, DELIVERY_FAILED, START; + ORDERED, IN_PROGRESS, IS_DELIVERED, DELIVERED, PAID, DELIVERY_FAILED, START, ORDER_PENDING; } -- 2.43.0 From e26430af9c2a1e0e891dfaa635656d124907c945 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 10 Jun 2013 17:15:54 +0200 Subject: [PATCH 09/16] [JMS] NEW WorkFlow works now. --- runJMS.sh | 2 +- .../common/AbstractDeliveryAction.java | 13 +++++++++--- .../DeliveryOrdersToCook.java | 4 ++-- .../driver/actions/OrderDeliveredInfo.java | 20 ++++++++++++------- .../valesriegler/driver/jms/JMSDriver.java | 2 +- .../CookedDeliveryOrders.java | 17 ++++++++-------- .../sbc/valesriegler/group/GroupAgent.java | 2 +- 7 files changed, 37 insertions(+), 23 deletions(-) diff --git a/runJMS.sh b/runJMS.sh index fad31d5..7bf81ca 100755 --- a/runJMS.sh +++ b/runJMS.sh @@ -25,7 +25,7 @@ echo "*** continuing launch process ***" # Run Groupgui -mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.group.GroupAgent" -Dexec.args="JMS" & +mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.group.GroupAgent" -Dexec.args="JMS tcp://localhost:61610?jms.prefetchPolicy.all=1" & #run pizzeria1 ./runJMSPizzaria.sh "tcp://localhost:61621?jms.prefetchPolicy.all=1" diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/AbstractDeliveryAction.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/AbstractDeliveryAction.java index 0764975..afb027e 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/AbstractDeliveryAction.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/AbstractDeliveryAction.java @@ -1,6 +1,7 @@ package at.ac.tuwien.sbc.valesriegler.common; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; +import at.ac.tuwien.sbc.valesriegler.types.GroupData; /** * Abstract delivery action that can be performed in the DS. In almost all cases the @@ -16,9 +17,15 @@ public abstract class AbstractDeliveryAction extends AbstractAction { super(null); this.groupData = groupData; } - - public DeliveryGroupData getGroupData() { - return groupData; + /** + * do not use. + * + * @return + */ + @Deprecated + @Override + public GroupData getGroupdata() { + throw new IllegalArgumentException("use getDeliveryGroupData() instead!"); } public DeliveryGroupData getDeliveryGroupData() { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java index 973b778..e2a63cf 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java @@ -68,9 +68,9 @@ public class DeliveryOrdersToCook implements MessageListener { Thread.sleep(po.getPizzaType().duration * 1000); po.setStatus(PizzaOrderStatus.DONE); Pizza p = Pizza.createPizzaFromPizzaOrder(po, cook.getId(), false); - dor.getGroupdata().getOrder().getCookedPizzas().add(p); + dor.getDeliveryGroupData().getOrder().getCookedPizzas().add(p); } - dor.getGroupdata().getOrder().setStatus(OrderStatus.DELIVERY_PENDING); + dor.getDeliveryGroupData().getOrder().setStatus(OrderStatus.DELIVERY_PENDING); // let pizzas be delivered. session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java index 539ec5e..2b03449 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java @@ -2,8 +2,8 @@ package at.ac.tuwien.sbc.valesriegler.driver.actions; import java.io.Serializable; -import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; -import at.ac.tuwien.sbc.valesriegler.types.GroupData; +import at.ac.tuwien.sbc.valesriegler.common.AbstractDeliveryAction; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; /** * response to the group's interest in pizza. @@ -11,14 +11,20 @@ import at.ac.tuwien.sbc.valesriegler.types.GroupData; * @author jan * */ -public class OrderDeliveredInfo extends AbstractAction implements Serializable { - private final int cookId; +public class OrderDeliveredInfo extends AbstractDeliveryAction implements Serializable { + private final int driverId; - public OrderDeliveredInfo(GroupData groupdata, int cookId) { + public OrderDeliveredInfo(DeliveryGroupData groupdata, int driverId) { super(groupdata); - this.cookId = cookId; + this.driverId = driverId; } - // TODO + public int getDriverId() { + return driverId; + } + @Override + public String toString() { + return "OrderDeliveredInfo [driverId=" + driverId + ", getDeliveryGroupData()=" + getDeliveryGroupData() + "]"; + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/JMSDriver.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/JMSDriver.java index 06318be..8742b41 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/JMSDriver.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/JMSDriver.java @@ -40,7 +40,7 @@ public class JMSDriver implements HasId { Session sessCookedDeliveryOrders = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); MessageConsumer consCookedDeliveryOrders = sessCookedDeliveryOrders.createConsumer(sessCookedDeliveryOrders - .createQueue("OrdersToCook")); + .createQueue("CookedDeliveryOrders")); consCookedDeliveryOrders.setMessageListener(new CookedDeliveryOrders(this)); } catch (JMSException e) { log.error("EXCEPTION!", e); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java index 4d2d889..5013d4a 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java @@ -15,8 +15,8 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.cook.actions.DeliveryOrderInfo; +import at.ac.tuwien.sbc.valesriegler.driver.actions.OrderDeliveredInfo; import at.ac.tuwien.sbc.valesriegler.driver.jms.JMSDriver; -import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderResponse; /** * @@ -40,24 +40,25 @@ public class CookedDeliveryOrders implements MessageListener { ObjectMessage objMsg = (ObjectMessage) msg; Object obj = objMsg.getObject(); - if (obj instanceof DeliveryOrderResponse) { - DeliveryOrderResponse dor = (DeliveryOrderResponse) obj; - log.debug("Received: " + dor); + if (obj instanceof DeliveryOrderInfo) { + DeliveryOrderInfo doi = (DeliveryOrderInfo) obj; + log.debug("Received: " + doi); // generate random delay Thread.sleep((long) (Math.random() * 10000)); - String addr = dor.getDeliveryGroupData().getAddress(); + String addr = doi.getDeliveryGroupData().getAddress(); // Deliver to destination broker ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Util.JMS_DELIVERY_DESTINATION); Connection connection = connectionFactory.createConnection(); connection.start(); + log.debug("CookedDelivery:ActiveMQConnectionFactory::" + connection); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer prod = session.createProducer(session.createQueue(addr)); prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - DeliveryOrderInfo doi = new DeliveryOrderInfo(dor.getDeliveryGroupData(), driver.getId()); - prod.send(session.createObjectMessage(doi)); + OrderDeliveredInfo odi = new OrderDeliveredInfo(doi.getDeliveryGroupData(), driver.getId()); + prod.send(session.createObjectMessage(odi)); session.close(); connection.close(); @@ -69,7 +70,7 @@ public class CookedDeliveryOrders implements MessageListener { session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); prod = session.createProducer(session.createQueue("PizzeriaConnector")); prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - prod.send(session.createObjectMessage(doi)); + prod.send(session.createObjectMessage(odi)); session.close(); connection.close(); 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 ae4b9ca..fe89716 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 @@ -51,7 +51,7 @@ public class GroupAgent { public static GroupAgent groupAgent; public static void main(String[] args) { - if (args.length != 1) { + if (args.length < 1) { throw new IllegalArgumentException(USAGE); } -- 2.43.0 From 65001a7bbf6ad676834f86298a1e0cfede43bbef Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Mon, 10 Jun 2013 17:18:17 +0200 Subject: [PATCH 10/16] [XVSM]Some NPE fixes, GUI fixes --- .../sbc/valesriegler/group/GroupAgent.java | 18 ++-- .../group/gui/DeliveryOverviewModel.java | 27 +----- .../group/gui/GroupOverviewModel.java | 13 ++- .../valesriegler/pizzeria/PizzeriaAgent.java | 22 ++--- .../gui/tablemodels/DeliveryOrdersModel.java | 19 ++++ .../gui/tablemodels/GroupsOverviewModel.java | 19 ++++ .../tuwien/sbc/valesriegler/types/Order.java | 11 +-- .../sbc/valesriegler/xvsm/DriverXVSM.java | 96 ++++++++++--------- .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 10 +- 9 files changed, 128 insertions(+), 107 deletions(-) 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 ae4b9ca..a087999 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,16 +1,5 @@ package at.ac.tuwien.sbc.valesriegler.group; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -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; @@ -22,6 +11,11 @@ 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.swing.*; +import java.util.*; /** * The Main class of the Group component. @@ -67,7 +61,7 @@ public class GroupAgent { } groupAgent = new GroupAgent(); - if (!Util.runSimulation) { + if (! Util.runSimulation) { groupGui = new GroupGUI(); SwingUtilities.invokeLater(groupGui); } 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 53613e0..8bce9d4 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 @@ -53,34 +53,14 @@ public class DeliveryOverviewModel extends TableModel { } } - public void setOrdersTaken(List groups) { - for (DeliveryGroupData group : groups) { - changeStateOfGroup(group.getId(), DeliveryStatus.ORDERED); - } - - fireTableDataChanged(); - } - - private void changeStateOfGroup(int groupId, DeliveryStatus state) { - items.get(groupId).getDeliveryGroupData().setDeliveryStatus(state); - } - - public void setOrdersDelivered(List groups) { - for (DeliveryGroupData group : groups) { - changeStateOfGroup(group.getId(), group.getDeliveryStatus()); - } - - fireTableDataChanged(); - } - public void addDeliveries(List groups) { synchronized (items) { for (DeliveryGroupData group : groups) { final DeliveryGroup deliveryGroup = items.get(group.getId()); if(deliveryGroup==null) { - log.error("Delivery group not found!!!"); + log.error("Delivery group not found: {}", group.getId()); } - deliveryGroup.setDeliveryGroupData(group); + else deliveryGroup.setDeliveryGroupData(group); } } fireTableDataChanged(); @@ -118,6 +98,7 @@ public class DeliveryOverviewModel extends TableModel { } } } - return size == finished; + final boolean result = size == finished; + return result; } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java index fcdfcc5..a321dff 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java @@ -47,7 +47,18 @@ public class GroupOverviewModel extends TableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { List values = new ArrayList<>(items.values()); - Group group = values.get(rowIndex); + // TODO make sure this is necessary... +// Collections.sort(values, new Comparator() { +// @Override +// public int compare(Group o1, Group o2) { +// final int o1Id = o1.getId(); +// final int o2Id = o2.getId(); +// if(o1Ido2Id) return 1; +// else return 0; +// } +// }); + Group group = values.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch (wantedColumn) { case ID: diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java index 3e8c4d8..87a416c 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java @@ -1,27 +1,19 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria; -import java.util.List; - -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.jms.nac.JMSNAC; import at.ac.tuwien.sbc.valesriegler.pizzeria.actions.TableNew; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzeriaFrame; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.DeliveryDetailsModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.DeliveryOrdersModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.GroupsOverviewModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.OrdersOverviewModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.PizzasOfOrderModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.TablesOverviewModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.WaitersOfOrderModel; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.*; import at.ac.tuwien.sbc.valesriegler.pizzeria.jms.JMSPizzeriaConnector; import at.ac.tuwien.sbc.valesriegler.pizzeria.jms.PizzeriaJMSNACMsgListener; import at.ac.tuwien.sbc.valesriegler.types.Table; import at.ac.tuwien.sbc.valesriegler.xvsm.PizzeriaAgentXVSM; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.*; +import java.util.List; /** * The Main class of the Pizzeria compoment. @@ -61,7 +53,7 @@ public class PizzeriaAgent { case "XVSM" : pizzeriaAgent.createModels(); pizzeriaAgent.initXVSM(args[1]); - if (!Util.runSimulation) { + if (! Util.runSimulation) { pizzeriaAgent.initGUI(); } Util.useJMS = false; 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 c6bed84..336bfd3 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 @@ -6,6 +6,8 @@ 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 DeliveryOrdersModel extends AbstractOrdersModel { @@ -28,6 +30,23 @@ 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; + } + }); DeliveryGroupData group = values.get(rowIndex); Order order = group.getOrder(); String wantedColumn = COLUMNS[columnIndex]; 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 26b2ef4..142e14f 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,6 +7,8 @@ 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 { @@ -26,6 +28,23 @@ public class GroupsOverviewModel extends TableModel { 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/types/Order.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java index 687d84b..a463957 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java @@ -1,14 +1,13 @@ package at.ac.tuwien.sbc.valesriegler.types; +import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.common.Util; +import org.mozartspaces.capi3.Queryable; + import java.io.Serializable; import java.util.ArrayList; import java.util.List; -import org.mozartspaces.capi3.Queryable; - -import at.ac.tuwien.sbc.valesriegler.common.HasId; -import at.ac.tuwien.sbc.valesriegler.common.Util; - /** * Object denotes a Group's interest in pizzas and the state of the pizzas * themselves. @@ -79,7 +78,7 @@ public class Order implements Serializable, HasId { @Override public int getId() { - return id; + return Util.getIntSafe(id); } public List getOrderedPizzas() { 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 343b278..349b059 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 @@ -10,6 +10,7 @@ import at.ac.tuwien.sbc.valesriegler.types.Pizza; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; import org.mozartspaces.core.ContainerReference; import org.mozartspaces.core.MzsConstants; +import org.mozartspaces.core.MzsCoreException; import org.mozartspaces.core.TransactionReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,64 +43,68 @@ public class DriverXVSM extends AbstractXVSMConnector { @Override public void onEntriesWritten(List entries) throws Exception { - final List deliveries = castEntries(entries); + handleDeliveries(entries); - for (DeliveryGroupData delivery : deliveries) { - DeliveryGroupData template = new DeliveryGroupData(delivery.getId()); - template.setDeliveryStatus(DeliveryStatus.ORDERED); + } + }).createSpaceListenerImpl(); + } - final TransactionReference tx = capi.createTransaction( - 7000, - URI.create(String.format(Util.SERVER_ADDR, port))); - final String error = "Another driver wants to deliver this order!"; + private void handleDeliveries(List entries) throws MzsCoreException { + final List deliveries = castEntries(entries); - try { - // Get lock for delivering this delivery order - final DeliveryGroupData deliveryGroup = takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.DEFAULT, error); - final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, MzsConstants.RequestTimeout.INFINITE, "Cannot get the delivery order!"); - group.setDriverId(driverId); - group.setDeliveryStatus(DeliveryStatus.IN_PROGRESS); + for (DeliveryGroupData delivery : deliveries) { + DeliveryGroupData template = new DeliveryGroupData(delivery.getId()); + template.setDeliveryStatus(DeliveryStatus.ORDERED); - final List groups = Arrays.asList(group); -// sendItemsToContainer(groups, -// pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null); + final TransactionReference tx = capi.createTransaction( + 7000, + URI.create(String.format(Util.SERVER_ADDR, port))); + final String error = "Another driver wants to deliver this order!"; - if (!Util.runSimulation) { - Thread.sleep(3000); - } + try { + // Get lock for delivering this delivery order + final DeliveryGroupData deliveryGroup = takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.DEFAULT, error); + final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, MzsConstants.RequestTimeout.INFINITE, "Cannot get the delivery order!"); + group.setDriverId(driverId); + group.setDeliveryStatus(DeliveryStatus.IN_PROGRESS); - final String address = String.format(Util.SERVER_ADDR, Util.DELIVERY_CUSTOMERS_PORT); - ContainerReference cref = null; - boolean success = false; - try { - cref = capi.lookupContainer(group.getAddress(), URI.create(address), MzsConstants.RequestTimeout.ZERO, null); - log.debug("Looked up container {} successfully!", address); - success = true; + final List groups = Arrays.asList(group); +// sendItemsToContainer(groups, +// pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null); - } catch (Exception ex) { - log.info("Could not find container {}", address); - } - if (success) { - group.setDeliveryStatus(DeliveryStatus.DELIVERED); - if (!Util.runSimulation) { - sendItemsToContainer(Arrays.asList(new Bill(1000, group.getId())), cref, MzsConstants.RequestTimeout.DEFAULT, null); - } - } else { - group.setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED); - } - sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.DEFAULT, tx); - capi.commitTransaction(tx); + if (!Util.runSimulation) { + Thread.sleep(3000); + } + final String address = String.format(Util.SERVER_ADDR, Util.DELIVERY_CUSTOMERS_PORT); + ContainerReference cref = null; + boolean success = false; + try { + cref = capi.lookupContainer(group.getAddress(), URI.create(address), MzsConstants.RequestTimeout.ZERO, null); + log.debug("Looked up container {} successfully!", address); + success = true; - } catch (Exception e) { - log.info("DRiverXVSM exception"); - log.info(e.getMessage()); - e.printStackTrace(); + } catch (Exception ex) { + log.info("Could not find container {}", address); + } + if (success) { + group.setDeliveryStatus(DeliveryStatus.DELIVERED); + if (!Util.runSimulation) { + sendItemsToContainer(Arrays.asList(new Bill(1000, group.getId())), cref, MzsConstants.RequestTimeout.DEFAULT, null); } + } else { + group.setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED); } + sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.DEFAULT, tx); + capi.commitTransaction(tx); + + } catch (Exception e) { + log.info("DRiverXVSM exception"); + log.info(e.getMessage()); + e.printStackTrace(); } - }).createSpaceListenerImpl(); + } } public void listenForPreparedDeliveryPizzas() { @@ -143,6 +148,7 @@ 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/GroupAgentXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java index c2a829f..c42b0a2 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 @@ -83,7 +83,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { boolean finished = GroupAgent.getInstance().getDeliveryModel().hasFinished(); if (finished) { final long now = new Date().getTime(); - final long difference = startTime.get() - now; + final long difference = now - startTime.get(); log.info("Finished after {} seconds", difference / 1000); } } @@ -126,20 +126,20 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { 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"; + final String pizzeria1 = "9874"; + final String pizzeria2 = "9875"; 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(groups1); deliveryModel.addItems(groups2); deliveryModel.addItems(groups3); GroupAgent.getInstance().onDeliveryGroupsCreated(groups2); GroupAgent.getInstance().onDeliveryGroupsCreated(groups3); - GroupAgent.getInstance().onDeliveryGroupsCreated(groups1); +// GroupAgent.getInstance().onDeliveryGroupsCreated(groups1); log.info("ATTENTION: Now let's wait 60 seconds!"); -- 2.43.0 From c25b305e3072791ea4d249aca141139647c38395 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Mon, 10 Jun 2013 17:26:22 +0200 Subject: [PATCH 11/16] Merge it! --- .gitignore | 2 ++ .../tuwien/sbc/valesriegler/common/Util.java | 30 ++++++------------- .../cook/actions/DeliveryOrderInfo.java | 4 +-- .../sbc/valesriegler/cook/jms/JMSCook.java | 15 +++++----- .../DeliveryOrdersToCook.java | 18 ++++------- .../jms/messageListeners/OrdersToCook.java | 18 ++++------- .../driver/actions/OrderDeliveredInfo.java | 4 +-- .../valesriegler/driver/jms/JMSDriver.java | 13 ++++---- .../CookedDeliveryOrders.java | 18 ++++------- .../sbc/valesriegler/group/DeliveryGroup.java | 5 ++-- .../group/gui/DeliveryOverviewModel.java | 10 +++++++ 11 files changed, 55 insertions(+), 82 deletions(-) diff --git a/.gitignore b/.gitignore index 2404147..e49a1c9 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ target/* #active-mq persistent data activemq-data/* +.idea/* +.idea/**/* 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 3db298a..36d52c0 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,30 +1,18 @@ package at.ac.tuwien.sbc.valesriegler.common; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; -import java.net.URI; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.Set; - -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JTable; -import javax.swing.ScrollPaneConstants; -import javax.swing.border.TitledBorder; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - 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; +import org.slf4j.LoggerFactory; + +import javax.swing.*; +import javax.swing.border.TitledBorder; +import java.awt.*; +import java.net.URI; +import java.util.*; +import java.util.List; public abstract class Util { private static final Logger log = LoggerFactory.getLogger(Util.class); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java index 0abcc11..5fd5ee0 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java @@ -1,10 +1,10 @@ package at.ac.tuwien.sbc.valesriegler.cook.actions; -import java.io.Serializable; - import at.ac.tuwien.sbc.valesriegler.common.AbstractDeliveryAction; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; +import java.io.Serializable; + /** * response to the group's interest in pizza. * diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java index 79026eb..3e1b0eb 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java @@ -1,17 +1,16 @@ package at.ac.tuwien.sbc.valesriegler.cook.jms; -import javax.jms.Connection; -import javax.jms.JMSException; -import javax.jms.MessageConsumer; -import javax.jms.Session; - +import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.DeliveryOrdersToCook; +import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.OrdersToCook; import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.common.HasId; -import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.DeliveryOrdersToCook; -import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.OrdersToCook; +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.MessageConsumer; +import javax.jms.Session; /** * JMSCook registers all the Listeners. diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java index e2a63cf..dc26967 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java @@ -1,18 +1,5 @@ package at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners; -import javax.jms.Connection; -import javax.jms.DeliveryMode; -import javax.jms.JMSException; -import javax.jms.Message; -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.cook.actions.DeliveryOrderInfo; import at.ac.tuwien.sbc.valesriegler.cook.jms.JMSCook; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; @@ -20,6 +7,11 @@ import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; import at.ac.tuwien.sbc.valesriegler.types.Pizza; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; +import org.apache.activemq.ActiveMQConnectionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.jms.*; /** * Cook the requested pizza. diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java index 62b6875..a3facf7 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java @@ -1,18 +1,5 @@ package at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners; -import javax.jms.Connection; -import javax.jms.DeliveryMode; -import javax.jms.JMSException; -import javax.jms.Message; -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.cook.actions.OrderInfo; import at.ac.tuwien.sbc.valesriegler.cook.jms.JMSCook; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; @@ -20,6 +7,11 @@ import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; import at.ac.tuwien.sbc.valesriegler.types.Pizza; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; +import org.apache.activemq.ActiveMQConnectionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.jms.*; /** * Cook the requested pizza. diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java index 2b03449..aaa1e3f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java @@ -1,10 +1,10 @@ package at.ac.tuwien.sbc.valesriegler.driver.actions; -import java.io.Serializable; - import at.ac.tuwien.sbc.valesriegler.common.AbstractDeliveryAction; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; +import java.io.Serializable; + /** * response to the group's interest in pizza. * diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/JMSDriver.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/JMSDriver.java index 8742b41..65bbd43 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/JMSDriver.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/JMSDriver.java @@ -1,16 +1,15 @@ package at.ac.tuwien.sbc.valesriegler.driver.jms; -import javax.jms.Connection; -import javax.jms.JMSException; -import javax.jms.MessageConsumer; -import javax.jms.Session; - +import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.driver.jms.messageListeners.CookedDeliveryOrders; import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.common.HasId; -import at.ac.tuwien.sbc.valesriegler.driver.jms.messageListeners.CookedDeliveryOrders; +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.MessageConsumer; +import javax.jms.Session; /** * JMSDriver registers all the Listeners. diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java index 5013d4a..6c08250 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java @@ -1,22 +1,14 @@ package at.ac.tuwien.sbc.valesriegler.driver.jms.messageListeners; -import javax.jms.Connection; -import javax.jms.DeliveryMode; -import javax.jms.JMSException; -import javax.jms.Message; -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.Util; import at.ac.tuwien.sbc.valesriegler.cook.actions.DeliveryOrderInfo; import at.ac.tuwien.sbc.valesriegler.driver.actions.OrderDeliveredInfo; import at.ac.tuwien.sbc.valesriegler.driver.jms.JMSDriver; +import org.apache.activemq.ActiveMQConnectionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.jms.*; /** * diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java index 38aa521..fd21703 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java @@ -1,13 +1,12 @@ package at.ac.tuwien.sbc.valesriegler.group; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DeliveryGroup implements Runnable, HasId { private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class); 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 8bce9d4..eaf9545 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 @@ -29,6 +29,16 @@ public class DeliveryOverviewModel extends TableModel { return COLUMNS; } + @Override + public void addItems(List newItems) { + log.info("addItems()"); + super.addItems(newItems); + for (DeliveryGroup g : newItems) { + if (Util.useJMS) + g.orderSomeFood(); + } + } + @Override public Object getValueAt(int rowIndex, int columnIndex) { List values = new ArrayList<>(items.values()); -- 2.43.0 From 7feab3b9cd4d0e058d445b861a731d378f46ace8 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 10 Jun 2013 18:26:02 +0200 Subject: [PATCH 12/16] [JMS] PizzeriaGUI now displays DeliveryOrders. --- .../DeliveryOrdersToCook.java | 21 ++- .../CookedDeliveryOrders.java | 60 ++++--- .../sbc/valesriegler/group/DeliveryGroup.java | 6 +- .../gui/tablemodels/DeliveryOrdersModel.java | 151 +++++++++++------- .../pizzeria/jms/JMSPizzeriaConnector.java | 31 +++- 5 files changed, 180 insertions(+), 89 deletions(-) diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java index dc26967..3495fd1 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java @@ -1,5 +1,18 @@ package at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners; +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; +import javax.jms.Message; +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.cook.actions.DeliveryOrderInfo; import at.ac.tuwien.sbc.valesriegler.cook.jms.JMSCook; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; @@ -7,11 +20,6 @@ import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; import at.ac.tuwien.sbc.valesriegler.types.Pizza; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; -import org.apache.activemq.ActiveMQConnectionFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.jms.*; /** * Cook the requested pizza. @@ -41,6 +49,7 @@ public class DeliveryOrdersToCook implements MessageListener { for (PizzaOrder pizzaorder : dor.getDeliveryGroupData().getOrder().getOrderedPizzas()) { pizzaorder.setStatus(PizzaOrderStatus.IN_PREPARATION); + pizzaorder.setCookId(cook.getId()); } ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(cook.getCONNECTSTRING()); @@ -70,7 +79,7 @@ public class DeliveryOrdersToCook implements MessageListener { prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); prod.send(session.createObjectMessage(doi)); - // inform pizeria + // inform pizzeria prod = session.createProducer(session.createQueue("PizzeriaConnector")); prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // WTF: oi = new OrderInfo(orderrequest.getGroupdata(), cook.getId()); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java index 6c08250..060f876 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java @@ -1,14 +1,23 @@ package at.ac.tuwien.sbc.valesriegler.driver.jms.messageListeners; -import at.ac.tuwien.sbc.valesriegler.common.Util; -import at.ac.tuwien.sbc.valesriegler.cook.actions.DeliveryOrderInfo; -import at.ac.tuwien.sbc.valesriegler.driver.actions.OrderDeliveredInfo; -import at.ac.tuwien.sbc.valesriegler.driver.jms.JMSDriver; +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; +import javax.jms.Message; +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 javax.jms.*; +import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.cook.actions.DeliveryOrderInfo; +import at.ac.tuwien.sbc.valesriegler.driver.actions.OrderDeliveredInfo; +import at.ac.tuwien.sbc.valesriegler.driver.jms.JMSDriver; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus; /** * @@ -39,28 +48,39 @@ public class CookedDeliveryOrders implements MessageListener { // generate random delay Thread.sleep((long) (Math.random() * 10000)); String addr = doi.getDeliveryGroupData().getAddress(); + boolean delivered = false; + OrderDeliveredInfo odi = new OrderDeliveredInfo(doi.getDeliveryGroupData(), driver.getId()); // Deliver to destination broker - ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Util.JMS_DELIVERY_DESTINATION); - Connection connection = connectionFactory.createConnection(); - connection.start(); - log.debug("CookedDelivery:ActiveMQConnectionFactory::" + connection); + try { + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Util.JMS_DELIVERY_DESTINATION); + Connection connection = connectionFactory.createConnection(); + connection.start(); + log.debug("CookedDelivery:ActiveMQConnectionFactory::" + connection); - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageProducer prod = session.createProducer(session.createQueue(addr)); - prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - OrderDeliveredInfo odi = new OrderDeliveredInfo(doi.getDeliveryGroupData(), driver.getId()); - prod.send(session.createObjectMessage(odi)); - session.close(); - connection.close(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer prod = session.createProducer(session.createQueue(addr)); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + odi.getDeliveryGroupData().setDeliveryStatus(DeliveryStatus.DELIVERED); + prod.send(session.createObjectMessage(odi)); + session.close(); + connection.close(); + delivered = true; + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + + if (!delivered) { + odi.getDeliveryGroupData().setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED); + } // Inform pizzeria - connectionFactory = new ActiveMQConnectionFactory(driver.getCONNECTSTRING()); - connection = connectionFactory.createConnection(); + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(driver.getCONNECTSTRING()); + Connection connection = connectionFactory.createConnection(); connection.start(); - session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - prod = session.createProducer(session.createQueue("PizzeriaConnector")); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer prod = session.createProducer(session.createQueue("PizzeriaConnector")); prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); prod.send(session.createObjectMessage(odi)); session.close(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java index fd21703..66e447f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java @@ -1,12 +1,13 @@ package at.ac.tuwien.sbc.valesriegler.group; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class DeliveryGroup implements Runnable, HasId { private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class); @@ -44,6 +45,7 @@ public class DeliveryGroup implements Runnable, HasId { case START : // should not happen. break; case ORDER_PENDING : + getDeliveryGroupData().getOrder().genId(); JMSGroupConnector conn = JMSGroupConnector.getConnector(deliveryGroupData.getPizzeriaId()); conn.send(new DeliveryOrderRequest(deliveryGroupData)); break; 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 336bfd3..9335d60 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,74 +1,105 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; -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 java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; +import at.ac.tuwien.sbc.valesriegler.types.Order; + public class DeliveryOrdersModel extends AbstractOrdersModel { - private static final Logger log = LoggerFactory.getLogger(DeliveryOrdersModel.class); + private static final Logger log = LoggerFactory.getLogger(DeliveryOrdersModel.class); - private static final String ID = "Delivery ID"; - private static final String ORDER_ID = "Order ID"; - private static final String DELIVERY_ADDRESS = "Address"; - private static final String STATUS = "Status"; - private static final String LOAD_BALANCER_ID = "Moved by"; - private static final String ORIGINAL_PIZZERIA = "Moved from"; + private static final String ID = "Delivery ID"; + private static final String ORDER_ID = "Order ID"; + private static final String DELIVERY_ADDRESS = "Address"; + private static final String STATUS = "Status"; + private static final String LOAD_BALANCER_ID = "Moved by"; + private static final String ORIGINAL_PIZZERIA = "Moved from"; - private static final String[] COLUMNS = new String[] { ORDER_ID, 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() { - return COLUMNS; - } + @Override + protected String[] getColumns() { + return COLUMNS; + } - @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; - } - }); - DeliveryGroupData group = values.get(rowIndex); - Order order = group.getOrder(); - String wantedColumn = COLUMNS[columnIndex]; - switch (wantedColumn) { - case ID: - return group.getId(); - case ORDER_ID: - return order.getId(); - case DELIVERY_ADDRESS: - return group.getAddress(); - case STATUS: - return group.getDeliveryStatus(); - case LOAD_BALANCER_ID: - return group.getLoadBalancerId(); - case ORIGINAL_PIZZERIA: - return group.getOriginalPizzeriaId(); - default: - throw new RuntimeException(UNHANDLEDCOLUMN); - } - } + @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; + } + }); + DeliveryGroupData group = values.get(rowIndex); + Order order = group.getOrder(); + String wantedColumn = COLUMNS[columnIndex]; + switch (wantedColumn) { + case ID : + return group.getId(); + case ORDER_ID : + return order.getId(); + case DELIVERY_ADDRESS : + return group.getAddress(); + case STATUS : + return group.getDeliveryStatus(); + case LOAD_BALANCER_ID : + return group.getLoadBalancerId(); + case ORIGINAL_PIZZERIA : + return group.getOriginalPizzeriaId(); + default : + throw new RuntimeException(UNHANDLEDCOLUMN); + } + } - public DeliveryGroupData getGroupOfRow(int rowIndex) { - return new ArrayList<>(items.values()).get(rowIndex); - } + 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; + } + }); + return values.get(rowIndex); + // return new ArrayList<>(items.values()).get(rowIndex); + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java index 2c4e44b..20b0942 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java @@ -17,7 +17,10 @@ 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.cook.actions.OrderInfo; +import at.ac.tuwien.sbc.valesriegler.driver.actions.OrderDeliveredInfo; +import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderResponse; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderResponse; import at.ac.tuwien.sbc.valesriegler.group.actions.PayResponse; import at.ac.tuwien.sbc.valesriegler.group.actions.TableFree; @@ -123,6 +126,33 @@ public class JMSPizzeriaConnector extends AbstractPizzeriaConnector implements M t.setGroupId(-1); PizzeriaAgent.getInstance().getTablesModel().fireTableDataChanged(); 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()); + PizzeriaAgent.getInstance().getDeliveryOrdersModel().addItems(gd); + return; + + } else if (obj instanceof DeliveryOrderInfo) { + DeliveryOrderInfo deliveryOrderInfo = (DeliveryOrderInfo) obj; + log.debug("Received: " + deliveryOrderInfo); + ArrayList gd = new ArrayList(); + gd.add(deliveryOrderInfo.getDeliveryGroupData()); + PizzeriaAgent.getInstance().getDeliveryOrdersModel().addItems(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()); + PizzeriaAgent.getInstance().getDeliveryOrdersModel().addItems(gd); + return; + } log.warn("Unknown message received!" + obj); } @@ -132,7 +162,6 @@ public class JMSPizzeriaConnector extends AbstractPizzeriaConnector implements M log.error("EXCEPTION!", e); } } - @Override public void send(AbstractAction request) { try { -- 2.43.0 From 812428d5925226f28f0d6d4f274df3630dcc156b Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 10 Jun 2013 18:29:35 +0200 Subject: [PATCH 13/16] [JMS] added IF benchmark to Thread.sleep --- .../DeliveryOrdersToCook.java | 5 +++- .../jms/messageListeners/OrdersToCook.java | 23 ++++++++++++++----- .../CookedDeliveryOrders.java | 4 +++- .../sbc/valesriegler/group/DeliveryGroup.java | 5 +++- .../tuwien/sbc/valesriegler/group/Group.java | 5 +++- .../jms/messageListeners/CookedOrders.java | 5 +++- .../jms/messageListeners/WantADelivery.java | 5 +++- .../jms/messageListeners/WantToOrder.java | 5 +++- .../jms/messageListeners/WantToPay.java | 5 +++- .../messageListeners/WantToSitAtTable.java | 5 +++- 10 files changed, 52 insertions(+), 15 deletions(-) diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java index 3495fd1..2f68607 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java @@ -13,6 +13,7 @@ import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.cook.actions.DeliveryOrderInfo; import at.ac.tuwien.sbc.valesriegler.cook.jms.JMSCook; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; @@ -66,7 +67,9 @@ public class DeliveryOrdersToCook implements MessageListener { // generate delay for (PizzaOrder po : dor.getDeliveryGroupData().getOrder().getOrderedPizzas()) { - Thread.sleep(po.getPizzaType().duration * 1000); + if (!Util.runSimulation) { + Thread.sleep(po.getPizzaType().duration * 1000); + } po.setStatus(PizzaOrderStatus.DONE); Pizza p = Pizza.createPizzaFromPizzaOrder(po, cook.getId(), false); dor.getDeliveryGroupData().getOrder().getCookedPizzas().add(p); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java index a3facf7..aa08d0f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java @@ -1,5 +1,19 @@ package at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners; +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; +import javax.jms.Message; +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.Util; import at.ac.tuwien.sbc.valesriegler.cook.actions.OrderInfo; import at.ac.tuwien.sbc.valesriegler.cook.jms.JMSCook; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; @@ -7,11 +21,6 @@ import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; import at.ac.tuwien.sbc.valesriegler.types.Pizza; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; -import org.apache.activemq.ActiveMQConnectionFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.jms.*; /** * Cook the requested pizza. @@ -57,7 +66,9 @@ public class OrdersToCook implements MessageListener { // generate delay for (PizzaOrder po : orderrequest.getGroupdata().getOrder().getOrderedPizzas()) { - Thread.sleep(po.getPizzaType().duration * 1000); + if (!Util.runSimulation) { + Thread.sleep(po.getPizzaType().duration * 1000); + } po.setStatus(PizzaOrderStatus.DONE); Pizza p = Pizza.createPizzaFromPizzaOrder(po, cook.getId(), false); orderrequest.getGroupdata().getOrder().getCookedPizzas().add(p); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java index 060f876..16e76fc 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java @@ -46,7 +46,9 @@ public class CookedDeliveryOrders implements MessageListener { log.debug("Received: " + doi); // generate random delay - Thread.sleep((long) (Math.random() * 10000)); + if (!Util.runSimulation) { + Thread.sleep((long) (Math.random() * 10000)); + } String addr = doi.getDeliveryGroupData().getAddress(); boolean delivered = false; OrderDeliveredInfo odi = new OrderDeliveredInfo(doi.getDeliveryGroupData(), driver.getId()); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java index 66e447f..865caa7 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java @@ -4,6 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; @@ -40,7 +41,9 @@ public class DeliveryGroup implements Runnable, HasId { public void run() { log.debug("Thread started for:" + this); try { - Thread.sleep((long) (Math.random() * 10000)); + if (!Util.runSimulation) { + Thread.sleep((long) (Math.random() * 10000)); + } switch (deliveryGroupData.getDeliveryStatus()) { case START : // should not happen. break; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java index 13b2b83..49e19cb 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java @@ -4,6 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.PayRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.TableFree; @@ -39,7 +40,9 @@ public class Group implements Runnable, HasId { public void run() { log.debug("Thread started for:" + this); try { - Thread.sleep((long) (Math.random() * 10000)); + if (!Util.runSimulation) { + Thread.sleep((long) (Math.random() * 10000)); + } switch (groupData.getState()) { case NEW : diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/CookedOrders.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/CookedOrders.java index d7cb5f5..094f0f8 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/CookedOrders.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/CookedOrders.java @@ -13,6 +13,7 @@ import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.cook.actions.OrderInfo; import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; import at.ac.tuwien.sbc.valesriegler.waiter.actions.DeliverOrder; @@ -46,7 +47,9 @@ public class CookedOrders implements MessageListener { log.debug("Received: " + orderinfo); // generate random delay - Thread.sleep((long) (Math.random() * 10000)); + if (!Util.runSimulation) { + Thread.sleep((long) (Math.random() * 10000)); + } ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(waiter.getCONNECTSTRING()); Connection connection = connectionFactory.createConnection(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java index e9d0f0d..3e02f9c 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java @@ -13,6 +13,7 @@ import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderResponse; import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; @@ -45,7 +46,9 @@ public class WantADelivery implements MessageListener { log.debug("Received: " + dor); // generate random delay - Thread.sleep((long) (Math.random() * 10000)); + if (!Util.runSimulation) { + Thread.sleep((long) (Math.random() * 10000)); + } ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(waiter.getCONNECTSTRING()); Connection connection = connectionFactory.createConnection(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java index 77f4711..78a0ec8 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java @@ -13,6 +13,7 @@ import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderResponse; import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; @@ -45,7 +46,9 @@ public class WantToOrder implements MessageListener { log.debug("Received: " + orderrequest); // generate random delay - Thread.sleep((long) (Math.random() * 10000)); + if (!Util.runSimulation) { + Thread.sleep((long) (Math.random() * 10000)); + } ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(waiter.getCONNECTSTRING()); Connection connection = connectionFactory.createConnection(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java index 6776695..b89c6b3 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java @@ -13,6 +13,7 @@ import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.actions.PayRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.PayResponse; import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; @@ -45,7 +46,9 @@ public class WantToPay implements MessageListener { log.debug("Received: " + payrequest); // generate random delay - Thread.sleep((long) (Math.random() * 10000)); + if (!Util.runSimulation) { + Thread.sleep((long) (Math.random() * 10000)); + } ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(waiter.getCONNECTSTRING()); Connection connection = connectionFactory.createConnection(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToSitAtTable.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToSitAtTable.java index a62e151..71c9cb7 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToSitAtTable.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToSitAtTable.java @@ -14,6 +14,7 @@ import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.TableResponse; import at.ac.tuwien.sbc.valesriegler.types.Table; @@ -47,7 +48,9 @@ public class WantToSitAtTable implements MessageListener { log.debug("Received: " + tablerequest); // generate random delay - Thread.sleep((long) (Math.random() * 10000)); + if (!Util.runSimulation) { + Thread.sleep((long) (Math.random() * 10000)); + } ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(waiter.getCONNECTSTRING()); Connection connection = connectionFactory.createConnection(); -- 2.43.0 From 87a4e498d729e9267b08b2dee387b1529cc2f940 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 10 Jun 2013 19:24:41 +0200 Subject: [PATCH 14/16] [JMS] Benchmarks are theoretically possible now. GroupGUI responsesmissing, so it wont work. --- runJMS.sh | 3 +- .../tuwien/sbc/valesriegler/common/Util.java | 34 +++++++---- .../sbc/valesriegler/group/GroupAgent.java | 56 +++++++++++++++++-- .../group/jms/GroupJMSNACMsgListener.java | 12 +++- .../group/jms/JMSGroupConnector.java | 4 ++ .../sbc/valesriegler/jms/nac/JMSNAC.java | 1 + .../jms/nac/actions/BenchmarkStart.java | 17 ++++++ .../jms/nac/actions/BenchmarkStop.java | 17 ++++++ .../sbc/valesriegler/waiter/WaiterAgent.java | 8 ++- .../valesriegler/waiter/jms/JMSWaiter.java | 24 +++++++- .../waiter/jms/WaiterJMSNACMsgListener.java | 53 ++++++++++++++++++ 11 files changed, 208 insertions(+), 21 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/BenchmarkStart.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/BenchmarkStop.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/WaiterJMSNACMsgListener.java diff --git a/runJMS.sh b/runJMS.sh index 7bf81ca..d058c11 100755 --- a/runJMS.sh +++ b/runJMS.sh @@ -25,8 +25,9 @@ echo "*** continuing launch process ***" # Run Groupgui -mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.group.GroupAgent" -Dexec.args="JMS tcp://localhost:61610?jms.prefetchPolicy.all=1" & +#mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.group.GroupAgent" -Dexec.args="JMS tcp://localhost:61610?jms.prefetchPolicy.all=1" & #run pizzeria1 ./runJMSPizzaria.sh "tcp://localhost:61621?jms.prefetchPolicy.all=1" ./runJMSPizzaria.sh "tcp://localhost:61622?jms.prefetchPolicy.all=1" + 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 36d52c0..68634e3 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,22 +1,34 @@ package at.ac.tuwien.sbc.valesriegler.common; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.ScrollPaneConstants; +import javax.swing.border.TitledBorder; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + 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; -import org.slf4j.LoggerFactory; - -import javax.swing.*; -import javax.swing.border.TitledBorder; -import java.awt.*; -import java.net.URI; -import java.util.*; -import java.util.List; public abstract class Util { private static final Logger log = LoggerFactory.getLogger(Util.class); - public static boolean useJMS = false; + public static boolean useJMS = true; public static final String TABLE_ASSIGNED = "tables"; public static final String ASSIGN_TABLE = "assignTable"; @@ -42,7 +54,7 @@ public abstract class Util { public static final String DELIVERY_PROGRESS = "deliveryInProgress"; public static final String DELIVERY_DONE = "deliveryDone"; - public static final boolean runSimulation = false; + public static final boolean runSimulation = true; public static final String NUMBER_DISPLAY_FORMAT = String.format("%%0%dd", 3); 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 2f8b153..106f995 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,21 +1,32 @@ package at.ac.tuwien.sbc.valesriegler.group; +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 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; import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel; import at.ac.tuwien.sbc.valesriegler.group.jms.GroupJMSNACMsgListener; +import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC; +import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStart; +import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStop; 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; - -import javax.swing.*; -import java.util.*; /** * The Main class of the Group component. @@ -61,9 +72,42 @@ public class GroupAgent { } groupAgent = new GroupAgent(); - if (! Util.runSimulation) { + if (!Util.runSimulation) { groupGui = new GroupGUI(); SwingUtilities.invokeLater(groupGui); + } else { + JMSGroupConnector.getConnectors().put("tcp://localhost:61621?jms.prefetchPolicy.all=1", + new JMSGroupConnector("tcp://localhost:61621?jms.prefetchPolicy.all=1")); + JMSGroupConnector.getConnectors().put("tcp://localhost:61622?jms.prefetchPolicy.all=1", + new JMSGroupConnector("tcp://localhost:61622?jms.prefetchPolicy.all=1")); + try { + 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 = "tcp://localhost:61621?jms.prefetchPolicy.all=1"; + final String pizzeria2 = "tcp://localhost:61622?jms.prefetchPolicy.all=1"; + 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); + + Thread.sleep(20000); + jmsnac.sendNACMsg(new BenchmarkStart()); + Thread.sleep(60000); + jmsnac.sendNACMsg(new BenchmarkStop()); + } catch (InterruptedException | JMSException e) { + log.warn("EXCEPTION!", e); + } } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java index 5485fbe..70f439f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java @@ -12,6 +12,7 @@ import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; import at.ac.tuwien.sbc.valesriegler.jms.nac.AbstractJMSNACMsgListener; import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.AddressInfoRequest; import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.AddressInfoResponse; +import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStop; /** * Handles the NAC communication for the Group Agent. * @@ -35,13 +36,22 @@ public class GroupJMSNACMsgListener extends AbstractJMSNACMsgListener implements Object obj = objMsg.getObject(); if (obj instanceof AddressInfoRequest) { // DO NOTHING. + } else if (obj instanceof AddressInfoResponse) { AddressInfoResponse response = (AddressInfoResponse) obj; GroupAgent.getInstance().getPizzeriaIdentifiers().add(response.getAddress()); if (!Util.runSimulation) { GroupAgent.getGroupGui().getFrame().repaint(); + JMSGroupConnector.getConnectors().put(response.getAddress(), new JMSGroupConnector(response.getAddress())); + } + } else if (obj instanceof BenchmarkStop) { + for (String keys : JMSGroupConnector.getConnectors().keySet()) { + JMSGroupConnector c = JMSGroupConnector.getConnector(keys); + c.getConnection().close(); + GroupAgent.getInstance().getDeliveryModel().createStatistics(); } - JMSGroupConnector.getConnectors().put(response.getAddress(), new JMSGroupConnector(response.getAddress())); + } else if (obj instanceof BenchmarkStop) { + // DO NOTHING. } else { log.warn("Received unknown Object: " + obj); } 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 c382ba3..b15d33f 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 @@ -174,4 +174,8 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message return connectors.get(key); } + public Connection getConnection() { + return connection; + } + } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/JMSNAC.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/JMSNAC.java index a70f946..4605d65 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/JMSNAC.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/JMSNAC.java @@ -42,6 +42,7 @@ public class JMSNAC { } } public void sendNACMsg(AbstractNACMsg msg) throws JMSException { + log.debug("sendNACMsg():" + msg); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer prod = session.createProducer(session.createTopic(JMS_NAC_TOPIC)); prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/BenchmarkStart.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/BenchmarkStart.java new file mode 100644 index 0000000..1e00f35 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/BenchmarkStart.java @@ -0,0 +1,17 @@ +package at.ac.tuwien.sbc.valesriegler.jms.nac.actions; + +/** + * Message hat instructs everyone to reply their Address. + * + * @author jan + * + */ +public class BenchmarkStart extends AbstractNACMsg { + public BenchmarkStart() { + } + + @Override + public String toString() { + return "BenchmarkStop []"; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/BenchmarkStop.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/BenchmarkStop.java new file mode 100644 index 0000000..fc63c5a --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/BenchmarkStop.java @@ -0,0 +1,17 @@ +package at.ac.tuwien.sbc.valesriegler.jms.nac.actions; + +/** + * Message hat instructs everyone to reply their Address. + * + * @author jan + * + */ +public class BenchmarkStop extends AbstractNACMsg { + public BenchmarkStop() { + } + + @Override + public String toString() { + return "BenchmarkStop []"; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/WaiterAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/WaiterAgent.java index 0e59f52..241fe61 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/WaiterAgent.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/WaiterAgent.java @@ -15,6 +15,7 @@ import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; public class WaiterAgent { private static final String USAGE = "This application needs exactly 2 parameters: <\"XVSM\"|\"JMS\"> "; private static final Logger log = LoggerFactory.getLogger(WaiterAgent.class); + static public JMSWaiter jmsWaiter; public static void main(String[] args) { if (args.length != 3) { @@ -32,11 +33,16 @@ public class WaiterAgent { log.info("Middleware: " + mw + " ID:" + parsedId); if ("JMS".equalsIgnoreCase(mw)) { - new JMSWaiter(args[1], parsedId); + jmsWaiter = new JMSWaiter(args[1], parsedId); } else if ("XVSM".equalsIgnoreCase(mw)) { // TODO: XVSM Waiter? } else { throw new IllegalArgumentException(USAGE); } } + + public JMSWaiter getJmsWaiter() { + return jmsWaiter; + } + } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/JMSWaiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/JMSWaiter.java index 8ba3124..0d4f0c8 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/JMSWaiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/JMSWaiter.java @@ -9,6 +9,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC; import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.CookedOrders; import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantADelivery; import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToOrder; @@ -25,21 +27,33 @@ import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToSitAtTabl public class JMSWaiter implements HasId { private static final Logger log = LoggerFactory.getLogger(JMSWaiter.class); private final String CONNECTSTRING; + private Connection connection; + public String getCONNECTSTRING() { return CONNECTSTRING; } final private int id; + private JMSNAC jmsnac; public JMSWaiter(String jmsURL, int id) { CONNECTSTRING = jmsURL; this.id = id; log.info("I AM A WAITER WITH ID {}", id); + WaiterJMSNACMsgListener tmp = new WaiterJMSNACMsgListener(); + jmsnac = new JMSNAC(tmp); + + if (!Util.runSimulation) { + initJMS(); + } + } + + public void initJMS() { try { // Connecting to the Broker and to the output queue ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(CONNECTSTRING); - Connection connection = connectionFactory.createConnection(); + connection = connectionFactory.createConnection(); connection.start(); Session sessWantToSit = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); @@ -77,4 +91,12 @@ public class JMSWaiter implements HasId { public int getId() { return id; } + + public Connection getConnection() { + return connection; + } + + public void setConnection(Connection connection) { + this.connection = connection; + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/WaiterJMSNACMsgListener.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/WaiterJMSNACMsgListener.java new file mode 100644 index 0000000..f9d957d --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/WaiterJMSNACMsgListener.java @@ -0,0 +1,53 @@ +package at.ac.tuwien.sbc.valesriegler.waiter.jms; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.ObjectMessage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.jms.nac.AbstractJMSNACMsgListener; +import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.AddressInfoRequest; +import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.AddressInfoResponse; +import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStart; +import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStop; +import at.ac.tuwien.sbc.valesriegler.waiter.WaiterAgent; +/** + * Handles the NAC communication for the Group Agent. + * + * @author jan + * + */ +public class WaiterJMSNACMsgListener extends AbstractJMSNACMsgListener { + private static final Logger log = LoggerFactory.getLogger(WaiterJMSNACMsgListener.class); + + public WaiterJMSNACMsgListener() { + } + + @Override + public void onMessage(Message msg) { + try { + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + if (obj instanceof AddressInfoRequest) { + // DO NOTHING. + } else if (obj instanceof AddressInfoResponse) { + // DO NOTHING. + } else if (obj instanceof BenchmarkStop) { + WaiterAgent.jmsWaiter.getConnection().close(); + } else if (obj instanceof BenchmarkStart) { + WaiterAgent.jmsWaiter.initJMS(); + + } else { + log.warn("Received unknown Object: " + obj); + } + } else { + log.warn("Received unknown Message: " + msg); + } + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + } +} -- 2.43.0 From 390af9d658b6c1d7d280f48c06fbdca090138855 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 10 Jun 2013 20:07:48 +0200 Subject: [PATCH 15/16] [JMS] GroupGui now receives DeliveryOrder info --- .../DeliveryOrdersToCook.java | 7 + .../CookedDeliveryOrders.java | 6 + .../group/actions/DeliveryOrderResponse.java | 2 +- .../group/gui/DeliveryOverviewModel.java | 191 ++++++++-------- .../group/jms/JMSGroupConnector.java | 32 ++- .../valesriegler/types/DeliveryGroupData.java | 211 +++++++++--------- 6 files changed, 248 insertions(+), 201 deletions(-) diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java index 2f68607..fa46d9b 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java @@ -88,6 +88,13 @@ public class DeliveryOrdersToCook implements MessageListener { // WTF: oi = new OrderInfo(orderrequest.getGroupdata(), cook.getId()); prod.send(session.createObjectMessage(doi)); session.close(); + + // inform GroupGUI + prod = session.createProducer(session.createQueue("GroupConnector")); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + // WTF: oi = new OrderInfo(orderrequest.getGroupdata(), cook.getId()); + prod.send(session.createObjectMessage(doi)); + session.close(); connection.close(); } else { log.warn("Received unknown Object: " + obj); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java index 16e76fc..dfde528 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java @@ -86,6 +86,12 @@ public class CookedDeliveryOrders implements MessageListener { prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); prod.send(session.createObjectMessage(odi)); session.close(); + + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + prod = session.createProducer(session.createQueue("GroupConnector")); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + prod.send(session.createObjectMessage(odi)); + session.close(); connection.close(); } else { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderResponse.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderResponse.java index ea0e830..7b8493f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderResponse.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderResponse.java @@ -21,7 +21,7 @@ public class DeliveryOrderResponse extends AbstractDeliveryAction implements Ser @Override public String toString() { - return "DeliveryOrderResponse [getDeliveryGroupData()=" + getDeliveryGroupData() + "]"; + return "DeliveryOrderResponse [waiterId=" + waiterId + ", getDeliveryGroupData()=" + getDeliveryGroupData() + "]"; } public int getWaiterId() { 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 eaf9545..562ab71 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,114 +1,113 @@ 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); - - private static final String STATE = "State"; - private static final String PIZZAS = "Pizzas"; - private static final String ADDRESS = "Address"; - private static final String ID = "ID"; - private static final String PIZZERIA = "Pizzeria"; - private static final String[] COLUMNS = new String[]{ID, ADDRESS, PIZZAS, STATE, PIZZERIA}; + private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class); + private static final String STATE = "State"; + private static final String PIZZAS = "Pizzas"; + private static final String ADDRESS = "Address"; + private static final String ID = "ID"; + private static final String PIZZERIA = "Pizzeria"; + private static final String[] COLUMNS = new String[]{ID, ADDRESS, PIZZAS, STATE, PIZZERIA}; - @Override - protected String[] getColumns() { - return COLUMNS; - } + @Override + protected String[] getColumns() { + return COLUMNS; + } - @Override - public void addItems(List newItems) { - log.info("addItems()"); - super.addItems(newItems); - for (DeliveryGroup g : newItems) { - if (Util.useJMS) - g.orderSomeFood(); - } - } + @Override + public void addItems(List newItems) { + log.info("addItems()"); + super.addItems(newItems); + for (DeliveryGroup g : newItems) { + if (Util.useJMS) + g.orderSomeFood(); + } + } - @Override - public Object getValueAt(int rowIndex, int columnIndex) { - List values = new ArrayList<>(items.values()); - DeliveryGroup group = values.get(rowIndex); - String wantedColumn = COLUMNS[columnIndex]; - switch (wantedColumn) { - case ID: - int groupId = group.getId(); - return groupId; - case ADDRESS: - return group.getDeliveryGroupData().getAddress(); - case PIZZAS: - if (group.getDeliveryGroupData().getOrder() == null) - return "none"; - return Util.pizzaDisplay(group.getDeliveryGroupData().getOrder().getOrderedPizzas()); - case STATE: - return group.getDeliveryGroupData().getDeliveryStatus(); - case PIZZERIA: - return group.getDeliveryGroupData().getPizzeriaId(); - default: - throw new RuntimeException(UNHANDLEDCOLUMN); - } - } + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + List values = new ArrayList<>(items.values()); + DeliveryGroup group = values.get(rowIndex); + String wantedColumn = COLUMNS[columnIndex]; + switch (wantedColumn) { + case ID : + int groupId = group.getId(); + return groupId; + case ADDRESS : + return group.getDeliveryGroupData().getAddress(); + case PIZZAS : + if (group.getDeliveryGroupData().getOrder() == null) + return "none"; + return Util.pizzaDisplay(group.getDeliveryGroupData().getOrder().getOrderedPizzas()); + case STATE : + return group.getDeliveryGroupData().getDeliveryStatus(); + case PIZZERIA : + return group.getDeliveryGroupData().getPizzeriaId(); + default : + throw new RuntimeException(UNHANDLEDCOLUMN); + } + } - public void addDeliveries(List groups) { - synchronized (items) { - for (DeliveryGroupData group : groups) { - final DeliveryGroup deliveryGroup = items.get(group.getId()); - if(deliveryGroup==null) { - log.error("Delivery group not found: {}", group.getId()); - } - else deliveryGroup.setDeliveryGroupData(group); - } - } - fireTableDataChanged(); - } + public void addDeliveries(List groups) { + synchronized (items) { + for (DeliveryGroupData group : groups) { + final DeliveryGroup deliveryGroup = items.get(group.getId()); + if (deliveryGroup == null) { + log.error("Delivery group not found: {}", group.getId()); + } else + deliveryGroup.setDeliveryGroupData(group); + } + } + fireTableDataChanged(); + } - public void createStatistics() { - int size; - int finished; - synchronized (items) { - final Collection values = items.values(); - size = values.size(); - finished = 0; - for (DeliveryGroup group : values) { - final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); - if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { - finished++; - } - } - } - log.info("{} deliveries were ordered", size); - log.info("{} were finished", finished); - } + public void createStatistics() { + int size; + int finished; + synchronized (items) { + final Collection values = items.values(); + size = values.size(); + finished = 0; + for (DeliveryGroup group : values) { + final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); + if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { + finished++; + } + } + } + log.warn("{} deliveries were ordered", size); + log.warn("{} were finished", finished); + } - public boolean hasFinished() { - int size; - int finished; - synchronized (items) { - final Collection values = items.values(); - size = values.size(); - finished = 0; - for (DeliveryGroup group : values) { - final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); - if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { - finished++; - } - } - } - final boolean result = size == finished; - return result; - } + public boolean hasFinished() { + int size; + int finished; + synchronized (items) { + final Collection values = items.values(); + size = values.size(); + finished = 0; + for (DeliveryGroup group : values) { + final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); + if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { + finished++; + } + } + } + final boolean result = size == finished; + return result; + } } 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 b15d33f..ddafe41 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,5 +1,6 @@ package at.ac.tuwien.sbc.valesriegler.group.jms; +import java.util.ArrayList; import java.util.HashMap; import javax.jms.Connection; @@ -17,10 +18,13 @@ 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; @@ -28,6 +32,7 @@ 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.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.waiter.actions.DeliverOrder; /** @@ -95,10 +100,35 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(payresponse.getGroupdata().getId()); g.payForPizzas(payresponse.getWaiterId()); return; + + } else if (obj instanceof DeliveryOrderResponse) { + DeliveryOrderResponse deliveryOrderResponse = (DeliveryOrderResponse) obj; + log.warn("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.warn("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.warn("Received: " + orderDeliveredInfo); + orderDeliveredInfo.getDeliveryGroupData().setDriverId(orderDeliveredInfo.getDriverId()); + ArrayList gd = new ArrayList(); + gd.add(orderDeliveredInfo.getDeliveryGroupData()); + GroupAgent.getInstance().getDeliveryModel().addDeliveries(gd); + return; } } - log.warn("Unknown messagetype received!"); } catch (JMSException e) { log.error("EXCEPTION!", e); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryGroupData.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryGroupData.java index 9187c62..fbf81df 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryGroupData.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryGroupData.java @@ -1,113 +1,118 @@ package at.ac.tuwien.sbc.valesriegler.types; -import at.ac.tuwien.sbc.valesriegler.common.HasId; -import at.ac.tuwien.sbc.valesriegler.common.Util; +import java.io.Serializable; + import org.mozartspaces.capi3.Queryable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.Serializable; +import at.ac.tuwien.sbc.valesriegler.common.Util; @Queryable(autoindex = true) public class DeliveryGroupData implements Serializable, HasOrder { - private static final Logger log = LoggerFactory.getLogger(GroupData.class); - - private Integer id; - private Order order; - private String address; - private DeliveryStatus deliveryStatus = DeliveryStatus.START; - private String pizzeriaId; - - private Integer waiterIdOfOrder; - private Integer driverId; - - /** - * These fields are set only when the delivery order was shifted by a load balancer from one pizzeria to another - */ - private Integer loadBalancerId; - private String originalPizzeriaId; - - public DeliveryGroupData(int id) { - this.id = id; - } - - public DeliveryGroupData() { - //To change body of created methods use File | Settings | File Templates. - } - - public void setId(Integer id) { - this.id = id; - } - - public Order getOrder() { - return order; - } - - public void setOrder(Order order) { - this.order = order; - } - - public String getAddress() { - return address; - } - - public String getOriginalPizzeriaId() { - return originalPizzeriaId; - } - - public int getDriverId() { - return Util.getIntSafe(driverId); - } - - public void setDriverId(Integer driverId) { - this.driverId = driverId; - } - - public Integer getWaiterIdOfOrder() { - return waiterIdOfOrder; - } - - public void setWaiterIdOfOrder(Integer waiterIdOfOrder) { - this.waiterIdOfOrder = waiterIdOfOrder; - } - - - public void setOriginalPizzeriaId(String originalPizzeriaId) { - this.originalPizzeriaId = originalPizzeriaId; - } - - - public void setAddress(String address) { - this.address = address; - } - - public DeliveryStatus getDeliveryStatus() { - return deliveryStatus; - } - - public void setDeliveryStatus(DeliveryStatus deliveryStatus) { - this.deliveryStatus = deliveryStatus; - } - - public int getLoadBalancerId() { - return Util.getIntSafe(loadBalancerId); - } - - public void setLoadBalancerId(Integer loadBalancerId) { - this.loadBalancerId = loadBalancerId; - } - - - public String getPizzeriaId() { - return pizzeriaId; - } - - public void setPizzeriaId(String pizzeriaId) { - this.pizzeriaId = pizzeriaId; - } - - @Override - public int getId() { - return Util.getIntSafe(id); - } + private static final Logger log = LoggerFactory.getLogger(GroupData.class); + + private Integer id; + private Order order; + private String address; + private DeliveryStatus deliveryStatus = DeliveryStatus.START; + private String pizzeriaId; + + private Integer waiterIdOfOrder; + private Integer driverId; + + /** + * These fields are set only when the delivery order was shifted by a load balancer from one pizzeria to another + */ + private Integer loadBalancerId; + private String originalPizzeriaId; + + public DeliveryGroupData(int id) { + this.id = id; + } + + public DeliveryGroupData() { + // To change body of created methods use File | Settings | File Templates. + } + + public void setId(Integer id) { + this.id = id; + } + + @Override + public Order getOrder() { + return order; + } + + public void setOrder(Order order) { + this.order = order; + } + + public String getAddress() { + return address; + } + + public String getOriginalPizzeriaId() { + return originalPizzeriaId; + } + + public int getDriverId() { + return Util.getIntSafe(driverId); + } + + public void setDriverId(Integer driverId) { + this.driverId = driverId; + } + + public Integer getWaiterIdOfOrder() { + return waiterIdOfOrder; + } + + public void setWaiterIdOfOrder(Integer waiterIdOfOrder) { + this.waiterIdOfOrder = waiterIdOfOrder; + } + + public void setOriginalPizzeriaId(String originalPizzeriaId) { + this.originalPizzeriaId = originalPizzeriaId; + } + + public void setAddress(String address) { + this.address = address; + } + + public DeliveryStatus getDeliveryStatus() { + return deliveryStatus; + } + + public void setDeliveryStatus(DeliveryStatus deliveryStatus) { + this.deliveryStatus = deliveryStatus; + } + + public int getLoadBalancerId() { + return Util.getIntSafe(loadBalancerId); + } + + public void setLoadBalancerId(Integer loadBalancerId) { + this.loadBalancerId = loadBalancerId; + } + + public String getPizzeriaId() { + return pizzeriaId; + } + + public void setPizzeriaId(String pizzeriaId) { + this.pizzeriaId = pizzeriaId; + } + + @Override + public int getId() { + return Util.getIntSafe(id); + } + + @Override + public String toString() { + return "DeliveryGroupData [id=" + id + ", order=" + order + ", address=" + address + ", deliveryStatus=" + + deliveryStatus + ", pizzeriaId=" + pizzeriaId + ", waiterIdOfOrder=" + waiterIdOfOrder + ", driverId=" + + driverId + ", loadBalancerId=" + loadBalancerId + ", originalPizzeriaId=" + originalPizzeriaId + "]"; + } } -- 2.43.0 From fc2664fbaea8480c5fdd638d6785a25df496eae5 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 10 Jun 2013 20:38:47 +0200 Subject: [PATCH 16/16] [JMS] Benchmark works somehow. (its run twice :/ ) --- .../tuwien/sbc/valesriegler/group/GroupAgent.java | 4 +++- .../group/jms/GroupJMSNACMsgListener.java | 14 +++++++++++++- .../valesriegler/group/jms/JMSGroupConnector.java | 6 +++--- 3 files changed, 19 insertions(+), 5 deletions(-) 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 106f995..096ed93 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 @@ -101,10 +101,12 @@ public class GroupAgent { GroupAgent.getInstance().onDeliveryGroupsCreated(groups3); GroupAgent.getInstance().onDeliveryGroupsCreated(groups1); + log.warn("Starting benchmark in 20 sec"); Thread.sleep(20000); jmsnac.sendNACMsg(new BenchmarkStart()); Thread.sleep(60000); jmsnac.sendNACMsg(new BenchmarkStop()); + log.warn("Benchmark Stop Signal sent."); } catch (InterruptedException | JMSException e) { log.warn("EXCEPTION!", e); } @@ -134,7 +136,7 @@ public class GroupAgent { 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, + final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 1000, pizzaTypes1, fakeAddress, pizzeria); for (int i = 0; i < gc.numberOfGroups; i++) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java index 70f439f..0d45921 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java @@ -12,6 +12,7 @@ import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; import at.ac.tuwien.sbc.valesriegler.jms.nac.AbstractJMSNACMsgListener; import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.AddressInfoRequest; import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.AddressInfoResponse; +import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStart; import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStop; /** * Handles the NAC communication for the Group Agent. @@ -35,23 +36,34 @@ public class GroupJMSNACMsgListener extends AbstractJMSNACMsgListener implements ObjectMessage objMsg = (ObjectMessage) msg; Object obj = objMsg.getObject(); if (obj instanceof AddressInfoRequest) { + log.debug("Received: " + obj); + return; // DO NOTHING. } else if (obj instanceof AddressInfoResponse) { + log.debug("Received: " + obj); AddressInfoResponse response = (AddressInfoResponse) obj; GroupAgent.getInstance().getPizzeriaIdentifiers().add(response.getAddress()); if (!Util.runSimulation) { GroupAgent.getGroupGui().getFrame().repaint(); JMSGroupConnector.getConnectors().put(response.getAddress(), new JMSGroupConnector(response.getAddress())); } + return; + } else if (obj instanceof BenchmarkStop) { + log.debug("Received: " + obj); for (String keys : JMSGroupConnector.getConnectors().keySet()) { JMSGroupConnector c = JMSGroupConnector.getConnector(keys); c.getConnection().close(); GroupAgent.getInstance().getDeliveryModel().createStatistics(); } - } else if (obj instanceof BenchmarkStop) { + return; + + } else if (obj instanceof BenchmarkStart) { + log.debug("Received: " + obj); + return; // DO NOTHING. + } else { log.warn("Received unknown Object: " + obj); } 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 ddafe41..672c872 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 @@ -103,7 +103,7 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message } else if (obj instanceof DeliveryOrderResponse) { DeliveryOrderResponse deliveryOrderResponse = (DeliveryOrderResponse) obj; - log.warn("Received: " + deliveryOrderResponse); + log.debug("Received: " + deliveryOrderResponse); deliveryOrderResponse.getDeliveryGroupData().setWaiterIdOfOrder(deliveryOrderResponse.getWaiterId()); ArrayList gd = new ArrayList(); gd.add(deliveryOrderResponse.getDeliveryGroupData()); @@ -112,7 +112,7 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message } else if (obj instanceof DeliveryOrderInfo) { DeliveryOrderInfo deliveryOrderInfo = (DeliveryOrderInfo) obj; - log.warn("Received: " + deliveryOrderInfo); + log.debug("Received: " + deliveryOrderInfo); ArrayList gd = new ArrayList(); gd.add(deliveryOrderInfo.getDeliveryGroupData()); GroupAgent.getInstance().getDeliveryModel().addDeliveries(gd); @@ -120,7 +120,7 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message } else if (obj instanceof OrderDeliveredInfo) { OrderDeliveredInfo orderDeliveredInfo = (OrderDeliveredInfo) obj; - log.warn("Received: " + orderDeliveredInfo); + log.debug("Received: " + orderDeliveredInfo); orderDeliveredInfo.getDeliveryGroupData().setDriverId(orderDeliveredInfo.getDriverId()); ArrayList gd = new ArrayList(); gd.add(orderDeliveredInfo.getDeliveryGroupData()); -- 2.43.0