1 package at.ac.tuwien.sbc.valesriegler.group;
3 import at.ac.tuwien.sbc.valesriegler.common.Util;
4 import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel;
5 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupCreationDetailsRequest;
6 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel;
7 import at.ac.tuwien.sbc.valesriegler.group.jms.GroupJMSNACMsgListener;
8 import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector;
9 import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC;
10 import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStart;
11 import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStop;
12 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
13 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
14 import at.ac.tuwien.sbc.valesriegler.types.Order;
15 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
16 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import javax.jms.JMSException;
22 import java.lang.management.ManagementFactory;
26 * The Main class of the Group component.
28 * Start the communication and the group GUI:
30 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
33 public class GroupAgent {
34 private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
36 private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
37 private GroupOverviewModel groupModel;
38 private DeliveryOverviewModel deliveryOverviewModel;
39 private GroupAgentXVSM xvsm;
40 static private JMSNAC jmsnac;
41 static private GroupGUI groupGui;
43 * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
44 * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
45 * selecting for which pizzeria the customer groups are created
46 * In case of XVSM the identifier string is a port number
48 private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
50 public static GroupAgent groupAgent;
52 public static void main(String[] args) {
53 if (args.length < 1) {
54 throw new IllegalArgumentException(USAGE);
58 log.warn("Middleware: " + mw);
59 log.warn("JVM: " + ManagementFactory.getRuntimeMXBean().getName());
60 log.warn("THREAD: " + Thread.currentThread().getName());
62 if ("JMS".equalsIgnoreCase(mw)) {
64 } else if ("XVSM".equalsIgnoreCase(mw)) {
67 throw new IllegalArgumentException(USAGE);
70 groupAgent = new GroupAgent();
71 if (!Util.runSimulation) {
72 groupGui = new GroupGUI();
73 SwingUtilities.invokeLater(groupGui);
74 } else if(Util.useJMS) {
75 JMSGroupConnector.getConnectors().put("tcp://localhost:61621?jms.prefetchPolicy.all=1",
76 new JMSGroupConnector("tcp://localhost:61621?jms.prefetchPolicy.all=1"));
77 JMSGroupConnector.getConnectors().put("tcp://localhost:61622?jms.prefetchPolicy.all=1",
78 new JMSGroupConnector("tcp://localhost:61622?jms.prefetchPolicy.all=1"));
80 List<PizzaType> pizzaTypes1 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA,
81 PizzaType.MARGHERITA);
82 List<PizzaType> pizzaTypes2 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA);
83 List<PizzaType> pizzaTypes3 = Arrays.asList(PizzaType.SALAMI, PizzaType.MARGHERITA);
85 final String pizzeria1 = "tcp://localhost:61621?jms.prefetchPolicy.all=1";
86 final String pizzeria2 = "tcp://localhost:61622?jms.prefetchPolicy.all=1";
87 List<DeliveryGroup> groups1 = GroupAgent.getInstance().createGroups(pizzaTypes1, pizzeria1, 4);
88 List<DeliveryGroup> groups2 = GroupAgent.getInstance().createGroups(pizzaTypes2, pizzeria2, 3);
89 List<DeliveryGroup> groups3 = GroupAgent.getInstance().createGroups(pizzaTypes3, pizzeria2, 2);
91 final DeliveryOverviewModel deliveryModel = GroupAgent.getInstance().getDeliveryModel();
92 deliveryModel.addItems(groups1);
93 deliveryModel.addItems(groups2);
94 deliveryModel.addItems(groups3);
96 GroupAgent.getInstance().onDeliveryGroupsCreated(groups2);
97 GroupAgent.getInstance().onDeliveryGroupsCreated(groups3);
98 GroupAgent.getInstance().onDeliveryGroupsCreated(groups1);
100 log.warn("Starting benchmark in 20 sec");
102 jmsnac.sendNACMsg(new BenchmarkStart());
104 jmsnac.sendNACMsg(new BenchmarkStop());
105 log.warn("Benchmark Stop Signal sent.");
106 } catch (InterruptedException | JMSException e) {
107 log.warn("EXCEPTION!", e);
112 public GroupAgent() {
113 deliveryOverviewModel = new DeliveryOverviewModel();
114 groupModel = new GroupOverviewModel();
116 GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
117 jmsnac = new JMSNAC(tmp);
120 xvsm = new GroupAgentXVSM();
122 xvsm.listenForNewPizzerias();
124 if (Util.runSimulation) {
125 xvsm.listenToPizzeria("9875");
126 xvsm.listenToPizzeria("9874");
127 xvsm.runSimulation();
132 public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
133 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
134 final String fakeAddress = "address";
135 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress,
138 for (int i = 0; i < gc.numberOfGroups; i++) {
139 DeliveryGroup group = new DeliveryGroup();
140 final Order order = Util.createOrder(group, gc);
141 group.getDeliveryGroupData().setOrder(order);
142 group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
143 group.getDeliveryGroupData().setAddress(gc.address);
144 newDeliveryGroups.add(group);
147 return newDeliveryGroups;
150 public static GroupAgent getInstance() {
154 public Set<String> getPizzeriaIdentifiers() {
155 return pizzeriaIdentifiers;
158 public GroupOverviewModel getGroupModel() {
162 public static GroupGUI getGroupGui() {
166 public static JMSNAC getJmsnac() {
170 public DeliveryOverviewModel getDeliveryModel() {
171 return deliveryOverviewModel;
174 public void onGroupsCreated(List<Group> newGroups) {
176 List<GroupData> groupData = new ArrayList<>();
178 for (Group group : newGroups) {
179 groupData.add(group.getGroupData());
181 final String pizzeriaId = groupData.get(0).getPizzeriaId();
183 int pizzeriaSpacePort = 0;
185 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
186 } catch (NumberFormatException e) {
187 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
189 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
191 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
193 // start the space group in a new thread
194 for (GroupData group : groupData) {
195 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
201 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
203 List<DeliveryGroupData> groupData = new ArrayList<>();
204 for (DeliveryGroup group : newDeliveryGroups) {
205 groupData.add(group.getDeliveryGroupData());
207 final String pizzeriaId = groupData.get(0).getPizzeriaId();
209 int pizzeriaSpacePort = 0;
211 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
212 } catch (NumberFormatException e) {
213 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
216 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
218 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
220 if (!Util.runSimulation) {
221 for (DeliveryGroupData deliveryGroupData : groupData) {
222 new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
223 deliveryGroupData.getAddress())).start();