]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java
Merge branch 'master' of https://bitbucket.org/rgregor/sbc-ss-2013
[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.common.Util;
32 import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent;
33 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
34 import at.ac.tuwien.sbc.valesriegler.types.GroupState;
35 import at.ac.tuwien.sbc.valesriegler.types.Order;
36 import at.ac.tuwien.sbc.valesriegler.types.Table;
37
38 /**
39  * Responsible for XVSM Communication
40  * 
41  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
42  * 
43  */
44 public class XVSMConnector {
45         private static final Logger log = LoggerFactory.getLogger(XVSMConnector.class);
46
47         private ContainerReference tablesContainer;
48         private ContainerReference groupsContainer;
49 //      private ContainerReference notificationContainer;
50         private Capi capi;
51         private NotificationManager notificationMgr;
52
53         
54         public void initSpaceCommunication()  {
55                 try {
56                         MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
57                         capi = new Capi(core);
58                         notificationMgr = new NotificationManager(core);
59                 } catch (Exception e) {
60                         log.error("Space connection could not be established! Have you started the Space Server?");
61                         handleSpaceErrorAndTerminate(e);
62                 }
63         }
64         
65         public void useTablesContainer() {
66                 try {
67                         tablesContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.TABLES_CONTAINER, capi, createCoordinators(new AnyCoordinator()));
68                 } catch (MzsCoreException e) {
69                         handleSpaceErrorAndTerminate(e);
70                 }
71         }
72         
73         public void useGroupsContainer() {
74                 try {
75                         groupsContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.GROUPS_CONTAINER, capi, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
76                 } catch (MzsCoreException e) {
77                         handleSpaceErrorAndTerminate(e);
78                 }
79         }
80         
81         private List<Coordinator> createCoordinators(Coordinator... coordinator) {
82                 return Arrays.asList(coordinator);
83         }
84         
85         private void handleSpaceErrorAndTerminate(Exception e) {
86                 log.error(e.getMessage());
87                 System.exit(1);
88         }
89         
90         public void sendTablesToSpace(List<Table> tables) {
91                 try {
92                         List<Entry> entries = new ArrayList<>();
93                         for (Table table : tables) {
94                                 entries.add(new Entry(table));
95                         }
96                         capi.write(entries, tablesContainer);
97                         
98                         log.info("Wrote tables to Space!");
99
100                 } catch (MzsCoreException e) {
101                         log.info(e.getMessage());
102                 }
103         }
104
105         public void createGroups(List<GroupData> newGroups) {
106                 try {
107                         List<Entry> entries = new ArrayList<>();
108                         for (GroupData group : newGroups) {
109                                 entries.add(new Entry(group));
110                         }
111                         capi.write(entries, groupsContainer);
112                         
113                         log.info("Wrote new Groups to Space!");
114                 } catch (MzsCoreException e) {
115                         log.info(e.getMessage());
116                         e.printStackTrace();
117                 }
118         }
119
120         public void handleWaitingGroup(int id) {
121
122                 try {
123                         TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
124                         Table table = null;
125                         try {
126                                 ArrayList<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<Table> tables = new ArrayList<>();
217                         List<Entry> entryList = (List<Entry>) entries;
218                         for (Entry entry : entryList) {
219                                                 tables.add((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 }