]> 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.io.Serializable;
4 import java.net.URI;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.List;
8
9 import javax.swing.SwingUtilities;
10
11 import org.mozartspaces.capi3.AnyCoordinator;
12 import org.mozartspaces.capi3.Coordinator;
13 import org.mozartspaces.capi3.FifoCoordinator;
14 import org.mozartspaces.capi3.LindaCoordinator;
15 import org.mozartspaces.capi3.LindaCoordinator.LindaSelector;
16 import org.mozartspaces.core.Capi;
17 import org.mozartspaces.core.ContainerReference;
18 import org.mozartspaces.core.DefaultMzsCore;
19 import org.mozartspaces.core.Entry;
20 import org.mozartspaces.core.MzsConstants.RequestTimeout;
21 import org.mozartspaces.core.MzsCore;
22 import org.mozartspaces.core.MzsCoreException;
23 import org.mozartspaces.core.TransactionReference;
24 import org.mozartspaces.notifications.Notification;
25 import org.mozartspaces.notifications.NotificationListener;
26 import org.mozartspaces.notifications.NotificationManager;
27 import org.mozartspaces.notifications.Operation;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import at.ac.tuwien.sbc.valesriegler.DEP_Table;
32 import at.ac.tuwien.sbc.valesriegler.common.Util;
33 import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent;
34 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
35 import at.ac.tuwien.sbc.valesriegler.types.GroupState;
36 import at.ac.tuwien.sbc.valesriegler.types.Order;
37 import at.ac.tuwien.sbc.valesriegler.types.Table;
38
39
40 /**
41  * Responsible for XVSM Communication
42  * 
43  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
44  *
45  */
46 public class XVSMConnector {
47         private static final Logger log = LoggerFactory.getLogger(XVSMConnector.class);
48         
49         private ContainerReference tablesContainer;
50         private ContainerReference groupsContainer;
51 //      private ContainerReference notificationContainer;
52         private Capi capi;
53         private NotificationManager notificationMgr;
54
55         
56         public void initSpaceCommunication()  {
57                 try {
58                         MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
59                         capi = new Capi(core);
60                         notificationMgr = new NotificationManager(core);
61                 } catch (Exception e) {
62                         log.error("Space connection could not be established! Have you started the Space Server?");
63                         handleSpaceErrorAndTerminate(e);
64                 }
65         }
66         
67         public void useTablesContainer() {
68                 try {
69                         tablesContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.TABLES_CONTAINER, capi, createCoordinators(new AnyCoordinator()));
70                 } catch (MzsCoreException e) {
71                         handleSpaceErrorAndTerminate(e);
72                 }
73         }
74         
75         public void useGroupsContainer() {
76                 try {
77                         groupsContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.GROUPS_CONTAINER, capi, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
78                 } catch (MzsCoreException e) {
79                         handleSpaceErrorAndTerminate(e);
80                 }
81         }
82         
83         private List<Coordinator> createCoordinators(Coordinator... coordinator) {
84                 return Arrays.asList(coordinator);
85         }
86         
87         private void handleSpaceErrorAndTerminate(Exception e) {
88                 log.error(e.getMessage());
89                 System.exit(1);
90         }
91         
92         public void sendTablesToSpace(List<DEP_Table> tables) {
93                 try {
94                         List<Entry> entries = new ArrayList<>();
95                         for (DEP_Table table : tables) {
96                                 entries.add(new Entry(table));
97                         }
98                         capi.write(entries, tablesContainer);
99                         
100                         log.info("Wrote tables to Space!");
101                 } catch (MzsCoreException e) {
102                         log.info(e.getMessage());
103                 }
104         }
105
106         public void createGroups(List<GroupData> newGroups) {
107                 try {
108                         List<Entry> entries = new ArrayList<>();
109                         for (GroupData group : newGroups) {
110                                 entries.add(new Entry(group));
111                         }
112                         capi.write(entries, groupsContainer);
113                         
114                         log.info("Wrote new Groups to Space!");
115                 } catch (MzsCoreException e) {
116                         log.info(e.getMessage());
117                         e.printStackTrace();
118                 }
119         }
120
121         public void handleWaitingGroup(int id) {
122                 try {
123                         TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
124                         DEP_Table table = null;
125                         try {
126                                 ArrayList<DEP_Table> tables = capi.take(tablesContainer, FifoCoordinator.newSelector(), RequestTimeout.ZERO, tx);
127                                 table = tables.get(0);
128                         } catch (Exception e) {
129                                 log.info("There is no free table");
130                                 capi.rollbackTransaction(tx);
131                                 return;
132                         }
133                         
134                         GroupData groupData = new GroupData(null);
135                         groupData.setState(GroupState.WAITING);
136                         LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
137                         ArrayList<GroupData> waitingGroups = null;
138                         
139                         try {
140                                 waitingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
141                         } catch (Exception e) {
142                                 log.info("There is no waiting group");
143                                 capi.rollbackTransaction(tx);
144                                 return;
145                         }
146                         
147                         GroupData group = waitingGroups.get(0);
148                         group.setTable(new Table(table.getGroupId()));
149                         group.setTableWaiter(id);
150                         group.setState(GroupState.SITTING);
151                         
152                         try {
153                                 capi.write(groupsContainer, RequestTimeout.TRY_ONCE,  tx, new Entry(group));
154                         } catch (Exception e) {
155                                 e.printStackTrace();
156                                 capi.rollbackTransaction(tx);
157                                 return;
158                         }
159                         
160                         try {
161                                 capi.write(new Entry(table), tablesContainer, RequestTimeout.TRY_ONCE, tx);
162                         } catch (Exception e) {
163                                 e.printStackTrace();
164                                 capi.rollbackTransaction(tx);
165                                 return;
166                         }
167                         
168                         capi.commitTransaction(tx);
169                 } catch (MzsCoreException e) {
170                         e.printStackTrace();
171                 }
172         }
173
174         public void handleOrderRequest(int id) {
175                 try {
176                         TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
177                                         
178                         GroupData groupData = new GroupData(null);
179                         groupData.setState(GroupState.SITTING);
180                         LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
181                         ArrayList<GroupData> sittingGroups = null;
182                         
183                         try {
184                                 sittingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
185                         } catch (Exception e) {
186                                 log.info("There is no sitting group");
187                                 capi.rollbackTransaction(tx);
188                                 return;
189                         }
190                         GroupData sittingGroup = sittingGroups.get(0);
191                         Order order = sittingGroup.getOrder();
192                         sittingGroup.setOrderWaiter(id);
193                         sittingGroup.setState(GroupState.ORDERED);
194                         
195                         try {
196                                 capi.write(new Entry(sittingGroup), groupsContainer, RequestTimeout.TRY_ONCE, tx);
197                         } catch (Exception e) {
198                                 e.printStackTrace();
199                                 capi.rollbackTransaction(tx);
200                                 return;
201                         }
202                         
203                         capi.commitTransaction(tx);
204                         
205                 } catch (MzsCoreException e) {
206                         e.printStackTrace();
207                 }
208         }
209
210         public void initPizzeriaNotifications() {
211                 // When tables are written to the space the Pizzeria gets informed and sets the new tables in the tables model!
212                  NotificationListener tablesListener = new NotificationListener() {
213                     @Override
214                     public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
215                         
216                         final List<DEP_Table> tables = new ArrayList<>();
217                         List<Entry> entryList = (List<Entry>) entries;
218                         for (Entry entry : entryList) {
219                                                 tables.add((DEP_Table) entry.getValue());
220                                         }
221                         SwingUtilities.invokeLater(new Runnable() {
222                                                 @Override
223                                                 public void run() {
224                                                         PizzeriaAgent.getInstance().getTablesModel().mergeTables(tables);
225                                                 }
226                                         });
227                     }
228                 };
229                 
230                 NotificationListener groupsListener = new NotificationListener() {
231                     @Override
232                     public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
233                         List<GroupData> tables = (List<GroupData>) entries;
234 //                      TODO problem at the moment: Item type of groupoverviewmodel is Group and not GroupData
235                     }
236                 };
237                 
238                 try {
239                         notificationMgr.createNotification(tablesContainer, groupsListener, Operation.WRITE);
240                         notificationMgr.createNotification(tablesContainer, tablesListener, Operation.WRITE);
241                 } catch (Exception e) {
242                     e.printStackTrace();
243                 }
244         }
245         
246         public void initGroupNotifications() {
247                  NotificationListener notifListener = new NotificationListener() {
248                     @Override
249                     public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
250                         List<GroupData> groups = (List<GroupData>) entries;
251
252                         // when a groupdata is changed, call setItems on the groupoverviewmodel. TODO problem at the moment: Item type of groupoverviewmodel is Group and not GroupData
253                     }
254
255                 };
256                 try {
257                         notificationMgr.createNotification(groupsContainer, notifListener, Operation.WRITE);
258                 } catch (Exception e) {
259                    handleSpaceErrorAndTerminate(e);
260                 }
261         }
262
263         public void handlePaymentRequest() {
264         }
265
266         public void handlePizzaDistribution() {
267         }
268
269
270 }