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.DEP_Table;
32 import at.ac.tuwien.sbc.valesriegler.common.Util;
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;
41 * Responsible for XVSM Communication
43 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
46 public class XVSMConnector {
47 private static final Logger log = LoggerFactory.getLogger(XVSMConnector.class);
49 private ContainerReference tablesContainer;
50 private ContainerReference groupsContainer;
51 // private ContainerReference notificationContainer;
53 private NotificationManager notificationMgr;
56 public void initSpaceCommunication() {
58 MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
59 capi = new Capi(core);
60 notificationMgr = new NotificationManager(core);
61 } catch (Exception e) {
62 log.error("Space connection could not be established! Have you started the Space Server?");
63 handleSpaceErrorAndTerminate(e);
67 public void useTablesContainer() {
69 tablesContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.TABLES_CONTAINER, capi, createCoordinators(new AnyCoordinator()));
70 } catch (MzsCoreException e) {
71 handleSpaceErrorAndTerminate(e);
75 public void useGroupsContainer() {
77 groupsContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.GROUPS_CONTAINER, capi, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
78 } catch (MzsCoreException e) {
79 handleSpaceErrorAndTerminate(e);
83 private List<Coordinator> createCoordinators(Coordinator... coordinator) {
84 return Arrays.asList(coordinator);
87 private void handleSpaceErrorAndTerminate(Exception e) {
88 log.error(e.getMessage());
92 public void sendTablesToSpace(List<DEP_Table> tables) {
94 List<Entry> entries = new ArrayList<>();
95 for (DEP_Table table : tables) {
96 entries.add(new Entry(table));
98 capi.write(entries, tablesContainer);
100 log.info("Wrote tables to Space!");
101 } catch (MzsCoreException e) {
102 log.info(e.getMessage());
106 public void createGroups(List<GroupData> newGroups) {
108 List<Entry> entries = new ArrayList<>();
109 for (GroupData group : newGroups) {
110 entries.add(new Entry(group));
112 capi.write(entries, groupsContainer);
114 log.info("Wrote new Groups to Space!");
115 } catch (MzsCoreException e) {
116 log.info(e.getMessage());
121 public void handleWaitingGroup(int id) {
123 TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
124 DEP_Table table = null;
126 ArrayList<DEP_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);
134 GroupData groupData = new GroupData(null);
135 groupData.setState(GroupState.WAITING);
136 LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
137 ArrayList<GroupData> waitingGroups = null;
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);
147 GroupData group = waitingGroups.get(0);
148 group.setTable(new Table(table.getGroupId()));
149 group.setTableWaiter(id);
150 group.setState(GroupState.SITTING);
153 capi.write(groupsContainer, RequestTimeout.TRY_ONCE, tx, new Entry(group));
154 } catch (Exception e) {
156 capi.rollbackTransaction(tx);
161 capi.write(new Entry(table), tablesContainer, RequestTimeout.TRY_ONCE, tx);
162 } catch (Exception e) {
164 capi.rollbackTransaction(tx);
168 capi.commitTransaction(tx);
169 } catch (MzsCoreException e) {
174 public void handleOrderRequest(int id) {
176 TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
178 GroupData groupData = new GroupData(null);
179 groupData.setState(GroupState.SITTING);
180 LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
181 ArrayList<GroupData> sittingGroups = null;
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);
190 GroupData sittingGroup = sittingGroups.get(0);
191 Order order = sittingGroup.getOrder();
192 sittingGroup.setOrderWaiter(id);
193 sittingGroup.setState(GroupState.ORDERED);
196 capi.write(new Entry(sittingGroup), groupsContainer, RequestTimeout.TRY_ONCE, tx);
197 } catch (Exception e) {
199 capi.rollbackTransaction(tx);
203 capi.commitTransaction(tx);
205 } catch (MzsCoreException e) {
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() {
214 public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
216 final List<DEP_Table> tables = new ArrayList<>();
217 List<Entry> entryList = (List<Entry>) entries;
218 for (Entry entry : entryList) {
219 tables.add((DEP_Table) entry.getValue());
221 SwingUtilities.invokeLater(new Runnable() {
224 PizzeriaAgent.getInstance().getTablesModel().mergeTables(tables);
230 NotificationListener groupsListener = new NotificationListener() {
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
239 notificationMgr.createNotification(tablesContainer, groupsListener, Operation.WRITE);
240 notificationMgr.createNotification(tablesContainer, tablesListener, Operation.WRITE);
241 } catch (Exception e) {
246 public void initGroupNotifications() {
247 NotificationListener notifListener = new NotificationListener() {
249 public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
250 List<GroupData> groups = (List<GroupData>) entries;
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
257 notificationMgr.createNotification(groupsContainer, notifListener, Operation.WRITE);
258 } catch (Exception e) {
259 handleSpaceErrorAndTerminate(e);
263 public void handlePaymentRequest() {
266 public void handlePizzaDistribution() {