Mercurial > hg > Members > nobuyasu > jungle-network
annotate src/main/java/app/bbs/NetworkJungleBulletinBoard.java @ 135:2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 12 Jan 2014 06:18:37 +0900 |
parents | 00fcb468de27 |
children | c1d75b031b15 |
rev | line source |
---|---|
112 | 1 package app.bbs; |
38 | 2 |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
3 import java.io.File; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
4 import java.io.IOException; |
38 | 5 import java.nio.ByteBuffer; |
56 | 6 import java.util.Date; |
38 | 7 import java.util.concurrent.atomic.AtomicInteger; |
39 | 8 |
9 import alice.jungle.core.NetworkDefaultJungle; | |
124 | 10 import alice.jungle.persistent.AliceJournal; |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
11 import alice.jungle.persistent.NetworkJournal; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
12 import alice.jungle.persistent.PersistentJournal; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
13 import alice.jungle.transaction.JungleUpdater; |
38 | 14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; |
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; | |
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; | |
125 | 17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs.BoardMessage; |
38 | 18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children; |
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; | |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListReader; |
38 | 22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; |
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; | |
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; | |
25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; | |
26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; | |
27 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; | |
28 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; | |
29 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
30 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; | |
31 | |
124 | 32 public class NetworkJungleBulletinBoard implements NetworkBulletinBoard |
38 | 33 { |
34 private final Jungle jungle; | |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
35 private final NetworkJournal journal; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
36 private final String LOG_DIR; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
37 private Boolean persistentFlag; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
38 |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
39 private NetworkJungleBulletinBoard(String _uuid, NetworkJournal _journal) |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
40 { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
41 journal = _journal; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
42 jungle = new NetworkDefaultJungle(journal, _uuid,new DefaultTreeEditor(new DefaultTraverser())); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
43 BulletinBoardJungleManager.setJungle(jungle); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
44 persistentFlag = false; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
45 LOG_DIR = "./log"; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
46 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
47 |
124 | 48 public NetworkJungleBulletinBoard(String _uuid) |
38 | 49 { |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
50 this(_uuid, new AliceJournal()); |
124 | 51 jungle.createNewTree("boards"); |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
52 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
53 |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
54 public static NetworkBulletinBoard NewPersistentJungle(String _uuid) |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
55 { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
56 NetworkJungleBulletinBoard board = new NetworkJungleBulletinBoard(_uuid, new PersistentJournal()); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
57 board.persistentFlag = true; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
58 return board; |
38 | 59 } |
118
f64ff5bd66f5
Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents:
112
diff
changeset
|
60 |
124 | 61 public void init() { |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
62 if(!persistentFlag) { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
63 return; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
64 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
65 checkAndCreateLogDirectory(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
66 try { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
67 commitLogRecover(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
68 } catch (IOException e) { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
69 e.printStackTrace(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
70 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
71 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
72 |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
73 public void checkAndCreateLogDirectory() { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
74 File logFile = new File(LOG_DIR); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
75 if(!logFile.exists()) { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
76 logFile.mkdir(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
77 return; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
78 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
79 if (logFile.isFile()) { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
80 logFile.delete(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
81 logFile.mkdir(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
82 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
83 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
84 |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
85 public void commitLogRecover() throws IOException { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
86 File[] logFiles = new File(LOG_DIR).listFiles(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
87 for(File logFile : logFiles) { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
88 commitLogRecover(logFile); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
89 logFile.delete(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
90 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
91 if(jungle.getTreeByName("boards") == null) { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
92 jungle.createNewTree("boards"); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
93 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
94 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
95 |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
96 private void commitLogRecover(File logFile) throws IOException { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
97 journal.setInputFile(logFile); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
98 ChangeListReader reader = journal.getReader(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
99 if (reader == null) return; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
100 for (ChangeList chList : reader) { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
101 String treeName = chList.getTreeName(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
102 JungleTree tree = jungle.getTreeByName(treeName); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
103 if(tree == null) { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
104 tree = jungle.createNewTree(treeName); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
105 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
106 JungleTreeEditor editor = tree.getLocalTreeEditor(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
107 Either<Error, JungleTreeEditor> either = JungleUpdater.edit(editor, chList); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
108 editor = either.b(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
109 if(either.isA()) { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
110 throw new IOException("Failed commit log recovery"); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
111 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
112 editor.success(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
113 } |
118
f64ff5bd66f5
Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents:
112
diff
changeset
|
114 } |
38 | 115 |
116 public Iterable<String> getBoards() | |
117 { | |
118 JungleTree tree = jungle.getTreeByName("boards"); | |
119 Node node = tree.getRootNode(); | |
120 Children<Node> chs = node.getChildren(); | |
121 | |
122 IterableConverter.Converter<String,Node> converter = new IterableConverter.Converter<String,Node>(){ | |
123 public String conv(Node _b) { | |
124 ByteBuffer e = _b.getAttributes().get("name"); | |
125 return new String(e.array()); | |
126 } | |
127 }; | |
128 | |
129 return new IterableConverter<String,Node>(chs,converter); | |
130 } | |
131 | |
132 public void createBoards(final String _name,final String _author,final String _initMessage,final String _editKey) | |
133 { | |
134 if(null == jungle.createNewTree(_name)){ | |
135 throw new IllegalStateException(); | |
136 } | |
137 | |
138 JungleTree tree = jungle.getTreeByName("boards"); | |
139 JungleTreeEditor editor = tree.getTreeEditor(); | |
140 DefaultNodePath root = new DefaultNodePath(); | |
141 Either<Error,JungleTreeEditor> either = editor.addNewChildAt(root,0); | |
142 if(either.isA()){ | |
143 throw new IllegalStateException(); | |
144 } | |
145 editor = either.b(); | |
146 | |
147 either = editor.putAttribute(root.add(0),"name",ByteBuffer.wrap(_name.getBytes())); | |
148 if(either.isA()){ | |
149 throw new IllegalStateException(); | |
150 } | |
151 editor = either.b(); | |
129
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
152 final long timestamp = new Date().getTime(); |
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
153 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
154 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
|
155 if(either.isA()){ |
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
156 throw new IllegalStateException(); |
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
157 } |
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
158 editor = either.b(); |
38 | 159 Either<Error,JungleTreeEditor> result = editor.success(); |
160 if(result.isA()){ | |
161 throw new IllegalStateException(); | |
162 } | |
163 | |
164 tree = jungle.getTreeByName(_name); | |
165 editor = tree.getTreeEditor(); | |
166 either = editor.addNewChildAt(root,0); | |
167 if(either.isA()){ | |
168 throw new IllegalStateException(); | |
169 } | |
170 editor = either.b(); | |
171 | |
172 NodeEditor e = new NodeEditor(){ | |
173 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | |
174 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | |
175 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_initMessage.getBytes())).b(); | |
176 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | |
130 | 177 ByteBuffer tBuffer2 = ByteBuffer.allocate(16); |
178 _e = _e.getAttributes().put("timestamp",tBuffer2.putLong(timestamp)).b(); | |
38 | 179 return DefaultEither.newB(_e); |
180 } | |
181 }; | |
182 | |
183 either = editor.edit(root.add(0),e); | |
184 if(either.isA()){ | |
185 throw new IllegalStateException(); | |
186 } | |
187 editor = either.b(); | |
188 editor.success(); | |
189 } | |
190 | |
191 public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey) | |
192 { | |
193 JungleTree tree = jungle.getTreeByName(_board); | |
194 if(tree == null){ | |
195 throw new IllegalStateException(); | |
196 } | |
197 | |
46 | 198 Either<Error, JungleTreeEditor> either; |
38 | 199 do{ |
200 Node node = tree.getRootNode(); | |
201 int size = node.getChildren().size(); | |
202 DefaultNodePath path = new DefaultNodePath(); | |
203 | |
65 | 204 JungleTreeEditor editor = tree.getTreeEditor(); |
46 | 205 either = editor.addNewChildAt(path,size); |
38 | 206 if(either.isA()){ |
207 throw new IllegalStateException(); | |
208 } | |
209 editor = either.b(); | |
56 | 210 final long timestamp = new Date().getTime(); |
38 | 211 NodeEditor e = new NodeEditor(){ |
212 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | |
213 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | |
214 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); | |
215 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | |
56 | 216 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
217 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b(); | |
38 | 218 return DefaultEither.newB(_e); |
219 } | |
220 }; | |
221 path = path.add(size); | |
222 either = editor.edit(path,e); | |
223 if(either.isA()){ | |
224 throw new IllegalStateException(); | |
225 } | |
226 editor = either.b(); | |
46 | 227 either = editor.success(); |
93 | 228 |
46 | 229 }while(either.isA()); |
38 | 230 } |
231 | |
232 public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey) | |
233 { | |
65 | 234 for(;;) { |
38 | 235 DefaultNodePath path = new DefaultNodePath(); |
236 path = path.add(Integer.parseInt(_uuid)); | |
237 | |
238 JungleTree tree = jungle.getTreeByName(_board); | |
65 | 239 JungleTreeEditor editor = tree.getTreeEditor(); |
56 | 240 final long timestamp = new Date().getTime(); |
38 | 241 NodeEditor e = new NodeEditor(){ |
242 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ | |
243 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); | |
244 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); | |
245 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); | |
56 | 246 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
247 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b(); | |
38 | 248 return DefaultEither.newB(_e); |
249 } | |
250 }; | |
251 | |
65 | 252 Either<Error, JungleTreeEditor> either = editor.edit(path,e); |
38 | 253 if(either.isA()){ |
254 throw new IllegalStateException(); | |
255 } | |
256 editor = either.b(); | |
46 | 257 either = editor.success(); |
65 | 258 if(!either.isA()) { |
259 return; | |
260 } | |
261 } | |
262 | |
38 | 263 } |
264 | |
265 public Iterable<BoardMessage> getMessages(String _boardName) | |
266 { | |
267 JungleTree tree = jungle.getTreeByName(_boardName); | |
268 Node node = tree.getRootNode(); | |
269 Children<Node> chs = node.getChildren(); | |
270 final AtomicInteger counter = new AtomicInteger(0); | |
271 IterableConverter.Converter<BoardMessage,Node> converter = new IterableConverter.Converter<BoardMessage,Node>(){ | |
272 public BoardMessage conv(Node _b) { | |
273 String uuid = Integer.toString(counter.get()); | |
274 String author = new String(_b.getAttributes().get("author").array()); | |
275 String message = new String(_b.getAttributes().get("mes").array()); | |
276 counter.incrementAndGet(); | |
277 return new BoardMessageImpl(author,message,uuid); | |
278 } | |
279 }; | |
280 return new IterableConverter<BoardMessage,Node>(chs,converter); | |
281 } | |
282 | |
46 | 283 |
284 | |
38 | 285 private static class BoardMessageImpl implements BoardMessage |
286 { | |
287 private final String author; | |
288 private final String message; | |
289 private final String uuid; | |
290 | |
291 public BoardMessageImpl(String _author,String _message,String _uuid) | |
292 { | |
293 author = _author; | |
294 message = _message; | |
295 uuid = _uuid; | |
296 } | |
297 | |
298 public String getAuthor() | |
299 { | |
300 return author; | |
301 } | |
302 | |
303 public String getMessage() | |
304 { | |
305 return message; | |
306 } | |
307 | |
308 public String getUUID() | |
309 { | |
310 return uuid; | |
311 } | |
312 } | |
124 | 313 |
38 | 314 } |