]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java
großer Brocken an GruppenGui-Änderungen kombiniert mit ersten Teil des Bestellworkflows.
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / xvsm / XVSMConnector.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.core.Capi;
11 import org.mozartspaces.core.ContainerReference;
12 import org.mozartspaces.core.DefaultMzsCore;
13 import org.mozartspaces.core.Entry;
14 import org.mozartspaces.core.MzsCore;
15 import org.mozartspaces.core.MzsCoreException;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import at.ac.tuwien.sbc.valesriegler.common.SpaceUtil;
20 import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_Table;
21
22 /**
23  * Responsible for XVSM Communication
24  * 
25  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
26  *
27  */
28 public class XVSMConnector {
29         private static final Logger log = LoggerFactory.getLogger(XVSMConnector.class);
30         
31         private ContainerReference tablesContainer;
32         private Capi capi;
33
34         
35         public void initSpaceCommunication() throws MzsCoreException {
36                 MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
37                 capi = new Capi(core);
38                 tablesContainer = SpaceUtil.getOrCreateNamedContainer(SpaceUtil.SERVER_ADDR, SpaceUtil.TABLES_CONTAINER, capi, Arrays.asList((Coordinator) new AnyCoordinator()));
39         }
40         
41         public void sendFreeTablesToSpace(List<DEP_Table> tables) {
42                 try {
43                         List<Entry> entries = new ArrayList<>();
44                         for (DEP_Table table : tables) {
45                                 entries.add(new Entry(table));
46                         }
47                         capi.write(entries, tablesContainer);
48                         
49                         log.info("Wrote Free tables to Space!");
50                 } catch (MzsCoreException e) {
51                         e.printStackTrace();
52                 }
53         }
54
55         public List<DEP_Table> readTables() {
56                 ArrayList<DEP_Table> tables = new ArrayList<>();
57                 try {
58                         tables = capi.take(tablesContainer);
59                 } catch (MzsCoreException e) {
60                         log.error(e.getMessage());
61                         e.printStackTrace();
62                         System.exit(1);
63                 }
64                 return tables;
65         }
66
67 }