]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java
Make cook and waiter look around for work to do if they are idle
[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.mozartspaces.core.MzsConstants.RequestTimeout;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import at.ac.tuwien.sbc.valesriegler.common.Util;
13 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel;
14 import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector;
15 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
16 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
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  */
27 public class GroupAgent {
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                 // TODO: use jms and xvsm manager here.
38                 groupAgent = new GroupAgent(new JMSGroupConnector());
39                 
40                 SwingUtilities.invokeLater(new GroupGUI());
41         }
42
43         public GroupAgent(JMSGroupConnector groupconn) {
44                 groupModel = new GroupOverviewModel();
45                 if(Util.useJMS) {
46                         this.groupconn = groupconn;
47                         groupconn.init();
48                 }
49                 else {
50                         xvsm = new GroupAgentXVSM();
51
52                         xvsm.listenForTableAssigned();
53                         xvsm.listenForOrdersTaken();
54                         xvsm.listenForDeliveredOrders();
55                         xvsm.listenForPaymentRequest();
56                         xvsm.listenForPaymentDone();
57                 }
58         }
59
60         public static GroupAgent getInstance() {
61                 return groupAgent;
62         }
63
64         public AbstractGroupConnector getGroupcomm() {
65                 return groupconn;
66         }
67
68         public GroupOverviewModel getGroupModel() {
69                 return groupModel;
70         }
71
72         public void onGroupsCreated(List<Group> newGroups) {
73                 if(!Util.useJMS) {
74                         List<GroupData> groupData = new ArrayList<>();
75                         
76                         for (Group group : newGroups) {
77                                 groupData.add(group.getGroupData());
78                         }
79                         xvsm.sendNewGroupsToSpace(groupData);
80                         
81                         log.info("New Groups were sent to the space");
82                         
83                      // start the space group in a new thread
84                         for (GroupData group : groupData) {
85                                  new Thread(new SpaceGroup(group.getId())).start();
86                         }
87                 }
88
89         }
90 }