1 package at.ac.tuwien.sbc.valesriegler.xvsm;
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.Arrays;
8 import org.mozartspaces.capi3.AnyCoordinator;
9 import org.mozartspaces.capi3.Coordinator;
10 import org.mozartspaces.capi3.CountNotMetException;
11 import org.mozartspaces.capi3.LindaCoordinator;
12 import org.mozartspaces.capi3.LindaCoordinator.LindaSelector;
13 import org.mozartspaces.core.Capi;
14 import org.mozartspaces.core.CapiUtil;
15 import org.mozartspaces.core.ContainerReference;
16 import org.mozartspaces.core.DefaultMzsCore;
17 import org.mozartspaces.core.Entry;
18 import org.mozartspaces.core.MzsConstants.RequestTimeout;
19 import org.mozartspaces.core.MzsCore;
20 import org.mozartspaces.core.MzsCoreException;
21 import org.mozartspaces.core.TransactionReference;
22 import org.mozartspaces.notifications.NotificationManager;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import at.ac.tuwien.sbc.valesriegler.common.Util;
27 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
28 import at.ac.tuwien.sbc.valesriegler.types.Table;
30 public abstract class AbstractXVSMConnector {
31 private static final Logger log = LoggerFactory.getLogger(AbstractXVSMConnector.class);
33 protected ContainerReference tablesContainer;
34 protected ContainerReference groupsContainer;
35 protected ContainerReference assignTableContainer;
36 protected ContainerReference takeOrderContainer;
37 protected ContainerReference ordersContainer;
38 protected ContainerReference deliverPizzasContainer;
39 protected ContainerReference paymentContainer;
40 protected ContainerReference freeTablesContainer;
42 protected NotificationManager notificationMgr;
44 public AbstractXVSMConnector() {
45 initSpaceCommunication();
48 public void initSpaceCommunication() {
50 MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
51 capi = new Capi(core);
52 notificationMgr = new NotificationManager(core);
53 } catch (Exception e) {
54 log.error("Space connection could not be established! Have you started the Space Server?");
55 handleSpaceErrorAndTerminate(e);
59 public void useTablesContainer() {
60 tablesContainer = useContainer(Util.TABLES_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
63 public void useTakeOrderContainer() {
64 takeOrderContainer = useContainer(Util.TAKE_ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
67 public void useorderContainer() {
68 ordersContainer = useContainer(Util.ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
71 public void useGroupsContainer() {
72 groupsContainer = useContainer(Util.GROUPS_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
75 public void useAssignTableContainer() {
76 assignTableContainer = useContainer(Util.ASSIGN_TABLE, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
79 public void useFreeTablesContainer() {
80 freeTablesContainer = useContainer(Util.FREE_TABLES, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
83 private ContainerReference useContainer(String containerName, List<Coordinator> coordinators) {
85 return Util.getOrCreateNamedContainer(Util.SERVER_ADDR, containerName, capi, coordinators);
86 } catch (MzsCoreException e) {
87 handleSpaceErrorAndTerminate(e);
93 private List<Coordinator> createCoordinators(Coordinator... coordinator) {
94 return Arrays.asList(coordinator);
97 protected void handleSpaceErrorAndTerminate(Exception e) {
98 log.error(e.getMessage());
103 protected <T extends Serializable> void sendItemsToContainer(
104 List<T> items, ContainerReference cref, long timeout, TransactionReference tx) {
107 List<Entry> entries = new ArrayList<>();
108 for (Serializable item : items) {
109 entries.add(new Entry(item));
111 capi.write(entries, cref, timeout, tx);
112 } catch (Exception e) {
113 log.info(e.getMessage());
118 @SuppressWarnings("unchecked")
119 protected <T extends Serializable> T takeEntityByTemplateFromContainer(
120 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
121 throws MzsCoreException {
123 LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
124 T singleEntity = null;
126 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
128 return (T) CapiUtil.getSingleEntry(entities);
129 } catch (CountNotMetException e) {
131 capi.rollbackTransaction(tx);
133 throw new EntityNotFoundByTemplate(errorMsg);
137 protected <T extends Serializable> List<T> castEntries(List<? extends Serializable> entries) {
138 List<T> newList = new ArrayList<T>();
139 List<Entry> newEntries = (List<Entry>) entries;
140 for (Entry entry : newEntries) {
141 newList.add((T) entry.getValue());
146 public void sendTablesToSpace(List<Table> tables) {
147 sendItemsToContainer(tables, tablesContainer, RequestTimeout.DEFAULT, null);
150 public void sendFreeTablesToSpace(List<Table> tables) {
151 sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null);
152 sendTablesToSpace(tables);
155 public void sendNewGroupsToSpace(List<GroupData> newGroups) {
156 sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null);
157 sendItemsToContainer(newGroups, assignTableContainer, RequestTimeout.DEFAULT, null);