Mercurial > hg > Members > nobuyasu > jungle-network
annotate src/main/java/app/bbs/NetworkJungleBulletinBoard.java @ 130:652a6dff6288
Fixed
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 11 Jan 2014 09:10:02 +0900 |
parents | 1a3edba05f50 |
children | aa98fc3ec85f |
rev | line source |
---|---|
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; | |
124 | 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; | |
125 | 12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs.BoardMessage; |
38 | 13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children; |
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; | |
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; | |
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; | |
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; | |
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; | |
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; | |
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; | |
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; | |
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; | |
24 | |
124 | 25 public class NetworkJungleBulletinBoard implements NetworkBulletinBoard |
38 | 26 { |
27 private final Jungle jungle; | |
124 | 28 |
29 public NetworkJungleBulletinBoard(String _uuid) | |
38 | 30 { |
124 | 31 jungle = new NetworkDefaultJungle(new AliceJournal(), _uuid,new DefaultTreeEditor(new DefaultTraverser())); |
32 jungle.createNewTree("boards"); | |
123 | 33 BullentInBoardJungleManager.setJungle(jungle); |
38 | 34 } |
118
f64ff5bd66f5
Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents:
112
diff
changeset
|
35 |
124 | 36 public void init() { |
37 | |
118
f64ff5bd66f5
Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents:
112
diff
changeset
|
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(); | |
129
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
76 final long timestamp = new Date().getTime(); |
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
77 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
78 either = editor.putAttribute(root.add(0),"timestamp", tBuffer.putLong(timestamp)); |
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
79 if(either.isA()){ |
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
80 throw new IllegalStateException(); |
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
81 } |
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
82 editor = either.b(); |
38 | 83 Either<Error,JungleTreeEditor> result = editor.success(); |
84 if(result.isA()){ | |
85 throw new IllegalStateException(); | |
86 } | |
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(); | |
130 | 101 ByteBuffer tBuffer2 = ByteBuffer.allocate(16); |
102 _e = _e.getAttributes().put("timestamp",tBuffer2.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(); | |
113 } | |
114 | |
115 public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey) | |
116 { | |
117 JungleTree tree = jungle.getTreeByName(_board); | |
118 if(tree == null){ | |
119 throw new IllegalStateException(); | |
120 } | |
121 | |
46 | 122 Either<Error, JungleTreeEditor> either; |
38 | 123 do{ |
124 Node node = tree.getRootNode(); | |
125 int size = node.getChildren().size(); | |
126 DefaultNodePath path = new DefaultNodePath(); | |
127 | |
65 | 128 JungleTreeEditor editor = tree.getTreeEditor(); |
46 | 129 either = editor.addNewChildAt(path,size); |
38 | 130 if(either.isA()){ |
131 throw new IllegalStateException(); | |
132 } | |
133 editor = either.b(); | |
56 | 134 final long timestamp = new Date().getTime(); |
38 | 135 NodeEditor e = new NodeEditor(){ |
136 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | |
137 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | |
138 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); | |
139 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | |
56 | 140 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
141 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b(); | |
38 | 142 return DefaultEither.newB(_e); |
143 } | |
144 }; | |
145 path = path.add(size); | |
146 either = editor.edit(path,e); | |
147 if(either.isA()){ | |
148 throw new IllegalStateException(); | |
149 } | |
150 editor = either.b(); | |
46 | 151 either = editor.success(); |
93 | 152 |
46 | 153 }while(either.isA()); |
38 | 154 } |
155 | |
156 public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey) | |
157 { | |
65 | 158 for(;;) { |
38 | 159 DefaultNodePath path = new DefaultNodePath(); |
160 path = path.add(Integer.parseInt(_uuid)); | |
161 | |
162 JungleTree tree = jungle.getTreeByName(_board); | |
65 | 163 JungleTreeEditor editor = tree.getTreeEditor(); |
56 | 164 final long timestamp = new Date().getTime(); |
38 | 165 NodeEditor e = new NodeEditor(){ |
166 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | |
167 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | |
168 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); | |
169 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | |
56 | 170 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
171 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b(); | |
38 | 172 return DefaultEither.newB(_e); |
173 } | |
174 }; | |
175 | |
65 | 176 Either<Error, JungleTreeEditor> either = editor.edit(path,e); |
38 | 177 if(either.isA()){ |
178 throw new IllegalStateException(); | |
179 } | |
180 editor = either.b(); | |
46 | 181 either = editor.success(); |
65 | 182 if(!either.isA()) { |
183 return; | |
184 } | |
185 } | |
186 | |
38 | 187 } |
188 | |
189 public Iterable<BoardMessage> getMessages(String _boardName) | |
190 { | |
191 JungleTree tree = jungle.getTreeByName(_boardName); | |
192 Node node = tree.getRootNode(); | |
193 Children<Node> chs = node.getChildren(); | |
194 final AtomicInteger counter = new AtomicInteger(0); | |
195 IterableConverter.Converter<BoardMessage,Node> converter = new IterableConverter.Converter<BoardMessage,Node>(){ | |
196 public BoardMessage conv(Node _b) { | |
197 String uuid = Integer.toString(counter.get()); | |
198 String author = new String(_b.getAttributes().get("author").array()); | |
199 String message = new String(_b.getAttributes().get("mes").array()); | |
200 counter.incrementAndGet(); | |
201 return new BoardMessageImpl(author,message,uuid); | |
202 } | |
203 }; | |
204 return new IterableConverter<BoardMessage,Node>(chs,converter); | |
205 } | |
206 | |
46 | 207 |
208 | |
38 | 209 private static class BoardMessageImpl implements BoardMessage |
210 { | |
211 private final String author; | |
212 private final String message; | |
213 private final String uuid; | |
214 | |
215 public BoardMessageImpl(String _author,String _message,String _uuid) | |
216 { | |
217 author = _author; | |
218 message = _message; | |
219 uuid = _uuid; | |
220 } | |
221 | |
222 public String getAuthor() | |
223 { | |
224 return author; | |
225 } | |
226 | |
227 public String getMessage() | |
228 { | |
229 return message; | |
230 } | |
231 | |
232 public String getUUID() | |
233 { | |
234 return uuid; | |
235 } | |
236 } | |
124 | 237 |
38 | 238 } |