]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
[JMS] Benchmarks are theoretically possible now. GroupGUI responsesmissing, so it...
[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                                 Thread.sleep(20000);
105                                 jmsnac.sendNACMsg(new BenchmarkStart());
106                                 Thread.sleep(60000);
107                                 jmsnac.sendNACMsg(new BenchmarkStop());
108                         } catch (InterruptedException | JMSException e) {
109                                 log.warn("EXCEPTION!", e);
110                         }
111                 }
112         }
113
114         public GroupAgent() {
115                 deliveryOverviewModel = new DeliveryOverviewModel();
116                 groupModel = new GroupOverviewModel();
117                 if (Util.useJMS) {
118                         GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
119                         jmsnac = new JMSNAC(tmp);
120                 } else {
121
122                         xvsm = new GroupAgentXVSM();
123
124                         xvsm.listenForNewPizzerias();
125
126                         if (Util.runSimulation) {
127                                 xvsm.listenToPizzeria("9875");
128                                 xvsm.listenToPizzeria("9874");
129                                 xvsm.runSimulation();
130                         }
131                 }
132         }
133
134         public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
135                 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
136                 final String fakeAddress = "address";
137                 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress,
138                                 pizzeria);
139
140                 for (int i = 0; i < gc.numberOfGroups; i++) {
141                         DeliveryGroup group = new DeliveryGroup();
142                         final Order order = Util.createOrder(group, gc);
143                         group.getDeliveryGroupData().setOrder(order);
144                         group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
145                         group.getDeliveryGroupData().setAddress(gc.address);
146                         newDeliveryGroups.add(group);
147                 }
148
149                 return newDeliveryGroups;
150         }
151
152         public static GroupAgent getInstance() {
153                 return groupAgent;
154         }
155
156         public Set<String> getPizzeriaIdentifiers() {
157                 return pizzeriaIdentifiers;
158         }
159
160         public GroupOverviewModel getGroupModel() {
161                 return groupModel;
162         }
163
164         public static GroupGUI getGroupGui() {
165                 return groupGui;
166         }
167
168         public static JMSNAC getJmsnac() {
169                 return jmsnac;
170         }
171
172         public DeliveryOverviewModel getDeliveryModel() {
173                 return deliveryOverviewModel;
174         }
175
176         public void onGroupsCreated(List<Group> newGroups) {
177                 if (!Util.useJMS) {
178                         List<GroupData> groupData = new ArrayList<>();
179
180                         for (Group group : newGroups) {
181                                 groupData.add(group.getGroupData());
182                         }
183                         final String pizzeriaId = groupData.get(0).getPizzeriaId();
184
185                         int pizzeriaSpacePort = 0;
186                         try {
187                                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
188                         } catch (NumberFormatException e) {
189                                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
190                         }
191                         xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
192
193                         log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
194
195                         // start the space group in a new thread
196                         for (GroupData group : groupData) {
197                                 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
198                         }
199                 }
200
201         }
202
203         public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
204                 if (!Util.useJMS) {
205                         List<DeliveryGroupData> groupData = new ArrayList<>();
206                         for (DeliveryGroup group : newDeliveryGroups) {
207                                 groupData.add(group.getDeliveryGroupData());
208                         }
209                         final String pizzeriaId = groupData.get(0).getPizzeriaId();
210
211                         int pizzeriaSpacePort = 0;
212                         try {
213                                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
214                         } catch (NumberFormatException e) {
215                                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
216                         }
217
218                         xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
219
220                         log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
221
222                         if (!Util.runSimulation) {
223                                 for (DeliveryGroupData deliveryGroupData : groupData) {
224                                         new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
225                                                         deliveryGroupData.getAddress())).start();
226                                 }
227                         }
228                 }
229         }
230 }