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;
10 import java.util.concurrent.atomic.AtomicLong;
12 import javax.jms.JMSException;
13 import javax.swing.SwingUtilities;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 import at.ac.tuwien.sbc.valesriegler.common.Util;
19 import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel;
20 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupCreationDetailsRequest;
21 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel;
22 import at.ac.tuwien.sbc.valesriegler.group.jms.GroupJMSNACMsgListener;
23 import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector;
24 import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC;
25 import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStart;
26 import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStop;
27 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
28 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
29 import at.ac.tuwien.sbc.valesriegler.types.Order;
30 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
31 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
34 * The Main class of the Group component.
36 * Start the communication and the group GUI:
38 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
41 public class GroupAgent {
42 private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
44 private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
45 private GroupOverviewModel groupModel;
46 private DeliveryOverviewModel deliveryOverviewModel;
47 private GroupAgentXVSM xvsm;
48 static private JMSNAC jmsnac;
49 static private GroupGUI groupGui;
51 * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
52 * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
53 * selecting for which pizzeria the customer groups are created
54 * In case of XVSM the identifier string is a port number
56 private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
58 public static GroupAgent groupAgent;
63 public static AtomicLong startTime = new AtomicLong();
65 public static void main(String[] args) {
66 if (args.length < 1) {
67 throw new IllegalArgumentException(USAGE);
71 log.warn("Middleware: " + mw);
72 log.warn("JVM: " + ManagementFactory.getRuntimeMXBean().getName());
73 log.warn("THREAD: " + Thread.currentThread().getName());
75 if ("JMS".equalsIgnoreCase(mw)) {
77 } else if ("XVSM".equalsIgnoreCase(mw)) {
80 throw new IllegalArgumentException(USAGE);
83 groupAgent = new GroupAgent();
84 if (!Util.runSimulation) {
85 groupGui = new GroupGUI();
86 SwingUtilities.invokeLater(groupGui);
87 } else if (Util.useJMS) {
88 JMSGroupConnector.getConnectors().put("tcp://localhost:61621?jms.prefetchPolicy.all=1",
89 new JMSGroupConnector("tcp://localhost:61621?jms.prefetchPolicy.all=1"));
90 JMSGroupConnector.getConnectors().put("tcp://localhost:61622?jms.prefetchPolicy.all=1",
91 new JMSGroupConnector("tcp://localhost:61622?jms.prefetchPolicy.all=1"));
93 List<PizzaType> pizzaTypes1 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA,
94 PizzaType.MARGHERITA);
95 List<PizzaType> pizzaTypes2 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA);
96 List<PizzaType> pizzaTypes3 = Arrays.asList(PizzaType.SALAMI, PizzaType.MARGHERITA);
98 final String pizzeria1 = "tcp://localhost:61621?jms.prefetchPolicy.all=1";
99 final String pizzeria2 = "tcp://localhost:61622?jms.prefetchPolicy.all=1";
100 List<DeliveryGroup> groups1 = GroupAgent.getInstance().createGroups(pizzaTypes1, pizzeria1, 4);
101 List<DeliveryGroup> groups2 = GroupAgent.getInstance().createGroups(pizzaTypes2, pizzeria2, 3);
102 List<DeliveryGroup> groups3 = GroupAgent.getInstance().createGroups(pizzaTypes3, pizzeria2, 2);
104 final DeliveryOverviewModel deliveryModel = GroupAgent.getInstance().getDeliveryModel();
105 deliveryModel.addItems(groups1);
106 deliveryModel.addItems(groups2);
107 deliveryModel.addItems(groups3);
109 GroupAgent.getInstance().onDeliveryGroupsCreated(groups2);
110 GroupAgent.getInstance().onDeliveryGroupsCreated(groups3);
111 GroupAgent.getInstance().onDeliveryGroupsCreated(groups1);
113 log.warn("Starting benchmark in 20 sec");
115 jmsnac.sendNACMsg(new BenchmarkStart());
117 jmsnac.sendNACMsg(new BenchmarkStop());
118 log.warn("Benchmark Stop Signal sent.");
119 } catch (InterruptedException | JMSException e) {
120 log.warn("EXCEPTION!", e);
125 public GroupAgent() {
126 deliveryOverviewModel = new DeliveryOverviewModel();
127 groupModel = new GroupOverviewModel();
129 GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
130 jmsnac = new JMSNAC(tmp);
133 xvsm = new GroupAgentXVSM();
135 xvsm.listenForNewPizzerias();
137 if (Util.runSimulation) {
138 xvsm.listenToPizzeria("9875");
139 xvsm.listenToPizzeria("9874");
140 xvsm.runSimulation();
145 public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
146 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
147 final String fakeAddress = "address";
148 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress,
151 for (int i = 0; i < gc.numberOfGroups; i++) {
152 DeliveryGroup group = new DeliveryGroup();
153 final Order order = Util.createOrder(group, gc);
154 group.getDeliveryGroupData().setOrder(order);
155 group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
156 group.getDeliveryGroupData().setAddress(gc.address);
157 newDeliveryGroups.add(group);
160 return newDeliveryGroups;
163 public static GroupAgent getInstance() {
167 public Set<String> getPizzeriaIdentifiers() {
168 return pizzeriaIdentifiers;
171 public GroupOverviewModel getGroupModel() {
175 public static GroupGUI getGroupGui() {
179 public static JMSNAC getJmsnac() {
183 public DeliveryOverviewModel getDeliveryModel() {
184 return deliveryOverviewModel;
187 public void onGroupsCreated(List<Group> newGroups) {
189 List<GroupData> groupData = new ArrayList<>();
191 for (Group group : newGroups) {
192 groupData.add(group.getGroupData());
194 final String pizzeriaId = groupData.get(0).getPizzeriaId();
196 int pizzeriaSpacePort = 0;
198 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
199 } catch (NumberFormatException e) {
200 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
202 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
204 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
206 // start the space group in a new thread
207 for (GroupData group : groupData) {
208 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
214 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
216 List<DeliveryGroupData> groupData = new ArrayList<>();
217 for (DeliveryGroup group : newDeliveryGroups) {
218 groupData.add(group.getDeliveryGroupData());
220 final String pizzeriaId = groupData.get(0).getPizzeriaId();
222 int pizzeriaSpacePort = 0;
224 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
225 } catch (NumberFormatException e) {
226 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
229 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
231 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
233 if (!Util.runSimulation) {
234 for (DeliveryGroupData deliveryGroupData : groupData) {
235 new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
236 deliveryGroupData.getAddress())).start();