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