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.MzsTimeoutException;
19 import org.mozartspaces.core.MzsConstants.RequestTimeout;
20 import org.mozartspaces.core.MzsCore;
21 import org.mozartspaces.core.MzsCoreException;
22 import org.mozartspaces.core.TransactionReference;
23 import org.mozartspaces.notifications.NotificationManager;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import at.ac.tuwien.sbc.valesriegler.common.Util;
28 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
29 import at.ac.tuwien.sbc.valesriegler.types.Table;
31 public abstract class AbstractXVSMConnector {
32 private static final Logger log = LoggerFactory.getLogger(AbstractXVSMConnector.class);
34 protected ContainerReference tablesContainer;
35 protected ContainerReference groupsContainer;
36 protected ContainerReference assignTableContainer;
37 protected ContainerReference takeOrderContainer;
38 protected ContainerReference ordersContainer;
39 protected ContainerReference deliverPizzasContainer;
40 protected ContainerReference paymentContainer;
41 protected ContainerReference freeTablesContainer;
43 protected NotificationManager notificationMgr;
45 public AbstractXVSMConnector() {
46 initSpaceCommunication();
49 public void initSpaceCommunication() {
51 MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
52 capi = new Capi(core);
53 notificationMgr = new NotificationManager(core);
54 } catch (Exception e) {
55 log.error("Space connection could not be established! Have you started the Space Server?");
56 handleSpaceErrorAndTerminate(e);
60 public void useTablesContainer() {
61 tablesContainer = useContainer(Util.TABLES_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
64 public void useTakeOrderContainer() {
65 takeOrderContainer = useContainer(Util.TAKE_ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
68 public void useorderContainer() {
69 ordersContainer = useContainer(Util.ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
72 public void useGroupsContainer() {
73 groupsContainer = useContainer(Util.GROUPS_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
76 public void useAssignTableContainer() {
77 assignTableContainer = useContainer(Util.ASSIGN_TABLE, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
80 public void useFreeTablesContainer() {
81 freeTablesContainer = useContainer(Util.FREE_TABLES, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
84 private ContainerReference useContainer(String containerName, List<Coordinator> coordinators) {
86 return Util.getOrCreateNamedContainer(Util.SERVER_ADDR, containerName, capi, coordinators);
87 } catch (MzsCoreException e) {
88 handleSpaceErrorAndTerminate(e);
94 private List<Coordinator> createCoordinators(Coordinator... coordinator) {
95 return Arrays.asList(coordinator);
98 protected void handleSpaceErrorAndTerminate(Exception e) {
99 log.error(e.getMessage());
104 protected <T extends Serializable> void sendItemsToContainer(
105 List<T> items, ContainerReference cref, long timeout, TransactionReference tx) {
108 List<Entry> entries = new ArrayList<>();
109 for (Serializable item : items) {
110 entries.add(new Entry(item));
112 capi.write(entries, cref, timeout, tx);
113 } catch (Exception e) {
114 log.info(e.getMessage());
119 @SuppressWarnings("unchecked")
120 protected <T extends Serializable> T takeEntityByTemplateFromContainer(
121 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
122 throws MzsCoreException {
124 LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
125 T singleEntity = null;
127 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
129 return (T) CapiUtil.getSingleEntry(entities);
130 } catch (CountNotMetException e) {
131 capi.rollbackTransaction(tx);
133 throw new EntityNotFoundByTemplate(errorMsg);
134 } catch(MzsTimeoutException e) {
135 capi.rollbackTransaction(tx);
137 throw new EntityNotFoundByTemplate(errorMsg);
141 protected <T extends Serializable> List<T> castEntries(List<? extends Serializable> entries) {
142 List<T> newList = new ArrayList<T>();
143 List<Entry> newEntries = (List<Entry>) entries;
144 for (Entry entry : newEntries) {
145 newList.add((T) entry.getValue());
150 public void sendTablesToSpace(List<Table> tables) {
151 sendItemsToContainer(tables, tablesContainer, RequestTimeout.DEFAULT, null);
154 public void sendFreeTablesToSpace(List<Table> tables) {
155 sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null);
156 sendTablesToSpace(tables);
159 public void sendNewGroupsToSpace(List<GroupData> newGroups) {
160 sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null);
161 sendItemsToContainer(newGroups, assignTableContainer, RequestTimeout.DEFAULT, null);