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;
43 protected ContainerReference pizzaInProgressContainer;
45 protected NotificationManager notificationMgr;
47 public AbstractXVSMConnector() {
48 initSpaceCommunication();
51 public void initSpaceCommunication() {
53 MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
54 capi = new Capi(core);
55 notificationMgr = new NotificationManager(core);
56 } catch (Exception e) {
57 log.error("Space connection could not be established! Have you started the Space Server?");
58 handleSpaceErrorAndTerminate(e);
62 public void usePizzaInProgressContainer() {
63 pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
66 public void useTablesContainer() {
67 tablesContainer = useContainer(Util.TABLES_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
70 public void usePreparePizzasContainer() {
71 preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
74 public void useTakeOrderContainer() {
75 takeOrderContainer = useContainer(Util.TAKE_ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
78 public void useOrdersContainer() {
79 ordersContainer = useContainer(Util.ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
82 public void useGroupsContainer() {
83 groupsContainer = useContainer(Util.GROUPS_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
86 public void useAssignTableContainer() {
87 assignTableContainer = useContainer(Util.ASSIGN_TABLE, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
90 public void useFreeTablesContainer() {
91 freeTablesContainer = useContainer(Util.FREE_TABLES, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
94 public void useDeliverPizzasContainer() {
95 deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
98 private ContainerReference useContainer(String containerName, List<Coordinator> coordinators) {
100 return Util.getOrCreateNamedContainer(Util.SERVER_ADDR, containerName, capi, coordinators);
101 } catch (MzsCoreException e) {
102 handleSpaceErrorAndTerminate(e);
108 private List<Coordinator> createCoordinators(Coordinator... coordinator) {
109 return Arrays.asList(coordinator);
112 protected void handleSpaceErrorAndTerminate(Exception e) {
113 log.error(e.getMessage());
118 protected <T extends Serializable> void sendItemsToContainer(
119 List<T> items, ContainerReference cref, long timeout, TransactionReference tx) {
122 List<Entry> entries = new ArrayList<>();
123 for (Serializable item : items) {
124 entries.add(new Entry(item));
126 capi.write(entries, cref, timeout, tx);
127 } catch (Exception e) {
128 log.info(e.getMessage());
133 @SuppressWarnings("unchecked")
134 protected <T extends Serializable> T takeEntityByTemplateFromContainer(
135 T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
136 throws MzsCoreException {
138 LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
139 T singleEntity = null;
141 ArrayList<Serializable> entities = capi.take(ref, sel, timeout, tx);
143 return (T) CapiUtil.getSingleEntry(entities);
144 } catch (CountNotMetException e) {
145 capi.rollbackTransaction(tx);
147 throw new EntityNotFoundByTemplate(errorMsg);
148 } catch(MzsTimeoutException e) {
149 capi.rollbackTransaction(tx);
151 throw new EntityNotFoundByTemplate(errorMsg);
155 protected <T extends Serializable> List<T> castEntries(List<? extends Serializable> entries) {
156 List<T> newList = new ArrayList<T>();
157 List<Entry> newEntries = (List<Entry>) entries;
158 for (Entry entry : newEntries) {
159 newList.add((T) entry.getValue());
164 public void sendTablesToSpace(List<Table> tables) {
165 sendItemsToContainer(tables, tablesContainer, RequestTimeout.DEFAULT, null);
168 public void sendFreeTablesToSpace(List<Table> tables) {
169 sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null);
170 sendTablesToSpace(tables);
173 public void sendNewGroupsToSpace(List<GroupData> newGroups) {
174 sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null);
175 sendItemsToContainer(newGroups, assignTableContainer, RequestTimeout.DEFAULT, null);