From 240ea88e8d8fcbb74818bb9b679c7ac417573669 Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 10 Jun 2013 00:52:55 +0200 Subject: [PATCH] [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