]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
Create DeliveryGroup, DeliveryGroupData, DeliveryStatus. Add Deliveries overview...
[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.List;
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     public static GroupAgent groupAgent;
34
35     public static void main(String[] args) {
36         if (args.length != 1) {
37             throw new IllegalArgumentException(USAGE);
38         }
39
40         String mw = args[0];
41         log.info("Middleware: " + mw);
42
43         if ("JMS".equalsIgnoreCase(mw)) {
44             groupAgent = new GroupAgent(new JMSGroupConnector());
45             Util.useJMS = true;
46         } else if ("XVSM".equalsIgnoreCase(mw)) {
47             // TODO: XVSM Connector?
48             Util.useJMS = false;
49             groupAgent = new GroupAgent(null);
50         } else {
51             throw new IllegalArgumentException(USAGE);
52         }
53
54         SwingUtilities.invokeLater(new GroupGUI());
55     }
56
57     public GroupAgent(AbstractGroupConnector groupconn) {
58         groupModel = new GroupOverviewModel();
59         if (Util.useJMS) {
60             this.groupconn = groupconn;
61             groupconn.init();
62         } else {
63             xvsm = new GroupAgentXVSM();
64
65             xvsm.listenForTableAssigned();
66             xvsm.listenForOrdersTaken();
67             xvsm.listenForDeliveredOrders();
68             xvsm.listenForPaymentRequest();
69             xvsm.listenForPaymentDone();
70         }
71     }
72
73     public static GroupAgent getInstance() {
74         return groupAgent;
75     }
76
77     public AbstractGroupConnector getGroupcomm() {
78         return groupconn;
79     }
80
81     public GroupOverviewModel getGroupModel() {
82         return groupModel;
83     }
84
85     public DeliveryOverviewModel getDeliveryModel() {
86         return deliveryOverviewModel;
87     }
88
89     public void onGroupsCreated(List<Group> newGroups) {
90         if (!Util.useJMS) {
91             List<GroupData> groupData = new ArrayList<>();
92
93             for (Group group : newGroups) {
94                 groupData.add(group.getGroupData());
95             }
96             xvsm.sendNewGroupsToSpace(groupData);
97
98             log.info("New Groups were sent to the space");
99
100             // start the space group in a new thread
101             for (GroupData group : groupData) {
102                 new Thread(new SpaceGroup(group.getId())).start();
103             }
104         }
105
106     }
107
108
109 }