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