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 tablesContainer;
37 protected ContainerReference groupsContainer;
38 protected ContainerReference assignTableContainer;
39 protected ContainerReference takeOrderContainer;
40 protected ContainerReference ordersContainer;
41 protected ContainerReference preparePizzasContainer;
42 protected ContainerReference deliverPizzasContainer;
43 protected ContainerReference paymentContainer;
44 protected ContainerReference freeTablesContainer;
45 protected ContainerReference pizzaInProgressContainer;
46 protected ContainerReference orderCompleteContainer;
47 protected ContainerReference isEatingContainer;
48 protected ContainerReference hasPaidContainer;
50 protected NotificationManager notificationMgr;
52 public AbstractXVSMConnector() {
53 initSpaceCommunication();
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 protected ContainerReference useContainer(String containerName) {
69 return CapiUtil.lookupOrCreateContainer(containerName, URI.create(Util.SERVER_ADDR), createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)), null, capi);
70 } catch (MzsCoreException e) {
71 handleSpaceErrorAndTerminate(e);
74 throw new RuntimeException("Could not Create container " + containerName);
77 protected List<Coordinator> createCoordinators(Coordinator... coordinator) {
78 return Arrays.asList(coordinator);
81 protected void handleSpaceErrorAndTerminate(Exception e) {
82 log.error(e.getMessage());
87 protected <T extends Serializable> void sendItemsToContainer(
88 List<T> items, ContainerReference cref, long timeout, TransactionReference tx) {
91 List<Entry> entries = new ArrayList<>();
92 for (Serializable item : items) {
93 entries.add(new Entry(item));
95 capi.write(entries, cref, timeout, tx);
96 } catch (Exception e) {
97 log.info(e.getMessage());
102 @SuppressWarnings("unchecked")
103 protected <T extends Serializable> T takeMatchingEntity(
104 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
105 throws MzsCoreException {
107 LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
109 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
111 return (T) CapiUtil.getSingleEntry(entities);
112 } catch (CountNotMetException e) {
113 capi.rollbackTransaction(tx);
115 throw new EntityNotFoundByTemplate(errorMsg);
116 } catch(MzsTimeoutException e) {
117 capi.rollbackTransaction(tx);
119 throw new EntityNotFoundByTemplate(errorMsg);
123 protected <T extends Serializable> List<T> takeMatchingEntities(
124 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
125 throws MzsCoreException {
127 LindaSelector sel = LindaCoordinator.newSelector(entity, MzsConstants.Selecting.COUNT_MAX);
129 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
131 return (List<T>) entities;
132 } catch (CountNotMetException e) {
133 capi.rollbackTransaction(tx);
135 throw new EntityNotFoundByTemplate(errorMsg);
136 } catch(MzsTimeoutException e) {
137 capi.rollbackTransaction(tx);
139 throw new EntityNotFoundByTemplate(errorMsg);
143 protected <T extends Serializable> List<T> castEntries(List<? extends Serializable> entries) {
144 List<T> newList = new ArrayList<T>();
145 List<Entry> newEntries = (List<Entry>) entries;
146 for (Entry entry : newEntries) {
147 newList.add((T) entry.getValue());
152 public void sendTablesToSpace(List<Table> tables) {
153 sendItemsToContainer(tables, tablesContainer, RequestTimeout.DEFAULT, null);
156 public void sendFreeTablesToSpace(List<Table> tables) {
157 sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null);
158 sendTablesToSpace(tables);
161 public void sendNewGroupsToSpace(List<GroupData> newGroups) {
162 sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null);
163 sendItemsToContainer(newGroups, assignTableContainer, RequestTimeout.DEFAULT, null);