]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
removed | in exceptionhandler. again needed by jenkins.
[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 import java.util.concurrent.atomic.AtomicLong;
25
26 /**
27  * The Main class of the Group component.
28  * <p/>
29  * Start the communication and the group GUI:
30  * 
31  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
32  * @author jan
33  */
34 public class GroupAgent {
35         private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
36
37         private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
38         private GroupOverviewModel groupModel;
39         private DeliveryOverviewModel deliveryOverviewModel;
40         private GroupAgentXVSM xvsm;
41         static private JMSNAC jmsnac;
42         static private GroupGUI groupGui;
43         /**
44          * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
45          * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
46          * selecting for which pizzeria the customer groups are created
47          * In case of XVSM the identifier string is a port number
48          */
49         private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
50
51         public static GroupAgent groupAgent;
52
53         /**
54          * Benchmarkstuff
55          */
56         public static AtomicLong startTime = new AtomicLong();
57
58         public static void main(String[] args) {
59                 if (args.length < 1) {
60                         throw new IllegalArgumentException(USAGE);
61                 }
62
63                 String mw = args[0];
64                 log.warn("Middleware: " + mw);
65                 log.warn("JVM: " + ManagementFactory.getRuntimeMXBean().getName());
66                 log.warn("THREAD: " + Thread.currentThread().getName());
67
68                 if ("JMS".equalsIgnoreCase(mw)) {
69                         Util.useJMS = true;
70                 } else if ("XVSM".equalsIgnoreCase(mw)) {
71                         Util.useJMS = false;
72                 } else {
73                         throw new IllegalArgumentException(USAGE);
74                 }
75
76                 groupAgent = new GroupAgent();
77                 if (!Util.runSimulation) {
78                         groupGui = new GroupGUI();
79                         SwingUtilities.invokeLater(groupGui);
80                 } else if (Util.useJMS) {
81                         JMSGroupConnector.getConnectors().put("tcp://localhost:61621?jms.prefetchPolicy.all=1",
82                                         new JMSGroupConnector("tcp://localhost:61621?jms.prefetchPolicy.all=1"));
83                         JMSGroupConnector.getConnectors().put("tcp://localhost:61622?jms.prefetchPolicy.all=1",
84                                         new JMSGroupConnector("tcp://localhost:61622?jms.prefetchPolicy.all=1"));
85                         try {
86                                 List<PizzaType> pizzaTypes1 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA,
87                                                 PizzaType.MARGHERITA);
88                                 List<PizzaType> pizzaTypes2 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA);
89                                 List<PizzaType> pizzaTypes3 = Arrays.asList(PizzaType.SALAMI, PizzaType.MARGHERITA);
90
91                                 final String pizzeria1 = "tcp://localhost:61621?jms.prefetchPolicy.all=1";
92                                 final String pizzeria2 = "tcp://localhost:61622?jms.prefetchPolicy.all=1";
93                                 List<DeliveryGroup> groups1 = GroupAgent.getInstance().createGroups(pizzaTypes1, pizzeria1, 4);
94                                 List<DeliveryGroup> groups2 = GroupAgent.getInstance().createGroups(pizzaTypes2, pizzeria2, 3);
95                                 List<DeliveryGroup> groups3 = GroupAgent.getInstance().createGroups(pizzaTypes3, pizzeria2, 2);
96
97                                 final DeliveryOverviewModel deliveryModel = GroupAgent.getInstance().getDeliveryModel();
98                                 deliveryModel.addItems(groups1);
99                                 deliveryModel.addItems(groups2);
100                                 deliveryModel.addItems(groups3);
101
102                                 GroupAgent.getInstance().onDeliveryGroupsCreated(groups2);
103                                 GroupAgent.getInstance().onDeliveryGroupsCreated(groups3);
104                                 GroupAgent.getInstance().onDeliveryGroupsCreated(groups1);
105
106                                 log.warn("Starting benchmark in 20 sec");
107                                 Thread.sleep(20000);
108                 startTime.set(new Date().getTime());
109                                 jmsnac.sendNACMsg(new BenchmarkStart());
110                                 Thread.sleep(60000);
111                                 jmsnac.sendNACMsg(new BenchmarkStop());
112                                 log.warn("Benchmark Stop Signal sent.");
113                         } catch (InterruptedException e) {
114                                 log.warn("EXCEPTION!", e);
115                         } catch (JMSException e) {
116                                 log.warn("EXCEPTION!", e);
117                         }
118                 }
119         }
120
121         public GroupAgent() {
122                 deliveryOverviewModel = new DeliveryOverviewModel();
123                 groupModel = new GroupOverviewModel();
124                 if (Util.useJMS) {
125                         GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
126                         jmsnac = new JMSNAC(tmp);
127                 } else {
128
129                         xvsm = new GroupAgentXVSM();
130
131                         xvsm.listenForNewPizzerias();
132
133                         if (Util.runSimulation) {
134                                 xvsm.listenToPizzeria("9875");
135                                 xvsm.listenToPizzeria("9874");
136                                 xvsm.runSimulation();
137                         }
138                 }
139         }
140
141         public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
142                 List<DeliveryGroup> newDeliveryGroups = new ArrayList<DeliveryGroup>();
143                 final String fakeAddress = "address";
144                 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress,
145                                 pizzeria);
146
147                 for (int i = 0; i < gc.numberOfGroups; i++) {
148                         DeliveryGroup group = new DeliveryGroup();
149                         final Order order = Util.createOrder(group, gc, true);
150                         group.getDeliveryGroupData().setOrder(order);
151                         group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
152                         group.getDeliveryGroupData().setAddress(gc.address);
153                         newDeliveryGroups.add(group);
154                 }
155
156                 return newDeliveryGroups;
157         }
158
159         public static GroupAgent getInstance() {
160                 return groupAgent;
161         }
162
163         public Set<String> getPizzeriaIdentifiers() {
164                 return pizzeriaIdentifiers;
165         }
166
167         public GroupOverviewModel getGroupModel() {
168                 return groupModel;
169         }
170
171         public static GroupGUI getGroupGui() {
172                 return groupGui;
173         }
174
175         public static JMSNAC getJmsnac() {
176                 return jmsnac;
177         }
178
179         public DeliveryOverviewModel getDeliveryModel() {
180                 return deliveryOverviewModel;
181         }
182
183         public void onGroupsCreated(List<Group> newGroups) {
184                 if (!Util.useJMS) {
185                         List<GroupData> groupData = new ArrayList<GroupData>();
186
187                         for (Group group : newGroups) {
188                                 groupData.add(group.getGroupData());
189                         }
190                         final String pizzeriaId = groupData.get(0).getPizzeriaId();
191
192                         int pizzeriaSpacePort = 0;
193                         try {
194                                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
195                         } catch (NumberFormatException e) {
196                                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
197                         }
198                         xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
199
200                         log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
201
202                         // start the space group in a new thread
203                         for (GroupData group : groupData) {
204                                 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
205                         }
206                 }
207
208         }
209
210         public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
211                 if (!Util.useJMS) {
212                         List<DeliveryGroupData> groupData = new ArrayList<DeliveryGroupData>();
213                         for (DeliveryGroup group : newDeliveryGroups) {
214                                 groupData.add(group.getDeliveryGroupData());
215                         }
216                         final String pizzeriaId = groupData.get(0).getPizzeriaId();
217
218                         int pizzeriaSpacePort = 0;
219                         try {
220                                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
221                         } catch (NumberFormatException e) {
222                                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
223                         }
224
225                         xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
226
227                         log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
228
229                         if (!Util.runSimulation) {
230                                 for (DeliveryGroupData deliveryGroupData : groupData) {
231                                         new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
232                                                         deliveryGroupData.getAddress())).start();
233                                 }
234                         }
235                 }
236         }
237 }