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