]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java
XVSMConnector refactoring
[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.group.GroupAgent;
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  * Responsible for XVSM Communication
41  * 
42  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
43  * 
44  */
45 public class XVSMConnector {
46         private static final Logger log = LoggerFactory.getLogger(XVSMConnector.class);
47
48         private ContainerReference tablesContainer;
49         private ContainerReference groupsContainer;
50 //      private ContainerReference notificationContainer;
51         private Capi capi;
52         private NotificationManager notificationMgr;
53
54         
55         public void initSpaceCommunication()  {
56                 try {
57                         MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
58                         capi = new Capi(core);
59                         notificationMgr = new NotificationManager(core);
60                 } catch (Exception e) {
61                         log.error("Space connection could not be established! Have you started the Space Server?");
62                         handleSpaceErrorAndTerminate(e);
63                 }
64         }
65         
66         public void useTablesContainer() {
67                 try {
68                         tablesContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.TABLES_CONTAINER, capi, createCoordinators(new AnyCoordinator()));
69                 } catch (MzsCoreException e) {
70                         handleSpaceErrorAndTerminate(e);
71                 }
72         }
73         
74         public void useGroupsContainer() {
75                 try {
76                         groupsContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.GROUPS_CONTAINER, capi, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
77                 } catch (MzsCoreException e) {
78                         handleSpaceErrorAndTerminate(e);
79                 }
80         }
81
82         
83         public void sendTablesToSpace(List<Table> tables) {
84                 sendItemsToContainer(tables, tablesContainer);
85         }
86
87
88         public void sendGroupsToSpace(List<GroupData> newGroups) {
89                 sendItemsToContainer(newGroups, groupsContainer);
90         }
91
92         public void handleWaitingGroup(int id) {
93
94                 try {
95                         TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
96                         Table table = null;
97                         try {
98                                 ArrayList<Table> tables = capi.take(tablesContainer, FifoCoordinator.newSelector(), RequestTimeout.ZERO, tx);
99                                 table = tables.get(0);
100                         } catch (Exception e) {
101                                 log.info("There is no free table");
102                                 capi.rollbackTransaction(tx);
103                                 return;
104                         }
105                         
106                         GroupData groupData = new GroupData(null);
107                         groupData.setState(GroupState.WAITING);
108                         LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
109                         ArrayList<GroupData> waitingGroups = null;
110                         
111                         try {
112                                 waitingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
113                         } catch (Exception e) {
114                                 log.info("There is no waiting group");
115                                 capi.rollbackTransaction(tx);
116                                 return;
117                         }
118                         
119                         GroupData group = waitingGroups.get(0);
120                         group.setTable(new Table(table.getGroupId()));
121                         group.setTableWaiter(id);
122                         group.setState(GroupState.SITTING);
123                         
124                         try {
125                                 capi.write(groupsContainer, RequestTimeout.TRY_ONCE,  tx, new Entry(group));
126                         } catch (Exception e) {
127                                 e.printStackTrace();
128                                 capi.rollbackTransaction(tx);
129                                 return;
130                         }
131                         
132                         try {
133                                 capi.write(new Entry(table), tablesContainer, RequestTimeout.TRY_ONCE, tx);
134                         } catch (Exception e) {
135                                 e.printStackTrace();
136                                 capi.rollbackTransaction(tx);
137                                 return;
138                         }
139                         
140                         capi.commitTransaction(tx);
141                 } catch (MzsCoreException e) {
142                         e.printStackTrace();
143                 }
144         }
145
146         public void handleOrderRequest(int id) {
147                 try {
148                         TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
149                                         
150                         GroupData groupData = new GroupData(null);
151                         groupData.setState(GroupState.SITTING);
152                         LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
153                         ArrayList<GroupData> sittingGroups = null;
154                         
155                         try {
156                                 sittingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
157                         } catch (Exception e) {
158                                 log.info("There is no sitting group");
159                                 capi.rollbackTransaction(tx);
160                                 return;
161                         }
162                         GroupData sittingGroup = sittingGroups.get(0);
163                         Order order = sittingGroup.getOrder();
164                         sittingGroup.setOrderWaiter(id);
165                         sittingGroup.setState(GroupState.ORDERED);
166                         
167                         try {
168                                 capi.write(new Entry(sittingGroup), groupsContainer, RequestTimeout.TRY_ONCE, tx);
169                         } catch (Exception e) {
170                                 e.printStackTrace();
171                                 capi.rollbackTransaction(tx);
172                                 return;
173                         }
174                         
175                         capi.commitTransaction(tx);
176                         
177                 } catch (MzsCoreException e) {
178                         e.printStackTrace();
179                 }
180         }
181
182         public void initTablesNotifications(NotificationListener listener) {
183         try {
184                 notificationMgr.createNotification(tablesContainer, listener, Operation.WRITE);
185         } catch (Exception e) {
186             handleSpaceErrorAndTerminate(e);
187         }
188         }
189         
190         public void initGroupNotifications(NotificationListener listener) {
191         try {
192                 notificationMgr.createNotification(groupsContainer, listener, Operation.WRITE);
193         } catch (Exception e) {
194            handleSpaceErrorAndTerminate(e);
195         }
196         }
197
198         public void handlePaymentRequest() {
199         }
200
201         public void handlePizzaDistribution() {
202         }
203         
204         
205         private List<Coordinator> createCoordinators(Coordinator... coordinator) {
206                 return Arrays.asList(coordinator);
207         }
208         
209         private void handleSpaceErrorAndTerminate(Exception e) {
210                 log.error(e.getMessage());
211                 e.printStackTrace();
212                 System.exit(1);
213         }
214         
215         
216         private <T extends Serializable> void sendItemsToContainer(List<T> items, ContainerReference cref) {
217                 try {
218                         List<Entry> entries = new ArrayList<>();
219                         for (Serializable item : items) {
220                                 entries.add(new Entry(item));
221                         }
222                         capi.write(entries, cref);
223
224                 } catch (MzsCoreException e) {
225                         log.info(e.getMessage());
226                         e.printStackTrace();
227                 }
228         }
229
230
231 }