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