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());
91 public void sendTablesToSpace(List<Table> tables) {
93 List<Entry> entries = new ArrayList<>();
94 for (Table table : tables) {
95 entries.add(new Entry(table));
97 capi.write(entries, tablesContainer);
99 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) {
124 TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
127 ArrayList<Table> tables = capi.take(tablesContainer, FifoCoordinator.newSelector(), RequestTimeout.ZERO, tx);
128 table = tables.get(0);
129 } catch (Exception e) {
130 log.info("There is no free table");
131 capi.rollbackTransaction(tx);
135 GroupData groupData = new GroupData(null);
136 groupData.setState(GroupState.WAITING);
137 LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
138 ArrayList<GroupData> waitingGroups = null;
141 waitingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
142 } catch (Exception e) {
143 log.info("There is no waiting group");
144 capi.rollbackTransaction(tx);
148 GroupData group = waitingGroups.get(0);
149 group.setTable(new Table(table.getGroupId()));
150 group.setTableWaiter(id);
151 group.setState(GroupState.SITTING);
154 capi.write(groupsContainer, RequestTimeout.TRY_ONCE, tx, new Entry(group));
155 } catch (Exception e) {
157 capi.rollbackTransaction(tx);
162 capi.write(new Entry(table), tablesContainer, RequestTimeout.TRY_ONCE, tx);
163 } catch (Exception e) {
165 capi.rollbackTransaction(tx);
169 capi.commitTransaction(tx);
170 } catch (MzsCoreException e) {
175 public void handleOrderRequest(int id) {
177 TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
179 GroupData groupData = new GroupData(null);
180 groupData.setState(GroupState.SITTING);
181 LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
182 ArrayList<GroupData> sittingGroups = null;
185 sittingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
186 } catch (Exception e) {
187 log.info("There is no sitting group");
188 capi.rollbackTransaction(tx);
191 GroupData sittingGroup = sittingGroups.get(0);
192 Order order = sittingGroup.getOrder();
193 sittingGroup.setOrderWaiter(id);
194 sittingGroup.setState(GroupState.ORDERED);
197 capi.write(new Entry(sittingGroup), groupsContainer, RequestTimeout.TRY_ONCE, tx);
198 } catch (Exception e) {
200 capi.rollbackTransaction(tx);
204 capi.commitTransaction(tx);
206 } catch (MzsCoreException e) {
211 public void initPizzeriaNotifications() {
212 // When tables are written to the space the Pizzeria gets informed and sets the new tables in the tables model!
213 NotificationListener tablesListener = new NotificationListener() {
215 public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
217 final List<Table> tables = new ArrayList<>();
218 List<Entry> entryList = (List<Entry>) entries;
219 for (Entry entry : entryList) {
220 tables.add((Table) entry.getValue());
222 SwingUtilities.invokeLater(new Runnable() {
225 PizzeriaAgent.getInstance().getTablesModel().mergeTables(tables);
231 NotificationListener groupsListener = new NotificationListener() {
233 public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
234 List<GroupData> tables = (List<GroupData>) entries;
235 // TODO problem at the moment: Item type of groupoverviewmodel is Group and not GroupData
240 notificationMgr.createNotification(tablesContainer, groupsListener, Operation.WRITE);
241 notificationMgr.createNotification(tablesContainer, tablesListener, Operation.WRITE);
242 } catch (Exception e) {
247 public void initGroupNotifications() {
248 NotificationListener notifListener = new NotificationListener() {
250 public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
251 List<GroupData> groups = (List<GroupData>) entries;
253 // when a groupdata is changed, call setItems on the groupoverviewmodel. TODO problem at the moment: Item type of groupoverviewmodel is Group and not GroupData
258 notificationMgr.createNotification(groupsContainer, notifListener, Operation.WRITE);
259 } catch (Exception e) {
260 handleSpaceErrorAndTerminate(e);
264 public void handlePaymentRequest() {
267 public void handlePizzaDistribution() {