]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
[JMS] merged GroupAgent and added Util.runSimulation check.
[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.Collections;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8
9 import javax.swing.SwingUtilities;
10
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import at.ac.tuwien.sbc.valesriegler.common.Util;
15 import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel;
16 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupCreationDetailsRequest;
17 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel;
18 import at.ac.tuwien.sbc.valesriegler.group.jms.GroupJMSNACMsgListener;
19 import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC;
20 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
21 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
22 import at.ac.tuwien.sbc.valesriegler.types.Order;
23 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
24 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
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         public static void main(String[] args) {
54                 if (args.length != 1) {
55                         // throw new IllegalArgumentException(USAGE);
56                 }
57
58                 String mw = "JMS";// args[0];
59                 log.info("Middleware: " + mw);
60
61                 if ("JMS".equalsIgnoreCase(mw)) {
62                         Util.useJMS = true;
63                 } else if ("XVSM".equalsIgnoreCase(mw)) {
64                         Util.useJMS = false;
65                 } else {
66                         throw new IllegalArgumentException(USAGE);
67                 }
68
69                 groupAgent = new GroupAgent();
70
71                 if (!Util.runSimulation) {
72                         groupGui = new GroupGUI();
73                         SwingUtilities.invokeLater(groupGui);
74                 }
75         }
76
77         public GroupAgent() {
78                 deliveryOverviewModel = new DeliveryOverviewModel();
79                 groupModel = new GroupOverviewModel();
80                 if (Util.useJMS) {
81                         jmsnac = new JMSNAC(new GroupJMSNACMsgListener());
82                 } else {
83
84                         xvsm = new GroupAgentXVSM();
85
86                         xvsm.listenForNewPizzerias();
87
88                         if (Util.runSimulation) {
89                                 xvsm.listenToPizzeria("9875");
90                                 xvsm.listenToPizzeria("9874");
91                                 xvsm.runSimulation();
92                         }
93                 }
94         }
95
96         public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
97                 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
98                 final String fakeAddress = "address";
99                 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress,
100                                 pizzeria);
101
102                 for (int i = 0; i < gc.numberOfGroups; i++) {
103                         DeliveryGroup group = new DeliveryGroup();
104                         final Order order = Util.createOrder(group, gc);
105                         group.getDeliveryGroupData().setOrder(order);
106                         group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
107                         group.getDeliveryGroupData().setAddress(gc.address);
108                         newDeliveryGroups.add(group);
109                 }
110
111                 return newDeliveryGroups;
112         }
113
114         public static GroupAgent getInstance() {
115                 return groupAgent;
116         }
117
118         public Set<String> getPizzeriaIdentifiers() {
119                 return pizzeriaIdentifiers;
120         }
121
122         public GroupOverviewModel getGroupModel() {
123                 return groupModel;
124         }
125
126         public static GroupGUI getGroupGui() {
127                 return groupGui;
128         }
129
130         public static JMSNAC getJmsnac() {
131                 return jmsnac;
132         }
133
134         public DeliveryOverviewModel getDeliveryModel() {
135                 return deliveryOverviewModel;
136         }
137
138         public void onGroupsCreated(List<Group> newGroups) {
139                 if (!Util.useJMS) {
140                         List<GroupData> groupData = new ArrayList<>();
141
142                         for (Group group : newGroups) {
143                                 groupData.add(group.getGroupData());
144                         }
145                         final String pizzeriaId = groupData.get(0).getPizzeriaId();
146
147                         int pizzeriaSpacePort = 0;
148                         try {
149                                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
150                         } catch (NumberFormatException e) {
151                                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
152                         }
153                         xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
154
155                         log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
156
157                         // start the space group in a new thread
158                         for (GroupData group : groupData) {
159                                 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
160                         }
161                 }
162
163         }
164
165         public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
166                 if (!Util.useJMS) {
167                         List<DeliveryGroupData> groupData = new ArrayList<>();
168                         for (DeliveryGroup group : newDeliveryGroups) {
169                                 groupData.add(group.getDeliveryGroupData());
170                         }
171                         final String pizzeriaId = groupData.get(0).getPizzeriaId();
172
173                         int pizzeriaSpacePort = 0;
174                         try {
175                                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
176                         } catch (NumberFormatException e) {
177                                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
178                         }
179
180                         xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
181
182                         log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
183
184                         if (!Util.runSimulation) {
185                                 for (DeliveryGroupData deliveryGroupData : groupData) {
186                                         new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
187                                                         deliveryGroupData.getAddress())).start();
188                                 }
189                         }
190                 }
191         }
192 }