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