]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
Solve some bugs caused by incompatibilities of JMS and XVSM version
[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 java.util.ArrayList;
4 import java.util.List;
5
6 import javax.swing.SwingUtilities;
7
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import at.ac.tuwien.sbc.valesriegler.common.Util;
12 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel;
13 import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector;
14 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
15 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
16
17 /**
18  * The Main class of the Group component.
19  * <p />
20  * Start the communication and the group GUI:
21  * 
22  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
23  * @author jan
24  * 
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 GroupAgentXVSM xvsm;
33
34         public static GroupAgent groupAgent;
35
36         public static void main(String[] args) {
37                 if (args.length != 1) {
38                         throw new IllegalArgumentException(USAGE);
39                 }
40
41                 String mw = args[0];
42                 log.info("Middleware: " + mw);
43
44                 if ("JMS".equalsIgnoreCase(mw)) {
45                         groupAgent = new GroupAgent(new JMSGroupConnector());
46                         Util.useJMS = true;
47                 } else if ("XVSM".equalsIgnoreCase(mw)) {
48                         // TODO: XVSM Connector?
49                         Util.useJMS = false;
50                         groupAgent = new GroupAgent(null);
51                 } else {
52                         throw new IllegalArgumentException(USAGE);
53                 }
54
55                 SwingUtilities.invokeLater(new GroupGUI());
56         }
57
58         public GroupAgent(AbstractGroupConnector groupconn) {
59                 groupModel = new GroupOverviewModel();
60                 if (Util.useJMS) {
61                         this.groupconn = groupconn;
62                         groupconn.init();
63                 } else {
64                         xvsm = new GroupAgentXVSM();
65
66                         xvsm.listenForTableAssigned();
67                         xvsm.listenForOrdersTaken();
68                         xvsm.listenForDeliveredOrders();
69                         xvsm.listenForPaymentRequest();
70                         xvsm.listenForPaymentDone();
71                 }
72         }
73
74         public static GroupAgent getInstance() {
75                 return groupAgent;
76         }
77
78         public AbstractGroupConnector getGroupcomm() {
79                 return groupconn;
80         }
81
82         public GroupOverviewModel getGroupModel() {
83                 return groupModel;
84         }
85
86         public void onGroupsCreated(List<Group> newGroups) {
87                 if (!Util.useJMS) {
88                         List<GroupData> groupData = new ArrayList<>();
89
90                         for (Group group : newGroups) {
91                                 groupData.add(group.getGroupData());
92                         }
93                         xvsm.sendNewGroupsToSpace(groupData);
94
95                         log.info("New Groups were sent to the space");
96
97                         // start the space group in a new thread
98                         for (GroupData group : groupData) {
99                                 new Thread(new SpaceGroup(group.getId())).start();
100                         }
101                 }
102
103         }
104 }