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