]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
unified command line argument parsing a little.
[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                 } else if ("XVSM".equalsIgnoreCase(mw)) {
47                         // TODO: XVSM Connector?
48                         groupAgent = new GroupAgent(new JMSGroupConnector());
49                 } else {
50                         throw new IllegalArgumentException(USAGE);
51                 }
52
53                 SwingUtilities.invokeLater(new GroupGUI());
54         }
55
56         public GroupAgent(AbstractGroupConnector groupconn) {
57                 groupModel = new GroupOverviewModel();
58                 if (Util.useJMS) {
59                         this.groupconn = groupconn;
60                         groupconn.init();
61                 } else {
62                         xvsm = new GroupAgentXVSM();
63
64                         xvsm.listenForTableAssigned();
65                         xvsm.listenForOrdersTaken();
66                         xvsm.listenForDeliveredOrders();
67                         xvsm.listenForPaymentRequest();
68                         xvsm.listenForPaymentDone();
69                 }
70         }
71
72         public static GroupAgent getInstance() {
73                 return groupAgent;
74         }
75
76         public AbstractGroupConnector getGroupcomm() {
77                 return groupconn;
78         }
79
80         public GroupOverviewModel getGroupModel() {
81                 return groupModel;
82         }
83
84         public void onGroupsCreated(List<Group> newGroups) {
85                 if (!Util.useJMS) {
86                         List<GroupData> groupData = new ArrayList<>();
87
88                         for (Group group : newGroups) {
89                                 groupData.add(group.getGroupData());
90                         }
91                         xvsm.sendNewGroupsToSpace(groupData);
92
93                         log.info("New Groups were sent to the space");
94
95                         // start the space group in a new thread
96                         for (GroupData group : groupData) {
97                                 new Thread(new SpaceGroup(group.getId())).start();
98                         }
99                 }
100
101         }
102 }