Skip to content

Commit 0a09280

Browse files
author
zhourenjian
committed
Improving Simple Pipe/Compound Pipe stability.
Improving SimpleSerializable and SimpleRPCRunnable, supporting smaller serialized string
1 parent 181eda3 commit 0a09280

File tree

11 files changed

+213
-57
lines changed

11 files changed

+213
-57
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ static void pipeFailed(CompoundPipeRunnable pipe) {
4141
if (pipe.pipes[i] != null) {
4242
pipe.pipes[i].pipeFailed();
4343
}
44-
pipe.pipes[i] = null;
44+
// pipe.pipes[i] = null;
4545
}
46-
unregisterPipe(pipe.id);
46+
pipe.setupFailedRetries = 0;
47+
pipe.status = 0;
48+
pipe.lastSetupRetried = 0;
49+
// unregisterPipe(pipe.id);
4750
}
4851
}
4952

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private static String nextSessionKey() {
2626
long lastSetupRetried;
2727

2828
public CompoundPipeRunnable() {
29-
pipes = new CompoundPipeSession[100];
29+
pipes = new CompoundPipeSession[4];
3030
status = 0; // starting
3131
setupFailedRetries = 0;
3232
lastSetupRetried = 0;
@@ -60,6 +60,16 @@ public boolean pipeDestroy() {
6060
return true;
6161
}
6262

63+
@Override
64+
public void pipeInit() {
65+
super.pipeInit();
66+
for (int i = 0; i < pipes.length; i++) {
67+
if (pipes[i] != null) {
68+
pipes[i].pipeInit();
69+
}
70+
}
71+
}
72+
6373
@Override
6474
public boolean pipeSetup() {
6575
return true;
@@ -129,7 +139,7 @@ public void weave(CompoundPipeSession pipe) {
129139
}
130140
}
131141
if (!added) {
132-
CompoundPipeSession[] newPipes = new CompoundPipeSession[pipes.length + 100];
142+
CompoundPipeSession[] newPipes = new CompoundPipeSession[pipes.length + 4];
133143
System.arraycopy(pipes, 0, newPipes, 0, pipes.length);
134144
newPipes[pipes.length] = pipe;
135145
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ public void pipeCreated() {
3939
public boolean pipeDestroy() {
4040
if (!super.pipeDestroy()) return false;
4141

42-
PipeSessionClosedEvent evt = new PipeSessionClosedEvent();
43-
evt.session = session;
44-
pipeThrough(evt);
42+
/**
43+
* @j2sNative
44+
*/
45+
{
46+
PipeSessionClosedEvent evt = new PipeSessionClosedEvent();
47+
evt.session = session;
48+
pipeThrough(evt);
49+
}
4550

4651
SimplePipeRunnable pipe = SimplePipeHelper.getPipe(pipeKey);
4752
if (pipe instanceof CompoundPipeRunnable) {
@@ -53,6 +58,7 @@ public boolean pipeDestroy() {
5358

5459
@Override
5560
public void pipeFailed() {
61+
super.pipeFailed();
5662
SimplePipeRunnable pipe = SimplePipeHelper.getPipe(pipeKey);
5763
if (pipe instanceof CompoundPipeRunnable) {
5864
CompoundPipeRunnable cp = (CompoundPipeRunnable) pipe;

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @author zhou renjian
2424
*/
25-
class SimplePipeHelper {
25+
public class SimplePipeHelper {
2626

2727
static interface IPipeThrough {
2828
public void helpThrough(SimplePipeRunnable pipe, SimpleSerializable[] objs);
@@ -225,4 +225,13 @@ static boolean waitAMomentForClosing(final SimplePipeRunnable runnable) {
225225
return !runnable.isPipeLive();
226226
}
227227

228+
@J2SIgnore
229+
public static void helpClosing(SimplePipeRunnable pipe) {
230+
if (pipe.closer != null) {
231+
pipe.closer.helpClosing(pipe);
232+
} else {
233+
pipe.pipeClosed();
234+
}
235+
}
236+
228237
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ protected void doPipe(final HttpServletResponse resp, String key, String type, S
236236
long now = new Date().getTime();
237237
if ((lastPipeDataWritten == -1 && now - beforeLoop >= pipeQueryTimeout)
238238
|| (lastPipeDataWritten > 0
239-
&& now - lastPipeDataWritten >= pipeQueryTimeout)) {
239+
&& now - lastPipeDataWritten >= pipeQueryTimeout
240+
&& SimplePipeRequest.PIPE_TYPE_CONTINUUM.equals(type))) {
240241
output(writer, type, key, SimplePipeRequest.PIPE_STATUS_OK);
241242
lastPipeDataWritten = new Date().getTime();
242243
}

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,16 @@ private static void pipeRequest(final SimplePipeRunnable runnable) {
282282
*
283283
* @j2sNative
284284
* ajaxOut = runnable.ajaxOut;
285+
* if (ajaxOut.wrapped != true) {
285286
* runnable.ajaxOut = (function (aO, r) {
286287
* return function () {
287288
* aO.apply (r, []);
289+
* r.ajaxOut = aO;
288290
* net.sf.j2s.ajax.SimplePipeRequest.ajaxPipe (r);
289291
* };
290-
* }) (ajaxOut, runnable);
292+
* }) (ajaxOut, runnable);
293+
* runnable.ajaxOut.wrapped = true;
294+
* }
291295
*/ { if (ajaxOut == null) ajaxOut = null; /* no warning */ }
292296
if (checkXSS(url, serialize, runnable)) {
293297
// Already send out pipe request in XSS mode. Just return here.
@@ -888,6 +892,10 @@ public void run() {
888892
if (runnable != null) {
889893
if (runnable.queryEnded && runnable.queryFailedRetries < 3) {
890894
runnable.queryEnded = false;
895+
if (runnable.received == runnable.lastPipeDataReceived
896+
&& runnable.retries == runnable.queryFailedRetries) {
897+
runnable.queryFailedRetries++; // response must not be empty
898+
}
891899
pipeFun (runnable);
892900
}
893901
var spr = net.sf.j2s.ajax.SimplePipeRequest;
@@ -896,6 +904,8 @@ public void run() {
896904
if (last == -1) {
897905
last = created;
898906
}
907+
runnable.retries = runnable.queryFailedRetries;
908+
runnable.received = runnable.lastPipeDataReceived;
899909
if (runnable.queryFailedRetries >= 3
900910
|| now - last > 2 * spr.pipeLiveNotifyInterval) {
901911
runnable.pipeAlive = false;
@@ -1032,13 +1042,25 @@ public void run() {
10321042
} else {
10331043
request.open (method, url, true, null, null);
10341044
}
1035-
request.setRequestHeader ("User-Agent",
1036-
"Java2Script-Pacemaker/2.0.0 (+http://j2s.sourceforge.net)");
1045+
try {
1046+
request.setRequestHeader ("User-Agent",
1047+
"Java2Script-Pacemaker/2.0.0 (+http://j2s.sourceforge.net)");
1048+
} catch (e) {
1049+
log ("Setting 'User-Agent' header error : " + e);
1050+
}
10371051
if (method != null && method.toLowerCase () == "post") {
1038-
request.setRequestHeader ("Content-type",
1039-
"application/x-www-form-urlencoded");
1052+
try {
1053+
request.setRequestHeader ("Content-type",
1054+
"application/x-www-form-urlencoded");
1055+
} catch (e) {
1056+
log ("Setting 'Content-type' header error : " + e);
1057+
}
10401058
if (request.overrideMimeType) {
1041-
request.setRequestHeader ("Connection", "close");
1059+
try {
1060+
// request.setRequestHeader ("Connection", "close");
1061+
} catch (e) {
1062+
log ("Setting 'Connection' header error : " + e);
1063+
}
10421064
}
10431065
}
10441066
request.send(data);
@@ -1082,6 +1104,7 @@ public void run() {
10821104
p.xhrHandle = null;
10831105
document.domain = p.parentDomain;
10841106
runnable.queryEnded = true;
1107+
runnable.queryFailedRetries++; // Failed
10851108
}
10861109
}
10871110
var now = new Date ().getTime ();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ public abstract class SimplePipeRunnable extends SimpleRPCRunnable {
2929

3030
public boolean pipeAlive;
3131

32+
@J2SIgnore
3233
SimplePipeHelper.IPipeThrough helper; // For Java server side
3334

35+
@J2SIgnore
3436
SimplePipeHelper.IPipeClosing closer; // For Java server side
3537

3638
private boolean destroyed;
@@ -129,6 +131,7 @@ public void pipeCreated() {
129131
public void pipeFailed() {
130132
// to be override
131133
// notify that pipe is not created correctly.
134+
pipeDestroy();
132135
}
133136

134137
/**
@@ -138,6 +141,7 @@ public void pipeFailed() {
138141
public void pipeLost() {
139142
// to be override
140143
// notify that pipe is lost. Maybe trying to reconnect the pipe
144+
pipeDestroy();
141145
}
142146

143147
/**
@@ -148,6 +152,7 @@ public void pipeLost() {
148152
public void pipeClosed() {
149153
// to be override
150154
// notify that pipe is closed by server.
155+
pipeDestroy();
151156
}
152157

153158
/**

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleRPCRequest.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ protected static boolean checkXSS(String url, String serialize, SimpleRPCRunnabl
281281
} else {
282282
contents[0] = content;
283283
}
284-
g.idSet["x" + rnd] = contents;
284+
if (contents.length > 1) {
285+
g.idSet["x" + rnd] = contents;
286+
}
285287
// Only send the first request, later server return "continue", and client will get
286288
// the session id and continue later requests.
287289
net.sf.j2s.ajax.SimpleRPCRequest.callByScript(rnd, contents.length, 0, contents[0]);
@@ -301,6 +303,9 @@ protected static boolean checkXSS(String url, String serialize, SimpleRPCRunnabl
301303
var isOpera = (userAgent.indexOf ("opera") != -1);
302304
var isIE = (userAgent.indexOf ("msie") != -1) && !isOpera;
303305
if (isIE) {
306+
if (scriptObj.onreadystatechange == null) {
307+
return false; // already cleaned up
308+
}
304309
var done = false;
305310
var state = "" + scriptObj.readyState;
306311
if (state == "loaded" || state == "complete") {
@@ -309,6 +314,9 @@ protected static boolean checkXSS(String url, String serialize, SimpleRPCRunnabl
309314
}
310315
return done;
311316
} else {
317+
if (scriptObj.onerror == null) {
318+
return false; // already cleaned up
319+
}
312320
scriptObj.onerror = null;
313321
scriptObj.onload = null;
314322
return true;
@@ -330,15 +338,24 @@ protected static boolean checkXSS(String url, String serialize, SimpleRPCRunnabl
330338
script.type = "text/javascript";
331339
script.src = url + "?jzn=" + rnd + "&jzp=" + length
332340
+ "&jzc=" + (i + 1) + "&jzz=" + content;
333-
var fun = function () {
334-
if (window["net"] != null && !net.sf.j2s.ajax.SimpleRPCRequest.cleanUp(this)) {
335-
return; // IE, not completed yet
336-
}
337-
var idx = this.src.indexOf ("jzn=");
338-
var rid = this.src.substring (idx + 4, this.src.indexOf ("&", idx));
339-
net.sf.j2s.ajax.SimpleRPCRequest.xssNotify (rid, null);
340-
document.getElementsByTagName ("HEAD")[0].removeChild (this);
341-
};
341+
var fun = (function (oScript) {
342+
return function () {
343+
var g = net.sf.j2s.ajax.SimpleRPCRequest;
344+
var hKey = "h" + rnd;
345+
if (g.idSet[hKey] != null) {
346+
window.clearTimeout (g.idSet[hKey]);
347+
delete g.idSet[hKey];
348+
}
349+
if (window["net"] != null && !net.sf.j2s.ajax.SimpleRPCRequest.cleanUp(oScript)) {
350+
return; // IE, not completed yet
351+
}
352+
var src = oScript.src;
353+
var idx = src.indexOf ("jzn=");
354+
var rid = src.substring (idx + 4, src.indexOf ("&", idx));
355+
net.sf.j2s.ajax.SimpleRPCRequest.xssNotify (rid, null);
356+
document.getElementsByTagName ("HEAD")[0].removeChild (oScript);
357+
};
358+
}) (script);
342359
var userAgent = navigator.userAgent.toLowerCase ();
343360
var isOpera = (userAgent.indexOf ("opera") != -1);
344361
var isIE = (userAgent.indexOf ("msie") != -1) && !isOpera;
@@ -350,6 +367,7 @@ protected static boolean checkXSS(String url, String serialize, SimpleRPCRunnabl
350367
}
351368
var head = document.getElementsByTagName ("HEAD")[0];
352369
head.appendChild (script);
370+
g.idSet["h" + rnd] = window.setTimeout (fun, 20000); // 20s timeout
353371
*/
354372
native static void callByScript(String rnd, String length, String i, String content);
355373

@@ -390,18 +408,16 @@ static void xssNotify(String nameID, String response, String session) {
390408
}
391409
var k = "x" + nameID;
392410
var xcontent = g.idSet[k];
411+
// TODO: The following codes should be modified to send out requests one by one.
393412
if (xcontent != null) {
394-
//The following codes may be modified to send out requests one by one.
395-
if (xcontent != null) {
396-
for (var i = 0; i < xcontent.length; i++) {
397-
if (xcontent[i] != null) {
398-
g.callByScript(nameID, xcontent.length, i, xcontent[i]);
399-
xcontent[i] = null;
400-
}
401-
}
402-
g.idSet[k] = null;
403-
delete g.idSet[k];
404-
}
413+
for (var i = 0; i < xcontent.length; i++) {
414+
if (xcontent[i] != null) {
415+
g.callByScript(nameID, xcontent.length, i, xcontent[i]);
416+
xcontent[i] = null;
417+
}
418+
}
419+
g.idSet[k] = null;
420+
delete g.idSet[k];
405421
}
406422
*/ {}
407423
return;

0 commit comments

Comments
 (0)