]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java
Merge branch 'master' of
[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.net.URI;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
7
8 import org.mozartspaces.capi3.AnyCoordinator;
9 import org.mozartspaces.capi3.Coordinator;
10 import org.mozartspaces.capi3.FifoCoordinator;
11 import org.mozartspaces.capi3.LindaCoordinator;
12 import org.mozartspaces.capi3.LindaCoordinator.LindaSelector;
13 import org.mozartspaces.core.Capi;
14 import org.mozartspaces.core.ContainerReference;
15 import org.mozartspaces.core.DefaultMzsCore;
16 import org.mozartspaces.core.Entry;
17 import org.mozartspaces.core.MzsConstants.RequestTimeout;
18 import org.mozartspaces.core.MzsCore;
19 import org.mozartspaces.core.MzsCoreException;
20 import org.mozartspaces.core.TransactionReference;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import at.ac.tuwien.sbc.valesriegler.DEP_Table;
25 import at.ac.tuwien.sbc.valesriegler.common.SpaceUtil;
26 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
27 import at.ac.tuwien.sbc.valesriegler.types.GroupState;
28 import at.ac.tuwien.sbc.valesriegler.types.Order;
29 import at.ac.tuwien.sbc.valesriegler.types.Table;
30
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 {
39         private static final Logger log = LoggerFactory.getLogger(XVSMConnector.class);
40         
41         private ContainerReference tablesContainer;
42         private ContainerReference groupsContainer;
43 //      private ContainerReference notificationContainer;
44         private Capi capi;
45
46         
47         public void initSpaceCommunication()  {
48                 try {
49                         MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
50                         capi = new Capi(core);
51                 } catch (Exception e) {
52                         log.error("Space connection could not be established! Have you started the Space Server?");
53                         handleSpaceErrorAndTerminate(e);
54                 }
55         }
56         
57         public void useTablesContainer() {
58                 try {
59                         tablesContainer = SpaceUtil.getOrCreateNamedContainer(SpaceUtil.SERVER_ADDR, SpaceUtil.TABLES_CONTAINER, capi, createCoordinators(new AnyCoordinator()));
60                 } catch (MzsCoreException e) {
61                         handleSpaceErrorAndTerminate(e);
62                 }
63         }
64         
65         public void useGroupsContainer() {
66                 try {
67                         groupsContainer = SpaceUtil.getOrCreateNamedContainer(SpaceUtil.SERVER_ADDR, SpaceUtil.GROUPS_CONTAINER, capi, createCoordinators(new AnyCoordinator(), new LindaCoordinator()));
68                 } catch (MzsCoreException e) {
69                         handleSpaceErrorAndTerminate(e);
70                 }
71         }
72         
73         private List<Coordinator> createCoordinators(Coordinator... coordinator) {
74                 return Arrays.asList(coordinator);
75         }
76         
77         private void handleSpaceErrorAndTerminate(Exception e) {
78                 log.error(e.getMessage());
79                 System.exit(1);
80         }
81         
82         public void sendTablesToSpace(List<DEP_Table> tables) {
83                 try {
84                         List<Entry> entries = new ArrayList<>();
85                         for (DEP_Table table : tables) {
86                                 entries.add(new Entry(table));
87                         }
88                         capi.write(entries, tablesContainer);
89                         
90                         log.info("Wrote tables to Space!");
91                 } catch (MzsCoreException e) {
92                         log.info(e.getMessage());
93                 }
94         }
95
96         public List<DEP_Table> readTables() {
97                 ArrayList<DEP_Table> tables = new ArrayList<>();
98                 try {
99                         tables = capi.take(tablesContainer);
100                 } catch (MzsCoreException e) {
101                         log.error(e.getMessage());
102                         e.printStackTrace();
103                         System.exit(1);
104                 }
105                 return tables;
106         }
107
108         public void createGroups(List<GroupData> newGroups) {
109                 try {
110                         List<Entry> entries = new ArrayList<>();
111                         for (GroupData group : newGroups) {
112                                 entries.add(new Entry(group));
113                         }
114                         capi.write(entries, groupsContainer);
115                         
116                         log.info("Wrote new Groups to Space!");
117                 } catch (MzsCoreException e) {
118                         log.info(e.getMessage());
119                 }
120         }
121
122         public void handleWaitingGroup(int id) {
123                 try {
124                         TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(SpaceUtil.SERVER_ADDR));
125                         DEP_Table table = null;
126                         try {
127                                 ArrayList<DEP_Table> tables = capi.take(tablesContainer, FifoCoordinator.newSelector(), RequestTimeout.ZERO, tx);
128                                 table = tables.get(0);
129                         } catch (Exception e) {
130                                 log.info("There is no free table");
131                                 capi.rollbackTransaction(tx);
132                                 return;
133                         }
134                         
135                         GroupData groupData = new GroupData(null);
136                         groupData.setState(GroupState.WAITING);
137                         LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
138                         ArrayList<GroupData> waitingGroups = null;
139                         
140                         try {
141                                 waitingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
142                         } catch (Exception e) {
143                                 log.info("There is no waiting group");
144                                 capi.rollbackTransaction(tx);
145                                 return;
146                         }
147                         
148                         GroupData group = waitingGroups.get(0);
149                         group.setTable(new Table(table.getGroupId()));
150                         group.setTableWaiter(id);
151                         group.setState(GroupState.SITTING);
152                         
153                         try {
154                                 capi.write(groupsContainer, RequestTimeout.TRY_ONCE,  tx, new Entry(group));
155                         } catch (Exception e) {
156                                 e.printStackTrace();
157                                 capi.rollbackTransaction(tx);
158                                 return;
159                         }
160                         
161                         try {
162                                 capi.write(new Entry(table), tablesContainer, RequestTimeout.TRY_ONCE, tx);
163                         } catch (Exception e) {
164                                 e.printStackTrace();
165                                 capi.rollbackTransaction(tx);
166                                 return;
167                         }
168                         
169                         capi.commitTransaction(tx);
170                 } catch (MzsCoreException e) {
171                         e.printStackTrace();
172                 }
173         }
174
175         public void handleOrderRequest(int id) {
176                 try {
177                         TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(SpaceUtil.SERVER_ADDR));
178                                         
179                         GroupData groupData = new GroupData(null);
180                         groupData.setState(GroupState.SITTING);
181                         LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
182                         ArrayList<GroupData> sittingGroups = null;
183                         
184                         try {
185                                 sittingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
186                         } catch (Exception e) {
187                                 log.info("There is no sitting group");
188                                 capi.rollbackTransaction(tx);
189                                 return;
190                         }
191                         GroupData sittingGroup = sittingGroups.get(0);
192                         Order order = sittingGroup.getOrder();
193                         sittingGroup.setOrderWaiter(id);
194                         sittingGroup.setState(GroupState.ORDERED);
195                         
196                         try {
197                                 capi.write(new Entry(sittingGroup), groupsContainer, RequestTimeout.TRY_ONCE, tx);
198                         } catch (Exception e) {
199                                 e.printStackTrace();
200                                 capi.rollbackTransaction(tx);
201                                 return;
202                         }
203                         
204                         capi.commitTransaction(tx);
205                         
206                 } catch (MzsCoreException e) {
207                         e.printStackTrace();
208                 }
209         }
210
211
212
213         public void handlePizzaDistribution() {
214         }
215
216         public void handlePaymentRequest() {
217         }
218
219
220 }