]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
[XVSM] Waiters, Cooks and Pizzerias can be parameterized with a Space port. Group...
[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.GroupData;
8 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import javax.swing.*;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.List;
17
18 /**
19  * The Main class of the Group component.
20  * <p/>
21  * Start the communication and the group GUI:
22  *
23  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
24  * @author jan
25  */
26 public class GroupAgent {
27     private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
28
29     private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
30     private GroupOverviewModel groupModel;
31     private AbstractGroupConnector groupconn;
32     private DeliveryOverviewModel deliveryOverviewModel;
33     private GroupAgentXVSM xvsm;
34
35     /* Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
36     notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
37     selecting for which pizzeria the customer groups are created
38     In case of XVSM the identifier string is a port number*/
39     private List<String> pizzeriaIdentifiers = Collections.synchronizedList(new ArrayList<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 = 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         groupModel = new GroupOverviewModel();
66         if (Util.useJMS) {
67             this.groupconn = new JMSGroupConnector();
68             groupconn.init();
69         } else {
70             xvsm = new GroupAgentXVSM();
71
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 List<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 Groups were sent to the space");
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         //To change body of created methods use File | Settings | File Templates.
130     }
131 }