forked from m0ver/tinystruct-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtalk.java
More file actions
250 lines (225 loc) · 7.28 KB
/
talk.java
File metadata and controls
250 lines (225 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package tinystruct.examples;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.tinystruct.AbstractApplication;
import org.tinystruct.ApplicationException;
import org.tinystruct.data.component.Builder;
import org.tinystruct.system.ApplicationManager;
public class talk extends AbstractApplication {
private static final long TIMEOUT = 200;
protected final Map<String, Queue<Builder>> list = new ConcurrentHashMap<String, Queue<Builder>>();
protected final Map<String, Queue<Builder>> meetings = new ConcurrentHashMap<String, Queue<Builder>>();
protected final Map<String, List<String>> sessions = new ConcurrentHashMap<String, List<String>>();
@Override
public void init() {
this.setAction("talk/update", "update");
this.setAction("talk/save", "save");
this.setAction("talk/version", "version");
this.setAction("talk/testing", "testing");
}
/**
* To be used for testing.
* @param meetingCode
* @param sessionId
* @param message
* @return
*/
public String save(Object meetingCode, String sessionId, String message) {
if ( meetingCode != null ) {
if (message != null && !message.isEmpty()) {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d h:m:s");
final Builder builder = new Builder();
builder.put("user", "user_"+sessionId);
builder.put("time", format.format(new Date()));
builder.put("message", filter(message));
builder.put("session_id", sessionId);
return this.save(meetingCode, builder);
}
}
return "{}";
}
/**
* Save message and create a thread for copying it to message list of each session.
* @param meetingCode
* @param builder
* @return builder
*/
public final String save(final Object meetingCode, final Builder builder) {
final Queue<Builder> messages;
synchronized (this.meetings) {
if (this.meetings.get(meetingCode) == null) {
this.meetings.put(meetingCode.toString(), new ConcurrentLinkedQueue<Builder>());
}
messages = this.meetings.get(meetingCode);
messages.add(builder);
this.meetings.notifyAll();
}
new Thread(new Runnable(){
@Override
public void run() {
synchronized(talk.this.meetings) {
Builder message;
do {
try {
talk.this.meetings.wait(TIMEOUT);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while(talk.this.meetings.get(meetingCode) == null || (message = talk.this.meetings.get(meetingCode).poll()) == null);
talk.this.copy(meetingCode, message);
}
}
}).start();
return builder.toString();
}
/**
* Poll message from the messages of the session specified sessionId.
* @param sessionId
* @return message
* @throws ApplicationException
* @throws IOException
*/
public final String update(final String sessionId) throws ApplicationException, IOException {
Builder message;
Queue<Builder> messages;
synchronized (this.list) {
messages = this.list.get(sessionId);
while((message = messages.poll()) == null) {
try {
this.list.wait(TIMEOUT);
} catch (InterruptedException e) {
throw new ApplicationException(e.getMessage(), e);
}
}
return message.toString();
}
}
/**
* This function can be override.
* @param text
* @return
*/
protected String filter(String text) {
return text;
}
/**
* Copy message to the list of each session.
* @param meetingCode
* @param builder
*/
private final void copy(Object meetingCode, Builder builder) {
synchronized(this.list) {
final Collection<Entry<String, Queue<Builder>>> set = list.entrySet();
final Iterator<Entry<String, Queue<Builder>>> iterator = set.iterator();
while(iterator.hasNext()) {
Entry<String, Queue<Builder>> e = iterator.next();
if(this.sessions.get(meetingCode) != null && this.sessions.get(meetingCode).contains(e.getKey())) {
e.getValue().add(builder);
this.list.notifyAll();
}
}
}
}
@Override
public String version() {
return "Welcome to use tinystruct 2.0";
}
/**
* This is a testing. It can be executed with the command:
* $ bin/dispatcher --import-applications=tinystruct.examples.talk talk/testing/100
*
* @param n
* @return
* @throws ApplicationException
*/
public boolean testing(final int n) throws ApplicationException {
this.meetings.put("[M001]", new ConcurrentLinkedQueue<Builder>());
this.list.put("{A}", new ConcurrentLinkedQueue<Builder>());
this.list.put("{B}", new ConcurrentLinkedQueue<Builder>());
List<String> sess = new ArrayList<String>();
sess.add("{A}");
sess.add("{B}");
this.sessions.put("[M001]", sess);
new Thread(new Runnable(){
@Override
public void run() {
int i=0;
while(i++<n)
try {
ApplicationManager.call("talk/save/[M001]/{A}/A post "+i, null);
Thread.sleep(1);
} catch (ApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
int i=0;
while(i++<n)
try {
ApplicationManager.call("talk/save/[M001]/{B}/B post "+i, null);
Thread.sleep(1);
} catch (ApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("[A] is started...");
while(true)
try {
System.out.println("**A**:"+ApplicationManager.call("talk/update/{A}", null));
Thread.sleep(1);
} catch (ApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("[B] is started...");
while(true)
try {
System.out.println("**B**:"+ApplicationManager.call("talk/update/{B}", null));
Thread.sleep(1);
} catch (ApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
return true;
}
}