66
|
1 package jungle.app.bbs;
|
38
|
2
|
46
|
3 import java.io.IOException;
|
38
|
4 import java.nio.ByteBuffer;
|
56
|
5 import java.util.Date;
|
38
|
6 import java.util.concurrent.atomic.AtomicInteger;
|
39
|
7
|
|
8 import alice.jungle.core.NetworkDefaultJungle;
|
46
|
9 import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer;
|
|
10 import alice.jungle.transaction.NetworkDefaultJungleTreeEditor;
|
38
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
|
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
|
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
|
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children;
|
|
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node;
|
|
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
|
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
|
46
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
|
38
|
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode;
|
|
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor;
|
65
|
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultTreeNode;
|
38
|
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
|
|
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
|
|
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
|
|
25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
|
|
26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter;
|
66
|
27 import jungle.app.bbs.codesegment.HashLogUpdateCodeSegment;
|
38
|
28
|
39
|
29 public class NetworkJungleBulletinBoard implements BulletinBoard
|
38
|
30 {
|
|
31 private final Jungle jungle;
|
|
32
|
43
|
33 public NetworkJungleBulletinBoard(String _serverName)
|
38
|
34 {
|
51
|
35 jungle = new NetworkDefaultJungle(null,"hoge",new DefaultTreeEditor(new DefaultTraverser()), _serverName);
|
38
|
36 jungle.createNewTree("boards");
|
51
|
37 JungleManager.setJungle(jungle);
|
38
|
38 }
|
|
39
|
|
40 public Iterable<String> getBoards()
|
|
41 {
|
|
42 JungleTree tree = jungle.getTreeByName("boards");
|
|
43 Node node = tree.getRootNode();
|
|
44 Children<Node> chs = node.getChildren();
|
|
45
|
|
46 IterableConverter.Converter<String,Node> converter = new IterableConverter.Converter<String,Node>(){
|
|
47 public String conv(Node _b) {
|
|
48 ByteBuffer e = _b.getAttributes().get("name");
|
|
49 return new String(e.array());
|
|
50 }
|
|
51 };
|
|
52
|
|
53 return new IterableConverter<String,Node>(chs,converter);
|
|
54 }
|
|
55
|
|
56 public void createBoards(final String _name,final String _author,final String _initMessage,final String _editKey)
|
|
57 {
|
|
58 if(null == jungle.createNewTree(_name)){
|
|
59 throw new IllegalStateException();
|
|
60 }
|
|
61
|
|
62 JungleTree tree = jungle.getTreeByName("boards");
|
|
63 JungleTreeEditor editor = tree.getTreeEditor();
|
|
64 DefaultNodePath root = new DefaultNodePath();
|
|
65 Either<Error,JungleTreeEditor> either = editor.addNewChildAt(root,0);
|
|
66 if(either.isA()){
|
|
67 throw new IllegalStateException();
|
|
68 }
|
|
69 editor = either.b();
|
|
70
|
|
71 either = editor.putAttribute(root.add(0),"name",ByteBuffer.wrap(_name.getBytes()));
|
|
72 if(either.isA()){
|
|
73 throw new IllegalStateException();
|
|
74 }
|
|
75 editor = either.b();
|
|
76 Either<Error,JungleTreeEditor> result = editor.success();
|
|
77 if(result.isA()){
|
|
78 throw new IllegalStateException();
|
|
79 }
|
56
|
80 final long timestamp = new Date().getTime();
|
46
|
81 /* Put DataSegment */
|
|
82 try {
|
57
|
83 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor, 0, timestamp);
|
46
|
84 } catch (IOException e1) {
|
|
85 e1.printStackTrace();
|
|
86 }
|
38
|
87
|
|
88 tree = jungle.getTreeByName(_name);
|
|
89 editor = tree.getTreeEditor();
|
|
90 either = editor.addNewChildAt(root,0);
|
|
91 if(either.isA()){
|
|
92 throw new IllegalStateException();
|
|
93 }
|
|
94 editor = either.b();
|
|
95
|
|
96 NodeEditor e = new NodeEditor(){
|
|
97 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
98 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
99 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_initMessage.getBytes())).b();
|
|
100 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
56
|
101 ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
102 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
|
38
|
103 return DefaultEither.newB(_e);
|
|
104 }
|
|
105 };
|
|
106
|
|
107 either = editor.edit(root.add(0),e);
|
|
108 if(either.isA()){
|
|
109 throw new IllegalStateException();
|
|
110 }
|
|
111 editor = either.b();
|
|
112 editor.success();
|
46
|
113 /* Put DataSegment */
|
|
114 try {
|
57
|
115 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor, 0, timestamp);
|
46
|
116 } catch (IOException e1) {
|
|
117 e1.printStackTrace();
|
|
118 }
|
|
119
|
38
|
120 }
|
|
121
|
|
122 public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey)
|
|
123 {
|
|
124 JungleTree tree = jungle.getTreeByName(_board);
|
|
125 if(tree == null){
|
|
126 throw new IllegalStateException();
|
|
127 }
|
|
128
|
46
|
129 Either<Error, JungleTreeEditor> either;
|
38
|
130 do{
|
|
131 Node node = tree.getRootNode();
|
|
132 int size = node.getChildren().size();
|
|
133 DefaultNodePath path = new DefaultNodePath();
|
|
134
|
65
|
135 JungleTreeEditor editor = tree.getTreeEditor();
|
46
|
136 either = editor.addNewChildAt(path,size);
|
38
|
137 if(either.isA()){
|
|
138 throw new IllegalStateException();
|
|
139 }
|
|
140 editor = either.b();
|
56
|
141 final long timestamp = new Date().getTime();
|
38
|
142 NodeEditor e = new NodeEditor(){
|
|
143 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
144 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
145 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b();
|
|
146 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
56
|
147 ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
148 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
|
38
|
149 return DefaultEither.newB(_e);
|
|
150 }
|
|
151 };
|
|
152 path = path.add(size);
|
|
153 either = editor.edit(path,e);
|
|
154 if(either.isA()){
|
|
155 throw new IllegalStateException();
|
|
156 }
|
|
157 editor = either.b();
|
46
|
158 either = editor.success();
|
|
159 try {
|
65
|
160 NetworkDefaultJungleTreeEditor<DefaultTreeNode> netEditor = (NetworkDefaultJungleTreeEditor<DefaultTreeNode>) editor;
|
|
161 putTreeOperationLog(netEditor, size, timestamp);
|
46
|
162 } catch (IOException e1) {
|
|
163 e1.printStackTrace();
|
|
164 }
|
|
165 }while(either.isA());
|
51
|
166 /* Put DataSegment */
|
38
|
167 }
|
|
168
|
|
169 public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey)
|
|
170 {
|
65
|
171 for(;;) {
|
38
|
172 DefaultNodePath path = new DefaultNodePath();
|
|
173 path = path.add(Integer.parseInt(_uuid));
|
|
174
|
|
175 JungleTree tree = jungle.getTreeByName(_board);
|
65
|
176 JungleTreeEditor editor = tree.getTreeEditor();
|
56
|
177 final long timestamp = new Date().getTime();
|
38
|
178 NodeEditor e = new NodeEditor(){
|
|
179 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
180 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
181 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b();
|
|
182 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
56
|
183 ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
184 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
|
38
|
185 return DefaultEither.newB(_e);
|
|
186 }
|
|
187 };
|
|
188
|
65
|
189 Either<Error, JungleTreeEditor> either = editor.edit(path,e);
|
38
|
190 if(either.isA()){
|
|
191 throw new IllegalStateException();
|
|
192 }
|
|
193 editor = either.b();
|
46
|
194 either = editor.success();
|
|
195 try {
|
57
|
196 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor, Integer.parseInt(_uuid), timestamp);
|
46
|
197 } catch (IOException e1) {
|
|
198 e1.printStackTrace();
|
|
199 }
|
65
|
200 if(!either.isA()) {
|
|
201 return;
|
|
202 }
|
|
203 }
|
|
204
|
38
|
205 }
|
|
206
|
|
207 public Iterable<BoardMessage> getMessages(String _boardName)
|
|
208 {
|
|
209 JungleTree tree = jungle.getTreeByName(_boardName);
|
|
210 Node node = tree.getRootNode();
|
|
211 Children<Node> chs = node.getChildren();
|
|
212
|
|
213 final AtomicInteger counter = new AtomicInteger(0);
|
|
214 IterableConverter.Converter<BoardMessage,Node> converter = new IterableConverter.Converter<BoardMessage,Node>(){
|
|
215 public BoardMessage conv(Node _b) {
|
|
216 String uuid = Integer.toString(counter.get());
|
|
217 String author = new String(_b.getAttributes().get("author").array());
|
|
218 String message = new String(_b.getAttributes().get("mes").array());
|
|
219 counter.incrementAndGet();
|
|
220 return new BoardMessageImpl(author,message,uuid);
|
|
221 }
|
|
222 };
|
|
223
|
|
224 return new IterableConverter<BoardMessage,Node>(chs,converter);
|
|
225 }
|
|
226
|
65
|
227 private void putTreeOperationLog(NetworkDefaultJungleTreeEditor<DefaultTreeNode> editor, int pos, long timestamp) throws IOException {
|
46
|
228 String uuid = editor.getID();
|
|
229 String treeName = editor.getTreeName();
|
49
|
230 String updaterName = editor.getUpdaterName();
|
52
|
231 String revision = editor.getRevision();
|
46
|
232 Iterable<TreeOperation> log = editor.getTreeOperationLog();
|
57
|
233 putDataSegment(uuid, treeName, updaterName, log, revision, pos,timestamp);
|
46
|
234 }
|
|
235
|
57
|
236 private void putDataSegment(String _uuid, String _treeName, String _updaterName, Iterable<TreeOperation> _log, String nextRevision, int pos, long timestamp) throws IOException {
|
46
|
237 DefaultTreeOperationLogContainer container = new DefaultTreeOperationLogContainer();
|
|
238 container.setTreeName(_treeName);
|
|
239 container.setUUID(_uuid);
|
49
|
240 container.setUpdaterName(_updaterName);
|
46
|
241 container.setRevision(nextRevision);
|
57
|
242 container.setPosition(pos);
|
46
|
243 container.unconvert(_log);
|
56
|
244 container.setTimeStamp(timestamp);
|
|
245 HashLogUpdateCodeSegment cs = new HashLogUpdateCodeSegment();
|
47
|
246 cs.ods.put("log", container);
|
56
|
247 cs.ods.put("logString", container.getHashLogString());
|
52
|
248 /* If this node is not Root node, push log to parent node's DS */
|
50
|
249 if(!_updaterName.equals("node0")) {
|
51
|
250 cs.ods.put("parent", "childLog", container);
|
50
|
251 }
|
46
|
252 }
|
|
253
|
|
254
|
38
|
255 private static class BoardMessageImpl implements BoardMessage
|
|
256 {
|
|
257 private final String author;
|
|
258 private final String message;
|
|
259 private final String uuid;
|
|
260
|
|
261 public BoardMessageImpl(String _author,String _message,String _uuid)
|
|
262 {
|
|
263 author = _author;
|
|
264 message = _message;
|
|
265 uuid = _uuid;
|
|
266 }
|
|
267
|
|
268 public String getAuthor()
|
|
269 {
|
|
270 return author;
|
|
271 }
|
|
272
|
|
273 public String getMessage()
|
|
274 {
|
|
275 return message;
|
|
276 }
|
|
277
|
|
278 public String getUUID()
|
|
279 {
|
|
280 return uuid;
|
|
281 }
|
|
282 }
|
|
283 }
|