1313import java .util .Collections ;
1414import java .util .Comparator ;
1515import java .util .Date ;
16- import java .util .HashMap ;
16+ import java .util .Iterator ;
1717import java .util .List ;
1818import java .util .Map ;
1919import java .util .Vector ;
2020import java .util .concurrent .BlockingQueue ;
21+ import java .util .concurrent .ConcurrentHashMap ;
2122import java .util .concurrent .LinkedBlockingQueue ;
2223import java .util .concurrent .TimeUnit ;
2324
@@ -51,7 +52,11 @@ public static interface IPipeClosing {
5152@ J2SIgnore
5253private static long monitoringInterval =10000 ;// 10s
5354
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 );
5560
5661@ J2SIgnore
5762private static BlockingQueue <SimplePipeRunnable >toBeDestroyedPipes =new LinkedBlockingQueue <SimplePipeRunnable >();
@@ -66,16 +71,13 @@ private SimplePipeHelper() {
6671 */
6772@ J2SNative ({
6873"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 ();" ,
7176"}" ,
72- "net.sf.j2s.ajax.SimplePipeHelper.pipes [key] = pipe;"
77+ "net.sf.j2s.ajax.SimplePipeHelper.allPipes [key] = pipe;"
7378})
7479public static void registerPipe (String key ,SimplePipeRunnable pipe ) {
7580if (key ==null ||pipe ==null )return ;
76- if (pipes ==null ) {
77- pipes =Collections .synchronizedMap (new HashMap <String ,SimplePipeRunnable >(50 ));
78- }
7981pipes .put (key ,pipe );
8082}
8183
@@ -88,18 +90,14 @@ synchronized static String registerPipe(SimplePipeRunnable pipe) {
8890System .out .println ("ERROR!!! pipeKey should be null here! " +pipe .pipeKey );
8991}
9092// 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-
9593String key =nextPipeKey ();
9694while (pipes .get (key ) !=null ) {
97- key =nextPipeKey ();;
95+ key =nextPipeKey ();
9896}
99- pipes .put (key ,pipe );
97+ pipes .put (key ,pipe );// FIXME: In rare case, it will override another pipe
10098
10199//if (pipeMap == null) {
102- //pipeMap =Collections.synchronizedMap( newHashMap <String, List<SimpleSerializable>>() );
100+ //pipeMap = newConcurrentHashMap <String, List<SimpleSerializable>>();
103101//}
104102//List<SimpleSerializable> list = pipeMap.get(key);
105103//if (list == null) {
@@ -133,23 +131,20 @@ static String nextPipeKey() {
133131}
134132
135133@ J2SNative ({
136- "delete net.sf.j2s.ajax.SimplePipeHelper.pipes [key];"
134+ "delete net.sf.j2s.ajax.SimplePipeHelper.allPipes [key];"
137135})
138136public static void removePipe (String key ) {
139137if (key ==null ) {
140138System .out .println ("Removing pipe for null key???" );
141139new RuntimeException ("Removing null pipe key" ).printStackTrace ();
142140return ;
143141}
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 ();
153148}
154149}
155150//if (pipeMap != null) {
@@ -162,12 +157,12 @@ public static void removePipe(String key) {
162157}
163158
164159@ J2SNative ({
165- "var ps = net.sf.j2s.ajax.SimplePipeHelper.pipes ;" ,
160+ "var ps = net.sf.j2s.ajax.SimplePipeHelper.allPipes ;" ,
166161"if (ps == null || key == null) return null;" ,
167162"return ps[key];"
168163})
169164public static SimplePipeRunnable getPipe (String key ) {
170- if (pipes == null || key ==null )return null ;
165+ if (key ==null )return null ;
171166return pipes .get (key );
172167}
173168
@@ -297,35 +292,29 @@ static void helpClosing(SimplePipeRunnable pipe) {
297292public static String printStatistics2 () {
298293StringBuilder builder =new StringBuilder ();
299294builder .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 ++;
318308}
319309}
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 " );
329318}
330319}
331320}
@@ -336,34 +325,26 @@ public static String printStatistics2() {
336325public static String printStatistics () {
337326StringBuilder builder =new StringBuilder ();
338327builder .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 " );
355342}
356343}
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 " );
367348}
368349}
369350return builder .toString ();
@@ -386,10 +367,6 @@ private static void monitoringAllPipes() {
386367Thread .sleep (monitoringInterval );
387368}catch (InterruptedException e ) {
388369}
389- if (pipes ==null ) {
390- System .err .println ("Pipe sessions are null or empty! Managed pipe session monitor exited!" );
391- break ;
392- }
393370Object []allPipes =pipes .values ().toArray ();
394371for (int i =0 ;i <allPipes .length ;i ++) {
395372final SimplePipeRunnable pipe = (SimplePipeRunnable )allPipes [i ];
@@ -430,12 +407,7 @@ private static void monitoringAllPipes() {
430407e .printStackTrace ();
431408 }
432409}
433- if (pipes ==null /* || pipes.isEmpty()*/ ) {
434- monitored =false ;
435- break ;
436- }
437410}
438- System .err .println ("Pipe sessions are null or empty! Pipe session monitor exited!" );
439411}
440412
441413@ J2SIgnore