Skip to content

Commit 8ebb6fe

Browse files
authored
Merge pull request #7 from tinystruct/master
Stop waiting for the messages once its session was destroyed, Simplified code.
2 parents a866f04 + 1cd5a79 commit 8ebb6fe

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

src/tinystruct/examples/smalltalk.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,18 @@ public String update() throws ApplicationException, IOException {
211211
}
212212

213213
public String update(String meetingCode, String sessionId) throws ApplicationException, IOException {
214+
String error = "{ \"error\": \"expired\" }";
214215
if (this.meetings.containsKey(meetingCode)) {
215216
if(sessions.get(meetingCode) != null && sessions.get(meetingCode).contains(sessionId)) {
216217
return this.update(sessionId);
217218
}
218-
final HttpServletResponse response = (HttpServletResponse) this.context.getAttribute("HTTP_RESPONSE");
219-
response.setContentType("application/json");
220-
response.setStatus(403);
221-
return "{ \"error\": \"session-timeout\" }";
219+
error = "{ \"error\": \"session-timeout\" }";
222220
}
223-
221+
224222
final HttpServletResponse response = (HttpServletResponse) this.context.getAttribute("HTTP_RESPONSE");
225223
response.setContentType("application/json");
226224
response.setStatus(403);
227-
return "{ \"error\": \"expired\" }";
225+
return error;
228226
}
229227

230228
public String upload() throws ApplicationException {
@@ -329,19 +327,19 @@ public void sessionDestroyed(HttpSessionEvent arg0) {
329327
builder.put("time", format.format(new Date()));
330328
builder.put("cmd", "expired");
331329
this.save(meetingCode, builder);
332-
330+
333331
Queue<Builder> messages;
334332
List<String> session_ids;
335-
synchronized (meetings) {
333+
synchronized (this.meetings) {
336334
if((session_ids = this.sessions.get(meetingCode)) != null) {
337335
session_ids.remove(arg0.getSession().getId());
338336
}
339-
340-
if ((messages = meetings.get(meetingCode)) != null) {
337+
338+
if ((messages = this.meetings.get(meetingCode)) != null) {
341339
messages.remove(meetingCode);
342340
}
343-
344-
meetings.notifyAll();
341+
342+
this.meetings.notifyAll();
345343
}
346344

347345
synchronized (this.list) {

src/tinystruct/examples/talk.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,19 @@ public void run() {
125125
* @throws IOException
126126
*/
127127
public final String update(final String sessionId) throws ApplicationException, IOException {
128-
Builder message;
128+
Builder message = null;
129129
Queue<Builder> messages;
130130
synchronized (this.list) {
131131
messages = this.list.get(sessionId);
132-
while((message = messages.poll()) == null) {
132+
while(messages != null && (message = messages.poll()) == null) {
133133
try {
134134
this.list.wait(TIMEOUT);
135135
} catch (InterruptedException e) {
136136
throw new ApplicationException(e.getMessage(), e);
137137
}
138138
}
139139

140-
return message.toString();
140+
return message != null ? message.toString() : "";
141141
}
142142
}
143143

0 commit comments

Comments
 (0)