1 package at.ac.tuwien.sbc.valesriegler.xvsm;
3 import java.io.Serializable;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Collections;
10 import org.bouncycastle.crypto.RuntimeCryptoException;
11 import org.mozartspaces.capi3.AnyCoordinator.AnySelector;
12 import org.mozartspaces.capi3.CountNotMetException;
13 import org.mozartspaces.capi3.FifoCoordinator;
14 import org.mozartspaces.capi3.LindaCoordinator;
15 import org.mozartspaces.capi3.LindaCoordinator.LindaSelector;
16 import org.mozartspaces.core.Entry;
17 import org.mozartspaces.core.MzsConstants.RequestTimeout;
18 import org.mozartspaces.core.MzsCoreException;
19 import org.mozartspaces.core.TransactionReference;
20 import org.mozartspaces.notifications.Notification;
21 import org.mozartspaces.notifications.NotificationListener;
22 import org.mozartspaces.notifications.Operation;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import at.ac.tuwien.sbc.valesriegler.common.Util;
27 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
28 import at.ac.tuwien.sbc.valesriegler.types.GroupState;
29 import at.ac.tuwien.sbc.valesriegler.types.Order;
30 import at.ac.tuwien.sbc.valesriegler.types.Table;
33 * Responsible for XVSM Communication
35 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
38 public class XVSMConnector extends AbstractXVSMConnector {
39 static final Logger log = LoggerFactory.getLogger(XVSMConnector.class);
42 public void handleWaitingGroup(int id) {
45 TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
48 ArrayList<Table> tables = capi.take(tablesContainer, FifoCoordinator.newSelector(), RequestTimeout.ZERO, tx);
49 table = tables.get(0);
50 } catch (Exception e) {
51 log.info("There is no free table");
52 capi.rollbackTransaction(tx);
56 GroupData groupData = new GroupData(null);
57 groupData.setState(GroupState.WAITING);
58 LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
59 ArrayList<GroupData> waitingGroups = null;
62 waitingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
63 } catch (Exception e) {
64 log.info("There is no waiting group");
65 capi.rollbackTransaction(tx);
69 GroupData group = waitingGroups.get(0);
70 group.setTable(new Table(table.getGroupId()));
71 group.setTableWaiter(id);
72 group.setState(GroupState.SITTING);
75 capi.write(groupsContainer, RequestTimeout.TRY_ONCE, tx, new Entry(group));
76 } catch (Exception e) {
78 capi.rollbackTransaction(tx);
83 capi.write(new Entry(table), tablesContainer, RequestTimeout.TRY_ONCE, tx);
84 } catch (Exception e) {
86 capi.rollbackTransaction(tx);
90 capi.commitTransaction(tx);
91 } catch (MzsCoreException e) {
97 public void handleOrderRequest(int id) {
99 TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
101 GroupData groupData = new GroupData(null);
102 groupData.setState(GroupState.SITTING);
103 LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
104 ArrayList<GroupData> sittingGroups = null;
107 sittingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
108 } catch (Exception e) {
109 log.info("There is no sitting group");
110 capi.rollbackTransaction(tx);
113 GroupData sittingGroup = sittingGroups.get(0);
114 Order order = sittingGroup.getOrder();
115 sittingGroup.setOrderWaiter(id);
116 sittingGroup.setState(GroupState.ORDERED);
119 capi.write(new Entry(sittingGroup), groupsContainer, RequestTimeout.TRY_ONCE, tx);
120 } catch (Exception e) {
122 capi.rollbackTransaction(tx);
126 capi.commitTransaction(tx);
128 } catch (MzsCoreException e) {
133 public void initTablesNotifications(NotificationListener listener) {
135 notificationMgr.createNotification(tablesContainer, listener, Operation.WRITE);
136 } catch (Exception e) {
137 handleSpaceErrorAndTerminate(e);
141 public void initGroupNotifications(NotificationListener listener) {
143 notificationMgr.createNotification(groupsContainer, listener, Operation.WRITE);
144 } catch (Exception e) {
145 handleSpaceErrorAndTerminate(e);