]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java
Some space refactoring. Doing away with unnecessary containers. Auto-Reloading of...
[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 tableAssignedContainer;
37         protected ContainerReference assignTableContainer;
38         protected ContainerReference takeOrderContainer;
39         protected ContainerReference orderTakenContainer;
40         protected ContainerReference preparePizzasContainer;
41         protected ContainerReference deliverPizzasContainer;
42         protected ContainerReference paymentRequestContainer;
43         protected ContainerReference freeTablesContainer;
44         protected ContainerReference pizzaInProgressContainer;
45         protected ContainerReference orderCompleteContainer;
46         protected ContainerReference isEatingContainer;
47         protected ContainerReference paymentDoneContainer;
48         protected Capi capi;
49         protected NotificationManager notificationMgr;
50
51         public AbstractXVSMConnector() {
52                 initSpaceCommunication();
53         }
54
55         public void initSpaceCommunication() {
56                 try {
57                         MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
58                         capi = new Capi(core);
59                         notificationMgr = new NotificationManager(core);
60                 } catch (Exception e) {
61                         log.error("Space connection could not be established! Have you started the Space Server?");
62                         handleSpaceErrorAndTerminate(e);
63                 }
64         }
65         
66         protected ContainerReference useContainer(String containerName) {
67                 try {
68                         return CapiUtil.lookupOrCreateContainer(containerName, URI.create(Util.SERVER_ADDR), createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)), null, capi);
69                 } catch (MzsCoreException e) {
70                         handleSpaceErrorAndTerminate(e);
71                 }
72                 
73                 throw new RuntimeException("Could not Create container " + containerName);
74         }
75
76         protected List<Coordinator> createCoordinators(Coordinator... coordinator) {
77                 return Arrays.asList(coordinator);
78         }
79
80         protected void handleSpaceErrorAndTerminate(Exception e) {
81                 log.error(e.getMessage());
82                 e.printStackTrace();
83                 System.exit(1);
84         }
85
86         protected <T extends Serializable> void sendItemsToContainer(
87                 List<T> items, ContainerReference cref, long timeout, TransactionReference tx) {
88                         
89                         try {
90                                 List<Entry> entries = new ArrayList<>();
91                                 for (Serializable item : items) {
92                                         entries.add(new Entry(item));
93                                 }
94                                 capi.write(entries, cref, timeout, tx);
95                         } catch (Exception e) {
96                                 log.info(e.getMessage());
97                                 e.printStackTrace();
98                         }
99                 }
100
101         @SuppressWarnings("unchecked")
102         protected <T extends Serializable> T takeMatchingEntity(
103                         T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
104                         throws MzsCoreException {
105                                 try {
106                                         LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
107                                         
108                                         ArrayList<Serializable> entities = capi.take(ref,  sel, timeout, tx);
109                                         
110                                         return (T) CapiUtil.getSingleEntry(entities);
111                                 } catch (CountNotMetException e) {
112                                         capi.rollbackTransaction(tx);
113                                         
114                                         throw new EntityNotFoundByTemplate(errorMsg);
115                                 } catch(MzsTimeoutException e) {
116                                         capi.rollbackTransaction(tx);
117                                         
118                                         throw new EntityNotFoundByTemplate(errorMsg);
119                                 }
120                         }
121         
122         protected <T extends Serializable> List<T> takeMatchingEntities(
123                         T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
124                         throws MzsCoreException {
125                                 try {
126                                         LindaSelector sel = LindaCoordinator.newSelector(entity, MzsConstants.Selecting.COUNT_MAX);
127                                         
128                                         ArrayList<Serializable> entities = capi.take(ref,  sel, timeout, tx);
129                                         
130                                         return (List<T>) entities;
131                                 } catch (CountNotMetException e) {
132                                         capi.rollbackTransaction(tx);
133                                         
134                                         throw new EntityNotFoundByTemplate(errorMsg);
135                                 } catch(MzsTimeoutException e) {
136                                         capi.rollbackTransaction(tx);
137                                         
138                                         throw new EntityNotFoundByTemplate(errorMsg);
139                                 }
140                         }
141
142         protected <T extends Serializable> List<T> castEntries(List<? extends Serializable> entries) {
143                 List<T> newList = new ArrayList<T>();
144                 List<Entry> newEntries = (List<Entry>) entries;
145                 for (Entry entry : newEntries) {
146                         newList.add((T) entry.getValue());
147                 }
148                 return newList;
149         }
150
151 //      public void sendTablesToSpace(List<Table> tables) {
152 //              sendItemsToContainer(tables, tableOccupiedContainer, RequestTimeout.DEFAULT, null);
153 //      }
154
155         public void sendFreeTablesToSpace(List<Table> tables) {
156                 sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null);
157         }
158
159         public void sendNewGroupsToSpace(List<GroupData> newGroups) {
160 //              sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null);
161                 sendItemsToContainer(newGroups, assignTableContainer, RequestTimeout.DEFAULT, null);
162         }
163 }