Mercurial > hg > Members > nobuyasu > jungle-network
annotate src/main/java/app/bbs/NetworkJungleBulletinBoard.java @ 126:31be4d597859
Removed unnecessary files
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 11 Jan 2014 08:10:41 +0900 |
parents | 6104702a1880 |
children | 1a3edba05f50 |
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(); | |
76 Either<Error,JungleTreeEditor> result = editor.success(); | |
77 if(result.isA()){ | |
78 throw new IllegalStateException(); | |
79 } | |
56 | 80 final long timestamp = new Date().getTime(); |
93 | 81 |
38 | 82 |
83 tree = jungle.getTreeByName(_name); | |
84 editor = tree.getTreeEditor(); | |
85 either = editor.addNewChildAt(root,0); | |
86 if(either.isA()){ | |
87 throw new IllegalStateException(); | |
88 } | |
89 editor = either.b(); | |
90 | |
91 NodeEditor e = new NodeEditor(){ | |
92 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | |
93 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | |
94 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_initMessage.getBytes())).b(); | |
95 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | |
56 | 96 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
97 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b(); | |
38 | 98 return DefaultEither.newB(_e); |
99 } | |
100 }; | |
101 | |
102 either = editor.edit(root.add(0),e); | |
103 if(either.isA()){ | |
104 throw new IllegalStateException(); | |
105 } | |
106 editor = either.b(); | |
107 editor.success(); | |
108 } | |
109 | |
110 public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey) | |
111 { | |
112 JungleTree tree = jungle.getTreeByName(_board); | |
113 if(tree == null){ | |
114 throw new IllegalStateException(); | |
115 } | |
116 | |
46 | 117 Either<Error, JungleTreeEditor> either; |
38 | 118 do{ |
119 Node node = tree.getRootNode(); | |
120 int size = node.getChildren().size(); | |
121 DefaultNodePath path = new DefaultNodePath(); | |
122 | |
65 | 123 JungleTreeEditor editor = tree.getTreeEditor(); |
46 | 124 either = editor.addNewChildAt(path,size); |
38 | 125 if(either.isA()){ |
126 throw new IllegalStateException(); | |
127 } | |
128 editor = either.b(); | |
56 | 129 final long timestamp = new Date().getTime(); |
38 | 130 NodeEditor e = new NodeEditor(){ |
131 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | |
132 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | |
133 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); | |
134 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | |
56 | 135 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
136 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b(); | |
38 | 137 return DefaultEither.newB(_e); |
138 } | |
139 }; | |
140 path = path.add(size); | |
141 either = editor.edit(path,e); | |
142 if(either.isA()){ | |
143 throw new IllegalStateException(); | |
144 } | |
145 editor = either.b(); | |
46 | 146 either = editor.success(); |
93 | 147 |
46 | 148 }while(either.isA()); |
38 | 149 } |
150 | |
151 public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey) | |
152 { | |
65 | 153 for(;;) { |
38 | 154 DefaultNodePath path = new DefaultNodePath(); |
155 path = path.add(Integer.parseInt(_uuid)); | |
156 | |
157 JungleTree tree = jungle.getTreeByName(_board); | |
65 | 158 JungleTreeEditor editor = tree.getTreeEditor(); |
56 | 159 final long timestamp = new Date().getTime(); |
38 | 160 NodeEditor e = new NodeEditor(){ |
161 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | |
162 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | |
163 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); | |
164 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | |
56 | 165 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
166 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b(); | |
38 | 167 return DefaultEither.newB(_e); |
168 } | |
169 }; | |
170 | |
65 | 171 Either<Error, JungleTreeEditor> either = editor.edit(path,e); |
38 | 172 if(either.isA()){ |
173 throw new IllegalStateException(); | |
174 } | |
175 editor = either.b(); | |
46 | 176 either = editor.success(); |
65 | 177 if(!either.isA()) { |
178 return; | |
179 } | |
180 } | |
181 | |
38 | 182 } |
183 | |
184 public Iterable<BoardMessage> getMessages(String _boardName) | |
185 { | |
186 JungleTree tree = jungle.getTreeByName(_boardName); | |
187 Node node = tree.getRootNode(); | |
188 Children<Node> chs = node.getChildren(); | |
189 final AtomicInteger counter = new AtomicInteger(0); | |
190 IterableConverter.Converter<BoardMessage,Node> converter = new IterableConverter.Converter<BoardMessage,Node>(){ | |
191 public BoardMessage conv(Node _b) { | |
192 String uuid = Integer.toString(counter.get()); | |
193 String author = new String(_b.getAttributes().get("author").array()); | |
194 String message = new String(_b.getAttributes().get("mes").array()); | |
195 counter.incrementAndGet(); | |
196 return new BoardMessageImpl(author,message,uuid); | |
197 } | |
198 }; | |
199 return new IterableConverter<BoardMessage,Node>(chs,converter); | |
200 } | |
201 | |
46 | 202 |
203 | |
38 | 204 private static class BoardMessageImpl implements BoardMessage |
205 { | |
206 private final String author; | |
207 private final String message; | |
208 private final String uuid; | |
209 | |
210 public BoardMessageImpl(String _author,String _message,String _uuid) | |
211 { | |
212 author = _author; | |
213 message = _message; | |
214 uuid = _uuid; | |
215 } | |
216 | |
217 public String getAuthor() | |
218 { | |
219 return author; | |
220 } | |
221 | |
222 public String getMessage() | |
223 { | |
224 return message; | |
225 } | |
226 | |
227 public String getUUID() | |
228 { | |
229 return uuid; | |
230 } | |
231 } | |
124 | 232 |
38 | 233 } |