]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java
Space cook listens for pizza requests
[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.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
7
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;
26
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;
30
31 public abstract class AbstractXVSMConnector {
32         private static final Logger log = LoggerFactory.getLogger(AbstractXVSMConnector.class);
33
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 Capi capi;
44         protected NotificationManager notificationMgr;
45
46         public AbstractXVSMConnector() {
47                 initSpaceCommunication();
48         }
49
50         public void initSpaceCommunication() {
51                 try {
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);
58                 }
59         }
60
61         public void useTablesContainer() {
62                 tablesContainer = useContainer(Util.TABLES_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
63         }
64         
65         public void usePreparePizzasContainer() {
66                 preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
67         }
68
69         public void useTakeOrderContainer() {
70                 takeOrderContainer = useContainer(Util.TAKE_ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
71         }
72         
73         public void useOrdersContainer() {
74                 ordersContainer = useContainer(Util.ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
75         }
76
77         public void useGroupsContainer() {
78                 groupsContainer = useContainer(Util.GROUPS_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
79         }
80
81         public void useAssignTableContainer() {
82                 assignTableContainer = useContainer(Util.ASSIGN_TABLE, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
83         }
84
85         public void useFreeTablesContainer() {
86                 freeTablesContainer = useContainer(Util.FREE_TABLES, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
87         }
88         
89         public void useDeliverPizzasContainer() {
90                 deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
91         }
92
93         private ContainerReference useContainer(String containerName, List<Coordinator> coordinators) {
94                 try {
95                         return Util.getOrCreateNamedContainer(Util.SERVER_ADDR, containerName, capi, coordinators);
96                 } catch (MzsCoreException e) {
97                         handleSpaceErrorAndTerminate(e);
98                 }
99                 
100                 return null;
101         }
102
103         private List<Coordinator> createCoordinators(Coordinator... coordinator) {
104                 return Arrays.asList(coordinator);
105         }
106
107         protected void handleSpaceErrorAndTerminate(Exception e) {
108                 log.error(e.getMessage());
109                 e.printStackTrace();
110                 System.exit(1);
111         }
112
113         protected <T extends Serializable> void sendItemsToContainer(
114                 List<T> items, ContainerReference cref, long timeout, TransactionReference tx) {
115                         
116                         try {
117                                 List<Entry> entries = new ArrayList<>();
118                                 for (Serializable item : items) {
119                                         entries.add(new Entry(item));
120                                 }
121                                 capi.write(entries, cref, timeout, tx);
122                         } catch (Exception e) {
123                                 log.info(e.getMessage());
124                                 e.printStackTrace();
125                         }
126                 }
127
128         @SuppressWarnings("unchecked")
129         protected <T extends Serializable> T takeEntityByTemplateFromContainer(
130                         T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
131                         throws MzsCoreException {
132                                 try {
133                                         LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
134                                         T singleEntity = null;
135                                         
136                                         ArrayList<Serializable> entities = capi.take(ref,  sel, timeout, tx);
137                                         
138                                         return (T) CapiUtil.getSingleEntry(entities);
139                                 } catch (CountNotMetException e) {
140                                         capi.rollbackTransaction(tx);
141                                         
142                                         throw new EntityNotFoundByTemplate(errorMsg);
143                                 } catch(MzsTimeoutException e) {
144                                         capi.rollbackTransaction(tx);
145                                         
146                                         throw new EntityNotFoundByTemplate(errorMsg);
147                                 }
148                         }
149
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());
155                 }
156                 return newList;
157         }
158
159         public void sendTablesToSpace(List<Table> tables) {
160                 sendItemsToContainer(tables, tablesContainer, RequestTimeout.DEFAULT, null);
161         }
162
163         public void sendFreeTablesToSpace(List<Table> tables) {
164                 sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null);
165                 sendTablesToSpace(tables);
166         }
167
168         public void sendNewGroupsToSpace(List<GroupData> newGroups) {
169                 sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null);
170                 sendItemsToContainer(newGroups, assignTableContainer, RequestTimeout.DEFAULT, null);
171         }
172 }