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