1 package at.ac.tuwien.sbc.valesriegler.group;
3 import java.lang.management.ManagementFactory;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.HashSet;
11 import javax.jms.JMSException;
12 import javax.swing.SwingUtilities;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import at.ac.tuwien.sbc.valesriegler.common.Util;
18 import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel;
19 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupCreationDetailsRequest;
20 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel;
21 import at.ac.tuwien.sbc.valesriegler.group.jms.GroupJMSNACMsgListener;
22 import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector;
23 import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC;
24 import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStart;
25 import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStop;
26 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
27 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
28 import at.ac.tuwien.sbc.valesriegler.types.Order;
29 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
30 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
33 * The Main class of the Group component.
35 * Start the communication and the group GUI:
37 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
40 public class GroupAgent {
41 private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
43 private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
44 private GroupOverviewModel groupModel;
45 private DeliveryOverviewModel deliveryOverviewModel;
46 private GroupAgentXVSM xvsm;
47 static private JMSNAC jmsnac;
48 static private GroupGUI groupGui;
50 * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
51 * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
52 * selecting for which pizzeria the customer groups are created
53 * In case of XVSM the identifier string is a port number
55 private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
57 public static GroupAgent groupAgent;
59 public static void main(String[] args) {
60 if (args.length < 1) {
61 throw new IllegalArgumentException(USAGE);
65 log.warn("Middleware: " + mw);
66 log.warn("JVM: " + ManagementFactory.getRuntimeMXBean().getName());
67 log.warn("THREAD: " + Thread.currentThread().getName());
69 if ("JMS".equalsIgnoreCase(mw)) {
71 } else if ("XVSM".equalsIgnoreCase(mw)) {
74 throw new IllegalArgumentException(USAGE);
77 groupAgent = new GroupAgent();
78 if (!Util.runSimulation) {
79 groupGui = new GroupGUI();
80 SwingUtilities.invokeLater(groupGui);
82 JMSGroupConnector.getConnectors().put("tcp://localhost:61621?jms.prefetchPolicy.all=1",
83 new JMSGroupConnector("tcp://localhost:61621?jms.prefetchPolicy.all=1"));
84 JMSGroupConnector.getConnectors().put("tcp://localhost:61622?jms.prefetchPolicy.all=1",
85 new JMSGroupConnector("tcp://localhost:61622?jms.prefetchPolicy.all=1"));
87 List<PizzaType> pizzaTypes1 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA,
88 PizzaType.MARGHERITA);
89 List<PizzaType> pizzaTypes2 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA);
90 List<PizzaType> pizzaTypes3 = Arrays.asList(PizzaType.SALAMI, PizzaType.MARGHERITA);
92 final String pizzeria1 = "tcp://localhost:61621?jms.prefetchPolicy.all=1";
93 final String pizzeria2 = "tcp://localhost:61622?jms.prefetchPolicy.all=1";
94 List<DeliveryGroup> groups1 = GroupAgent.getInstance().createGroups(pizzaTypes1, pizzeria1, 4);
95 List<DeliveryGroup> groups2 = GroupAgent.getInstance().createGroups(pizzaTypes2, pizzeria2, 3);
96 List<DeliveryGroup> groups3 = GroupAgent.getInstance().createGroups(pizzaTypes3, pizzeria2, 2);
98 final DeliveryOverviewModel deliveryModel = GroupAgent.getInstance().getDeliveryModel();
99 deliveryModel.addItems(groups1);
100 deliveryModel.addItems(groups2);
101 deliveryModel.addItems(groups3);
103 GroupAgent.getInstance().onDeliveryGroupsCreated(groups2);
104 GroupAgent.getInstance().onDeliveryGroupsCreated(groups3);
105 GroupAgent.getInstance().onDeliveryGroupsCreated(groups1);
107 log.warn("Starting benchmark in 20 sec");
109 jmsnac.sendNACMsg(new BenchmarkStart());
111 jmsnac.sendNACMsg(new BenchmarkStop());
112 log.warn("Benchmark Stop Signal sent.");
113 } catch (InterruptedException | JMSException e) {
114 log.warn("EXCEPTION!", e);
119 public GroupAgent() {
120 deliveryOverviewModel = new DeliveryOverviewModel();
121 groupModel = new GroupOverviewModel();
123 GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
124 jmsnac = new JMSNAC(tmp);
127 xvsm = new GroupAgentXVSM();
129 xvsm.listenForNewPizzerias();
131 if (Util.runSimulation) {
132 xvsm.listenToPizzeria("9875");
133 xvsm.listenToPizzeria("9874");
134 xvsm.runSimulation();
139 public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
140 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
141 final String fakeAddress = "address";
142 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 1000, pizzaTypes1, fakeAddress,
145 for (int i = 0; i < gc.numberOfGroups; i++) {
146 DeliveryGroup group = new DeliveryGroup();
147 final Order order = Util.createOrder(group, gc);
148 group.getDeliveryGroupData().setOrder(order);
149 group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
150 group.getDeliveryGroupData().setAddress(gc.address);
151 newDeliveryGroups.add(group);
154 return newDeliveryGroups;
157 public static GroupAgent getInstance() {
161 public Set<String> getPizzeriaIdentifiers() {
162 return pizzeriaIdentifiers;
165 public GroupOverviewModel getGroupModel() {
169 public static GroupGUI getGroupGui() {
173 public static JMSNAC getJmsnac() {
177 public DeliveryOverviewModel getDeliveryModel() {
178 return deliveryOverviewModel;
181 public void onGroupsCreated(List<Group> newGroups) {
183 List<GroupData> groupData = new ArrayList<>();
185 for (Group group : newGroups) {
186 groupData.add(group.getGroupData());
188 final String pizzeriaId = groupData.get(0).getPizzeriaId();
190 int pizzeriaSpacePort = 0;
192 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
193 } catch (NumberFormatException e) {
194 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
196 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
198 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
200 // start the space group in a new thread
201 for (GroupData group : groupData) {
202 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
208 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
210 List<DeliveryGroupData> groupData = new ArrayList<>();
211 for (DeliveryGroup group : newDeliveryGroups) {
212 groupData.add(group.getDeliveryGroupData());
214 final String pizzeriaId = groupData.get(0).getPizzeriaId();
216 int pizzeriaSpacePort = 0;
218 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
219 } catch (NumberFormatException e) {
220 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
223 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
225 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
227 if (!Util.runSimulation) {
228 for (DeliveryGroupData deliveryGroupData : groupData) {
229 new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
230 deliveryGroupData.getAddress())).start();