Mercurial > hg > Members > nobuyasu > jungle-network
comparison src/main/java/app/bbs/NetworkJungleBulletinBoard.java @ 146:29734d7d6521
Added ShowMessageWithTimeStampServlet
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 28 Jan 2014 08:47:45 +0900 |
parents | 7a7bac51a3cc |
children | 202ea963ec20 |
comparison
equal
deleted
inserted
replaced
145:0340a3321e6c | 146:29734d7d6521 |
---|---|
127 } | 127 } |
128 }; | 128 }; |
129 | 129 |
130 return new IterableConverter<String,Node>(chs,converter); | 130 return new IterableConverter<String,Node>(chs,converter); |
131 } | 131 } |
132 | |
133 public long getRenewTime(String _boardName) { | |
134 JungleTree tree = jungle.getTreeByName(_boardName); | |
135 ByteBuffer b = tree.getRootNode().getAttributes().get("renewtime"); | |
136 long timestamp; | |
137 if (b == null) { | |
138 timestamp = 0; | |
139 } else { | |
140 timestamp = b.getLong(0); | |
141 } | |
142 return timestamp; | |
143 } | |
144 | |
132 | 145 |
133 public void createBoards(final String _name,final String _author,final String _initMessage,final String _editKey) | 146 public void createBoards(final String _name,final String _author,final String _initMessage,final String _editKey) |
134 { | 147 { |
135 requestCounter.incrementAndGet(); | 148 requestCounter.incrementAndGet(); |
136 if(null == jungle.createNewTree(_name)){ | 149 if(null == jungle.createNewTree(_name)){ |
151 throw new IllegalStateException(); | 164 throw new IllegalStateException(); |
152 } | 165 } |
153 editor = either.b(); | 166 editor = either.b(); |
154 final long timestamp = new Date().getTime(); | 167 final long timestamp = new Date().getTime(); |
155 ByteBuffer tBuffer = ByteBuffer.allocate(16); | 168 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
156 either = editor.putAttribute(root.add(0),"timestamp", tBuffer.putLong(timestamp)); | 169 tBuffer.putLong(timestamp); |
157 if(either.isA()){ | 170 either = editor.putAttribute(root.add(0),"timestamp", tBuffer); |
158 throw new IllegalStateException(); | 171 if(either.isA()){ |
159 } | 172 throw new IllegalStateException(); |
160 editor = either.b(); | 173 } |
161 Either<Error,JungleTreeEditor> result = editor.success(); | 174 either = either.b().success(); |
162 if(result.isA()){ | 175 if(either.isA()){ |
163 throw new IllegalStateException(); | 176 throw new IllegalStateException(); |
164 } | 177 } |
178 | |
165 | 179 |
166 tree = jungle.getTreeByName(_name); | 180 tree = jungle.getTreeByName(_name); |
167 editor = tree.getTreeEditor(); | 181 editor = tree.getTreeEditor(); |
168 either = editor.addNewChildAt(root,0); | 182 either = editor.addNewChildAt(root,0); |
169 if(either.isA()){ | 183 if(either.isA()){ |
175 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | 189 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ |
176 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | 190 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); |
177 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_initMessage.getBytes())).b(); | 191 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_initMessage.getBytes())).b(); |
178 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | 192 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); |
179 ByteBuffer tBuffer2 = ByteBuffer.allocate(16); | 193 ByteBuffer tBuffer2 = ByteBuffer.allocate(16); |
180 _e = _e.getAttributes().put("timestamp",tBuffer2.putLong(timestamp)).b(); | 194 tBuffer2.putLong(timestamp); |
195 _e = _e.getAttributes().put("timestamp",tBuffer2).b(); | |
181 return DefaultEither.newB(_e); | 196 return DefaultEither.newB(_e); |
182 } | 197 } |
183 }; | 198 }; |
184 | 199 |
185 either = editor.edit(root.add(0),e); | 200 either = editor.edit(root.add(0),e); |
186 if(either.isA()){ | 201 if(either.isA()){ |
187 throw new IllegalStateException(); | 202 throw new IllegalStateException(); |
188 } | 203 } |
189 editor = either.b(); | 204 either.b().success(); |
190 editor.success(); | 205 |
206 editor = tree.getLocalTreeEditor(); | |
207 either = editor.putAttribute(root, "renewtime", tBuffer); | |
208 either.b().success(); | |
209 | |
191 } | 210 } |
192 | 211 |
193 public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey) | 212 public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey) |
194 { | 213 { |
195 requestCounter.incrementAndGet(); | 214 requestCounter.incrementAndGet(); |
197 if(tree == null){ | 216 if(tree == null){ |
198 throw new IllegalStateException(); | 217 throw new IllegalStateException(); |
199 } | 218 } |
200 | 219 |
201 Either<Error, JungleTreeEditor> either; | 220 Either<Error, JungleTreeEditor> either; |
221 final long timestamp = new Date().getTime(); | |
222 final ByteBuffer tBuffer = ByteBuffer.allocate(16); | |
223 tBuffer.putLong(timestamp); | |
202 do{ | 224 do{ |
203 Node node = tree.getRootNode(); | 225 Node node = tree.getRootNode(); |
204 int size = node.getChildren().size(); | 226 int size = node.getChildren().size(); |
205 DefaultNodePath path = new DefaultNodePath(); | 227 DefaultNodePath path = new DefaultNodePath(); |
206 | 228 |
208 either = editor.addNewChildAt(path,size); | 230 either = editor.addNewChildAt(path,size); |
209 if(either.isA()){ | 231 if(either.isA()){ |
210 throw new IllegalStateException(); | 232 throw new IllegalStateException(); |
211 } | 233 } |
212 editor = either.b(); | 234 editor = either.b(); |
213 final long timestamp = new Date().getTime(); | 235 |
214 NodeEditor e = new NodeEditor(){ | 236 NodeEditor e = new NodeEditor(){ |
215 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | 237 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ |
216 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | 238 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); |
217 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); | 239 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); |
218 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | 240 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); |
219 ByteBuffer tBuffer = ByteBuffer.allocate(16); | 241 _e = _e.getAttributes().put("timestamp",tBuffer).b(); |
220 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b(); | |
221 return DefaultEither.newB(_e); | 242 return DefaultEither.newB(_e); |
222 } | 243 } |
223 }; | 244 }; |
224 path = path.add(size); | 245 path = path.add(size); |
225 either = editor.edit(path,e); | 246 either = editor.edit(path,e); |
226 if(either.isA()){ | 247 if(either.isA()){ |
227 throw new IllegalStateException(); | 248 throw new IllegalStateException(); |
228 } | 249 } |
229 editor = either.b(); | 250 editor = either.b(); |
230 either = editor.success(); | 251 either = editor.success(); |
231 | |
232 }while(either.isA()); | 252 }while(either.isA()); |
253 | |
254 /* | |
255 * Record latest renew time | |
256 */ | |
257 DefaultNodePath root = new DefaultNodePath(); | |
258 do { | |
259 JungleTreeEditor editor = tree.getLocalTreeEditor(); | |
260 either = editor.putAttribute(root, "renewtime", tBuffer); | |
261 either = either.b().success(); | |
262 }while(either.isA()); | |
263 | |
264 | |
233 } | 265 } |
234 | 266 |
235 public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey) | 267 public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey) |
236 { | 268 { |
237 requestCounter.incrementAndGet(); | 269 requestCounter.incrementAndGet(); |
238 final long timestamp = new Date().getTime(); | 270 final long timestamp = new Date().getTime(); |
239 for(;;) { | 271 final ByteBuffer tBuffer = ByteBuffer.allocate(16); |
272 tBuffer.putLong(timestamp); | |
273 JungleTree tree = jungle.getTreeByName(_board); | |
274 Either<Error, JungleTreeEditor> either = null; | |
275 do { | |
240 DefaultNodePath path = new DefaultNodePath(); | 276 DefaultNodePath path = new DefaultNodePath(); |
241 path = path.add(Integer.parseInt(_uuid)); | 277 path = path.add(Integer.parseInt(_uuid)); |
242 | 278 |
243 JungleTree tree = jungle.getTreeByName(_board); | |
244 JungleTreeEditor editor = tree.getTreeEditor(); | 279 JungleTreeEditor editor = tree.getTreeEditor(); |
245 NodeEditor e = new NodeEditor(){ | 280 NodeEditor e = new NodeEditor(){ |
246 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | 281 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ |
247 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | 282 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); |
248 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); | 283 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); |
249 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | 284 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); |
250 ByteBuffer tBuffer = ByteBuffer.allocate(16); | 285 _e = _e.getAttributes().put("timestamp",tBuffer).b(); |
251 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b(); | |
252 return DefaultEither.newB(_e); | 286 return DefaultEither.newB(_e); |
253 } | 287 } |
254 }; | 288 }; |
255 | 289 either = editor.edit(path,e); |
256 Either<Error, JungleTreeEditor> either = editor.edit(path,e); | |
257 if(either.isA()){ | 290 if(either.isA()){ |
258 throw new IllegalStateException(); | 291 throw new IllegalStateException(); |
259 } | 292 } |
260 editor = either.b(); | 293 editor = either.b(); |
261 either = editor.success(); | 294 either = editor.success(); |
262 if(!either.isA()) { | 295 }while(either.isA()); |
263 return; | 296 /* |
264 } | 297 * Record latest renew time |
265 } | 298 */ |
299 DefaultNodePath root = new DefaultNodePath(); | |
300 do { | |
301 JungleTreeEditor editor = tree.getLocalTreeEditor(); | |
302 either = editor.putAttribute(root, "renewtime", tBuffer); | |
303 either = either.b().success(); | |
304 }while(either.isA()); | |
305 | |
266 | 306 |
267 } | 307 } |
268 | 308 |
269 public Iterable<BoardMessage> getMessages(String _boardName) | 309 public Iterable<BoardMessage> getMessages(String _boardName) |
270 { | 310 { |
287 | 327 |
288 public int getRequestNum() { | 328 public int getRequestNum() { |
289 return requestCounter.get(); | 329 return requestCounter.get(); |
290 } | 330 } |
291 | 331 |
332 | |
292 private static class BoardMessageImpl implements BoardMessage | 333 private static class BoardMessageImpl implements BoardMessage |
293 { | 334 { |
294 private final String author; | 335 private final String author; |
295 private final String message; | 336 private final String message; |
296 private final String uuid; | 337 private final String uuid; |