]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
[XVSM] Some fixes for making sure that Cook only prepares normal pizzas when there...
[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 | 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, 100, 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, true);
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 }