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