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