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;
8 import java.util.HashSet;
11 import java.util.concurrent.atomic.AtomicLong;
13 import javax.jms.JMSException;
14 import javax.swing.SwingUtilities;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import at.ac.tuwien.sbc.valesriegler.common.Util;
20 import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel;
21 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupCreationDetailsRequest;
22 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel;
23 import at.ac.tuwien.sbc.valesriegler.group.jms.GroupJMSNACMsgListener;
24 import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector;
25 import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC;
26 import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStart;
27 import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStop;
28 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
29 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
30 import at.ac.tuwien.sbc.valesriegler.types.Order;
31 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
32 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
35 * The Main class of the Group component.
37 * Start the communication and the group GUI:
39 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
42 public class GroupAgent {
43 private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
45 private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
46 private GroupOverviewModel groupModel;
47 private DeliveryOverviewModel deliveryOverviewModel;
48 private GroupAgentXVSM xvsm;
49 static private JMSNAC jmsnac;
50 static private GroupGUI groupGui;
52 * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
53 * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
54 * selecting for which pizzeria the customer groups are created
55 * In case of XVSM the identifier string is a port number
57 private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
59 public static GroupAgent groupAgent;
64 public static AtomicLong startTime = new AtomicLong();
66 public static void main(String[] args) {
67 if (args.length < 1) {
68 throw new IllegalArgumentException(USAGE);
72 log.warn("Middleware: " + mw);
73 log.warn("JVM: " + ManagementFactory.getRuntimeMXBean().getName());
74 log.warn("THREAD: " + Thread.currentThread().getName());
76 if ("JMS".equalsIgnoreCase(mw)) {
78 } else if ("XVSM".equalsIgnoreCase(mw)) {
81 throw new IllegalArgumentException(USAGE);
84 groupAgent = new GroupAgent();
85 if (!Util.runSimulation) {
86 groupGui = new GroupGUI();
87 SwingUtilities.invokeLater(groupGui);
89 JMSGroupConnector.getConnectors().put("tcp://localhost:61621?jms.prefetchPolicy.all=1",
90 new JMSGroupConnector("tcp://localhost:61621?jms.prefetchPolicy.all=1"));
91 JMSGroupConnector.getConnectors().put("tcp://localhost:61622?jms.prefetchPolicy.all=1",
92 new JMSGroupConnector("tcp://localhost:61622?jms.prefetchPolicy.all=1"));
94 List<PizzaType> pizzaTypes1 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA,
95 PizzaType.MARGHERITA);
96 List<PizzaType> pizzaTypes2 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA);
97 List<PizzaType> pizzaTypes3 = Arrays.asList(PizzaType.SALAMI, PizzaType.MARGHERITA);
99 final String pizzeria1 = "tcp://localhost:61621?jms.prefetchPolicy.all=1";
100 final String pizzeria2 = "tcp://localhost:61622?jms.prefetchPolicy.all=1";
101 List<DeliveryGroup> groups1 = GroupAgent.getInstance().createGroups(pizzaTypes1, pizzeria1, 4);
102 List<DeliveryGroup> groups2 = GroupAgent.getInstance().createGroups(pizzaTypes2, pizzeria2, 3);
103 List<DeliveryGroup> groups3 = GroupAgent.getInstance().createGroups(pizzaTypes3, pizzeria2, 2);
105 final DeliveryOverviewModel deliveryModel = GroupAgent.getInstance().getDeliveryModel();
106 deliveryModel.addItems(groups1);
107 deliveryModel.addItems(groups2);
108 deliveryModel.addItems(groups3);
110 GroupAgent.getInstance().onDeliveryGroupsCreated(groups2);
111 GroupAgent.getInstance().onDeliveryGroupsCreated(groups3);
112 GroupAgent.getInstance().onDeliveryGroupsCreated(groups1);
115 log.warn("Starting benchmark.");
116 startTime.set(new Date().getTime());
117 jmsnac.sendNACMsg(new BenchmarkStart());
119 jmsnac.sendNACMsg(new BenchmarkStop());
120 log.warn("Benchmark Stop Signal sent.");
121 } catch (InterruptedException | JMSException e) {
122 log.warn("EXCEPTION!", e);
127 public GroupAgent() {
128 deliveryOverviewModel = new DeliveryOverviewModel();
129 groupModel = new GroupOverviewModel();
131 GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
132 jmsnac = new JMSNAC(tmp);
135 xvsm = new GroupAgentXVSM();
137 xvsm.listenForNewPizzerias();
139 if (Util.runSimulation) {
140 xvsm.listenToPizzeria("9875");
141 xvsm.listenToPizzeria("9874");
142 xvsm.runSimulation();
147 public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
148 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
149 final String fakeAddress = "address";
150 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress,
153 for (int i = 0; i < gc.numberOfGroups; i++) {
154 DeliveryGroup group = new DeliveryGroup();
155 final Order order = Util.createOrder(group, gc);
156 group.getDeliveryGroupData().setOrder(order);
157 group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
158 group.getDeliveryGroupData().setAddress(gc.address);
159 newDeliveryGroups.add(group);
162 return newDeliveryGroups;
165 public static GroupAgent getInstance() {
169 public Set<String> getPizzeriaIdentifiers() {
170 return pizzeriaIdentifiers;
173 public GroupOverviewModel getGroupModel() {
177 public static GroupGUI getGroupGui() {
181 public static JMSNAC getJmsnac() {
185 public DeliveryOverviewModel getDeliveryModel() {
186 return deliveryOverviewModel;
189 public void onGroupsCreated(List<Group> newGroups) {
191 List<GroupData> groupData = new ArrayList<>();
193 for (Group group : newGroups) {
194 groupData.add(group.getGroupData());
196 final String pizzeriaId = groupData.get(0).getPizzeriaId();
198 int pizzeriaSpacePort = 0;
200 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
201 } catch (NumberFormatException e) {
202 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
204 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
206 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
208 // start the space group in a new thread
209 for (GroupData group : groupData) {
210 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
216 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
218 List<DeliveryGroupData> groupData = new ArrayList<>();
219 for (DeliveryGroup group : newDeliveryGroups) {
220 groupData.add(group.getDeliveryGroupData());
222 final String pizzeriaId = groupData.get(0).getPizzeriaId();
224 int pizzeriaSpacePort = 0;
226 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
227 } catch (NumberFormatException e) {
228 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
231 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
233 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
235 if (!Util.runSimulation) {
236 for (DeliveryGroupData deliveryGroupData : groupData) {
237 new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
238 deliveryGroupData.getAddress())).start();