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 preparePizzasContainer;
40 protected ContainerReference deliverPizzasContainer;
41 protected ContainerReference paymentContainer;
42 protected ContainerReference freeTablesContainer;
44 protected NotificationManager notificationMgr;
46 public AbstractXVSMConnector() {
47 initSpaceCommunication();
50 public void initSpaceCommunication() {
52 MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
53 capi = new Capi(core);
54 notificationMgr = new NotificationManager(core);
55 } catch (Exception e) {
56 log.error("Space connection could not be established! Have you started the Space Server?");
57 handleSpaceErrorAndTerminate(e);
61 public void useTablesContainer() {
62 tablesContainer = useContainer(Util.TABLES_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
65 public void usePreparePizzasContainer() {
66 preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
69 public void useTakeOrderContainer() {
70 takeOrderContainer = useContainer(Util.TAKE_ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
73 public void useOrdersContainer() {
74 ordersContainer = useContainer(Util.ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
77 public void useGroupsContainer() {
78 groupsContainer = useContainer(Util.GROUPS_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
81 public void useAssignTableContainer() {
82 assignTableContainer = useContainer(Util.ASSIGN_TABLE, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
85 public void useFreeTablesContainer() {
86 freeTablesContainer = useContainer(Util.FREE_TABLES, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
89 public void useDeliverPizzasContainer() {
90 deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
93 private ContainerReference useContainer(String containerName, List<Coordinator> coordinators) {
95 return Util.getOrCreateNamedContainer(Util.SERVER_ADDR, containerName, capi, coordinators);
96 } catch (MzsCoreException e) {
97 handleSpaceErrorAndTerminate(e);
103 private List<Coordinator> createCoordinators(Coordinator... coordinator) {
104 return Arrays.asList(coordinator);
107 protected void handleSpaceErrorAndTerminate(Exception e) {
108 log.error(e.getMessage());
113 protected <T extends Serializable> void sendItemsToContainer(
114 List<T> items, ContainerReference cref, long timeout, TransactionReference tx) {
117 List<Entry> entries = new ArrayList<>();
118 for (Serializable item : items) {
119 entries.add(new Entry(item));
121 capi.write(entries, cref, timeout, tx);
122 } catch (Exception e) {
123 log.info(e.getMessage());
128 @SuppressWarnings("unchecked")
129 protected <T extends Serializable> T takeEntityByTemplateFromContainer(
130 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
131 throws MzsCoreException {
133 LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
134 T singleEntity = null;
136 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
138 return (T) CapiUtil.getSingleEntry(entities);
139 } catch (CountNotMetException e) {
140 capi.rollbackTransaction(tx);
142 throw new EntityNotFoundByTemplate(errorMsg);
143 } catch(MzsTimeoutException e) {
144 capi.rollbackTransaction(tx);
146 throw new EntityNotFoundByTemplate(errorMsg);
150 protected <T extends Serializable> List<T> castEntries(List<? extends Serializable> entries) {
151 List<T> newList = new ArrayList<T>();
152 List<Entry> newEntries = (List<Entry>) entries;
153 for (Entry entry : newEntries) {
154 newList.add((T) entry.getValue());
159 public void sendTablesToSpace(List<Table> tables) {
160 sendItemsToContainer(tables, tablesContainer, RequestTimeout.DEFAULT, null);
163 public void sendFreeTablesToSpace(List<Table> tables) {
164 sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null);
165 sendTablesToSpace(tables);
168 public void sendNewGroupsToSpace(List<GroupData> newGroups) {
169 sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null);
170 sendItemsToContainer(newGroups, assignTableContainer, RequestTimeout.DEFAULT, null);