Mercurial > hg > Members > nobuyasu > jungle-network
annotate src/jungle/app/bbs/NetworkJungleBulletinBoard.java @ 91:2c7b3f2b2ee1
Removed serverName field
author | one |
---|---|
date | Mon, 11 Nov 2013 19:22:47 +0900 |
parents | eef737ab3b2c |
children | e95faa202b4f |
rev | line source |
---|---|
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; | |
75
87ec5dd0dc27
Rename from alice.jungle.datasegment.store.operation to alice.jungle.datasegment.store.container
one
parents:
66
diff
changeset
|
9 import alice.jungle.datasegment.store.container.DefaultTreeOperationLogContainer; |
83 | 10 import alice.jungle.persistence.AliceJournal; |
46 | 11 import alice.jungle.transaction.NetworkDefaultJungleTreeEditor; |
38 | 12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; |
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; | |
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; | |
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children; | |
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; | |
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; | |
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; | |
46 | 19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation; |
38 | 20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; |
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; | |
65 | 22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultTreeNode; |
38 | 23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; |
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; | |
25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; | |
26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
27 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; | |
66 | 28 import jungle.app.bbs.codesegment.HashLogUpdateCodeSegment; |
38 | 29 |
39 | 30 public class NetworkJungleBulletinBoard implements BulletinBoard |
38 | 31 { |
32 private final Jungle jungle; | |
33 | |
91 | 34 public NetworkJungleBulletinBoard(String _uuid) |
38 | 35 { |
91 | 36 jungle = new NetworkDefaultJungle(new AliceJournal(), _uuid,new DefaultTreeEditor(new DefaultTraverser())); |
38 | 37 jungle.createNewTree("boards"); |
51 | 38 JungleManager.setJungle(jungle); |
38 | 39 } |
40 | |
41 public Iterable<String> getBoards() | |
42 { | |
43 JungleTree tree = jungle.getTreeByName("boards"); | |
44 Node node = tree.getRootNode(); | |
45 Children<Node> chs = node.getChildren(); | |
46 | |
47 IterableConverter.Converter<String,Node> converter = new IterableConverter.Converter<String,Node>(){ | |
48 public String conv(Node _b) { | |
49 ByteBuffer e = _b.getAttributes().get("name"); | |
50 return new String(e.array()); | |
51 } | |
52 }; | |
53 | |
54 return new IterableConverter<String,Node>(chs,converter); | |
55 } | |
56 | |
57 public void createBoards(final String _name,final String _author,final String _initMessage,final String _editKey) | |
58 { | |
59 if(null == jungle.createNewTree(_name)){ | |
60 throw new IllegalStateException(); | |
61 } | |
62 | |
63 JungleTree tree = jungle.getTreeByName("boards"); | |
64 JungleTreeEditor editor = tree.getTreeEditor(); | |
65 DefaultNodePath root = new DefaultNodePath(); | |
66 Either<Error,JungleTreeEditor> either = editor.addNewChildAt(root,0); | |
67 if(either.isA()){ | |
68 throw new IllegalStateException(); | |
69 } | |
70 editor = either.b(); | |
71 | |
72 either = editor.putAttribute(root.add(0),"name",ByteBuffer.wrap(_name.getBytes())); | |
73 if(either.isA()){ | |
74 throw new IllegalStateException(); | |
75 } | |
76 editor = either.b(); | |
77 Either<Error,JungleTreeEditor> result = editor.success(); | |
78 if(result.isA()){ | |
79 throw new IllegalStateException(); | |
80 } | |
56 | 81 final long timestamp = new Date().getTime(); |
46 | 82 /* Put DataSegment */ |
83 try { | |
90 | 84 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor,timestamp); |
46 | 85 } catch (IOException e1) { |
86 e1.printStackTrace(); | |
87 } | |
38 | 88 |
89 tree = jungle.getTreeByName(_name); | |
90 editor = tree.getTreeEditor(); | |
91 either = editor.addNewChildAt(root,0); | |
92 if(either.isA()){ | |
93 throw new IllegalStateException(); | |
94 } | |
95 editor = either.b(); | |
96 | |
97 NodeEditor e = new NodeEditor(){ | |
98 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | |
99 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | |
100 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_initMessage.getBytes())).b(); | |
101 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | |
56 | 102 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
103 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b(); | |
38 | 104 return DefaultEither.newB(_e); |
105 } | |
106 }; | |
107 | |
108 either = editor.edit(root.add(0),e); | |
109 if(either.isA()){ | |
110 throw new IllegalStateException(); | |
111 } | |
112 editor = either.b(); | |
113 editor.success(); | |
46 | 114 /* Put DataSegment */ |
115 try { | |
90 | 116 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor, timestamp); |
46 | 117 } catch (IOException e1) { |
118 e1.printStackTrace(); | |
119 } | |
120 | |
38 | 121 } |
122 | |
123 public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey) | |
124 { | |
125 JungleTree tree = jungle.getTreeByName(_board); | |
126 if(tree == null){ | |
127 throw new IllegalStateException(); | |
128 } | |
129 | |
46 | 130 Either<Error, JungleTreeEditor> either; |
38 | 131 do{ |
132 Node node = tree.getRootNode(); | |
133 int size = node.getChildren().size(); | |
134 DefaultNodePath path = new DefaultNodePath(); | |
135 | |
65 | 136 JungleTreeEditor editor = tree.getTreeEditor(); |
46 | 137 either = editor.addNewChildAt(path,size); |
38 | 138 if(either.isA()){ |
139 throw new IllegalStateException(); | |
140 } | |
141 editor = either.b(); | |
56 | 142 final long timestamp = new Date().getTime(); |
38 | 143 NodeEditor e = new NodeEditor(){ |
144 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | |
145 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | |
146 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); | |
147 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | |
56 | 148 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
149 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b(); | |
38 | 150 return DefaultEither.newB(_e); |
151 } | |
152 }; | |
153 path = path.add(size); | |
154 either = editor.edit(path,e); | |
155 if(either.isA()){ | |
156 throw new IllegalStateException(); | |
157 } | |
158 editor = either.b(); | |
46 | 159 either = editor.success(); |
160 try { | |
65 | 161 NetworkDefaultJungleTreeEditor<DefaultTreeNode> netEditor = (NetworkDefaultJungleTreeEditor<DefaultTreeNode>) editor; |
90 | 162 putTreeOperationLog(netEditor, timestamp); |
46 | 163 } catch (IOException e1) { |
164 e1.printStackTrace(); | |
165 } | |
166 }while(either.isA()); | |
51 | 167 /* Put DataSegment */ |
38 | 168 } |
169 | |
170 public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey) | |
171 { | |
65 | 172 for(;;) { |
38 | 173 DefaultNodePath path = new DefaultNodePath(); |
174 path = path.add(Integer.parseInt(_uuid)); | |
175 | |
176 JungleTree tree = jungle.getTreeByName(_board); | |
65 | 177 JungleTreeEditor editor = tree.getTreeEditor(); |
56 | 178 final long timestamp = new Date().getTime(); |
38 | 179 NodeEditor e = new NodeEditor(){ |
180 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | |
181 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | |
182 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); | |
183 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | |
56 | 184 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
185 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b(); | |
38 | 186 return DefaultEither.newB(_e); |
187 } | |
188 }; | |
189 | |
65 | 190 Either<Error, JungleTreeEditor> either = editor.edit(path,e); |
38 | 191 if(either.isA()){ |
192 throw new IllegalStateException(); | |
193 } | |
194 editor = either.b(); | |
46 | 195 either = editor.success(); |
196 try { | |
90 | 197 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor, timestamp); |
46 | 198 } catch (IOException e1) { |
199 e1.printStackTrace(); | |
200 } | |
65 | 201 if(!either.isA()) { |
202 return; | |
203 } | |
204 } | |
205 | |
38 | 206 } |
207 | |
208 public Iterable<BoardMessage> getMessages(String _boardName) | |
209 { | |
210 JungleTree tree = jungle.getTreeByName(_boardName); | |
211 Node node = tree.getRootNode(); | |
212 Children<Node> chs = node.getChildren(); | |
213 | |
214 final AtomicInteger counter = new AtomicInteger(0); | |
215 IterableConverter.Converter<BoardMessage,Node> converter = new IterableConverter.Converter<BoardMessage,Node>(){ | |
216 public BoardMessage conv(Node _b) { | |
217 String uuid = Integer.toString(counter.get()); | |
218 String author = new String(_b.getAttributes().get("author").array()); | |
219 String message = new String(_b.getAttributes().get("mes").array()); | |
220 counter.incrementAndGet(); | |
221 return new BoardMessageImpl(author,message,uuid); | |
222 } | |
223 }; | |
224 | |
225 return new IterableConverter<BoardMessage,Node>(chs,converter); | |
226 } | |
227 | |
90 | 228 private void putTreeOperationLog(NetworkDefaultJungleTreeEditor<DefaultTreeNode> editor, long timestamp) throws IOException { |
46 | 229 String uuid = editor.getID(); |
230 String treeName = editor.getTreeName(); | |
49 | 231 String updaterName = editor.getUpdaterName(); |
52 | 232 String revision = editor.getRevision(); |
46 | 233 Iterable<TreeOperation> log = editor.getTreeOperationLog(); |
90 | 234 putDataSegment(uuid, treeName, updaterName, log, revision, timestamp); |
46 | 235 } |
236 | |
90 | 237 private void putDataSegment(String _uuid, String _treeName, String _updaterName, Iterable<TreeOperation> _log, String nextRevision,long timestamp) throws IOException { |
46 | 238 DefaultTreeOperationLogContainer container = new DefaultTreeOperationLogContainer(); |
239 container.setTreeName(_treeName); | |
240 container.setUUID(_uuid); | |
49 | 241 container.setUpdaterName(_updaterName); |
46 | 242 container.setRevision(nextRevision); |
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 } |