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