112
|
1 package 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();
|
|
103 }
|
|
104
|
|
105 public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey)
|
|
106 {
|
|
107 JungleTree tree = jungle.getTreeByName(_board);
|
|
108 if(tree == null){
|
|
109 throw new IllegalStateException();
|
|
110 }
|
|
111
|
46
|
112 Either<Error, JungleTreeEditor> either;
|
38
|
113 do{
|
|
114 Node node = tree.getRootNode();
|
|
115 int size = node.getChildren().size();
|
|
116 DefaultNodePath path = new DefaultNodePath();
|
|
117
|
65
|
118 JungleTreeEditor editor = tree.getTreeEditor();
|
46
|
119 either = editor.addNewChildAt(path,size);
|
38
|
120 if(either.isA()){
|
|
121 throw new IllegalStateException();
|
|
122 }
|
|
123 editor = either.b();
|
56
|
124 final long timestamp = new Date().getTime();
|
38
|
125 NodeEditor e = new NodeEditor(){
|
|
126 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
127 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
128 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b();
|
|
129 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
56
|
130 ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
131 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
|
38
|
132 return DefaultEither.newB(_e);
|
|
133 }
|
|
134 };
|
|
135 path = path.add(size);
|
|
136 either = editor.edit(path,e);
|
|
137 if(either.isA()){
|
|
138 throw new IllegalStateException();
|
|
139 }
|
|
140 editor = either.b();
|
46
|
141 either = editor.success();
|
93
|
142
|
46
|
143 }while(either.isA());
|
38
|
144 }
|
|
145
|
|
146 public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey)
|
|
147 {
|
65
|
148 for(;;) {
|
38
|
149 DefaultNodePath path = new DefaultNodePath();
|
|
150 path = path.add(Integer.parseInt(_uuid));
|
|
151
|
|
152 JungleTree tree = jungle.getTreeByName(_board);
|
65
|
153 JungleTreeEditor editor = tree.getTreeEditor();
|
56
|
154 final long timestamp = new Date().getTime();
|
38
|
155 NodeEditor e = new NodeEditor(){
|
|
156 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
157 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
158 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b();
|
|
159 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
56
|
160 ByteBuffer tBuffer = ByteBuffer.allocate(16);
|
|
161 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
|
38
|
162 return DefaultEither.newB(_e);
|
|
163 }
|
|
164 };
|
|
165
|
65
|
166 Either<Error, JungleTreeEditor> either = editor.edit(path,e);
|
38
|
167 if(either.isA()){
|
|
168 throw new IllegalStateException();
|
|
169 }
|
|
170 editor = either.b();
|
46
|
171 either = editor.success();
|
65
|
172 if(!either.isA()) {
|
|
173 return;
|
|
174 }
|
|
175 }
|
|
176
|
38
|
177 }
|
|
178
|
|
179 public Iterable<BoardMessage> getMessages(String _boardName)
|
|
180 {
|
|
181 JungleTree tree = jungle.getTreeByName(_boardName);
|
|
182 Node node = tree.getRootNode();
|
|
183 Children<Node> chs = node.getChildren();
|
|
184
|
|
185 final AtomicInteger counter = new AtomicInteger(0);
|
|
186 IterableConverter.Converter<BoardMessage,Node> converter = new IterableConverter.Converter<BoardMessage,Node>(){
|
|
187 public BoardMessage conv(Node _b) {
|
|
188 String uuid = Integer.toString(counter.get());
|
|
189 String author = new String(_b.getAttributes().get("author").array());
|
|
190 String message = new String(_b.getAttributes().get("mes").array());
|
|
191 counter.incrementAndGet();
|
|
192 return new BoardMessageImpl(author,message,uuid);
|
|
193 }
|
|
194 };
|
|
195 return new IterableConverter<BoardMessage,Node>(chs,converter);
|
|
196 }
|
|
197
|
46
|
198
|
|
199
|
38
|
200 private static class BoardMessageImpl implements BoardMessage
|
|
201 {
|
|
202 private final String author;
|
|
203 private final String message;
|
|
204 private final String uuid;
|
|
205
|
|
206 public BoardMessageImpl(String _author,String _message,String _uuid)
|
|
207 {
|
|
208 author = _author;
|
|
209 message = _message;
|
|
210 uuid = _uuid;
|
|
211 }
|
|
212
|
|
213 public String getAuthor()
|
|
214 {
|
|
215 return author;
|
|
216 }
|
|
217
|
|
218 public String getMessage()
|
|
219 {
|
|
220 return message;
|
|
221 }
|
|
222
|
|
223 public String getUUID()
|
|
224 {
|
|
225 return uuid;
|
|
226 }
|
|
227 }
|
|
228 }
|