81
|
1 package alice.jungle.persistence;
|
|
2
|
83
|
3 import java.io.EOFException;
|
|
4 import java.io.File;
|
|
5 import java.io.FileInputStream;
|
|
6 import java.io.FileNotFoundException;
|
|
7 import java.io.FileOutputStream;
|
|
8 import java.io.IOException;
|
|
9 import java.io.InputStream;
|
|
10 import java.io.OutputStream;
|
|
11 import java.util.Iterator;
|
|
12
|
|
13 import org.msgpack.MessagePack;
|
|
14
|
|
15 import alice.jungle.operations.NetworkTreeOperationLog;
|
|
16
|
81
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList;
|
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListReader;
|
|
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListWriter;
|
|
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.Journal;
|
|
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.Result;
|
83
|
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
|
81
|
23
|
|
24 public class NetworkJournal implements Journal {
|
|
25
|
|
26 private static final NetworkChangeListWriter WRITER = new NetworkChangeListWriter();
|
|
27 private static final NetworkChangeListReader READER = new NetworkChangeListReader();
|
83
|
28 private static MessagePack msgpack = new MessagePack();
|
|
29 private static OutputStream out = null;
|
|
30 private static InputStream in = null;
|
|
31
|
|
32 public NetworkJournal() {
|
|
33
|
|
34 }
|
|
35
|
|
36 public NetworkJournal(File file) throws FileNotFoundException {
|
|
37 out = new FileOutputStream(file, true);
|
|
38 in = new FileInputStream(file);
|
|
39 }
|
|
40
|
81
|
41 @Override
|
|
42 public ChangeListReader getReader() {
|
|
43 return READER;
|
|
44 }
|
|
45 @Override
|
|
46 public ChangeListWriter getWriter() {
|
|
47 return WRITER;
|
|
48 }
|
|
49
|
83
|
50 public void close() throws IOException {
|
|
51 out.close();
|
|
52 in.close();
|
|
53 }
|
|
54
|
85
|
55 public void setOutputFile(File file) throws FileNotFoundException {
|
|
56 out = new FileOutputStream(file);
|
|
57 }
|
|
58
|
|
59 public void setInputFile(File file) throws FileNotFoundException {
|
|
60 in = new FileInputStream(file);
|
|
61 }
|
|
62
|
83
|
63 public void setOutputStream(OutputStream _out) {
|
|
64 out = _out;
|
|
65 }
|
|
66
|
|
67 public OutputStream getOutputStream() {
|
|
68 return out;
|
|
69 }
|
|
70
|
|
71 public void setInputStream(InputStream _in) {
|
|
72 in = _in;
|
|
73 }
|
|
74
|
|
75 public InputStream getInputStream() {
|
|
76 return in;
|
|
77 }
|
|
78
|
81
|
79 private static class NetworkChangeListWriter implements ChangeListWriter
|
|
80 {
|
|
81 @Override
|
|
82 public Result write(ChangeList _operations)
|
|
83 {
|
83
|
84 NetworkTreeOperationLog log = new NetworkTreeOperationLog(_operations);
|
|
85 try {
|
|
86 msgpack.write(out, log);
|
|
87 out.flush();
|
|
88 } catch (IOException e) {
|
|
89 return null;
|
|
90 }
|
81
|
91 return Result.SUCCESS;
|
|
92 }
|
|
93 }
|
|
94
|
84
|
95 public static MessagePack getMessagePack() {
|
83
|
96 return msgpack;
|
|
97 }
|
|
98
|
81
|
99 private static class NetworkChangeListReader implements ChangeListReader
|
|
100 {
|
|
101 @Override
|
|
102 public ChangeListReader newReader()
|
|
103 {
|
|
104 return this;
|
|
105 }
|
|
106
|
|
107 @Override
|
83
|
108 public ChangeList read()
|
81
|
109 {
|
83
|
110 try {
|
|
111 final NetworkTreeOperationLog readLog = msgpack.read(in, NetworkTreeOperationLog.class);
|
|
112 ChangeList cl = new ChangeList() {
|
|
113 @Override
|
|
114 public Iterator<TreeOperation> iterator() {
|
|
115 return readLog.iterator();
|
|
116 }
|
|
117 };
|
|
118 return cl;
|
|
119 } catch (EOFException e){
|
|
120 // e.printStackTrace();
|
|
121 } catch (IOException e) {
|
|
122 // e.printStackTrace();
|
|
123 }
|
81
|
124 return null;
|
|
125 }
|
|
126
|
|
127 }
|
|
128 }
|