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