13
13
import java .util .Collections ;
14
14
import java .util .Comparator ;
15
15
import java .util .Date ;
16
- import java .util .HashMap ;
16
+ import java .util .Iterator ;
17
17
import java .util .List ;
18
18
import java .util .Map ;
19
19
import java .util .Vector ;
20
20
import java .util .concurrent .BlockingQueue ;
21
+ import java .util .concurrent .ConcurrentHashMap ;
21
22
import java .util .concurrent .LinkedBlockingQueue ;
22
23
import java .util .concurrent .TimeUnit ;
23
24
@@ -51,7 +52,11 @@ public static interface IPipeClosing {
51
52
@ J2SIgnore
52
53
private static long monitoringInterval = 10000 ; // 10s
53
54
54
- static Map <String , SimplePipeRunnable > pipes ;
55
+ // allPipes is for JavaScript, as pipes is for Java
56
+ static Object allPipes = null ;
57
+
58
+ @ J2SIgnore
59
+ private static Map <String , SimplePipeRunnable > pipes = new ConcurrentHashMap <String , SimplePipeRunnable >(50 );
55
60
56
61
@ J2SIgnore
57
62
private static BlockingQueue <SimplePipeRunnable > toBeDestroyedPipes = new LinkedBlockingQueue <SimplePipeRunnable >();
@@ -66,16 +71,13 @@ private SimplePipeHelper() {
66
71
*/
67
72
@ J2SNative ({
68
73
"if (key == null || pipe == null) return;" ,
69
- "if (net.sf.j2s.ajax.SimplePipeHelper.pipes == null) {" ,
70
- " net.sf.j2s.ajax.SimplePipeHelper.pipes = new Object ();" ,
74
+ "if (net.sf.j2s.ajax.SimplePipeHelper.allPipes == null) {" ,
75
+ " net.sf.j2s.ajax.SimplePipeHelper.allPipes = new Object ();" ,
71
76
"}" ,
72
- "net.sf.j2s.ajax.SimplePipeHelper.pipes [key] = pipe;"
77
+ "net.sf.j2s.ajax.SimplePipeHelper.allPipes [key] = pipe;"
73
78
})
74
79
public static void registerPipe (String key , SimplePipeRunnable pipe ) {
75
80
if (key == null || pipe == null ) return ;
76
- if (pipes == null ) {
77
- pipes = Collections .synchronizedMap (new HashMap <String , SimplePipeRunnable >(50 ));
78
- }
79
81
pipes .put (key , pipe );
80
82
}
81
83
@@ -88,18 +90,14 @@ synchronized static String registerPipe(SimplePipeRunnable pipe) {
88
90
System .out .println ("ERROR!!! pipeKey should be null here! " + pipe .pipeKey );
89
91
}
90
92
// if (pipe == null) return null; // should never register null pipe!
91
- if (pipes == null ) {
92
- pipes = Collections .synchronizedMap (new HashMap <String , SimplePipeRunnable >(50 ));
93
- }
94
-
95
93
String key = nextPipeKey ();
96
94
while (pipes .get (key ) != null ) {
97
- key = nextPipeKey ();;
95
+ key = nextPipeKey ();
98
96
}
99
- pipes .put (key , pipe );
97
+ pipes .put (key , pipe ); // FIXME: In rare case, it will override another pipe
100
98
101
99
// if (pipeMap == null) {
102
- // pipeMap = Collections.synchronizedMap( new HashMap <String, List<SimpleSerializable>>() );
100
+ // pipeMap = new ConcurrentHashMap <String, List<SimpleSerializable>>();
103
101
// }
104
102
// List<SimpleSerializable> list = pipeMap.get(key);
105
103
// if (list == null) {
@@ -133,23 +131,20 @@ static String nextPipeKey() {
133
131
}
134
132
135
133
@ J2SNative ({
136
- "delete net.sf.j2s.ajax.SimplePipeHelper.pipes [key];"
134
+ "delete net.sf.j2s.ajax.SimplePipeHelper.allPipes [key];"
137
135
})
138
136
public static void removePipe (String key ) {
139
137
if (key == null ) {
140
138
System .out .println ("Removing pipe for null key???" );
141
139
new RuntimeException ("Removing null pipe key" ).printStackTrace ();
142
140
return ;
143
141
}
144
- SimplePipeRunnable pipe = null ;
145
- if (pipes != null ) {
146
- pipe = pipes .remove (key );
147
- if (pipe != null ) {
148
- pipe .pipeAlive = false ;
149
- pipe .pipeClearData ();
150
- synchronized (pipe ) {
151
- pipe .notifyAll ();
152
- }
142
+ SimplePipeRunnable pipe = pipes .remove (key );
143
+ if (pipe != null ) {
144
+ pipe .pipeAlive = false ;
145
+ pipe .pipeClearData ();
146
+ synchronized (pipe ) {
147
+ pipe .notifyAll ();
153
148
}
154
149
}
155
150
// if (pipeMap != null) {
@@ -162,12 +157,12 @@ public static void removePipe(String key) {
162
157
}
163
158
164
159
@ J2SNative ({
165
- "var ps = net.sf.j2s.ajax.SimplePipeHelper.pipes ;" ,
160
+ "var ps = net.sf.j2s.ajax.SimplePipeHelper.allPipes ;" ,
166
161
"if (ps == null || key == null) return null;" ,
167
162
"return ps[key];"
168
163
})
169
164
public static SimplePipeRunnable getPipe (String key ) {
170
- if (pipes == null || key == null ) return null ;
165
+ if (key == null ) return null ;
171
166
return pipes .get (key );
172
167
}
173
168
@@ -297,35 +292,29 @@ static void helpClosing(SimplePipeRunnable pipe) {
297
292
public static String printStatistics2 () {
298
293
StringBuilder builder = new StringBuilder ();
299
294
builder .append ("Pipe monitor<br />\r \n " );
300
- if (pipes != null ) {
301
- builder .append ("Totoal pipe count: " + pipes .size () + "<br />\r \n " );
302
- // buffer.append("Totoal pipe map count: " + pipeMap.size() + "<br />\r\n");
303
- Object [] keys = pipes .keySet ().toArray ();
304
- for (int i = 0 ; i < keys .length ; i ++) {
305
- String key = (String ) keys [i ];
306
- SimplePipeRunnable p = pipes .get (key );
307
- List <SimpleSerializable > list = p != null ? p .pipeData : null ; //pipeMap.get(key);
308
- if (p instanceof CompoundPipeRunnable ) {
309
- CompoundPipeRunnable cp = (CompoundPipeRunnable ) p ;
310
- int activeCount = 0 ;
311
- for (int j = 0 ; j < cp .pipes .length ; j ++) {
312
- if (cp .pipes [j ] != null ) {
313
- activeCount ++;
314
- }
315
- }
316
- if (activeCount > 2 ) {
317
- builder .append (i + " Pipe (active=" + activeCount + ") " + cp .pipeKey + " status=" + cp .status + " pipeAlive=" + cp .isPipeLive () + " created=" + new Date (cp .lastSetup ) + "<br />\r \n " );
295
+ builder .append ("Totoal pipe count: " + pipes .size () + "<br />\r \n " );
296
+ // buffer.append("Total pipe map count: " + pipeMap.size() + "<br />\r\n");
297
+ int i = 0 ;
298
+ for (Iterator <SimplePipeRunnable > itr = pipes .values ().iterator (); itr .hasNext ();) {
299
+ SimplePipeRunnable p = (SimplePipeRunnable ) itr .next ();
300
+ i ++;
301
+ List <SimpleSerializable > list = p .pipeData ; //pipeMap.get(key);
302
+ if (p instanceof CompoundPipeRunnable ) {
303
+ CompoundPipeRunnable cp = (CompoundPipeRunnable ) p ;
304
+ int activeCount = 0 ;
305
+ for (int j = 0 ; j < cp .pipes .length ; j ++) {
306
+ if (cp .pipes [j ] != null ) {
307
+ activeCount ++;
318
308
}
319
309
}
320
- if (list != null ) {
321
- int size = list .size ();
322
- if (size > 20 ) {
323
- if (p != null ) {
324
- builder .append (i + "::: pipe " + p .pipeKey + " size : " + size + " / " + p .pipeAlive + "<br />\r \n " );
325
- } else {
326
- builder .append ("Error pipe " + key + " with size : " + size + "<br />\r \n " );
327
- }
328
- }
310
+ if (activeCount > 2 ) {
311
+ builder .append (i + " Pipe (active=" + activeCount + ") " + cp .pipeKey + " status=" + cp .status + " pipeAlive=" + cp .isPipeLive () + " created=" + new Date (cp .lastSetup ) + "<br />\r \n " );
312
+ }
313
+ }
314
+ if (list != null ) {
315
+ int size = list .size ();
316
+ if (size > 20 ) {
317
+ builder .append (i + "::: pipe " + p .pipeKey + " size : " + size + " / " + p .pipeAlive + "<br />\r \n " );
329
318
}
330
319
}
331
320
}
@@ -336,34 +325,26 @@ public static String printStatistics2() {
336
325
public static String printStatistics () {
337
326
StringBuilder builder = new StringBuilder ();
338
327
builder .append ("Pipe monitor<br />\r \n " );
339
- if (pipes != null ) {
340
- builder .append ("Totoal pipe count: " + pipes .size () + "<br />\r \n " );
341
- // buffer.append("Totoal pipe map count: " + pipeMap.size() + "<br />\r\n");
342
- Object [] keys = pipes .keySet ().toArray ();
343
- for (int i = 0 ; i < keys .length ; i ++) {
344
- String key = (String ) keys [i ];
345
- SimplePipeRunnable p = pipes .get (key );
346
- List <SimpleSerializable > list = p != null ? p .pipeData : null ; //pipeMap.get(key);
347
- if (p instanceof CompoundPipeRunnable ) {
348
- CompoundPipeRunnable cp = (CompoundPipeRunnable ) p ;
349
- builder .append (i + "Pipe " + cp .pipeKey + " status=" + cp .status + " pipeAlive=" + cp .isPipeLive () + " created=" + new Date (cp .lastSetup ) + "<br />\r \n " );
350
- for (int j = 0 ; j < cp .pipes .length ; j ++) {
351
- CompoundPipeSession ps = cp .pipes [j ];
352
- if (ps != null ) {
353
- builder .append (j + " : " + ps .session + " / " + ps .isPipeLive () + " pipeAlive=" + ps .pipeAlive + "<br />\r \n " );
354
- }
328
+ builder .append ("Totoal pipe count: " + pipes .size () + "<br />\r \n " );
329
+ // buffer.append("Total pipe map count: " + pipeMap.size() + "<br />\r\n");
330
+ int i = 0 ;
331
+ for (Iterator <SimplePipeRunnable > itr = pipes .values ().iterator (); itr .hasNext ();) {
332
+ SimplePipeRunnable p = (SimplePipeRunnable ) itr .next ();
333
+ i ++;
334
+ List <SimpleSerializable > list = p .pipeData ; //pipeMap.get(key);
335
+ if (p instanceof CompoundPipeRunnable ) {
336
+ CompoundPipeRunnable cp = (CompoundPipeRunnable ) p ;
337
+ builder .append (i + "Pipe " + cp .pipeKey + " status=" + cp .status + " pipeAlive=" + cp .isPipeLive () + " created=" + new Date (cp .lastSetup ) + "<br />\r \n " );
338
+ for (int j = 0 ; j < cp .pipes .length ; j ++) {
339
+ CompoundPipeSession ps = cp .pipes [j ];
340
+ if (ps != null ) {
341
+ builder .append (j + " : " + ps .session + " / " + ps .isPipeLive () + " pipeAlive=" + ps .pipeAlive + "<br />\r \n " );
355
342
}
356
343
}
357
- if (list != null ) {
358
- int size = list .size ();
359
- //if (size > 5) {
360
- if (p != null ) {
361
- builder .append ("::: pipe " + p .pipeKey + " size : " + size + " / " + p .pipeAlive + "<br />\r \n " );
362
- } else {
363
- builder .append ("Error pipe " + key + " with size : " + size + "<br />\r \n " );
364
- }
365
- //}
366
- }
344
+ }
345
+ if (list != null ) {
346
+ int size = list .size ();
347
+ builder .append ("::: pipe " + p .pipeKey + " size : " + size + " / " + p .pipeAlive + "<br />\r \n " );
367
348
}
368
349
}
369
350
return builder .toString ();
@@ -386,10 +367,6 @@ private static void monitoringAllPipes() {
386
367
Thread .sleep (monitoringInterval );
387
368
} catch (InterruptedException e ) {
388
369
}
389
- if (pipes == null ) {
390
- System .err .println ("Pipe sessions are null or empty! Managed pipe session monitor exited!" );
391
- break ;
392
- }
393
370
Object [] allPipes = pipes .values ().toArray ();
394
371
for (int i = 0 ; i < allPipes .length ; i ++) {
395
372
final SimplePipeRunnable pipe = (SimplePipeRunnable ) allPipes [i ];
@@ -430,12 +407,7 @@ private static void monitoringAllPipes() {
430
407
e .printStackTrace ();
431
408
}
432
409
}
433
- if (pipes == null /* || pipes.isEmpty()*/ ) {
434
- monitored = false ;
435
- break ;
436
- }
437
410
}
438
- System .err .println ("Pipe sessions are null or empty! Pipe session monitor exited!" );
439
411
}
440
412
441
413
@ J2SIgnore
0 commit comments