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