Skip to content

Commit 1ed4e26

Browse files
authored
Merge pull request #1 from m0ver/master
Bring fork up to date
2 parents ff77c5b + 386786a commit 1ed4e26

File tree

5 files changed

+90
-107
lines changed

5 files changed

+90
-107
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<img src="https://raw.githubusercontent.com/tinystruct/tinystruct2.0/master/favicon.png" title="tinystruct2.0" />
2+
23
tinystruct2.0
34
=========
45
[![Build Status](https://travis-ci.org/m0ver/tinystruct2.0.svg?branch=master)](https://travis-ci.org/m0ver/tinystruct2.0)

WEB-INF/lib/struct-2.0.jar

-14 KB
Binary file not shown.

src/tinystruct/examples/reading.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.tinystruct.dom.Element;
3939
import org.tinystruct.system.Authentication;
4040
import org.tinystruct.system.util.Base64;
41-
import org.tinystruct.system.util.URLFileLoader;
41+
import org.tinystruct.system.util.URLResourceLoader;
4242

4343
import custom.objects.User;
4444
import custom.objects.article;
@@ -583,7 +583,7 @@ public static void main(String[]args) throws ApplicationException, IOException{
583583

584584
while(p <= 47) {
585585
URL url = new URL("http://www.old-gospel.net/viewthread.php?tid=446&extra=page%3D1&page="+(p++));
586-
URLFileLoader file = new URLFileLoader(url);
586+
URLResourceLoader file = new URLResourceLoader(url);
587587
file.setCharset("gbk");
588588
String content = file.getContent().toString();
589589
Matcher m = pattern.matcher(content);

src/tinystruct/examples/smalltalk.java

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Date;
1515
import java.util.List;
1616
import java.util.Queue;
17+
import java.util.concurrent.ArrayBlockingQueue;
1718
import java.util.concurrent.ConcurrentLinkedQueue;
1819

1920
import javax.servlet.ServletException;
@@ -61,31 +62,23 @@ public talk index() {
6162
}
6263

6364
List<String> session_ids;
64-
synchronized (this.meetings) {
65-
final String sessionId = request.getSession().getId();
66-
if (this.meetings.get(meetingCode) == null) {
67-
this.meetings.put(meetingCode.toString(), new ConcurrentLinkedQueue<Builder>());
68-
}
69-
70-
// If the current user is not in the list of the sessions, we create a default session list for the meeting generated.
71-
if((session_ids = this.sessions.get(meetingCode)) == null)
72-
{
73-
this.sessions.put(meetingCode.toString(), session_ids = new ArrayList<String>());
74-
}
75-
76-
if(!session_ids.contains(sessionId))
77-
session_ids.add(sessionId);
65+
final String sessionId = request.getSession().getId();
66+
if (this.meetings.get(meetingCode) == null) {
67+
this.meetings.put(meetingCode.toString(), new ArrayBlockingQueue<Builder>(DEFAULT_MESSAGE_POOL_SIZE));
68+
}
7869

79-
this.meetings.notifyAll();
70+
// If the current user is not in the list of the sessions, we create a default session list for the meeting generated.
71+
if((session_ids = this.sessions.get(meetingCode)) == null)
72+
{
73+
this.sessions.put(meetingCode.toString(), session_ids = new ArrayList<String>());
8074
}
75+
76+
if(!session_ids.contains(sessionId))
77+
session_ids.add(sessionId);
8178

82-
synchronized (this.list) {
83-
final String sessionId = request.getSession().getId();
84-
if(!this.list.containsKey(sessionId))
85-
{
86-
this.list.put(sessionId, new ConcurrentLinkedQueue<Builder>());
87-
}
88-
this.list.notifyAll();
79+
if(!this.list.containsKey(sessionId))
80+
{
81+
this.list.put(sessionId, new ConcurrentLinkedQueue<Builder>());
8982
}
9083

9184
this.setVariable("meeting_code", meetingCode.toString());
@@ -308,13 +301,10 @@ public void sessionCreated(HttpSessionEvent arg0) {
308301
System.out.println("New meeting generated by HttpSessionListener:" + meetingCode);
309302
}
310303

311-
synchronized (this.list) {
312-
final String sessionId = arg0.getSession().getId();
313-
if(!this.list.containsKey(sessionId))
314-
{
315-
this.list.put(sessionId, new ConcurrentLinkedQueue<Builder>());
316-
}
317-
this.list.notifyAll();
304+
final String sessionId = arg0.getSession().getId();
305+
if(!this.list.containsKey(sessionId))
306+
{
307+
this.list.put(sessionId, new ConcurrentLinkedQueue<Builder>());
318308
}
319309
}
320310

@@ -331,25 +321,18 @@ public void sessionDestroyed(HttpSessionEvent arg0) {
331321

332322
Queue<Builder> messages;
333323
List<String> session_ids;
334-
synchronized (this.meetings) {
335-
if((session_ids = this.sessions.get(meetingCode)) != null) {
336-
session_ids.remove(arg0.getSession().getId());
337-
}
338-
339-
if ((messages = this.meetings.get(meetingCode)) != null) {
340-
messages.remove(meetingCode);
341-
}
324+
if((session_ids = this.sessions.get(meetingCode)) != null) {
325+
session_ids.remove(arg0.getSession().getId());
326+
}
342327

343-
this.meetings.notifyAll();
328+
if ((messages = this.meetings.get(meetingCode)) != null) {
329+
messages.remove(meetingCode);
344330
}
345331

346-
synchronized (this.list) {
347-
final String sessionId = arg0.getSession().getId();
348-
if(this.list.containsKey(sessionId))
349-
{
350-
this.list.remove(sessionId);
351-
}
352-
this.list.notifyAll();
332+
final String sessionId = arg0.getSession().getId();
333+
if(this.list.containsKey(sessionId))
334+
{
335+
this.list.remove(sessionId);
353336
}
354337
}
355338
}

src/tinystruct/examples/talk.java

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.Map;
1111
import java.util.Map.Entry;
1212
import java.util.Queue;
13+
import java.util.concurrent.ArrayBlockingQueue;
14+
import java.util.concurrent.BlockingQueue;
1315
import java.util.concurrent.ConcurrentHashMap;
1416
import java.util.concurrent.ConcurrentLinkedQueue;
1517
import java.util.concurrent.ExecutorService;
@@ -23,11 +25,13 @@
2325

2426
public class talk extends AbstractApplication {
2527

26-
private static final long TIMEOUT = 200;
28+
private static final long TIMEOUT = 1;
29+
private static final int DEFAULT_POOL_SIZE = 3;
30+
protected static final int DEFAULT_MESSAGE_POOL_SIZE = 10;
31+
protected final Map<String, BlockingQueue<Builder>> meetings = new ConcurrentHashMap<String, BlockingQueue<Builder>>();
2732
protected final Map<String, Queue<Builder>> list = new ConcurrentHashMap<String, Queue<Builder>>();
28-
protected final Map<String, Queue<Builder>> meetings = new ConcurrentHashMap<String, Queue<Builder>>();
2933
protected final Map<String, List<String>> sessions = new ConcurrentHashMap<String, List<String>>();
30-
private final ExecutorService service = Executors.newFixedThreadPool(3);
34+
private ExecutorService service;
3135

3236
@Override
3337
public void init() {
@@ -36,23 +40,25 @@ public void init() {
3640
this.setAction("talk/version", "version");
3741
this.setAction("talk/testing", "testing");
3842

39-
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
40-
@Override
41-
public void run() {
42-
service.shutdown();
43-
while (true) {
44-
try {
45-
System.out.println("Waiting for the service to terminate...");
46-
if (service.awaitTermination(5, TimeUnit.SECONDS)) {
47-
System.out.println("Service will be terminated soon.");
43+
if (this.service != null) {
44+
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
45+
@Override
46+
public void run() {
47+
service.shutdown();
48+
while (true) {
49+
try {
50+
System.out.println("Waiting for the service to terminate...");
51+
if (service.awaitTermination(5, TimeUnit.SECONDS)) {
52+
System.out.println("Service will be terminated soon.");
4853
break;
49-
}
50-
} catch (InterruptedException e) {
51-
e.printStackTrace();
52-
}
53-
}
54-
}
55-
}));
54+
}
55+
} catch (InterruptedException e) {
56+
e.printStackTrace();
57+
}
58+
}
59+
}
60+
}));
61+
}
5662
}
5763

5864
/**
@@ -86,37 +92,38 @@ public String save(Object meetingCode, String sessionId, String message) {
8692
* @return builder
8793
*/
8894
public final String save(final Object meetingCode, final Builder builder) {
89-
final Queue<Builder> messages;
90-
synchronized (this.meetings) {
91-
if (this.meetings.get(meetingCode) == null) {
92-
this.meetings.put(meetingCode.toString(), new ConcurrentLinkedQueue<Builder>());
93-
}
95+
BlockingQueue<Builder> messages;
96+
if ((messages = this.meetings.get(meetingCode)) == null) {
97+
this.meetings.put(meetingCode.toString(), messages = new ArrayBlockingQueue<Builder>(DEFAULT_MESSAGE_POOL_SIZE));
98+
}
9499

95-
messages = this.meetings.get(meetingCode);
96-
messages.add(builder);
97-
this.meetings.notifyAll();
100+
try {
101+
messages.put(builder);
102+
} catch (InterruptedException e) {
103+
e.printStackTrace();
98104
}
99105

100-
service.execute(new Runnable(){
106+
this.getService().execute(new Runnable(){
101107
@Override
102108
public void run() {
103-
synchronized(talk.this.meetings) {
104109
Builder message;
105110
do {
106111
try {
107-
talk.this.meetings.wait(TIMEOUT);
112+
Thread.sleep(TIMEOUT);
108113
} catch (InterruptedException e) {
109-
e.printStackTrace();
114+
e.printStackTrace();
110115
}
111116
} while(talk.this.meetings.get(meetingCode) == null || (message = talk.this.meetings.get(meetingCode).poll()) == null);
112-
113117
talk.this.copy(meetingCode, message);
114-
}
115118
}
116119
});
117120
return builder.toString();
118121
}
119122

123+
private ExecutorService getService() {
124+
return this.service!=null? this.service : Executors.newFixedThreadPool(DEFAULT_POOL_SIZE);
125+
}
126+
120127
/**
121128
* Poll message from the messages of the session specified sessionId.
122129
* @param sessionId
@@ -126,19 +133,16 @@ public void run() {
126133
*/
127134
public final String update(final String sessionId) throws ApplicationException, IOException {
128135
Builder message;
129-
Queue<Builder> messages;
130-
synchronized (this.list) {
131-
messages = this.list.get(sessionId);
132-
while((message = messages.poll()) == null) {
133-
try {
134-
this.list.wait(TIMEOUT);
135-
} catch (InterruptedException e) {
136-
throw new ApplicationException(e.getMessage(), e);
137-
}
136+
Queue<Builder> messages = this.list.get(sessionId);
137+
while((message = messages.poll()) == null) {
138+
try {
139+
Thread.sleep(TIMEOUT);
140+
} catch (InterruptedException e) {
141+
throw new ApplicationException(e.getMessage(), e);
138142
}
139-
140-
return message.toString();
141143
}
144+
145+
return message.toString();
142146
}
143147

144148
/**
@@ -156,22 +160,17 @@ protected String filter(String text) {
156160
* @param builder
157161
*/
158162
private final void copy(Object meetingCode, Builder builder) {
159-
synchronized(this.list) {
160-
final Collection<Entry<String, Queue<Builder>>> set = list.entrySet();
163+
final Collection<Entry<String, Queue<Builder>>> set = this.list.entrySet();
161164
final Iterator<Entry<String, Queue<Builder>>> iterator = set.iterator();
162-
final List<String> meeting_session;
163-
if((meeting_session = this.sessions.get(meetingCode)) != null) {
165+
final List<String> _sessions;
166+
if((_sessions = this.sessions.get(meetingCode)) != null) {
164167
while(iterator.hasNext()) {
165-
Entry<String, Queue<Builder>> e = iterator.next();
166-
if(meeting_session.contains(e.getKey())) {
167-
e.getValue().add(builder);
168-
this.list.notifyAll();
168+
Entry<String, Queue<Builder>> list = iterator.next();
169+
if(_sessions.contains(list.getKey())) {
170+
list.getValue().add(builder);
169171
}
170172
}
171173
}
172-
else
173-
this.list.notifyAll();
174-
}
175174
}
176175

177176
@Override
@@ -188,7 +187,7 @@ public String version() {
188187
* @throws ApplicationException
189188
*/
190189
public boolean testing(final int n) throws ApplicationException {
191-
this.meetings.put("[M001]", new ConcurrentLinkedQueue<Builder>());
190+
this.meetings.put("[M001]", new ArrayBlockingQueue<Builder>(DEFAULT_MESSAGE_POOL_SIZE));
192191
this.list.put("{A}", new ConcurrentLinkedQueue<Builder>());
193192
this.list.put("{B}", new ConcurrentLinkedQueue<Builder>());
194193

@@ -197,7 +196,7 @@ public boolean testing(final int n) throws ApplicationException {
197196
sess.add("{B}");
198197
this.sessions.put("[M001]", sess);
199198

200-
service.execute(new Runnable(){
199+
this.getService().execute(new Runnable(){
201200
@Override
202201
public void run() {
203202
int i=0;
@@ -215,7 +214,7 @@ public void run() {
215214
}
216215
});
217216

218-
service.execute(new Runnable(){
217+
this.getService().execute(new Runnable(){
219218
@Override
220219
public void run() {
221220
int i=0;
@@ -233,7 +232,7 @@ public void run() {
233232
}
234233
});
235234

236-
service.execute(new Runnable(){
235+
this.getService().execute(new Runnable(){
237236
@Override
238237
public void run() {
239238
// TODO Auto-generated method stub
@@ -252,7 +251,7 @@ public void run() {
252251
}
253252
});
254253

255-
service.execute(new Runnable(){
254+
this.getService().execute(new Runnable(){
256255
@Override
257256
public void run() {
258257
// TODO Auto-generated method stub

0 commit comments

Comments
 (0)