From 87a4e498d729e9267b08b2dee387b1529cc2f940 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 10 Jun 2013 19:24:41 +0200 Subject: [PATCH] [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