1 package at.ac.tuwien.sbc.valesriegler.xvsm;
3 import at.ac.tuwien.sbc.valesriegler.common.HasId;
4 import at.ac.tuwien.sbc.valesriegler.common.Util;
5 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
6 import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceListenerImplBuilder;
7 import org.mozartspaces.capi3.Coordinator;
8 import org.mozartspaces.capi3.CountNotMetException;
9 import org.mozartspaces.capi3.FifoCoordinator;
10 import org.mozartspaces.capi3.LindaCoordinator;
11 import org.mozartspaces.capi3.LindaCoordinator.LindaSelector;
12 import org.mozartspaces.core.*;
13 import org.mozartspaces.notifications.NotificationManager;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import java.io.Serializable;
20 import java.util.concurrent.atomic.AtomicLong;
22 public abstract class AbstractXVSMConnector {
23 public static Object lockObject = new Object();
24 public static AtomicLong timeOflastOperation = new AtomicLong(new Date().getTime());
26 private static final Logger log = LoggerFactory.getLogger(AbstractXVSMConnector.class);
28 protected ContainerReference tableAssignedContainer;
29 protected ContainerReference assignTableContainer;
30 protected ContainerReference takeOrderContainer;
31 protected ContainerReference orderTakenContainer;
32 protected ContainerReference deliveryOrderTakenContainer;
33 protected ContainerReference preparePizzasContainer;
34 protected ContainerReference prepareDeliveryPizzasContainer;
35 protected ContainerReference preparedPizzasContainer;
36 protected ContainerReference preparedDeliveryPizzasContainer;
37 protected ContainerReference paymentRequestContainer;
38 protected ContainerReference freeTablesContainer;
39 protected ContainerReference pizzaInProgressContainer;
40 protected ContainerReference orderDeliveredContainer;
41 protected ContainerReference paymentDoneContainer;
42 protected ContainerReference pizzeriaInfoContainer;
43 protected ContainerReference phoneCallsContainer;
44 protected ContainerReference groupAgentInfoContainer;
45 protected ContainerReference deliverDeliveryOrderContainer;
46 protected ContainerReference pizzeriaGroupContainer;
47 protected ContainerReference pizzeriaDeliveryContainer;
48 protected ContainerReference pizzeriaTableContainer;
51 protected NotificationManager notificationMgr;
54 private HashMap<String, ContainerReference> containers = new HashMap<String, ContainerReference>();
56 public AbstractXVSMConnector(int port) {
58 initSpaceCommunication(port);
61 public void initSpaceCommunication(int port) {
63 // Configuration config = CommonsXmlConfiguration.load(0);
64 // config.setEmbeddedSpace(false);
65 MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
66 capi = new Capi(core);
67 notificationMgr = new NotificationManager(core);
68 } catch (Exception e) {
69 log.error("Space connection could not be established! Have you started the Space Server?");
70 Util.handleSpaceErrorAndTerminate(e);
74 protected ContainerReference useContainer(String containerName) {
75 return useContainerOfSpaceWithPort(containerName, port);
78 protected ContainerReference useContainerOfSpaceWithPort(String containerName, int spacePort) {
79 String key = containerName + port;
80 ContainerReference container = containers.get(key);
81 if(container != null) return container;
84 final String address = String.format(Util.SERVER_ADDR, spacePort);
85 container = CapiUtil.lookupOrCreateContainer(containerName, URI.create(address), createCoordinators(new FifoCoordinator(), new LindaCoordinator(false)), null, capi);
86 containers.put(key, container);
88 } catch (MzsCoreException e) {
89 Util.handleSpaceErrorAndTerminate(e);
92 throw new RuntimeException("Could not Create container " + containerName);
95 protected List<Coordinator> createCoordinators(Coordinator... coordinator) {
96 return Arrays.asList(coordinator);
99 protected <T extends Serializable> void sendItemsToContainer(
100 List<T> items, ContainerReference cref, long timeout, TransactionReference tx) {
103 List<Entry> entries = new ArrayList<>();
104 for (Serializable item : items) {
105 entries.add(new Entry(item));
107 capi.write(entries, cref, timeout, tx);
108 } catch (Exception e) {
109 log.info(e.getMessage());
114 @SuppressWarnings("unchecked")
116 * Searches for one entity matching the given template object by linda selection.
118 protected <T extends Serializable> T takeMatchingEntity(
119 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
120 throws MzsCoreException {
122 LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
124 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
126 return (T) CapiUtil.getSingleEntry(entities);
127 } catch (CountNotMetException e) {
128 capi.rollbackTransaction(tx);
130 throw new EntityNotFoundByTemplate(errorMsg);
131 } catch (MzsTimeoutException e) {
132 capi.rollbackTransaction(tx);
134 throw new EntityNotFoundByTemplate(errorMsg);
138 protected <T extends Serializable> T takeMatchingEntityIfItExists(
139 T entity, ContainerReference ref, TransactionReference tx, long timeout)
140 throws MzsCoreException {
142 LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
144 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
146 return (T) CapiUtil.getSingleEntry(entities);
147 } catch (CountNotMetException e) {
148 } catch (MzsTimeoutException e) {
149 capi.rollbackTransaction(tx);
151 throw new EntityNotFoundByTemplate();
158 * Searches for all entities matching the given template object by linda selection and takes them.
160 protected <T extends Serializable> List<T> takeMatchingEntities(
161 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
162 throws MzsCoreException {
164 LindaSelector sel = LindaCoordinator.newSelector(entity, MzsConstants.Selecting.COUNT_MAX);
166 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
168 return (List<T>) entities;
169 } catch (CountNotMetException e) {
170 capi.rollbackTransaction(tx);
172 throw new EntityNotFoundByTemplate(errorMsg);
173 } catch (MzsTimeoutException e) {
174 capi.rollbackTransaction(tx);
176 throw new EntityNotFoundByTemplate(errorMsg);
181 * Searches for all entities matching the given template object by linda selection and reads them.
183 protected <T extends Serializable> List<T> readMatchingEntities(
184 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
185 throws MzsCoreException {
187 LindaSelector sel = LindaCoordinator.newSelector(entity, MzsConstants.Selecting.COUNT_MAX);
189 ArrayList<Serializable> entities = capi.read(ref, sel, timeout, tx);
191 return (List<T>) entities;
192 } catch (CountNotMetException e) {
193 capi.rollbackTransaction(tx);
195 throw new EntityNotFoundByTemplate(errorMsg);
196 } catch (MzsTimeoutException e) {
197 capi.rollbackTransaction(tx);
199 throw new EntityNotFoundByTemplate(errorMsg);
202 protected <T extends Serializable> List<T> castEntries(List<? extends Serializable> entries) {
203 List<T> newList = new ArrayList<T>();
204 if (entries.size() == 0) return newList;
206 Serializable firstEntry = entries.get(0);
207 if (firstEntry instanceof Entry) {
209 List<Entry> newEntries = (List<Entry>) entries;
210 for (Entry entry : newEntries) {
211 newList.add((T) entry.getValue());
215 return (List<T>) entries;
220 protected <T extends HasId> T getSingleEntity(final List<T> entities) {
221 if (entities.size() != 1) {
222 throw new RuntimeException("Only one entity was expected!");
224 return entities.get(0);
227 protected <T extends Serializable> GroupData getSingleGroup(final List<T> entities) {
228 List<GroupData> groups = castEntries(entities);
229 if (groups.size() != 1) {
230 throw new RuntimeException("Only one group was expected!");
232 return groups.get(0);
235 protected TransactionReference getDefaultTransaction() throws MzsCoreException {
236 return capi.createTransaction(
237 Util.SPACE_TRANSACTION_TIMEOUT,
238 URI.create(String.format(Util.SERVER_ADDR, port)));
241 protected SpaceListenerImplBuilder getDefaultBuilder() {
242 return new SpaceListenerImplBuilder().setCapi(capi).setNotificationManager(notificationMgr);