]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSMConnector.java
Show orders in PizzeriaGUI
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / xvsm / WaiterXVSMConnector.java
1 package at.ac.tuwien.sbc.valesriegler.xvsm;
2
3 import java.io.Serializable;
4 import java.net.URI;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Collections;
8 import java.util.List;
9
10 import org.mozartspaces.capi3.CountNotMetException;
11 import org.mozartspaces.core.MzsConstants.RequestTimeout;
12 import org.mozartspaces.core.MzsCoreException;
13 import org.mozartspaces.core.MzsTimeoutException;
14 import org.mozartspaces.core.TransactionException;
15 import org.mozartspaces.core.TransactionReference;
16 import org.mozartspaces.notifications.Notification;
17 import org.mozartspaces.notifications.NotificationListener;
18 import org.mozartspaces.notifications.Operation;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import at.ac.tuwien.sbc.valesriegler.common.Util;
23 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
24 import at.ac.tuwien.sbc.valesriegler.types.GroupState;
25 import at.ac.tuwien.sbc.valesriegler.types.OrderStatus;
26 import at.ac.tuwien.sbc.valesriegler.types.Table;
27
28 public class WaiterXVSMConnector extends AbstractXVSMConnector {
29         private static final Logger log = LoggerFactory.getLogger(WaiterXVSMConnector.class);
30         private final int waiterId;
31         
32         public WaiterXVSMConnector(int waiterId) {
33                 super();
34                 
35                 this.waiterId = waiterId;
36                 
37                 useFreeTablesContainer();
38                 useAssignTableContainer();
39                 useTablesContainer();
40                 useTakeOrderContainer();
41                 useorderContainer();
42                 useGroupsContainer();
43         }
44         
45         public void listenForFreeTable() {
46                         
47                 NotificationListener freeTableListener = new NotificationListener() {
48             @Override
49             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
50                
51                                 log.info("{} tables have become free", entries.size());
52                                 
53                                 List<Table> tables = castEntries(entries);
54                                 Collections.shuffle(tables);
55                                 
56                                 for (Table table : tables) {
57                                         try {
58                                                 TransactionReference tx = capi.createTransaction(3000, URI.create(Util.SERVER_ADDR));
59                                                 
60                                                 try {
61                                                         // Acquire a lock for the free table in the FreeTableContainer
62                                                         int id = table.getId();
63                                                         log.info("Try to find the table with id {}", id);
64                                                 
65                                                         Table tableTemplate = new Table(id);
66                                                         Table lockedFreeTable = takeEntityByTemplateFromContainer(tableTemplate, freeTablesContainer, tx, 1000, String.format("There was no free table found with id %d", id));
67                                                         
68                                                         log.info("Table with id {} was found", id);
69                                                         
70                                                         GroupData groupTemplate = new GroupData();
71                                                         GroupData lockedGroup = takeEntityByTemplateFromContainer(groupTemplate, assignTableContainer, tx, RequestTimeout.DEFAULT, "There is no group waiting for a table at the moment");
72                                         
73                                                         assignGroupToTable(lockedGroup, lockedFreeTable, tx);
74                                                         
75                                                 } catch (IllegalArgumentException e) {
76                                                         log.info("IllegalArgumentException");
77                                                         e.printStackTrace();
78                                                 } catch (EntityNotFoundByTemplate e) {
79                                                         log.info(e.getMessage());
80                                                 }  catch (Exception e) {
81                                                         log.error("AN INNER EXCEPTION");
82                                                         e.printStackTrace();
83                                                 }
84                                         } catch (TransactionException e) {
85                                                 log.info("An unimportant TransactionException has occurred");
86                                         } catch (Exception e) {
87                                                 log.error("OUTER EXCEPTION");
88                                         }
89                                         
90                                 }
91             }
92         };
93         try {
94                         notificationMgr.createNotification(freeTablesContainer, freeTableListener, Operation.WRITE);
95                         log.info("Created freeTablesContainer notification for a waiter");
96                 } catch (MzsCoreException e) {
97                         e.printStackTrace();
98                 } catch (InterruptedException e) {
99                         e.printStackTrace();
100                 }
101                         
102         }
103         
104         public void listenForNewGuests() {
105                 NotificationListener newGroupsListener = new NotificationListener() {
106             @Override
107             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
108                
109                                 log.info("New guest groups have arrived");
110                                 
111                                 List<GroupData> groups = castEntries(entries);
112                                 Collections.shuffle(groups);
113                                 
114                                 for (GroupData group : groups) {
115                                         try {
116                                                 TransactionReference tx = capi.createTransaction(1500, URI.create(Util.SERVER_ADDR));
117                                                 
118                                                 try {
119                                                         // Acquire a lock for the group in the AssignTableContainer
120                                                         String groupNotFound = String.format("Group with id %d was already assigned a table by another waiter!", group.getId());
121                                                         
122                                                         GroupData lockedGroup = takeEntityByTemplateFromContainer(new GroupData(group.getId()), assignTableContainer, tx, 1000, groupNotFound);
123                                                         // Acquire a lock for one free table in the TablesContainer
124                                                         Table lockedFreeTable = takeEntityByTemplateFromContainer(new Table(null), freeTablesContainer, tx, RequestTimeout.DEFAULT, String.format("No free table for group with id %d could be found", group.getId()));
125                                                         
126                                                         assignGroupToTable(lockedGroup, lockedFreeTable, tx);
127                                                         
128                                                 } catch (IllegalArgumentException e) {
129                                                         log.info("IllegalArgumentException");
130                                                         e.printStackTrace();
131                                                 } catch (EntityNotFoundByTemplate e) {
132                                                         log.info(e.getMessage());
133                                                 }  catch (Exception e) {
134                                                         log.error("AN INNER EXCEPTION");
135                                                         e.printStackTrace();
136                                                 }
137                                         } catch (TransactionException e) {
138                                                 log.info("An unimportant TransactionException has occurred");
139                                         } catch (Exception e) {
140                                                 log.error("OUTER EXCEPTION");
141                                         }
142                                         
143                                 }
144             }
145         };
146         try {
147                         notificationMgr.createNotification(assignTableContainer, newGroupsListener, Operation.WRITE);
148                         log.info("Created assingTableContainer notification for a waiter");
149                 } catch (MzsCoreException e) {
150                         e.printStackTrace();
151                 } catch (InterruptedException e) {
152                         e.printStackTrace();
153                 }
154         }
155
156         public void listenForOrders() {
157                 NotificationListener ordersListener = new NotificationListener() {
158             @Override
159             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
160                log.info("A new order has arrived!");
161                
162                List<GroupData> groups = castEntries(entries);
163                if(groups.size() != 1) throw new RuntimeException("Multiple orders in one notification?! That should not happen!");
164                
165                GroupData groupData = groups.get(0);
166                
167                try {
168                    TransactionReference tx = capi.createTransaction(1500, URI.create(Util.SERVER_ADDR));
169                    GroupData entity = new GroupData(groupData.getId());
170                    entity.setState(GroupState.ORDER_PENDING);
171                    takeEntityByTemplateFromContainer(entity, takeOrderContainer, tx, RequestTimeout.DEFAULT, String.format("The order for group %d was already taken by an other waiter!", groupData.getId()));
172                    
173                   
174                                         log.info("Will now write taken order from group {} to space", groupData.getId());
175                                         groupData.setOrderWaiter(waiterId);
176                                         groupData.setState(GroupState.ORDERED);
177                                            groupData.getOrder().setStatus(OrderStatus.ORDERED);
178                                            sendItemsToContainer(Arrays.asList(groupData), ordersContainer, RequestTimeout.ZERO, tx);
179                                            sendItemsToContainer(Arrays.asList(groupData), groupsContainer, RequestTimeout.ZERO, tx);
180                                            capi.commitTransaction(tx);
181                         
182                    
183                    log.info("Waiter has taken order from group {}", groupData.getId());
184                    
185                } catch (MzsCoreException e) {
186                    log.info("ERROR in listenForOrders");
187                    e.printStackTrace();
188                }
189             }
190                 };
191                 
192         try {
193                         notificationMgr.createNotification(takeOrderContainer, ordersListener, Operation.WRITE);
194                         log.info("Created takeOrderContainer notification for a waiter");
195                 } catch (MzsCoreException e) {
196                         e.printStackTrace();
197                 } catch (InterruptedException e) {
198                         e.printStackTrace();
199                 }
200         }
201
202         private void assignGroupToTable(GroupData lockedGroup,
203                         Table lockedFreeTable, TransactionReference tx)
204                         throws MzsCoreException {
205                 // The new group sits down at the table
206                 lockedFreeTable.setGroupId(lockedGroup.getId());
207
208                 
209                 // The new group now wants to order
210                 lockedGroup.setState(GroupState.ORDER_PENDING);
211                 lockedGroup.setTable(lockedFreeTable);
212                 lockedGroup.setTableWaiter(waiterId);
213                 
214                 sendItemsToContainer(Arrays.asList(lockedFreeTable), tablesContainer, RequestTimeout.ZERO, tx);
215                 sendItemsToContainer(Arrays.asList(lockedGroup), takeOrderContainer, RequestTimeout.ZERO, tx);
216                 
217                 
218                 sendItemsToContainer(Arrays.asList(lockedGroup), groupsContainer, RequestTimeout.ZERO, tx);
219                 
220                 try {
221                         capi.commitTransaction(tx);
222                         log.info("Assigned table to group with groupId {}", lockedGroup.getId());
223                 } catch (Exception e) {
224                         log.info("Assigning a table to group with groupId {} failed", lockedGroup.getId());
225                 }
226                 
227         }
228
229 }