1 package at.ac.tuwien.sbc.valesriegler.xvsm;
3 import java.io.Serializable;
5 import java.util.ArrayList;
6 import java.util.Arrays;
9 import javax.swing.SwingUtilities;
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;
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;
40 * Responsible for XVSM Communication
42 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
45 public class XVSMConnector {
46 private static final Logger log = LoggerFactory.getLogger(XVSMConnector.class);
48 private ContainerReference tablesContainer;
49 private ContainerReference groupsContainer;
50 // private ContainerReference notificationContainer;
52 private NotificationManager notificationMgr;
55 public void initSpaceCommunication() {
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);
66 public void useTablesContainer() {
68 tablesContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.TABLES_CONTAINER, capi, createCoordinators(new AnyCoordinator()));
69 } catch (MzsCoreException e) {
70 handleSpaceErrorAndTerminate(e);
74 public void useGroupsContainer() {
76 groupsContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.GROUPS_CONTAINER, capi, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
77 } catch (MzsCoreException e) {
78 handleSpaceErrorAndTerminate(e);
83 public void sendTablesToSpace(List<Table> tables) {
84 sendItemsToContainer(tables, tablesContainer);
88 public void sendGroupsToSpace(List<GroupData> newGroups) {
89 sendItemsToContainer(newGroups, groupsContainer);
92 public void handleWaitingGroup(int id) {
95 TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
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);
106 GroupData groupData = new GroupData(null);
107 groupData.setState(GroupState.WAITING);
108 LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
109 ArrayList<GroupData> waitingGroups = null;
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);
119 GroupData group = waitingGroups.get(0);
120 group.setTable(new Table(table.getGroupId()));
121 group.setTableWaiter(id);
122 group.setState(GroupState.SITTING);
125 capi.write(groupsContainer, RequestTimeout.TRY_ONCE, tx, new Entry(group));
126 } catch (Exception e) {
128 capi.rollbackTransaction(tx);
133 capi.write(new Entry(table), tablesContainer, RequestTimeout.TRY_ONCE, tx);
134 } catch (Exception e) {
136 capi.rollbackTransaction(tx);
140 capi.commitTransaction(tx);
141 } catch (MzsCoreException e) {
146 public void handleOrderRequest(int id) {
148 TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
150 GroupData groupData = new GroupData(null);
151 groupData.setState(GroupState.SITTING);
152 LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
153 ArrayList<GroupData> sittingGroups = null;
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);
162 GroupData sittingGroup = sittingGroups.get(0);
163 Order order = sittingGroup.getOrder();
164 sittingGroup.setOrderWaiter(id);
165 sittingGroup.setState(GroupState.ORDERED);
168 capi.write(new Entry(sittingGroup), groupsContainer, RequestTimeout.TRY_ONCE, tx);
169 } catch (Exception e) {
171 capi.rollbackTransaction(tx);
175 capi.commitTransaction(tx);
177 } catch (MzsCoreException e) {
182 public void initTablesNotifications(NotificationListener listener) {
184 notificationMgr.createNotification(tablesContainer, listener, Operation.WRITE);
185 } catch (Exception e) {
186 handleSpaceErrorAndTerminate(e);
190 public void initGroupNotifications(NotificationListener listener) {
192 notificationMgr.createNotification(groupsContainer, listener, Operation.WRITE);
193 } catch (Exception e) {
194 handleSpaceErrorAndTerminate(e);
198 public void handlePaymentRequest() {
201 public void handlePizzaDistribution() {
205 private List<Coordinator> createCoordinators(Coordinator... coordinator) {
206 return Arrays.asList(coordinator);
209 private void handleSpaceErrorAndTerminate(Exception e) {
210 log.error(e.getMessage());
216 private <T extends Serializable> void sendItemsToContainer(List<T> items, ContainerReference cref) {
218 List<Entry> entries = new ArrayList<>();
219 for (Serializable item : items) {
220 entries.add(new Entry(item));
222 capi.write(entries, cref);
224 } catch (MzsCoreException e) {
225 log.info(e.getMessage());