Skip to content

Commit 05ca985

Browse files
committed
Create talk.java
1 parent 2ba03be commit 05ca985

1 file changed

Lines changed: 235 additions & 0 deletions

File tree

src/tinystruct/examples/talk.java

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

0 commit comments

Comments
 (0)