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 org.mozartspaces.capi3.Coordinator;
7 import org.mozartspaces.capi3.CountNotMetException;
8 import org.mozartspaces.capi3.FifoCoordinator;
9 import org.mozartspaces.capi3.LindaCoordinator;
10 import org.mozartspaces.capi3.LindaCoordinator.LindaSelector;
11 import org.mozartspaces.core.*;
12 import org.mozartspaces.notifications.NotificationManager;
13 import org.mozartspaces.notifications.Operation;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Date;
22 import java.util.List;
23 import java.util.concurrent.atomic.AtomicLong;
25 public abstract class AbstractXVSMConnector {
26 public static Object lockObject = new Object();
27 public static AtomicLong timeOflastOperation = new AtomicLong(new Date().getTime());
29 private static final Logger log = LoggerFactory.getLogger(AbstractXVSMConnector.class);
31 protected ContainerReference tableAssignedContainer;
32 protected ContainerReference assignTableContainer;
33 protected ContainerReference takeOrderContainer;
34 protected ContainerReference orderTakenContainer;
35 protected ContainerReference deliveryOrderTakenContainer;
36 protected ContainerReference preparePizzasContainer;
37 protected ContainerReference prepareDeliveryPizzasContainer;
38 protected ContainerReference preparedPizzasContainer;
39 protected ContainerReference preparedDeliveryPizzasContainer;
40 protected ContainerReference paymentRequestContainer;
41 protected ContainerReference freeTablesContainer;
42 protected ContainerReference pizzaInProgressContainer;
43 protected ContainerReference orderCompleteContainer;
44 protected ContainerReference paymentDoneContainer;
45 protected ContainerReference pizzeriaInfoContainer;
46 protected ContainerReference phoneCallsContainer;
47 protected ContainerReference groupAgentInfoContainer;
48 protected ContainerReference deliverDeliveryOrder;
51 protected NotificationManager notificationMgr;
54 public AbstractXVSMConnector(int port) {
56 initSpaceCommunication(port);
59 public void initSpaceCommunication(int port) {
61 // Configuration config = CommonsXmlConfiguration.load(0);
62 // config.setEmbeddedSpace(false);
63 MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
64 capi = new Capi(core);
65 notificationMgr = new NotificationManager(core);
66 } catch (Exception e) {
67 log.error("Space connection could not be established! Have you started the Space Server?");
68 handleSpaceErrorAndTerminate(e);
72 protected ContainerReference useContainer(String containerName) {
73 return useContainerOfSpaceWithPort(containerName, port);
76 protected ContainerReference useContainerOfSpaceWithPort(String containerName, int spacePort) {
78 final String address = String.format(Util.SERVER_ADDR, spacePort);
79 return CapiUtil.lookupOrCreateContainer(containerName, URI.create(address), createCoordinators(new FifoCoordinator(), new LindaCoordinator(false)), null, capi);
80 } catch (MzsCoreException e) {
81 handleSpaceErrorAndTerminate(e);
84 throw new RuntimeException("Could not Create container " + containerName);
87 protected List<Coordinator> createCoordinators(Coordinator... coordinator) {
88 return Arrays.asList(coordinator);
91 protected void handleSpaceErrorAndTerminate(Exception e) {
92 log.error(e.getMessage());
97 protected void createNotification(SpaceListener listener,
98 ContainerReference cref) {
99 listener.startHandlingAbsenceOfNotifications();
101 notificationMgr.createNotification(cref, listener, Operation.WRITE);
102 } catch (Exception e) {
103 handleSpaceErrorAndTerminate(e);
107 protected <T extends Serializable> void sendItemsToContainer(
108 List<T> items, ContainerReference cref, long timeout, TransactionReference tx) {
111 List<Entry> entries = new ArrayList<>();
112 for (Serializable item : items) {
113 entries.add(new Entry(item));
115 capi.write(entries, cref, timeout, tx);
116 } catch (Exception e) {
117 log.info(e.getMessage());
122 @SuppressWarnings("unchecked")
124 * Searches for one entity matching the given template object by linda selection.
126 protected <T extends Serializable> T takeMatchingEntity(
127 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
128 throws MzsCoreException {
130 LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
132 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
134 return (T) CapiUtil.getSingleEntry(entities);
135 } catch (CountNotMetException e) {
136 capi.rollbackTransaction(tx);
138 throw new EntityNotFoundByTemplate(errorMsg);
139 } catch (MzsTimeoutException e) {
140 capi.rollbackTransaction(tx);
142 throw new EntityNotFoundByTemplate(errorMsg);
147 * Searches for all entities matching the given template object by linda selection and takes them.
149 protected <T extends Serializable> List<T> takeMatchingEntities(
150 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
151 throws MzsCoreException {
153 LindaSelector sel = LindaCoordinator.newSelector(entity, MzsConstants.Selecting.COUNT_MAX);
155 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
157 return (List<T>) entities;
158 } catch (CountNotMetException e) {
159 capi.rollbackTransaction(tx);
161 throw new EntityNotFoundByTemplate(errorMsg);
162 } catch (MzsTimeoutException e) {
163 capi.rollbackTransaction(tx);
165 throw new EntityNotFoundByTemplate(errorMsg);
170 * Searches for all entities matching the given template object by linda selection and reads them.
172 protected <T extends Serializable> List<T> readMatchingEntities(
173 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
174 throws MzsCoreException {
176 LindaSelector sel = LindaCoordinator.newSelector(entity, MzsConstants.Selecting.COUNT_MAX);
178 ArrayList<Serializable> entities = capi.read(ref, sel, timeout, tx);
180 return (List<T>) entities;
181 } catch (CountNotMetException e) {
182 capi.rollbackTransaction(tx);
184 throw new EntityNotFoundByTemplate(errorMsg);
185 } catch (MzsTimeoutException e) {
186 capi.rollbackTransaction(tx);
188 throw new EntityNotFoundByTemplate(errorMsg);
191 protected <T extends Serializable> List<T> castEntries(List<? extends Serializable> entries) {
192 List<T> newList = new ArrayList<T>();
193 if (entries.size() == 0) return newList;
195 Serializable firstEntry = entries.get(0);
196 if (firstEntry instanceof Entry) {
198 List<Entry> newEntries = (List<Entry>) entries;
199 for (Entry entry : newEntries) {
200 newList.add((T) entry.getValue());
204 return (List<T>) entries;
209 protected <T extends HasId> T getSingleEntity(final List<T> entities) {
210 if (entities.size() != 1) {
211 throw new RuntimeException("Only one entity was expected!");
213 return entities.get(0);
216 protected <T extends Serializable> GroupData getSingleGroup(final List<T> entities) {
217 List<GroupData> groups = castEntries(entities);
218 if (groups.size() != 1) {
219 throw new RuntimeException("Only one group was expected!");
221 return groups.get(0);
224 protected TransactionReference getDefaultTransaction() throws MzsCoreException {
225 return capi.createTransaction(
226 Util.SPACE_TRANSACTION_TIMEOUT,
227 URI.create(String.format(Util.SERVER_ADDR, port)));