]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java
Some space refactoring. Doing away with unnecessary containers. Auto-Reloading of...
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / xvsm / GroupAgentXVSM.java
1 package at.ac.tuwien.sbc.valesriegler.xvsm;
2
3 import java.io.Serializable;
4 import java.util.List;
5
6 import javax.swing.SwingUtilities;
7
8 import org.mozartspaces.notifications.Notification;
9 import org.mozartspaces.notifications.NotificationListener;
10 import org.mozartspaces.notifications.Operation;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import at.ac.tuwien.sbc.valesriegler.common.Util;
15 import at.ac.tuwien.sbc.valesriegler.group.GroupAgent;
16 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
17 import at.ac.tuwien.sbc.valesriegler.types.Table;
18
19 public class GroupAgentXVSM extends AbstractXVSMConnector {
20         private static final Logger log = LoggerFactory.getLogger(GroupAgentXVSM.class);
21         
22         public GroupAgentXVSM() {
23                 super();
24                 assignTableContainer = useContainer(Util.ASSIGN_TABLE) ;
25                 orderCompleteContainer = useContainer(Util.ORDER_COMPLETE) ;
26                 paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST) ;
27                 paymentDoneContainer = useContainer(Util.PAYMENT_DONE);
28                 tableAssignedContainer = useContainer(Util.TABLE_ASSIGNED);
29                 orderTakenContainer = useContainer(Util.ORDER_TAKEN);
30         }
31         
32         public void listenForDeliveredOrders() {
33                 NotificationListener deliveredOrders = new NotificationListener() {
34             @Override
35             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
36                 final List<GroupData> groups = castEntries(entries);
37                 
38                 log.info("{} order was delivered!", groups.size());
39                 if(groups.size() != 1) {
40                         throw new RuntimeException("A waiter should only deliver one order at once!");
41                 }
42                 final int groupId = groups.get(0).getOrder().getId();
43                            
44                 SwingUtilities.invokeLater(new Runnable() {
45                                         @Override
46                                         public void run() {
47                                                 GroupAgent.getInstance().getGroupModel().setGroupEating(groupId);
48                                         }
49                                 });
50             }
51         };
52           try {
53                 notificationMgr.createNotification(orderCompleteContainer, deliveredOrders, Operation.WRITE);
54         } catch (Exception e) {
55            handleSpaceErrorAndTerminate(e);
56         }
57         }
58
59         public void listenForPaymentRequest() {
60                 NotificationListener paymentRequest = new NotificationListener() {
61             @Override
62             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
63                 log.info("PAY REQUEST by");
64                 final List<GroupData> groups = castEntries(entries);
65                 
66                 if(groups.size() != 1) {
67                         throw new RuntimeException("A group only says as a whole that it wants to pay");
68                 }
69                final GroupData group = groups.get(0);
70                log.info("PAY REQUEST by {}", group.getId());
71                            
72                 SwingUtilities.invokeLater(new Runnable() {
73                                         @Override
74                                         public void run() {
75                                                 GroupAgent.getInstance().getGroupModel().setGroupWantsToPay(group.getId());
76                                         }
77                                 });
78                 
79             }
80         };
81           try {
82                 notificationMgr.createNotification(paymentRequestContainer, paymentRequest, Operation.WRITE);
83         } catch (Exception e) {
84            handleSpaceErrorAndTerminate(e);
85         }
86         }
87
88         public void listenForPaymentDone() {
89                 NotificationListener payment = new NotificationListener() {
90             @Override
91             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
92                 
93                 final List<GroupData> groups = castEntries(entries);
94                 
95                 if(groups.size() != 1) {
96                         throw new RuntimeException("A group can only have paid as a whole!");
97                 }
98                final GroupData group = groups.get(0);
99                
100                            
101                 SwingUtilities.invokeLater(new Runnable() {
102                                         @Override
103                                         public void run() {
104                                                 GroupAgent.getInstance().getGroupModel().setGroupHasPaid(group.getId());
105                                         }
106                                 });
107                 
108             }
109         };
110           try {
111                 notificationMgr.createNotification(paymentDoneContainer, payment, Operation.WRITE);
112         } catch (Exception e) {
113            handleSpaceErrorAndTerminate(e);
114         }
115         }
116
117         public void listenForTableAssigned() {
118                 NotificationListener tableAssignmentListener = new NotificationListener() {
119                         
120             @Override
121             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
122                log.info("A table was assigned to a group, heureka!");
123                                 
124                                 final List<Table> tables = castEntries(entries);
125                                 
126                                 SwingUtilities.invokeLater(new Runnable() {
127                                         @Override
128                                         public void run() {
129                                                 GroupAgent.getInstance().getGroupModel().setGroupsSitting(tables);
130                                         }
131                                 });
132             }
133         };
134         try {
135                         notificationMgr.createNotification(tableAssignedContainer, tableAssignmentListener, Operation.WRITE);
136                         log.info("Created tableOccupiedContainer notification for the group agent");
137         } catch (Exception e) {
138             handleSpaceErrorAndTerminate(e);
139          }
140         }
141
142         public void listenForOrdersTaken() {
143                 NotificationListener orderTakenListener = new NotificationListener() {
144             @Override
145             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
146                
147                                 
148                                 final List<GroupData> groups = castEntries(entries);
149                                 
150                         if(groups.size() != 1) {
151                         throw new RuntimeException("A group always orders as a whole!");
152                 }
153                final GroupData group = groups.get(0);
154                                 
155                                 SwingUtilities.invokeLater(new Runnable() {
156                                         
157                                         @Override
158                                         public void run() {
159                                                 GroupAgent.getInstance().getGroupModel().setOrderTaken(group);
160                                         }
161                                 });
162             }
163         };
164         try {
165                         notificationMgr.createNotification(orderTakenContainer, orderTakenListener, Operation.WRITE);
166                         log.info("Created orderTakenContainer notification for the group agent");
167         } catch (Exception e) {
168             handleSpaceErrorAndTerminate(e);
169          }
170         }
171
172 }