]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java
Change the pizzeria models so that they display (more) correct values
[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                 e.printStackTrace();
88                 System.exit(1);
89         }
90         
91         public void sendTablesToSpace(List<Table> tables) {
92                 try {
93                         List<Entry> entries = new ArrayList<>();
94                         for (Table table : tables) {
95                                 entries.add(new Entry(table));
96                         }
97                         capi.write(entries, tablesContainer);
98                         
99                         log.info("Wrote tables to Space!");
100
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
123                 try {
124                         TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
125                         Table table = null;
126                         try {
127                                 ArrayList<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(Util.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         public void initPizzeriaNotifications() {
212                 // When tables are written to the space the Pizzeria gets informed and sets the new tables in the tables model!
213                  NotificationListener tablesListener = new NotificationListener() {
214                     @Override
215                     public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
216                         
217                         final List<Table> tables = new ArrayList<>();
218                         List<Entry> entryList = (List<Entry>) entries;
219                         for (Entry entry : entryList) {
220                                                 tables.add((Table) entry.getValue());
221                                         }
222                         SwingUtilities.invokeLater(new Runnable() {
223                                                 @Override
224                                                 public void run() {
225                                                         PizzeriaAgent.getInstance().getTablesModel().mergeTables(tables);
226                                                 }
227                                         });
228                     }
229                 };
230                 
231                 NotificationListener groupsListener = new NotificationListener() {
232                     @Override
233                     public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
234                         List<GroupData> tables = (List<GroupData>) entries;
235 //                      TODO problem at the moment: Item type of groupoverviewmodel is Group and not GroupData
236                     }
237                 };
238                 
239                 try {
240                         notificationMgr.createNotification(tablesContainer, groupsListener, Operation.WRITE);
241                         notificationMgr.createNotification(tablesContainer, tablesListener, Operation.WRITE);
242                 } catch (Exception e) {
243                     e.printStackTrace();
244                 }
245         }
246         
247         public void initGroupNotifications() {
248                  NotificationListener notifListener = new NotificationListener() {
249                     @Override
250                     public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
251                         List<GroupData> groups = (List<GroupData>) entries;
252
253                         // when a groupdata is changed, call setItems on the groupoverviewmodel. TODO problem at the moment: Item type of groupoverviewmodel is Group and not GroupData
254                     }
255
256                 };
257                 try {
258                         notificationMgr.createNotification(groupsContainer, notifListener, Operation.WRITE);
259                 } catch (Exception e) {
260                    handleSpaceErrorAndTerminate(e);
261                 }
262         }
263
264         public void handlePaymentRequest() {
265         }
266
267         public void handlePizzaDistribution() {
268         }
269
270
271 }