]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
[XVSM]Some NPE fixes, GUI fixes
[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.jms.nac.JMSNAC;
9 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
10 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
11 import at.ac.tuwien.sbc.valesriegler.types.Order;
12 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
13 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import javax.swing.*;
18 import java.util.*;
19
20 /**
21  * The Main class of the Group component.
22  * <p/>
23  * Start the communication and the group GUI:
24  * 
25  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
26  * @author jan
27  */
28 public class GroupAgent {
29         private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
30
31         private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
32         private GroupOverviewModel groupModel;
33         private DeliveryOverviewModel deliveryOverviewModel;
34         private GroupAgentXVSM xvsm;
35         static private JMSNAC jmsnac;
36         static private GroupGUI groupGui;
37         /**
38          * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
39          * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
40          * selecting for which pizzeria the customer groups are created
41          * In case of XVSM the identifier string is a port number
42          */
43         private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
44
45         public static GroupAgent groupAgent;
46
47         public static void main(String[] args) {
48                 if (args.length != 1) {
49                         throw new IllegalArgumentException(USAGE);
50                 }
51
52                 String mw = args[0];
53                 log.info("Middleware: " + mw);
54
55                 if ("JMS".equalsIgnoreCase(mw)) {
56                         Util.useJMS = true;
57                 } else if ("XVSM".equalsIgnoreCase(mw)) {
58                         Util.useJMS = false;
59                 } else {
60                         throw new IllegalArgumentException(USAGE);
61                 }
62
63                 groupAgent = new GroupAgent();
64                 if (! Util.runSimulation) {
65                         groupGui = new GroupGUI();
66                         SwingUtilities.invokeLater(groupGui);
67                 }
68         }
69
70         public GroupAgent() {
71                 deliveryOverviewModel = new DeliveryOverviewModel();
72                 groupModel = new GroupOverviewModel();
73                 if (Util.useJMS) {
74                         GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
75                         jmsnac = new JMSNAC(tmp);
76                 } else {
77
78                         xvsm = new GroupAgentXVSM();
79
80                         xvsm.listenForNewPizzerias();
81
82                         if (Util.runSimulation) {
83                                 xvsm.listenToPizzeria("9875");
84                                 xvsm.listenToPizzeria("9874");
85                                 xvsm.runSimulation();
86                         }
87                 }
88         }
89
90         public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
91                 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
92                 final String fakeAddress = "address";
93                 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress,
94                                 pizzeria);
95
96                 for (int i = 0; i < gc.numberOfGroups; i++) {
97                         DeliveryGroup group = new DeliveryGroup();
98                         final Order order = Util.createOrder(group, gc);
99                         group.getDeliveryGroupData().setOrder(order);
100                         group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
101                         group.getDeliveryGroupData().setAddress(gc.address);
102                         newDeliveryGroups.add(group);
103                 }
104
105                 return newDeliveryGroups;
106         }
107
108         public static GroupAgent getInstance() {
109                 return groupAgent;
110         }
111
112         public Set<String> getPizzeriaIdentifiers() {
113                 return pizzeriaIdentifiers;
114         }
115
116         public GroupOverviewModel getGroupModel() {
117                 return groupModel;
118         }
119
120         public static GroupGUI getGroupGui() {
121                 return groupGui;
122         }
123
124         public static JMSNAC getJmsnac() {
125                 return jmsnac;
126         }
127
128         public DeliveryOverviewModel getDeliveryModel() {
129                 return deliveryOverviewModel;
130         }
131
132         public void onGroupsCreated(List<Group> newGroups) {
133                 if (!Util.useJMS) {
134                         List<GroupData> groupData = new ArrayList<>();
135
136                         for (Group group : newGroups) {
137                                 groupData.add(group.getGroupData());
138                         }
139                         final String pizzeriaId = groupData.get(0).getPizzeriaId();
140
141                         int pizzeriaSpacePort = 0;
142                         try {
143                                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
144                         } catch (NumberFormatException e) {
145                                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
146                         }
147                         xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
148
149                         log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
150
151                         // start the space group in a new thread
152                         for (GroupData group : groupData) {
153                                 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
154                         }
155                 }
156
157         }
158
159         public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
160                 if (!Util.useJMS) {
161                         List<DeliveryGroupData> groupData = new ArrayList<>();
162                         for (DeliveryGroup group : newDeliveryGroups) {
163                                 groupData.add(group.getDeliveryGroupData());
164                         }
165                         final String pizzeriaId = groupData.get(0).getPizzeriaId();
166
167                         int pizzeriaSpacePort = 0;
168                         try {
169                                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
170                         } catch (NumberFormatException e) {
171                                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
172                         }
173
174                         xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
175
176                         log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
177
178                         if (!Util.runSimulation) {
179                                 for (DeliveryGroupData deliveryGroupData : groupData) {
180                                         new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
181                                                         deliveryGroupData.getAddress())).start();
182                                 }
183                         }
184                 }
185         }
186 }