]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
[XVSM] run jms simulation only when Util.useJMs==true
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / GroupAgent.java
1 package at.ac.tuwien.sbc.valesriegler.group;
2
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;
19
20 import javax.jms.JMSException;
21 import javax.swing.*;
22 import java.lang.management.ManagementFactory;
23 import java.util.*;
24
25 /**
26  * The Main class of the Group component.
27  * <p/>
28  * Start the communication and the group GUI:
29  * 
30  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
31  * @author jan
32  */
33 public class GroupAgent {
34         private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
35
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;
42         /**
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
47          */
48         private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
49
50         public static GroupAgent groupAgent;
51
52         public static void main(String[] args) {
53                 if (args.length < 1) {
54                         throw new IllegalArgumentException(USAGE);
55                 }
56
57                 String mw = args[0];
58                 log.warn("Middleware: " + mw);
59                 log.warn("JVM: " + ManagementFactory.getRuntimeMXBean().getName());
60                 log.warn("THREAD: " + Thread.currentThread().getName());
61
62                 if ("JMS".equalsIgnoreCase(mw)) {
63                         Util.useJMS = true;
64                 } else if ("XVSM".equalsIgnoreCase(mw)) {
65                         Util.useJMS = false;
66                 } else {
67                         throw new IllegalArgumentException(USAGE);
68                 }
69
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"));
79                         try {
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);
84
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);
90
91                                 final DeliveryOverviewModel deliveryModel = GroupAgent.getInstance().getDeliveryModel();
92                                 deliveryModel.addItems(groups1);
93                                 deliveryModel.addItems(groups2);
94                                 deliveryModel.addItems(groups3);
95
96                                 GroupAgent.getInstance().onDeliveryGroupsCreated(groups2);
97                                 GroupAgent.getInstance().onDeliveryGroupsCreated(groups3);
98                                 GroupAgent.getInstance().onDeliveryGroupsCreated(groups1);
99
100                                 log.warn("Starting benchmark in 20 sec");
101                                 Thread.sleep(20000);
102                                 jmsnac.sendNACMsg(new BenchmarkStart());
103                                 Thread.sleep(60000);
104                                 jmsnac.sendNACMsg(new BenchmarkStop());
105                                 log.warn("Benchmark Stop Signal sent.");
106                         } catch (InterruptedException | JMSException e) {
107                                 log.warn("EXCEPTION!", e);
108                         }
109                 }
110         }
111
112         public GroupAgent() {
113                 deliveryOverviewModel = new DeliveryOverviewModel();
114                 groupModel = new GroupOverviewModel();
115                 if (Util.useJMS) {
116                         GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
117                         jmsnac = new JMSNAC(tmp);
118                 } else {
119
120                         xvsm = new GroupAgentXVSM();
121
122                         xvsm.listenForNewPizzerias();
123
124                         if (Util.runSimulation) {
125                                 xvsm.listenToPizzeria("9875");
126                                 xvsm.listenToPizzeria("9874");
127                                 xvsm.runSimulation();
128                         }
129                 }
130         }
131
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,
136                                 pizzeria);
137
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);
145                 }
146
147                 return newDeliveryGroups;
148         }
149
150         public static GroupAgent getInstance() {
151                 return groupAgent;
152         }
153
154         public Set<String> getPizzeriaIdentifiers() {
155                 return pizzeriaIdentifiers;
156         }
157
158         public GroupOverviewModel getGroupModel() {
159                 return groupModel;
160         }
161
162         public static GroupGUI getGroupGui() {
163                 return groupGui;
164         }
165
166         public static JMSNAC getJmsnac() {
167                 return jmsnac;
168         }
169
170         public DeliveryOverviewModel getDeliveryModel() {
171                 return deliveryOverviewModel;
172         }
173
174         public void onGroupsCreated(List<Group> newGroups) {
175                 if (!Util.useJMS) {
176                         List<GroupData> groupData = new ArrayList<>();
177
178                         for (Group group : newGroups) {
179                                 groupData.add(group.getGroupData());
180                         }
181                         final String pizzeriaId = groupData.get(0).getPizzeriaId();
182
183                         int pizzeriaSpacePort = 0;
184                         try {
185                                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
186                         } catch (NumberFormatException e) {
187                                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
188                         }
189                         xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
190
191                         log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
192
193                         // start the space group in a new thread
194                         for (GroupData group : groupData) {
195                                 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
196                         }
197                 }
198
199         }
200
201         public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
202                 if (!Util.useJMS) {
203                         List<DeliveryGroupData> groupData = new ArrayList<>();
204                         for (DeliveryGroup group : newDeliveryGroups) {
205                                 groupData.add(group.getDeliveryGroupData());
206                         }
207                         final String pizzeriaId = groupData.get(0).getPizzeriaId();
208
209                         int pizzeriaSpacePort = 0;
210                         try {
211                                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
212                         } catch (NumberFormatException e) {
213                                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
214                         }
215
216                         xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
217
218                         log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
219
220                         if (!Util.runSimulation) {
221                                 for (DeliveryGroupData deliveryGroupData : groupData) {
222                                         new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
223                                                         deliveryGroupData.getAddress())).start();
224                                 }
225                         }
226                 }
227         }
228 }