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 private ContainerReference deliverPizzasContainer;
38 private ContainerReference paymentContainer;
39 protected ContainerReference freeTablesContainer;
41 protected NotificationManager notificationMgr;
43 public AbstractXVSMConnector() {
44 initSpaceCommunication();
47 public void initSpaceCommunication() {
49 MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
50 capi = new Capi(core);
51 notificationMgr = new NotificationManager(core);
52 } catch (Exception e) {
53 log.error("Space connection could not be established! Have you started the Space Server?");
54 handleSpaceErrorAndTerminate(e);
58 public void useTablesContainer() {
59 tablesContainer = useContainer(Util.TABLES_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
62 public void useTakeOrderContainer() {
63 takeOrderContainer = useContainer(Util.TAKE_ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
66 public void useGroupsContainer() {
67 groupsContainer = useContainer(Util.GROUPS_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
70 public void useAssignTableContainer() {
71 assignTableContainer = useContainer(Util.ASSIGN_TABLE, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
74 public void useFreeTablesContainer() {
75 freeTablesContainer = useContainer(Util.FREE_TABLES, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
78 private ContainerReference useContainer(String containerName, List<Coordinator> coordinators) {
80 return Util.getOrCreateNamedContainer(Util.SERVER_ADDR, containerName, capi, coordinators);
81 } catch (MzsCoreException e) {
82 handleSpaceErrorAndTerminate(e);
88 private List<Coordinator> createCoordinators(Coordinator... coordinator) {
89 return Arrays.asList(coordinator);
92 protected void handleSpaceErrorAndTerminate(Exception e) {
93 log.error(e.getMessage());
98 protected <T extends Serializable> void sendItemsToContainer(
99 List<T> items, ContainerReference cref) {
102 List<Entry> entries = new ArrayList<>();
103 for (Serializable item : items) {
104 entries.add(new Entry(item));
106 capi.write(entries, cref);
107 } catch (Exception e) {
108 log.info(e.getMessage());
113 @SuppressWarnings("unchecked")
114 protected <T extends Serializable> T takeEntityByTemplateFromContainer(
115 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
116 throws MzsCoreException {
118 LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
119 T singleEntity = null;
121 ArrayList<Serializable> entities = capi.take(ref, sel, RequestTimeout.DEFAULT, tx);
123 return (T) CapiUtil.getSingleEntry(entities);
124 } catch (CountNotMetException e) {
126 capi.rollbackTransaction(tx);
128 throw new EntityNotFoundByTemplate(errorMsg);
132 protected <T extends Serializable> List<T> castEntries(List<? extends Serializable> entries) {
133 List<T> newList = new ArrayList<T>();
134 List<Entry> newEntries = (List<Entry>) entries;
135 for (Entry entry : newEntries) {
136 newList.add((T) entry.getValue());
141 public void sendTablesToSpace(List<Table> tables) {
142 sendItemsToContainer(tables, tablesContainer);
145 public void sendFreeTablesToSpace(List<Table> tables) {
146 sendItemsToContainer(tables, freeTablesContainer);
147 sendTablesToSpace(tables);
150 public void sendNewGroupsToSpace(List<GroupData> newGroups) {
151 sendItemsToContainer(newGroups, groupsContainer);
152 sendItemsToContainer(newGroups, assignTableContainer);