]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java
Space waiters pay and overall remaining space workflow refactoring
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / xvsm / AbstractXVSMConnector.java
1 package at.ac.tuwien.sbc.valesriegler.xvsm;
2
3 import java.io.Serializable;
4 import java.net.URI;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.List;
8
9 import org.mozartspaces.capi3.AnyCoordinator;
10 import org.mozartspaces.capi3.Coordinator;
11 import org.mozartspaces.capi3.CountNotMetException;
12 import org.mozartspaces.capi3.LindaCoordinator;
13 import org.mozartspaces.capi3.LindaCoordinator.LindaSelector;
14 import org.mozartspaces.core.Capi;
15 import org.mozartspaces.core.CapiUtil;
16 import org.mozartspaces.core.ContainerReference;
17 import org.mozartspaces.core.DefaultMzsCore;
18 import org.mozartspaces.core.Entry;
19 import org.mozartspaces.core.MzsConstants;
20 import org.mozartspaces.core.MzsTimeoutException;
21 import org.mozartspaces.core.MzsConstants.RequestTimeout;
22 import org.mozartspaces.core.MzsCore;
23 import org.mozartspaces.core.MzsCoreException;
24 import org.mozartspaces.core.TransactionReference;
25 import org.mozartspaces.notifications.NotificationManager;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import at.ac.tuwien.sbc.valesriegler.common.Util;
30 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
31 import at.ac.tuwien.sbc.valesriegler.types.Table;
32
33 public abstract class AbstractXVSMConnector {
34         private static final Logger log = LoggerFactory.getLogger(AbstractXVSMConnector.class);
35
36         protected ContainerReference tablesContainer;
37         protected ContainerReference groupsContainer;
38         protected ContainerReference assignTableContainer;
39         protected ContainerReference takeOrderContainer;
40         protected ContainerReference ordersContainer;
41         protected ContainerReference preparePizzasContainer;
42         protected ContainerReference deliverPizzasContainer;
43         protected ContainerReference paymentContainer;
44         protected ContainerReference freeTablesContainer;
45         protected ContainerReference pizzaInProgressContainer;
46         protected ContainerReference orderCompleteContainer;
47         protected ContainerReference isEatingContainer;
48         protected ContainerReference hasPaidContainer;
49         protected Capi capi;
50         protected NotificationManager notificationMgr;
51
52         public AbstractXVSMConnector() {
53                 initSpaceCommunication();
54         }
55
56         public void initSpaceCommunication() {
57                 try {
58                         MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
59                         capi = new Capi(core);
60                         notificationMgr = new NotificationManager(core);
61                 } catch (Exception e) {
62                         log.error("Space connection could not be established! Have you started the Space Server?");
63                         handleSpaceErrorAndTerminate(e);
64                 }
65         }
66         
67         protected ContainerReference useContainer(String containerName) {
68                 try {
69                         return CapiUtil.lookupOrCreateContainer(containerName, URI.create(Util.SERVER_ADDR), createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)), null, capi);
70                 } catch (MzsCoreException e) {
71                         handleSpaceErrorAndTerminate(e);
72                 }
73                 
74                 throw new RuntimeException("Could not Create container " + containerName);
75         }
76
77         protected List<Coordinator> createCoordinators(Coordinator... coordinator) {
78                 return Arrays.asList(coordinator);
79         }
80
81         protected void handleSpaceErrorAndTerminate(Exception e) {
82                 log.error(e.getMessage());
83                 e.printStackTrace();
84                 System.exit(1);
85         }
86
87         protected <T extends Serializable> void sendItemsToContainer(
88                 List<T> items, ContainerReference cref, long timeout, TransactionReference tx) {
89                         
90                         try {
91                                 List<Entry> entries = new ArrayList<>();
92                                 for (Serializable item : items) {
93                                         entries.add(new Entry(item));
94                                 }
95                                 capi.write(entries, cref, timeout, tx);
96                         } catch (Exception e) {
97                                 log.info(e.getMessage());
98                                 e.printStackTrace();
99                         }
100                 }
101
102         @SuppressWarnings("unchecked")
103         protected <T extends Serializable> T takeMatchingEntity(
104                         T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
105                         throws MzsCoreException {
106                                 try {
107                                         LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
108                                         
109                                         ArrayList<Serializable> entities = capi.take(ref,  sel, timeout, tx);
110                                         
111                                         return (T) CapiUtil.getSingleEntry(entities);
112                                 } catch (CountNotMetException e) {
113                                         capi.rollbackTransaction(tx);
114                                         
115                                         throw new EntityNotFoundByTemplate(errorMsg);
116                                 } catch(MzsTimeoutException e) {
117                                         capi.rollbackTransaction(tx);
118                                         
119                                         throw new EntityNotFoundByTemplate(errorMsg);
120                                 }
121                         }
122         
123         protected <T extends Serializable> List<T> takeMatchingEntities(
124                         T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
125                         throws MzsCoreException {
126                                 try {
127                                         LindaSelector sel = LindaCoordinator.newSelector(entity, MzsConstants.Selecting.COUNT_MAX);
128                                         
129                                         ArrayList<Serializable> entities = capi.take(ref,  sel, timeout, tx);
130                                         
131                                         return (List<T>) entities;
132                                 } catch (CountNotMetException e) {
133                                         capi.rollbackTransaction(tx);
134                                         
135                                         throw new EntityNotFoundByTemplate(errorMsg);
136                                 } catch(MzsTimeoutException e) {
137                                         capi.rollbackTransaction(tx);
138                                         
139                                         throw new EntityNotFoundByTemplate(errorMsg);
140                                 }
141                         }
142
143         protected <T extends Serializable> List<T> castEntries(List<? extends Serializable> entries) {
144                 List<T> newList = new ArrayList<T>();
145                 List<Entry> newEntries = (List<Entry>) entries;
146                 for (Entry entry : newEntries) {
147                         newList.add((T) entry.getValue());
148                 }
149                 return newList;
150         }
151
152         public void sendTablesToSpace(List<Table> tables) {
153                 sendItemsToContainer(tables, tablesContainer, RequestTimeout.DEFAULT, null);
154         }
155
156         public void sendFreeTablesToSpace(List<Table> tables) {
157                 sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null);
158                 sendTablesToSpace(tables);
159         }
160
161         public void sendNewGroupsToSpace(List<GroupData> newGroups) {
162                 sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null);
163                 sendItemsToContainer(newGroups, assignTableContainer, RequestTimeout.DEFAULT, null);
164         }
165 }