Skip to content

Commit c4d5ced

Browse files
committed
Update smalltalk.java
1 parent a4dc0fc commit c4d5ced

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

src/tinystruct/examples/smalltalk.java

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
public class smalltalk extends AbstractApplication {
3939

4040
private static final long TIMEOUT = 1000;
41-
private volatile Map<String, Queue<Builder>> sessions;
4241
private final Map<String, Map<String, Queue<Builder>>> groups = Collections.synchronizedMap(new HashMap<String, Map<String, Queue<Builder>>>());
4342

4443
@Override
@@ -67,8 +66,9 @@ public smalltalk index() {
6766
meeting_code = java.util.UUID.randomUUID().toString();
6867
session.setAttribute("meeting_code", meeting_code);
6968

70-
this.sessions = new HashMap<String, Queue<Builder>>();
71-
this.groups.put(meeting_code.toString(), this.sessions);
69+
final Map<String, Queue<Builder>> sessions = new HashMap<String, Queue<Builder>>();
70+
sessions.put(session.getId(), new ArrayDeque<Builder>());
71+
this.groups.put(meeting_code.toString(), sessions);
7272

7373
System.out.println("New meeting generated:" + meeting_code);
7474
}
@@ -130,24 +130,36 @@ public String start(String name) throws ApplicationException {
130130
return name;
131131
}
132132

133-
public String update() throws ApplicationException {
133+
public String update() throws ApplicationException, IOException {
134134
final HttpServletRequest request = (HttpServletRequest) this.context.getAttribute("HTTP_REQUEST");
135+
final HttpServletResponse response = (HttpServletResponse) this.context.getAttribute("HTTP_RESPONSE");
135136
final HttpSession session = request.getSession();
136137
final Object meeting_code = session.getAttribute("meeting_code");
137138

138139
if ( meeting_code != null ) {
139-
this.checkup(meeting_code);
140-
141140
Builder message;
142-
final String sessionId = session.getId();
143-
synchronized (this.sessions) {
141+
142+
Map<String, Queue<Builder>> sessions;
143+
synchronized (this.groups) {
144+
if ((sessions = this.groups.get(meeting_code)) == null) {
145+
this.groups.put(meeting_code.toString(), new HashMap<String, Queue<Builder>>());
146+
return "{}";
147+
}
148+
149+
final String sessionId = session.getId();
144150
do {
145151
try {
146-
this.sessions.wait(TIMEOUT);
152+
this.groups.wait(TIMEOUT);
147153
} catch (InterruptedException e) {
148154
throw new ApplicationException(e.getMessage(), e);
149155
}
150-
} while(this.sessions.get(sessionId) == null || (message = this.sessions.get(sessionId).poll()) == null);
156+
} while(sessions.get(sessionId) == null || (message = sessions.get(sessionId).poll()) == null);
157+
158+
// @Todo
159+
// To review why the context is not thread-safe.
160+
// Use response.getWriter() to avoid the inconformity issue.
161+
response.getWriter().println(message);
162+
response.getWriter().close();
151163

152164
System.out.println("["+sessionId+"][" + session.getAttribute("meeting_code") + "]:" + message);
153165
System.out.println("-------------");
@@ -174,19 +186,22 @@ public String save() {
174186
builder.put("time", format.format(new Date()));
175187
builder.put("message", filter(request.getParameter("text")));
176188

177-
this.checkup(meeting_code);
189+
Map<String, Queue<Builder>> sessions;
190+
synchronized (this.groups) {
191+
if ((sessions = this.groups.get(meeting_code)) == null) {
192+
this.groups.put(meeting_code.toString(), new HashMap<String, Queue<Builder>>());
193+
}
178194

179-
final String sessionId = session.getId();
180-
synchronized (this.sessions) {
181-
if (this.sessions.get(sessionId) == null) {
182-
this.sessions.put(sessionId, new ArrayDeque<Builder>());
195+
final String sessionId = session.getId();
196+
if (sessions.get(sessionId) == null) {
197+
sessions.put(sessionId, new ArrayDeque<Builder>());
183198
}
184199

185-
final Collection<Queue<Builder>> set = this.sessions.values();
200+
final Collection<Queue<Builder>> set = sessions.values();
186201
final Iterator<Queue<Builder>> iterator = set.iterator();
187202
while(iterator.hasNext()) {
188203
iterator.next().add(builder);
189-
this.sessions.notifyAll();
204+
this.groups.notifyAll();
190205
}
191206
}
192207
return builder.toString();
@@ -212,22 +227,24 @@ public String command() {
212227
builder.put("user", session.getAttribute("user"));
213228
builder.put("cmd", request.getParameter("cmd"));
214229

215-
this.checkup(meeting_code);
230+
Map<String, Queue<Builder>> sessions;
231+
synchronized (this.groups) {
232+
if ((sessions = this.groups.get(meeting_code)) == null) {
233+
this.groups.put(meeting_code.toString(), new HashMap<String, Queue<Builder>>());
234+
}
216235

217-
final String sessionId = session.getId();
218-
synchronized (this.sessions) {
219-
if (this.sessions.get(sessionId) == null) {
220-
this.sessions.put(sessionId, new ArrayDeque<Builder>());
236+
final String sessionId = session.getId();
237+
if (sessions.get(sessionId) == null) {
238+
sessions.put(sessionId, new ArrayDeque<Builder>());
221239
}
222240

223-
final Collection<Queue<Builder>> set = this.sessions.values();
241+
final Collection<Queue<Builder>> set = sessions.values();
224242
final Iterator<Queue<Builder>> iterator = set.iterator();
225243
while(iterator.hasNext()) {
226244
iterator.next().add(builder);
227-
this.sessions.notifyAll();
245+
this.groups.notifyAll();
228246
}
229247
}
230-
231248
return "{}";
232249
}
233250

@@ -240,7 +257,7 @@ public String upload() throws ApplicationException {
240257
response.setContentType("text/html;charset=UTF-8");
241258

242259
// Create path components to save the file
243-
final String path = this.context.getAttribute("system.directory") != null ? this.context.getAttribute("system.directory").toString() + "/files" : "files";
260+
final String path = this.config.get("system.directory") != null ? this.config.get("system.directory").toString() + "/files" : "files";
244261

245262
final Builders builders = new Builders();
246263
try {
@@ -301,15 +318,6 @@ protected smalltalk exit() {
301318
return this;
302319
}
303320

304-
private void checkup(final Object meeting_code) {
305-
if ((this.sessions = this.groups.get(meeting_code)) == null) {
306-
this.sessions = new HashMap<String, Queue<Builder>>();
307-
this.groups.put(meeting_code.toString(), this.sessions);
308-
309-
this.setVariable("meeting_code", meeting_code.toString());
310-
}
311-
}
312-
313321
protected String filter(String text) {
314322
text = text.replaceAll("<script(.*)>(.*)<\\/script>", "");
315323
return text;

0 commit comments

Comments
 (0)