Skip to content

Commit 545fe52

Browse files
committed
Fixed bug of Simple Pipe
Better support of SimpleSerializable
1 parent c405f6d commit 545fe52

File tree

5 files changed

+201
-111
lines changed

5 files changed

+201
-111
lines changed

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeHelper.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ private SimplePipeHelper() {
7171
*/
7272
@J2SNative({
7373
"if (key == null || pipe == null) return;",
74-
"if (net.sf.j2s.ajax.SimplePipeHelper.allPipes == null) {",
75-
" net.sf.j2s.ajax.SimplePipeHelper.allPipes = new Object ();",
74+
"var sph = net.sf.j2s.ajax.SimplePipeHelper;",
75+
"if (sph.allPipes == null) {",
76+
" sph.allPipes = new Object ();",
7677
"}",
77-
"net.sf.j2s.ajax.SimplePipeHelper.allPipes[key] = pipe;"
78+
"sph.allPipes[key] = pipe;"
7879
})
7980
public static void registerPipe(String key, SimplePipeRunnable pipe) {
8081
if (key == null || pipe == null) return;
@@ -131,7 +132,10 @@ static String nextPipeKey() {
131132
}
132133

133134
@J2SNative({
134-
"delete net.sf.j2s.ajax.SimplePipeHelper.allPipes[key];"
135+
"var sph = net.sf.j2s.ajax.SimplePipeHelper;",
136+
"if (sph.allPipes != null) {",
137+
" delete sph.allPipes[key];",
138+
"}"
135139
})
136140
public static void removePipe(String key) {
137141
if (key == null) {

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeRequest.java

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -326,36 +326,40 @@ public void run() {
326326
notifyingPipes.remove(p);
327327
}
328328
}
329-
} else if ((now - p.lastPipeNotified) * (2 + p.pipeSequence - p.notifySequence)
329+
} else {
330+
p.lastLiveDetected = System.currentTimeMillis();
331+
p.updateStatus(true);
332+
if ((now - p.lastPipeNotified) * (2 + p.pipeSequence - p.notifySequence)
330333
< pipeLiveNotifyInterval + pipeLiveNotifyInterval) {
331-
// do nothing
332-
} else if (r.pipeSequence != r.notifySequence) {
333-
p.lastPipeNotified = now;
334-
final HttpRequest request = getRequest();
335-
final String pipeKey = p.pipeKey;
336-
final long sequence = p.pipeSequence;
337-
String pipeRequestData = constructRequest(pipeKey, PIPE_TYPE_NOTIFY, sequence);
338-
request.registerOnReadyStateChange(new XHRCallbackAdapter() {
339-
public void onLoaded() {
340-
String response = request.getResponseText();
341-
if (response != null && p.notifySequence < sequence && response.indexOf("$p1p3b$") != 0) {
342-
p.notifySequence = sequence;
343-
}
344-
if (response != null && response.indexOf("\"" + PIPE_STATUS_LOST + "\"") != -1) {
345-
p.pipeAlive = false;
346-
p.pipeLost();
347-
SimplePipeHelper.removePipe(pipeKey);
348-
// may need to inform user that connection is already lost!
349-
synchronized (notifyingMutex) {
350-
notifyingPipes.remove(p);
334+
// do nothing
335+
} else if (r.pipeSequence != r.notifySequence) {
336+
p.lastPipeNotified = now;
337+
final HttpRequest request = getRequest();
338+
final String pipeKey = p.pipeKey;
339+
final long sequence = p.pipeSequence;
340+
String pipeRequestData = constructRequest(pipeKey, PIPE_TYPE_NOTIFY, sequence);
341+
request.registerOnReadyStateChange(new XHRCallbackAdapter() {
342+
public void onLoaded() {
343+
String response = request.getResponseText();
344+
if (response != null && p.notifySequence < sequence && response.indexOf("$p1p3b$") != 0) {
345+
p.notifySequence = sequence;
351346
}
352-
} else {
353-
p.lastLiveDetected = System.currentTimeMillis();
354-
p.updateStatus(true);
355-
}
356-
}
357-
});
358-
sendRequest(request, p.getPipeMethod(), p.getPipeURL(), pipeRequestData, pipes.length == 1 ? false : queueNotifying);
347+
if (response != null && response.indexOf("\"" + PIPE_STATUS_LOST + "\"") != -1) {
348+
p.pipeAlive = false;
349+
p.pipeLost();
350+
SimplePipeHelper.removePipe(pipeKey);
351+
// may need to inform user that connection is already lost!
352+
synchronized (notifyingMutex) {
353+
notifyingPipes.remove(p);
354+
}
355+
} else {
356+
p.lastLiveDetected = System.currentTimeMillis();
357+
p.updateStatus(true);
358+
}
359+
}
360+
});
361+
sendRequest(request, p.getPipeMethod(), p.getPipeURL(), pipeRequestData, pipes.length == 1 ? false : queueNotifying);
362+
}
359363
}
360364
} // end of pipes for-loop
361365
} // end of while true
@@ -1385,7 +1389,8 @@ public void run() {
13851389
if (last <= 0) {
13861390
last = created;
13871391
}
1388-
if ((runnable.queryEnded || (now - last >= spr.pipeLiveNotifyInterval
1392+
if (runnable.isPipeLive() // may be false after a few queries
1393+
&& (runnable.queryEnded || (now - last >= spr.pipeLiveNotifyInterval
13891394
&& (lastXHR == -1 || now - lastXHR >= spr.pipeLiveNotifyInterval)))
13901395
&& runnable.queryFailedRetries < 3) {
13911396
runnable.queryEnded = false;
@@ -1398,7 +1403,12 @@ public void run() {
13981403
}
13991404
runnable.retries = runnable.queryFailedRetries;
14001405
runnable.received = runnable.lastPipeDataReceived;
1401-
if (runnable.queryFailedRetries >= 3
1406+
if (!runnable.isPipeLive()) { // try to clean up for lost pipes
1407+
runnable.pipeAlive = false;
1408+
runnable.pipeClosed();
1409+
sph.removePipe(key);
1410+
spr.pipeIFrameClean (key);
1411+
} else if (runnable.queryFailedRetries >= 3
14021412
|| now - last > 3 * spr.pipeLiveNotifyInterval) {
14031413
if (key == runnable.pipeKey) {
14041414
runnable.pipeAlive = false;
@@ -1419,7 +1429,7 @@ public void run() {
14191429
Thread queryThread = new Thread(new Runnable() {
14201430
public void run() {
14211431
SimplePipeRunnable runnable = null;
1422-
while ((runnable = SimplePipeHelper.getPipe(key)) != null) {
1432+
while ((runnable = SimplePipeHelper.getPipe(key)) != null && runnable.isPipeLive()) {
14231433
pipeQuery(runnable);
14241434

14251435
long now = System.currentTimeMillis();
@@ -1443,6 +1453,12 @@ public void run() {
14431453
//e.printStackTrace();
14441454
}
14451455
}
1456+
if (runnable != null) { // runnable is still in SimplePipeHelper before #removePipe
1457+
runnable.pipeAlive = false;
1458+
runnable.pipeClosed();
1459+
}
1460+
SimplePipeHelper.removePipe(key);
1461+
14461462
}
14471463
}, "Simple Pipe Query Worker");
14481464
queryThread.setDaemon(true);

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeRunnable.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ public void ajaxIn() {
185185
@Override
186186
public void ajaxRun() {
187187
lastLiveDetected = System.currentTimeMillis();
188-
pipeKey = SimplePipeHelper.registerPipe(this);
188+
String registeredKey = SimplePipeHelper.registerPipe(this);
189+
pipeKey = registeredKey;
189190
pipeAlive = pipeSetup();
190191
if (!pipeAlive) {
191192
SimplePipeHelper.removePipe(pipeKey);
@@ -483,6 +484,9 @@ protected void updateStatus(boolean live) {
483484
* @j2sIgnore
484485
*/
485486
public SimpleSerializable[] through(Object ... args) {
487+
if (args instanceof SimpleSerializable[]) {
488+
return (SimpleSerializable[]) args;
489+
}
486490
return null;
487491
}
488492

@@ -557,13 +561,13 @@ public void pipeThrough(Object ... args) {
557561

558562
if (objs == null || objs.length == 0) return;
559563

560-
if (pipe instanceof SimplePipeRunnable) {
561-
SimplePipeRunnable pipeRunnable = (SimplePipeRunnable) pipe;
562-
if (pipeRunnable.helper != null) {
563-
pipeRunnable.helper.helpThrough(pipe, objs);
564+
//if (pipe instanceof SimplePipeRunnable) {
565+
//SimplePipeRunnable pipeRunnable = (SimplePipeRunnable) pipe;
566+
if (pipe.helper != null) {
567+
pipe.helper.helpThrough(pipe, objs);
564568
return;
565569
}
566-
}
570+
//}
567571
for (int i = 0; i < objs.length; i++) {
568572
pipe.deal(objs[i]);
569573
}
@@ -580,13 +584,13 @@ public void pipeThrough(SimpleSerializable ... args) {
580584
if (args == null || args.length == 0) return;
581585
SimplePipeRunnable pipe = SimplePipeHelper.getPipe(pipeKey);
582586
if (pipe == null) return;
583-
if (pipe instanceof SimplePipeRunnable) {
584-
SimplePipeRunnable pipeRunnable = (SimplePipeRunnable) pipe;
585-
if (pipeRunnable.helper != null) {
586-
pipeRunnable.helper.helpThrough(pipe, args);
587+
//if (pipe instanceof SimplePipeRunnable) {
588+
//SimplePipeRunnable pipeRunnable = (SimplePipeRunnable) pipe;
589+
if (pipe.helper != null) {
590+
pipe.helper.helpThrough(pipe, args);
587591
return;
588592
}
589-
}
593+
//}
590594
for (int i = 0; i < args.length; i++) {
591595
pipe.deal(args[i]);
592596
}

0 commit comments

Comments
 (0)