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 org.mozartspaces.capi3.AnyCoordinator;
10 import org.mozartspaces.capi3.Coordinator;
11 import org.mozartspaces.capi3.CountNotMetException;
12 import org.mozartspaces.capi3.LindaCoordinator;
13 import org.mozartspaces.capi3.LindaCoordinator.LindaSelector;
14 import org.mozartspaces.core.Capi;
15 import org.mozartspaces.core.CapiUtil;
16 import org.mozartspaces.core.ContainerReference;
17 import org.mozartspaces.core.DefaultMzsCore;
18 import org.mozartspaces.core.Entry;
19 import org.mozartspaces.core.MzsConstants;
20 import org.mozartspaces.core.MzsTimeoutException;
21 import org.mozartspaces.core.MzsConstants.RequestTimeout;
22 import org.mozartspaces.core.MzsCore;
23 import org.mozartspaces.core.MzsCoreException;
24 import org.mozartspaces.core.TransactionReference;
25 import org.mozartspaces.notifications.NotificationManager;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import at.ac.tuwien.sbc.valesriegler.common.Util;
30 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
31 import at.ac.tuwien.sbc.valesriegler.types.Table;
33 public abstract class AbstractXVSMConnector {
34 private static final Logger log = LoggerFactory.getLogger(AbstractXVSMConnector.class);
36 protected ContainerReference tableAssignedContainer;
37 protected ContainerReference assignTableContainer;
38 protected ContainerReference takeOrderContainer;
39 protected ContainerReference orderTakenContainer;
40 protected ContainerReference preparePizzasContainer;
41 protected ContainerReference deliverPizzasContainer;
42 protected ContainerReference paymentRequestContainer;
43 protected ContainerReference freeTablesContainer;
44 protected ContainerReference pizzaInProgressContainer;
45 protected ContainerReference orderCompleteContainer;
46 protected ContainerReference isEatingContainer;
47 protected ContainerReference paymentDoneContainer;
49 protected NotificationManager notificationMgr;
51 public AbstractXVSMConnector() {
52 initSpaceCommunication();
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 protected ContainerReference useContainer(String containerName) {
68 return CapiUtil.lookupOrCreateContainer(containerName, URI.create(Util.SERVER_ADDR), createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)), null, capi);
69 } catch (MzsCoreException e) {
70 handleSpaceErrorAndTerminate(e);
73 throw new RuntimeException("Could not Create container " + containerName);
76 protected List<Coordinator> createCoordinators(Coordinator... coordinator) {
77 return Arrays.asList(coordinator);
80 protected void handleSpaceErrorAndTerminate(Exception e) {
81 log.error(e.getMessage());
86 protected <T extends Serializable> void sendItemsToContainer(
87 List<T> items, ContainerReference cref, long timeout, TransactionReference tx) {
90 List<Entry> entries = new ArrayList<>();
91 for (Serializable item : items) {
92 entries.add(new Entry(item));
94 capi.write(entries, cref, timeout, tx);
95 } catch (Exception e) {
96 log.info(e.getMessage());
101 @SuppressWarnings("unchecked")
102 protected <T extends Serializable> T takeMatchingEntity(
103 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
104 throws MzsCoreException {
106 LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
108 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
110 return (T) CapiUtil.getSingleEntry(entities);
111 } catch (CountNotMetException e) {
112 capi.rollbackTransaction(tx);
114 throw new EntityNotFoundByTemplate(errorMsg);
115 } catch(MzsTimeoutException e) {
116 capi.rollbackTransaction(tx);
118 throw new EntityNotFoundByTemplate(errorMsg);
122 protected <T extends Serializable> List<T> takeMatchingEntities(
123 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
124 throws MzsCoreException {
126 LindaSelector sel = LindaCoordinator.newSelector(entity, MzsConstants.Selecting.COUNT_MAX);
128 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
130 return (List<T>) entities;
131 } catch (CountNotMetException e) {
132 capi.rollbackTransaction(tx);
134 throw new EntityNotFoundByTemplate(errorMsg);
135 } catch(MzsTimeoutException e) {
136 capi.rollbackTransaction(tx);
138 throw new EntityNotFoundByTemplate(errorMsg);
142 protected <T extends Serializable> List<T> castEntries(List<? extends Serializable> entries) {
143 List<T> newList = new ArrayList<T>();
144 List<Entry> newEntries = (List<Entry>) entries;
145 for (Entry entry : newEntries) {
146 newList.add((T) entry.getValue());
151 // public void sendTablesToSpace(List<Table> tables) {
152 // sendItemsToContainer(tables, tableOccupiedContainer, RequestTimeout.DEFAULT, null);
155 public void sendFreeTablesToSpace(List<Table> tables) {
156 sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null);
159 public void sendNewGroupsToSpace(List<GroupData> newGroups) {
160 // sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null);
161 sendItemsToContainer(newGroups, assignTableContainer, RequestTimeout.DEFAULT, null);