66
|
1 package jungle.app.bbs;
|
38
|
2
|
|
3 import java.nio.ByteBuffer;
|
56
|
4 import java.util.Date;
|
38
|
5 import java.util.concurrent.atomic.AtomicInteger;
|
39
|
6
|
|
7 import alice.jungle.core.NetworkDefaultJungle;
|
105
|
8 import alice.jungle.persistent.AliceJournal;
|
38
|
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
|
|
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
|
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children;
|
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node;
|
|
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
|
|
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
|
|
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode;
|
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor;
|
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
|
|
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
|
|
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
|
|
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
|
|
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter;
|
|
23
|
39
|
24 public class NetworkJungleBulletinBoard implements BulletinBoard
|
38
|
25 {
|
|
26 private final Jungle jungle;
|
|
27
|
91
|
28 public NetworkJungleBulletinBoard(String _uuid)
|
38
|
29 {
|
91
|
30 jungle = new NetworkDefaultJungle(new AliceJournal(), _uuid,new DefaultTreeEditor(new DefaultTraverser()));
|
38
|
31 jungle.createNewTree("boards");
|
51
|
32 JungleManager.setJungle(jungle);
|
38
|
33 }
|
|
34
|
|
35 public Iterable<String> getBoards()
|
|
36 {
|
|
37 JungleTree tree = jungle.getTreeByName("boards");
|
|
38 Node node = tree.getRootNode();
|
|
39 Children<Node> chs = node.getChildren();
|
|
40
|
|
41 IterableConverter.Converter<String,Node> converter = new IterableConverter.Converter<String,Node>(){
|
|
42 public String conv(Node _b) {
|
|
43 ByteBuffer e = _b.getAttributes().get("name");
|
|
44 return new String(e.array());
|
|
45 }
|
|
46 };
|
|
47
|
|
48 return new IterableConverter<String,Node>(chs,converter);
|
|
49 }
|
|
50
|
|
51 public void createBoards(final String _name,final String _author,final String _initMessage,final String _editKey)
|
|
52 {
|
|
53 if(null == jungle.createNewTree(_name)){
|
|
54 throw new IllegalStateException();
|
|
55 }
|
|
56
|
|
57 JungleTree tree = jungle.getTreeByName("boards");
|
|
58 JungleTreeEditor editor = tree.getTreeEditor();
|
|
59 DefaultNodePath root = new DefaultNodePath();
|
|
60 Either<Error,JungleTreeEditor> either = editor.addNewChildAt(root,0);
|
|
61 if(either.isA()){
|
|
62 throw new IllegalStateException();
|
|
63 }
|
|
64 editor = either.b();
|
|
65
|
|
66 either = editor.putAttribute(root.add(0),"name",ByteBuffer.wrap(_name.getBytes()));
|
|
67 if(either.isA()){
|
|
68 throw new IllegalStateException();
|
|
69 }
|
|
70 editor = either.b();
|
|
71 Either<Error,JungleTreeEditor> result = editor.success();
|
|
72 if(result.isA()){
|
|
73 throw new IllegalStateException();
|
|
74 }
|
56
|
75 final long timestamp = new Date().getTime();
|
93
|
76
|
38
|
77
|
|
78 tree = jungle.getTreeByName(_name);
|
|
79 editor = tree.getTreeEditor();
|
|
80 either = editor.addNewChildAt(root,0);
|
|
81 if(either.isA()){
|
|
82 throw new IllegalStateException();
|
|
83 }
|
|
84 editor = either.b();
|
|
85
|
|
86 NodeEditor e = new NodeEditor(){
|
|
87 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
88 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
89 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_initMessage.getBytes())).b();
|
|
90 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
56
|
91 ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
92 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
|
38
|
93 return DefaultEither.newB(_e);
|
|
94 }
|
|
95 };
|
|
96
|
|
97 either = editor.edit(root.add(0),e);
|
|
98 if(either.isA()){
|
|
99 throw new IllegalStateException();
|
|
100 }
|
|
101 editor = either.b();
|
|
102 editor.success();
|
46
|
103
|
38
|
104 }
|
|
105
|
|
106 public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey)
|
|
107 {
|
|
108 JungleTree tree = jungle.getTreeByName(_board);
|
|
109 if(tree == null){
|
|
110 throw new IllegalStateException();
|
|
111 }
|
|
112
|
46
|
113 Either<Error, JungleTreeEditor> either;
|
38
|
114 do{
|
|
115 Node node = tree.getRootNode();
|
|
116 int size = node.getChildren().size();
|
|
117 DefaultNodePath path = new DefaultNodePath();
|
|
118
|
65
|
119 JungleTreeEditor editor = tree.getTreeEditor();
|
46
|
120 either = editor.addNewChildAt(path,size);
|
38
|
121 if(either.isA()){
|
|
122 throw new IllegalStateException();
|
|
123 }
|
|
124 editor = either.b();
|
56
|
125 final long timestamp = new Date().getTime();
|
38
|
126 NodeEditor e = new NodeEditor(){
|
|
127 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
128 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
129 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b();
|
|
130 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
56
|
131 ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
132 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
|
38
|
133 return DefaultEither.newB(_e);
|
|
134 }
|
|
135 };
|
|
136 path = path.add(size);
|
|
137 either = editor.edit(path,e);
|
|
138 if(either.isA()){
|
|
139 throw new IllegalStateException();
|
|
140 }
|
|
141 editor = either.b();
|
46
|
142 either = editor.success();
|
93
|
143
|
46
|
144 }while(either.isA());
|
51
|
145 /* Put DataSegment */
|
38
|
146 }
|
|
147
|
|
148 public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey)
|
|
149 {
|
65
|
150 for(;;) {
|
38
|
151 DefaultNodePath path = new DefaultNodePath();
|
|
152 path = path.add(Integer.parseInt(_uuid));
|
|
153
|
|
154 JungleTree tree = jungle.getTreeByName(_board);
|
65
|
155 JungleTreeEditor editor = tree.getTreeEditor();
|
56
|
156 final long timestamp = new Date().getTime();
|
38
|
157 NodeEditor e = new NodeEditor(){
|
|
158 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
159 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
160 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b();
|
|
161 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
56
|
162 ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
163 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
|
38
|
164 return DefaultEither.newB(_e);
|
|
165 }
|
|
166 };
|
|
167
|
65
|
168 Either<Error, JungleTreeEditor> either = editor.edit(path,e);
|
38
|
169 if(either.isA()){
|
|
170 throw new IllegalStateException();
|
|
171 }
|
|
172 editor = either.b();
|
46
|
173 either = editor.success();
|
65
|
174 if(!either.isA()) {
|
|
175 return;
|
|
176 }
|
|
177 }
|
|
178
|
38
|
179 }
|
|
180
|
|
181 public Iterable<BoardMessage> getMessages(String _boardName)
|
|
182 {
|
|
183 JungleTree tree = jungle.getTreeByName(_boardName);
|
|
184 Node node = tree.getRootNode();
|
|
185 Children<Node> chs = node.getChildren();
|
|
186
|
|
187 final AtomicInteger counter = new AtomicInteger(0);
|
|
188 IterableConverter.Converter<BoardMessage,Node> converter = new IterableConverter.Converter<BoardMessage,Node>(){
|
|
189 public BoardMessage conv(Node _b) {
|
|
190 String uuid = Integer.toString(counter.get());
|
|
191 String author = new String(_b.getAttributes().get("author").array());
|
|
192 String message = new String(_b.getAttributes().get("mes").array());
|
|
193 counter.incrementAndGet();
|
|
194 return new BoardMessageImpl(author,message,uuid);
|
|
195 }
|
|
196 };
|
|
197
|
|
198 return new IterableConverter<BoardMessage,Node>(chs,converter);
|
|
199 }
|
|
200
|
46
|
201
|
|
202
|
38
|
203 private static class BoardMessageImpl implements BoardMessage
|
|
204 {
|
|
205 private final String author;
|
|
206 private final String message;
|
|
207 private final String uuid;
|
|
208
|
|
209 public BoardMessageImpl(String _author,String _message,String _uuid)
|
|
210 {
|
|
211 author = _author;
|
|
212 message = _message;
|
|
213 uuid = _uuid;
|
|
214 }
|
|
215
|
|
216 public String getAuthor()
|
|
217 {
|
|
218 return author;
|
|
219 }
|
|
220
|
|
221 public String getMessage()
|
|
222 {
|
|
223 return message;
|
|
224 }
|
|
225
|
|
226 public String getUUID()
|
|
227 {
|
|
228 return uuid;
|
|
229 }
|
|
230 }
|
|
231 }
|