Skip to content

Commit 8d3203a

Browse files
committed
Update smalltalk.java
1 parent 206157c commit 8d3203a

1 file changed

Lines changed: 45 additions & 4 deletions

File tree

src/tinystruct/examples/smalltalk.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import javax.servlet.ServletException;
2323
import javax.servlet.http.HttpServletRequest;
2424
import javax.servlet.http.HttpServletResponse;
25+
import javax.servlet.http.HttpSessionEvent;
26+
import javax.servlet.http.HttpSessionListener;
2527

2628
import org.tinystruct.AbstractApplication;
2729
import org.tinystruct.ApplicationException;
@@ -34,7 +36,7 @@
3436
import org.tinystruct.transfer.http.upload.ContentDisposition;
3537
import org.tinystruct.transfer.http.upload.MultipartFormData;
3638

37-
public class smalltalk extends AbstractApplication {
39+
public class smalltalk extends AbstractApplication implements HttpSessionListener {
3840

3941
private static final long TIMEOUT = 1000;
4042
private final Map<String, Map<String, Queue<Builder>>> groups = Collections.synchronizedMap(new HashMap<String, Map<String, Queue<Builder>>>());
@@ -72,7 +74,7 @@ public smalltalk index() {
7274
if ((sessions = this.groups.get(meeting_code)) == null) {
7375
this.groups.put(meeting_code.toString(), sessions = new HashMap<String, Queue<Builder>>());
7476
}
75-
77+
7678
final String sessionId = request.getSession().getId();
7779
if (sessions.get(sessionId) == null) {
7880
sessions.put(sessionId, new ArrayDeque<Builder>());
@@ -86,7 +88,7 @@ public smalltalk index() {
8688
if ((topic = this.getVariable(meeting_code.toString())) != null) {
8789
this.setVariable("topic", topic.getValue().toString().replaceAll("[\r\n]", "<br />"), true);
8890
}
89-
91+
9092
return this;
9193
}
9294

@@ -311,5 +313,44 @@ protected String filter(String text) {
311313
public String version() {
312314
return "Welcome to use tinystruct 2.0";
313315
}
314-
316+
317+
@Override
318+
public void sessionCreated(HttpSessionEvent arg0) {
319+
Object meeting_code = arg0.getSession().getAttribute("meeting_code");
320+
if ( meeting_code == null ) {
321+
meeting_code = java.util.UUID.randomUUID().toString();
322+
arg0.getSession().setAttribute("meeting_code", meeting_code);
323+
324+
System.out.println("New meeting generated by HttpSessionListener:" + meeting_code);
325+
}
326+
327+
Map<String, Queue<Builder>> sessions;
328+
synchronized (groups) {
329+
if ((sessions = groups.get(meeting_code)) == null) {
330+
groups.put(meeting_code.toString(), sessions = new HashMap<String, Queue<Builder>>());
331+
}
332+
333+
final String sessionId = arg0.getSession().getId();
334+
if (sessions.get(sessionId) == null) {
335+
sessions.put(sessionId, new ArrayDeque<Builder>());
336+
}
337+
338+
groups.notifyAll();
339+
}
340+
}
341+
342+
@Override
343+
public void sessionDestroyed(HttpSessionEvent arg0) {
344+
Object meeting_code = arg0.getSession().getAttribute("meeting_code");
345+
String sessionId = arg0.getSession().getId();
346+
if ( meeting_code != null ) {
347+
Map<String, Queue<Builder>> sessions;
348+
synchronized (groups) {
349+
if ((sessions = groups.get(meeting_code)) != null) {
350+
sessions.remove(sessionId);
351+
groups.notifyAll();
352+
}
353+
}
354+
}
355+
}
315356
}

0 commit comments

Comments
 (0)