]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java
Waiter takes Order from guests
[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.MzsConstants.RequestTimeout;
19 import org.mozartspaces.core.MzsCore;
20 import org.mozartspaces.core.MzsCoreException;
21 import org.mozartspaces.core.TransactionReference;
22 import org.mozartspaces.notifications.NotificationManager;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import at.ac.tuwien.sbc.valesriegler.common.Util;
27 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
28 import at.ac.tuwien.sbc.valesriegler.types.Table;
29
30 public abstract class AbstractXVSMConnector {
31         private static final Logger log = LoggerFactory.getLogger(AbstractXVSMConnector.class);
32
33         protected ContainerReference tablesContainer;
34         protected ContainerReference groupsContainer;
35         protected ContainerReference assignTableContainer;
36         protected ContainerReference takeOrderContainer;
37         protected ContainerReference ordersContainer;
38         protected ContainerReference deliverPizzasContainer;
39         protected ContainerReference paymentContainer;
40         protected ContainerReference freeTablesContainer;
41         protected Capi capi;
42         protected NotificationManager notificationMgr;
43
44         public AbstractXVSMConnector() {
45                 initSpaceCommunication();
46         }
47
48         public void initSpaceCommunication() {
49                 try {
50                         MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
51                         capi = new Capi(core);
52                         notificationMgr = new NotificationManager(core);
53                 } catch (Exception e) {
54                         log.error("Space connection could not be established! Have you started the Space Server?");
55                         handleSpaceErrorAndTerminate(e);
56                 }
57         }
58
59         public void useTablesContainer() {
60                 tablesContainer = useContainer(Util.TABLES_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
61         }
62
63         public void useTakeOrderContainer() {
64                 takeOrderContainer = useContainer(Util.TAKE_ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
65         }
66         
67         public void useorderContainer() {
68                 ordersContainer = useContainer(Util.ORDER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
69         }
70
71         public void useGroupsContainer() {
72                 groupsContainer = useContainer(Util.GROUPS_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
73         }
74
75         public void useAssignTableContainer() {
76                 assignTableContainer = useContainer(Util.ASSIGN_TABLE, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ;
77         }
78
79         public void useFreeTablesContainer() {
80                 freeTablesContainer = useContainer(Util.FREE_TABLES, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
81         }
82
83         private ContainerReference useContainer(String containerName, List<Coordinator> coordinators) {
84                 try {
85                         return Util.getOrCreateNamedContainer(Util.SERVER_ADDR, containerName, capi, coordinators);
86                 } catch (MzsCoreException e) {
87                         handleSpaceErrorAndTerminate(e);
88                 }
89                 
90                 return null;
91         }
92
93         private List<Coordinator> createCoordinators(Coordinator... coordinator) {
94                 return Arrays.asList(coordinator);
95         }
96
97         protected void handleSpaceErrorAndTerminate(Exception e) {
98                 log.error(e.getMessage());
99                 e.printStackTrace();
100                 System.exit(1);
101         }
102
103         protected <T extends Serializable> void sendItemsToContainer(
104                 List<T> items, ContainerReference cref, long timeout, TransactionReference tx) {
105                         
106                         try {
107                                 List<Entry> entries = new ArrayList<>();
108                                 for (Serializable item : items) {
109                                         entries.add(new Entry(item));
110                                 }
111                                 capi.write(entries, cref, timeout, tx);
112                         } catch (Exception e) {
113                                 log.info(e.getMessage());
114                                 e.printStackTrace();
115                         }
116                 }
117
118         @SuppressWarnings("unchecked")
119         protected <T extends Serializable> T takeEntityByTemplateFromContainer(
120                         T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg)
121                         throws MzsCoreException {
122                                 try {
123                                         LindaSelector sel = LindaCoordinator.newSelector(entity, 1);
124                                         T singleEntity = null;
125                                         
126                                         ArrayList<Serializable> entities = capi.take(ref,  sel, timeout, tx);
127                                         
128                                         return (T) CapiUtil.getSingleEntry(entities);
129                                 } catch (CountNotMetException e) {
130                                         log.info(errorMsg);
131                                         capi.rollbackTransaction(tx);
132                                         
133                                         throw new EntityNotFoundByTemplate(errorMsg);
134                                 }
135                         }
136
137         protected <T extends Serializable> List<T> castEntries(List<? extends Serializable> entries) {
138                 List<T> newList = new ArrayList<T>();
139                 List<Entry> newEntries = (List<Entry>) entries;
140                 for (Entry entry : newEntries) {
141                         newList.add((T) entry.getValue());
142                 }
143                 return newList;
144         }
145
146         public void sendTablesToSpace(List<Table> tables) {
147                 sendItemsToContainer(tables, tablesContainer, RequestTimeout.DEFAULT, null);
148         }
149
150         public void sendFreeTablesToSpace(List<Table> tables) {
151                 sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null);
152                 sendTablesToSpace(tables);
153         }
154
155         public void sendNewGroupsToSpace(List<GroupData> newGroups) {
156                 sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null);
157                 sendItemsToContainer(newGroups, assignTableContainer, RequestTimeout.DEFAULT, null);
158         }
159
160 }