]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java
Space cooks prepare pizzas and Pizzeria GUI shows that
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / xvsm / XVSMConnector.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.bouncycastle.crypto.RuntimeCryptoException;
11 import org.mozartspaces.capi3.AnyCoordinator.AnySelector;
12 import org.mozartspaces.capi3.CountNotMetException;
13 import org.mozartspaces.capi3.FifoCoordinator;
14 import org.mozartspaces.capi3.LindaCoordinator;
15 import org.mozartspaces.capi3.LindaCoordinator.LindaSelector;
16 import org.mozartspaces.core.Entry;
17 import org.mozartspaces.core.MzsConstants.RequestTimeout;
18 import org.mozartspaces.core.MzsCoreException;
19 import org.mozartspaces.core.TransactionReference;
20 import org.mozartspaces.notifications.Notification;
21 import org.mozartspaces.notifications.NotificationListener;
22 import org.mozartspaces.notifications.Operation;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import at.ac.tuwien.sbc.valesriegler.common.Util;
27 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
28 import at.ac.tuwien.sbc.valesriegler.types.GroupState;
29 import at.ac.tuwien.sbc.valesriegler.types.Order;
30 import at.ac.tuwien.sbc.valesriegler.types.Table;
31
32 /**
33  * Responsible for XVSM Communication
34  * 
35  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
36  * 
37  */
38 public class XVSMConnector extends AbstractXVSMConnector {
39         static final Logger log = LoggerFactory.getLogger(XVSMConnector.class);
40
41         @Deprecated
42         public void handleWaitingGroup(int id) {
43
44                 try {
45                         TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
46                         Table table = null;
47                         try {
48                                 ArrayList<Table> tables = capi.take(tablesContainer, FifoCoordinator.newSelector(), RequestTimeout.ZERO, tx);
49                                 table = tables.get(0);
50                         } catch (Exception e) {
51                                 log.info("There is no free table");
52                                 capi.rollbackTransaction(tx);
53                                 return;
54                         }
55                         
56                         GroupData groupData = new GroupData(null);
57                         groupData.setState(GroupState.WAITING);
58                         LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
59                         ArrayList<GroupData> waitingGroups = null;
60                         
61                         try {
62                                 waitingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
63                         } catch (Exception e) {
64                                 log.info("There is no waiting group");
65                                 capi.rollbackTransaction(tx);
66                                 return;
67                         }
68                         
69                         GroupData group = waitingGroups.get(0);
70                         group.setTable(new Table(table.getGroupId()));
71                         group.setTableWaiter(id);
72                         group.setState(GroupState.SITTING);
73                         
74                         try {
75                                 capi.write(groupsContainer, RequestTimeout.TRY_ONCE,  tx, new Entry(group));
76                         } catch (Exception e) {
77                                 e.printStackTrace();
78                                 capi.rollbackTransaction(tx);
79                                 return;
80                         }
81                         
82                         try {
83                                 capi.write(new Entry(table), tablesContainer, RequestTimeout.TRY_ONCE, tx);
84                         } catch (Exception e) {
85                                 e.printStackTrace();
86                                 capi.rollbackTransaction(tx);
87                                 return;
88                         }
89                         
90                         capi.commitTransaction(tx);
91                 } catch (MzsCoreException e) {
92                         e.printStackTrace();
93                 }
94         }
95
96         @Deprecated
97         public void handleOrderRequest(int id) {
98                 try {
99                         TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
100                                         
101                         GroupData groupData = new GroupData(null);
102                         groupData.setState(GroupState.SITTING);
103                         LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
104                         ArrayList<GroupData> sittingGroups = null;
105                         
106                         try {
107                                 sittingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
108                         } catch (Exception e) {
109                                 log.info("There is no sitting group");
110                                 capi.rollbackTransaction(tx);
111                                 return;
112                         }
113                         GroupData sittingGroup = sittingGroups.get(0);
114                         Order order = sittingGroup.getOrder();
115                         sittingGroup.setOrderWaiter(id);
116                         sittingGroup.setState(GroupState.ORDERED);
117                         
118                         try {
119                                 capi.write(new Entry(sittingGroup), groupsContainer, RequestTimeout.TRY_ONCE, tx);
120                         } catch (Exception e) {
121                                 e.printStackTrace();
122                                 capi.rollbackTransaction(tx);
123                                 return;
124                         }
125                         
126                         capi.commitTransaction(tx);
127                         
128                 } catch (MzsCoreException e) {
129                         e.printStackTrace();
130                 }
131         }
132
133         public void initTablesNotifications(NotificationListener listener) {
134         try {
135                 notificationMgr.createNotification(tablesContainer, listener, Operation.WRITE);
136         } catch (Exception e) {
137             handleSpaceErrorAndTerminate(e);
138         }
139         }
140         
141         public void initGroupNotifications(NotificationListener listener) {
142         try {
143                 notificationMgr.createNotification(groupsContainer, listener, Operation.WRITE);
144         } catch (Exception e) {
145            handleSpaceErrorAndTerminate(e);
146         }
147         }
148         
149         
150         
151 }