]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
[JMS] corrected benchmarking.
[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 java.lang.management.ManagementFactory;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Set;
10
11 import javax.jms.JMSException;
12 import javax.swing.SwingUtilities;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
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;
31
32 /**
33  * The Main class of the Group component.
34  * <p/>
35  * Start the communication and the group GUI:
36  * 
37  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
38  * @author jan
39  */
40 public class GroupAgent {
41         private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
42
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;
49         /**
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
54          */
55         private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
56
57         public static GroupAgent groupAgent;
58
59         public static void main(String[] args) {
60                 if (args.length < 1) {
61                         throw new IllegalArgumentException(USAGE);
62                 }
63
64                 String mw = args[0];
65                 log.warn("Middleware: " + mw);
66                 log.warn("JVM: " + ManagementFactory.getRuntimeMXBean().getName());
67                 log.warn("THREAD: " + Thread.currentThread().getName());
68
69                 if ("JMS".equalsIgnoreCase(mw)) {
70                         Util.useJMS = true;
71                 } else if ("XVSM".equalsIgnoreCase(mw)) {
72                         Util.useJMS = false;
73                 } else {
74                         throw new IllegalArgumentException(USAGE);
75                 }
76
77                 groupAgent = new GroupAgent();
78                 if (!Util.runSimulation) {
79                         groupGui = new GroupGUI();
80                         SwingUtilities.invokeLater(groupGui);
81                 } else {
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"));
86                         try {
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);
91
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);
97
98                                 final DeliveryOverviewModel deliveryModel = GroupAgent.getInstance().getDeliveryModel();
99                                 deliveryModel.addItems(groups1);
100                                 deliveryModel.addItems(groups2);
101                                 deliveryModel.addItems(groups3);
102
103                                 GroupAgent.getInstance().onDeliveryGroupsCreated(groups2);
104                                 GroupAgent.getInstance().onDeliveryGroupsCreated(groups3);
105                                 GroupAgent.getInstance().onDeliveryGroupsCreated(groups1);
106
107                                 log.warn("Starting benchmark in 20 sec");
108                                 Thread.sleep(20000);
109                                 jmsnac.sendNACMsg(new BenchmarkStart());
110                                 Thread.sleep(60000);
111                                 jmsnac.sendNACMsg(new BenchmarkStop());
112                                 log.warn("Benchmark Stop Signal sent.");
113                         } catch (InterruptedException | JMSException e) {
114                                 log.warn("EXCEPTION!", e);
115                         }
116                 }
117         }
118
119         public GroupAgent() {
120                 deliveryOverviewModel = new DeliveryOverviewModel();
121                 groupModel = new GroupOverviewModel();
122                 if (Util.useJMS) {
123                         GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
124                         jmsnac = new JMSNAC(tmp);
125                 } else {
126
127                         xvsm = new GroupAgentXVSM();
128
129                         xvsm.listenForNewPizzerias();
130
131                         if (Util.runSimulation) {
132                                 xvsm.listenToPizzeria("9875");
133                                 xvsm.listenToPizzeria("9874");
134                                 xvsm.runSimulation();
135                         }
136                 }
137         }
138
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,
143                                 pizzeria);
144
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);
152                 }
153
154                 return newDeliveryGroups;
155         }
156
157         public static GroupAgent getInstance() {
158                 return groupAgent;
159         }
160
161         public Set<String> getPizzeriaIdentifiers() {
162                 return pizzeriaIdentifiers;
163         }
164
165         public GroupOverviewModel getGroupModel() {
166                 return groupModel;
167         }
168
169         public static GroupGUI getGroupGui() {
170                 return groupGui;
171         }
172
173         public static JMSNAC getJmsnac() {
174                 return jmsnac;
175         }
176
177         public DeliveryOverviewModel getDeliveryModel() {
178                 return deliveryOverviewModel;
179         }
180
181         public void onGroupsCreated(List<Group> newGroups) {
182                 if (!Util.useJMS) {
183                         List<GroupData> groupData = new ArrayList<>();
184
185                         for (Group group : newGroups) {
186                                 groupData.add(group.getGroupData());
187                         }
188                         final String pizzeriaId = groupData.get(0).getPizzeriaId();
189
190                         int pizzeriaSpacePort = 0;
191                         try {
192                                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
193                         } catch (NumberFormatException e) {
194                                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
195                         }
196                         xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
197
198                         log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
199
200                         // start the space group in a new thread
201                         for (GroupData group : groupData) {
202                                 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
203                         }
204                 }
205
206         }
207
208         public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
209                 if (!Util.useJMS) {
210                         List<DeliveryGroupData> groupData = new ArrayList<>();
211                         for (DeliveryGroup group : newDeliveryGroups) {
212                                 groupData.add(group.getDeliveryGroupData());
213                         }
214                         final String pizzeriaId = groupData.get(0).getPizzeriaId();
215
216                         int pizzeriaSpacePort = 0;
217                         try {
218                                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
219                         } catch (NumberFormatException e) {
220                                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
221                         }
222
223                         xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
224
225                         log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
226
227                         if (!Util.runSimulation) {
228                                 for (DeliveryGroupData deliveryGroupData : groupData) {
229                                         new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
230                                                         deliveryGroupData.getAddress())).start();
231                                 }
232                         }
233                 }
234         }
235 }