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.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;
39 * Responsible for XVSM Communication
41 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
44 public class XVSMConnector {
45 private static final Logger log = LoggerFactory.getLogger(XVSMConnector.class);
47 private ContainerReference tablesContainer;
48 private ContainerReference groupsContainer;
49 // private ContainerReference notificationContainer;
51 private NotificationManager notificationMgr;
54 public void initSpaceCommunication() {
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);
65 public void useTablesContainer() {
67 tablesContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.TABLES_CONTAINER, capi, createCoordinators(new AnyCoordinator()));
68 } catch (MzsCoreException e) {
69 handleSpaceErrorAndTerminate(e);
73 public void useGroupsContainer() {
75 groupsContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.GROUPS_CONTAINER, capi, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
76 } catch (MzsCoreException e) {
77 handleSpaceErrorAndTerminate(e);
81 private List<Coordinator> createCoordinators(Coordinator... coordinator) {
82 return Arrays.asList(coordinator);
85 private void handleSpaceErrorAndTerminate(Exception e) {
86 log.error(e.getMessage());
90 public void sendTablesToSpace(List<Table> tables) {
92 List<Entry> entries = new ArrayList<>();
93 for (Table table : tables) {
94 entries.add(new Entry(table));
96 capi.write(entries, tablesContainer);
98 log.info("Wrote tables to Space!");
100 } catch (MzsCoreException e) {
101 log.info(e.getMessage());
105 public void createGroups(List<GroupData> newGroups) {
107 List<Entry> entries = new ArrayList<>();
108 for (GroupData group : newGroups) {
109 entries.add(new Entry(group));
111 capi.write(entries, groupsContainer);
113 log.info("Wrote new Groups to Space!");
114 } catch (MzsCoreException e) {
115 log.info(e.getMessage());
120 public void handleWaitingGroup(int id) {
123 TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
126 ArrayList<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<Table> tables = new ArrayList<>();
217 List<Entry> entryList = (List<Entry>) entries;
218 for (Entry entry : entryList) {
219 tables.add((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() {