]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
Some UI Fixes in PizzeriaGUI inter-table communication.
[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             xvsm.listenForTableAssigned();
73             xvsm.listenForOrdersTaken();
74             xvsm.listenForDeliveredOrders();
75             xvsm.listenForPaymentRequest();
76             xvsm.listenForPaymentDone();
77         }
78     }
79
80     public static GroupAgent getInstance() {
81         return groupAgent;
82     }
83
84     public AbstractGroupConnector getGroupcomm() {
85         return groupconn;
86     }
87
88     public Set<String> getPizzeriaIdentifiers() {
89         return pizzeriaIdentifiers;
90     }
91
92     public GroupOverviewModel getGroupModel() {
93         return groupModel;
94     }
95
96     public DeliveryOverviewModel getDeliveryModel() {
97         return deliveryOverviewModel;
98     }
99
100     public void onGroupsCreated(List<Group> newGroups) {
101         if (!Util.useJMS) {
102             List<GroupData> groupData = new ArrayList<>();
103
104             for (Group group : newGroups) {
105                 groupData.add(group.getGroupData());
106             }
107             final String pizzeriaId = groupData.get(0).getPizzeriaId();
108
109             int pizzeriaSpacePort = 0;
110             try {
111                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
112             } catch (NumberFormatException e) {
113                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
114             }
115             xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
116
117             log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
118
119             // start the space group in a new thread
120             for (GroupData group : groupData) {
121                 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
122             }
123         }
124
125     }
126
127
128     public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
129         if (!Util.useJMS) {
130             List<DeliveryGroupData> groupData = new ArrayList<>();
131             for (DeliveryGroup group : newDeliveryGroups) {
132                 groupData.add(group.getDeliveryGroupData());
133             }
134             final String pizzeriaId = groupData.get(0).getPizzeriaId();
135
136             int pizzeriaSpacePort = 0;
137             try {
138                 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
139             } catch (NumberFormatException e) {
140                 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
141             }
142
143             xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
144
145             log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
146
147
148
149         }
150     }
151 }