9
9
import java .io .IOException ;
10
10
import java .io .OutputStream ;
11
11
import java .text .SimpleDateFormat ;
12
- import java .util .ArrayDeque ;
12
+ import java .util .ArrayList ;
13
13
import java .util .Arrays ;
14
- import java .util .Collection ;
15
14
import java .util .Date ;
16
- import java .util .HashMap ;
17
- import java .util .Iterator ;
18
- import java .util .Map ;
15
+ import java .util .List ;
19
16
import java .util .Queue ;
20
- import java .util .concurrent .ConcurrentHashMap ;
17
+ import java .util .concurrent .ConcurrentLinkedQueue ;
21
18
22
19
import javax .servlet .ServletException ;
23
20
import javax .servlet .http .HttpServletRequest ;
24
21
import javax .servlet .http .HttpServletResponse ;
25
22
import javax .servlet .http .HttpSessionEvent ;
26
23
import javax .servlet .http .HttpSessionListener ;
27
24
28
- import org .tinystruct .AbstractApplication ;
29
25
import org .tinystruct .ApplicationException ;
30
26
import org .tinystruct .data .component .Builder ;
31
27
import org .tinystruct .data .component .Builders ;
36
32
import org .tinystruct .transfer .http .upload .ContentDisposition ;
37
33
import org .tinystruct .transfer .http .upload .MultipartFormData ;
38
34
39
- public class smalltalk extends AbstractApplication implements HttpSessionListener {
40
-
41
- private static final long TIMEOUT = 1000 ;
42
- private final Map <String , Map <String , Queue <Builder >>> groups = new ConcurrentHashMap <String , Map <String , Queue <Builder >>>();
43
-
44
- @ Override
35
+ public class smalltalk extends talk implements HttpSessionListener {
36
+
45
37
public void init () {
38
+ super .init ();
39
+
46
40
this .setAction ("talk" , "index" );
47
41
this .setAction ("talk/join" , "join" );
48
42
this .setAction ("talk/start" , "start" );
49
- this .setAction ("talk/update" , "update" );
50
- this .setAction ("talk/save" , "save" );
51
43
this .setAction ("talk/upload" , "upload" );
52
44
this .setAction ("talk/command" , "command" );
53
45
this .setAction ("talk/topic" , "topic" );
54
46
this .setAction ("talk/matrix" , "matrix" );
55
- this .setAction ("talk/version" , "version" );
56
47
57
48
this .setVariable ("message" , "" );
58
49
this .setVariable ("topic" , "" );
59
50
}
60
51
61
- public smalltalk index () {
52
+ public talk index () {
62
53
final HttpServletRequest request = (HttpServletRequest ) this .context .getAttribute ("HTTP_REQUEST" );
63
54
Object meetingCode = request .getSession ().getAttribute ("meeting_code" );
64
55
@@ -69,18 +60,21 @@ public smalltalk index() {
69
60
System .out .println ("New meeting generated:" + meetingCode );
70
61
}
71
62
72
- Map <String , Queue < Builder >> sessions ;
73
- synchronized (this .groups ) {
74
- if (( sessions = this .groups .get (meetingCode ) ) == null ) {
75
- this .groups .put (meetingCode .toString (), sessions = new HashMap < String , Queue < Builder > >());
63
+ List <String > session_ids ;
64
+ synchronized (this .meetings ) {
65
+ if (this .meetings .get (meetingCode ) == null ) {
66
+ this .meetings .put (meetingCode .toString (), new ConcurrentLinkedQueue < Builder >());
76
67
}
77
68
78
- final String sessionId = request .getSession ().getId ();
79
- if (sessions .get (sessionId ) == null ) {
80
- sessions .put (sessionId , new ArrayDeque <Builder >());
69
+ // If the current user is not in the list of the sessions, we create a default session list for the meeting generated.
70
+ if ((session_ids = this .sessions .get (meetingCode )) == null )
71
+ {
72
+ this .sessions .put (meetingCode .toString (), session_ids = new ArrayList <String >());
81
73
}
82
74
83
- this .groups .notifyAll ();
75
+ session_ids .add (request .getSession ().getId ());
76
+
77
+ this .meetings .notifyAll ();
84
78
}
85
79
86
80
this .setVariable ("meeting_code" , meetingCode .toString ());
@@ -104,7 +98,7 @@ public String matrix() throws ApplicationException {
104
98
}
105
99
106
100
public String join (String meetingCode ) throws ApplicationException {
107
- if (groups .containsKey (meetingCode )) {
101
+ if (meetings .containsKey (meetingCode )) {
108
102
final HttpServletRequest request = (HttpServletRequest ) this .context .getAttribute ("HTTP_REQUEST" );
109
103
final HttpServletResponse response = (HttpServletResponse ) this .context .getAttribute ("HTTP_RESPONSE" );
110
104
request .getSession ().setAttribute ("meeting_code" , meetingCode );
@@ -118,8 +112,7 @@ public String join(String meetingCode) throws ApplicationException {
118
112
return "Invalid meeting code." ;
119
113
}
120
114
121
- return "Please start the conversation with your name: "
122
- + this .config .get ("default.base_url" ) + "talk/start/YOUR NAME" ;
115
+ return "Please start the conversation with your name: " + this .config .get ("default.base_url" ) + "talk/start/YOUR NAME" ;
123
116
}
124
117
125
118
public String start (String name ) throws ApplicationException {
@@ -146,8 +139,7 @@ public String command() {
146
139
147
140
final Object meetingCode = request .getSession ().getAttribute ("meeting_code" );
148
141
final String sessionId = request .getSession ().getId ();
149
-
150
- if ( meetingCode != null ) {
142
+ if ( meetingCode != null && sessions .get (meetingCode ).contains (sessionId )) {
151
143
if (request .getSession ().getAttribute ("user" ) == null ) {
152
144
return "{ \" error\" : \" missing user\" }" ;
153
145
}
@@ -156,7 +148,7 @@ public String command() {
156
148
builder .put ("user" , request .getSession ().getAttribute ("user" ));
157
149
builder .put ("cmd" , request .getParameter ("cmd" ));
158
150
159
- return this .save (sessionId , meetingCode , builder );
151
+ return this .save (meetingCode , builder );
160
152
}
161
153
162
154
return "{ \" error\" : \" expired\" }" ;
@@ -168,22 +160,21 @@ public String save() {
168
160
response .setContentType ("application/json" );
169
161
170
162
final Object meetingCode = request .getSession ().getAttribute ("meeting_code" );
171
- if ( meetingCode != null ) {
163
+ final String sessionId = request .getSession ().getId ();
164
+ if ( meetingCode != null && sessions .get (meetingCode ).contains (sessionId )) {
172
165
String message ;
173
166
if ((message = request .getParameter ("text" )) != null && !message .isEmpty ()) {
174
167
String [] agent = request .getHeader ("User-Agent" ).split (" " );
175
168
this .setVariable ("browser" , agent [agent .length - 1 ]);
176
169
177
170
final SimpleDateFormat format = new SimpleDateFormat ("yyyy-M-d h:m:s" );
178
-
179
171
final Builder builder = new Builder ();
180
- final String sessionId = request .getSession ().getId ();
181
172
builder .put ("user" , request .getSession ().getAttribute ("user" ));
182
173
builder .put ("time" , format .format (new Date ()));
183
174
builder .put ("message" , filter (message ));
184
- builder .put ("sessionId " , sessionId );
175
+ builder .put ("session_id " , sessionId );
185
176
186
- return this .save (sessionId , meetingCode , builder );
177
+ return this .save (meetingCode , builder );
187
178
}
188
179
}
189
180
@@ -194,56 +185,12 @@ public String update() throws ApplicationException, IOException {
194
185
final HttpServletRequest request = (HttpServletRequest ) this .context .getAttribute ("HTTP_REQUEST" );
195
186
final Object meetingCode = request .getSession ().getAttribute ("meeting_code" );
196
187
final String sessionId = request .getSession ().getId ();
197
- if ( meetingCode != null ) {
198
- return update (sessionId , meetingCode );
188
+ if ( meetingCode != null && sessions . get ( meetingCode ). contains ( sessionId ) ) {
189
+ return update (sessionId );
199
190
}
200
191
return "" ;
201
192
}
202
-
203
- public String save (final String sessionId , final Object meetingCode , final Builder builder ) {
204
- Map <String , Queue <Builder >> sessions ;
205
- synchronized (this .groups ) {
206
- if ((sessions = this .groups .get (meetingCode )) == null ) {
207
- this .groups .put (meetingCode .toString (), sessions = new HashMap <String , Queue <Builder >>());
208
- }
209
-
210
- final Collection <Queue <Builder >> set = sessions .values ();
211
- final Iterator <Queue <Builder >> iterator = set .iterator ();
212
- while (true ) {
213
- if (!iterator .hasNext ()) break ;
214
- iterator .next ().add (builder );
215
- this .groups .notifyAll ();
216
- }
217
-
218
- if (sessions .get (sessionId ) == null ) {
219
- sessions .put (sessionId , new ArrayDeque <Builder >());
220
- sessions .get (sessionId ).add (builder );
221
- this .groups .notifyAll ();
222
- }
223
-
224
- return builder .toString ();
225
- }
226
- }
227
-
228
- public String update (final String sessionId , final Object meetingCode ) throws ApplicationException , IOException {
229
- Builder message ;
230
-
231
- Map <String , Queue <Builder >> sessions ;
232
- synchronized (this .groups ) {
233
- sessions = this .groups .get (meetingCode );
234
-
235
- do {
236
- try {
237
- this .groups .wait (TIMEOUT );
238
- } catch (InterruptedException e ) {
239
- throw new ApplicationException (e .getMessage (), e );
240
- }
241
- } while (sessions == null || sessions .get (sessionId ) == null || (message = sessions .get (sessionId ).poll ()) == null );
242
-
243
- return message .toString ();
244
- }
245
- }
246
-
193
+
247
194
public String upload () throws ApplicationException {
248
195
final HttpServletRequest request = (HttpServletRequest ) this .context .getAttribute ("HTTP_REQUEST" );
249
196
final HttpServletResponse response = (HttpServletResponse ) this .context .getAttribute ("HTTP_RESPONSE" );
@@ -304,22 +251,18 @@ public boolean topic() {
304
251
return false ;
305
252
}
306
253
307
- protected smalltalk exit () {
254
+ protected talk exit () {
308
255
final HttpServletRequest request = (HttpServletRequest ) this .context .getAttribute ("HTTP_REQUEST" );
309
256
request .getSession ().removeAttribute ("meeting_code" );
310
257
return this ;
311
258
}
312
259
260
+ @ Override
313
261
protected String filter (String text ) {
314
262
text = text .replaceAll ("<script(.*)>(.*)<\\ /script>" , "" );
315
263
return text ;
316
264
}
317
265
318
- @ Override
319
- public String version () {
320
- return "Welcome to use tinystruct 2.0" ;
321
- }
322
-
323
266
@ Override
324
267
public void sessionCreated (HttpSessionEvent arg0 ) {
325
268
Object meetingCode = arg0 .getSession ().getAttribute ("meeting_code" );
@@ -330,31 +273,39 @@ public void sessionCreated(HttpSessionEvent arg0) {
330
273
System .out .println ("New meeting generated by HttpSessionListener:" + meetingCode );
331
274
}
332
275
333
- Map <String , Queue <Builder >> sessions ;
334
- synchronized (groups ) {
335
- if ((sessions = groups .get (meetingCode )) == null ) {
336
- groups .put (meetingCode .toString (), sessions = new HashMap <String , Queue <Builder >>());
337
- }
338
-
276
+ synchronized (this .list ) {
339
277
final String sessionId = arg0 .getSession ().getId ();
340
- if (sessions .get (sessionId ) == null ) {
341
- sessions .put (sessionId , new ArrayDeque <Builder >());
278
+ if (!this .list .containsKey (sessionId ))
279
+ {
280
+ this .list .put (sessionId , new ConcurrentLinkedQueue <Builder >());
281
+ this .list .notifyAll ();
342
282
}
343
-
344
- groups .notifyAll ();
345
283
}
346
284
}
347
285
348
286
@ Override
349
287
public void sessionDestroyed (HttpSessionEvent arg0 ) {
350
288
Object meetingCode = arg0 .getSession ().getAttribute ("meeting_code" );
351
- String sessionId = arg0 .getSession ().getId ();
352
289
if ( meetingCode != null ) {
353
- Map <String , Queue <Builder >> sessions ;
354
- synchronized (groups ) {
355
- if ((sessions = groups .get (meetingCode )) != null ) {
356
- sessions .remove (sessionId );
357
- groups .notifyAll ();
290
+ Queue <Builder > messages ;
291
+ List <String > session_ids ;
292
+ synchronized (meetings ) {
293
+ if ((session_ids = this .sessions .get (meetingCode )) != null )
294
+ {
295
+ session_ids .remove (arg0 .getSession ().getId ());
296
+ }
297
+
298
+ if ((messages = meetings .get (meetingCode )) != null ) {
299
+ messages .remove (meetingCode );
300
+ meetings .notifyAll ();
301
+ }
302
+ }
303
+ synchronized (this .list ) {
304
+ final String sessionId = arg0 .getSession ().getId ();
305
+ if (this .list .containsKey (sessionId ))
306
+ {
307
+ this .list .remove (sessionId );
308
+ this .list .notifyAll ();
358
309
}
359
310
}
360
311
}
0 commit comments