Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41b67a8

Browse files
committed
Fixed bug that Simple RPC/Pipe depends on java.utils.* classes in
JavaScript Fixed bug on SWT related codes
1 parent ce12102 commit 41b67a8

File tree

10 files changed

+178
-13
lines changed

10 files changed

+178
-13
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package net.sf.j2s.ajax;
22

33
public class CompoundPipeRunnable extends SimplePipeRunnable {
4-
4+
5+
/**
6+
* @j2sNative
7+
var hexStr = "0123456789abcdef";
8+
var key = "";
9+
for (var i = 0; i < 4; i++) {
10+
var hex = Math.round(15 * Math.random());
11+
key += "" + hexStr.charAt(hex);
12+
}
13+
return key;
14+
*/
515
private static String nextSessionKey() {
616
StringBuilder keyBuilder = new StringBuilder(4);
717
for (int i = 0; i < 4; i++) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
import net.sf.j2s.ajax.SimpleSerializable;
2222
import net.sf.j2s.annotation.J2SIgnore;
2323
import net.sf.j2s.annotation.J2SNative;
24+
import net.sf.j2s.annotation.J2SRequireImport;
2425

2526
/**
2627
*
2728
* @author zhou renjian
2829
*/
30+
@J2SRequireImport(SimplePipeSequence.class)
2931
public abstract class SimplePipeRunnable extends SimpleRPCRunnable {
3032

3133
/**
@@ -488,6 +490,11 @@ public SimpleSerializable[] through(Object ... args) {
488490
* Deal the object from pipe.
489491
* @param ss
490492
* @return boolean Whether the object is dealt
493+
* @j2sNative
494+
if ("net.sf.j2s.ajax.SimpleSerializable".equals(ss.getClass().getName())) {
495+
return true; // seldom or never reach this branch, just ignore
496+
}
497+
return false;
491498
*/
492499
public boolean deal(SimpleSerializable ss) {
493500
try {

sources/net.sf.j2s.ajax/ajaxpipeserlvet/net/sf/j2s/ajax/CompoundPipeRPCHttpServlet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package net.sf.j2s.ajax;
22

3+
/**
4+
*
5+
* @author zhourenjian
6+
*
7+
* @j2sIgnore
8+
*/
39
public class CompoundPipeRPCHttpServlet extends SimplePipeRPCHttpServlet {
410

511
private static final long serialVersionUID = 1605715722698968061L;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
/**
2525
*
2626
* @author zhou renjian
27+
*
28+
* @j2sIgnore
2729
*/
2830
public class SimplePipeHttpServlet extends HttpServlet {
2931

sources/net.sf.j2s.ajax/ajaxpipeserlvet/net/sf/j2s/ajax/SimplePipeRPCHttpServlet.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
/**
1818
*
1919
* @author zhou renjian
20+
*
21+
* @j2sIgnore
2022
*/
2123
public class SimplePipeRPCHttpServlet extends SimpleRPCHttpServlet {
2224

sources/net.sf.j2s.ajax/ajaxrpcservlet/net/sf/j2s/ajax/SimpleRPCHttpServlet.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
* @author zhou renjian
3232
*
3333
* 2006-10-10
34+
*
35+
* @j2sIgnore
3436
*/
3537
public class SimpleRPCHttpServlet extends HttpServlet {
3638

sources/net.sf.j2s.ajax/ajaxswt/net/sf/j2s/ajax/SimplePipeSWTRequest.java

Lines changed: 130 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
package net.sf.j2s.ajax;
1212

1313
import java.io.ByteArrayOutputStream;
14-
import java.io.IOException;
1514

1615
import net.sf.j2s.ajax.HttpRequest;
1716
import net.sf.j2s.ajax.SimpleRPCRequest;
@@ -136,10 +135,13 @@ public void run() {
136135
String pipeKey = runnable.pipeKey;
137136
String pipeMethod = runnable.getPipeMethod();
138137
String pipeURL = runnable.getPipeURL();
139-
140-
String pipeRequestData = constructRequest(pipeKey, PIPE_TYPE_NOTIFY, runnable.pipeSequence);
138+
long sequence = runnable.pipeSequence;
139+
String pipeRequestData = constructRequest(pipeKey, PIPE_TYPE_NOTIFY, sequence);
141140
sendRequest(request, pipeMethod, pipeURL, pipeRequestData, false);
142141
String response = request.getResponseText();
142+
if (response != null && runnable.notifySequence < sequence && response.indexOf("$p1p3b$") != 0) {
143+
runnable.notifySequence = sequence;
144+
}
143145
if (response != null && response.indexOf("\"" + PIPE_STATUS_LOST + "\"") != -1) {
144146
SWTHelper.syncExec(disp, new Runnable() {
145147
public void run() {
@@ -150,6 +152,9 @@ public void run() {
150152
SimplePipeHelper.removePipe(pipeKey);
151153
// may need to inform user that connection is already lost!
152154
break;
155+
} else {
156+
runnable.lastLiveDetected = System.currentTimeMillis();
157+
runnable.updateStatus(true);
153158
}
154159
} else {
155160
break;
@@ -288,10 +293,30 @@ static void swtPipeContinuum(final SimplePipeRunnable runnable) {
288293
HttpRequest pipeRequest = getRequestWithMonitor(new HttpRequest.IXHRReceiving() {
289294
public boolean receiving(ByteArrayOutputStream baos, byte b[], int off, int len) {
290295
baos.write(b, off, len);
296+
byte[] bytes = baos.toByteArray();
297+
int resetIndex = 0;
298+
try {
299+
resetIndex = swtParseReceivedBytes(bytes);
300+
} catch (RuntimeException e) { // invalid simple format
301+
int length = bytes.length;
302+
if (length < 100) {
303+
System.out.println("[ERROR]: " + new String(bytes));
304+
} else {
305+
System.out.println("[ERROR]: " + new String(bytes, 0, 100) + " ..");
306+
}
307+
throw e;
308+
}
309+
if (resetIndex > 0) {
310+
baos.reset();
311+
if (resetIndex < bytes.length) {
312+
baos.write(bytes, resetIndex, bytes.length - resetIndex);
313+
}
314+
}
315+
291316
/*
292-
* It is OK to convert to string as SimpleSerialize's
293-
* serialized string contains only ASCII chars.
294-
*/
317+
// It is OK to convert to string as SimpleSerialize's
318+
// serialized string contains only ASCII chars.
319+
// [20151228] ASCII characters only is broken after 2.0.0
295320
String string = baos.toString();
296321
String resetString = swtParseReceived(string);
297322
if (resetString != null && resetString.length() > 0) {
@@ -315,7 +340,10 @@ public void run() {
315340
} catch (IOException e) {
316341
e.printStackTrace();
317342
}
343+
} else if (resetString != null && resetString.length() == 0) {
344+
baos.reset();
318345
}
346+
// */
319347
return true;
320348
}
321349

@@ -370,19 +398,30 @@ static String swtParseReceived(String string) {
370398
}
371399
}
372400
if ((ss = SimpleSerializable.parseInstance(string, end)) == null
401+
|| ss == SimpleSerializable.ERROR
373402
|| !ss.deserialize(string, end)) {
374403
break;
375404
}
376405
String key = string.substring(start, end);
377406
final SimplePipeRunnable runnable = SimplePipeHelper.getPipe(key);
378407
if (runnable != null) { // should be always fulfill this condition
379-
//runnable.deal(ss);
380-
final SimpleSerializable instance = ss;
381-
SWTHelper.syncExec(Display.getDefault(), new Runnable() {
382-
public void run() {
383-
runnable.deal(instance);
408+
runnable.lastPipeDataReceived = System.currentTimeMillis();
409+
if (ss != SimpleSerializable.UNKNOWN) {
410+
if (ss instanceof SimplePipeSequence) {
411+
long sequence = ((SimplePipeSequence) ss).sequence;
412+
if (sequence > runnable.pipeSequence) {
413+
runnable.pipeSequence = sequence;
414+
}
415+
} else {
416+
//runnable.deal(ss);
417+
final SimpleSerializable instance = ss;
418+
SWTHelper.syncExec(Display.getDefault(), new Runnable() {
419+
public void run() {
420+
runnable.deal(instance);
421+
}
422+
});
384423
}
385-
});
424+
}
386425
}
387426

388427
start = restStringIndex(string, start);
@@ -393,4 +432,83 @@ public void run() {
393432
return string;
394433
}
395434

435+
@J2SIgnore
436+
static int swtParseReceivedBytes(final byte[] bytes) {
437+
if (bytes == null) {
438+
return -1;
439+
}
440+
SimpleSerializable ss = null;
441+
int start = 0;
442+
while (bytes.length > start + PIPE_KEY_LENGTH) { // should be bigger than 48 ( 32 + 6 + 1 + 8 + 1)
443+
int end = start + PIPE_KEY_LENGTH;
444+
if (PIPE_STATUS_DESTROYED == bytes[end]) {
445+
final String key = new String(bytes, start, PIPE_KEY_LENGTH);
446+
final SimplePipeRunnable pipe = SimplePipeHelper.getPipe(key);
447+
if (pipe != null) {
448+
if (key.equals(pipe.pipeKey)) {
449+
pipe.pipeAlive = false;
450+
//pipe.pipeClosed();
451+
SWTHelper.syncExec(Display.getDefault(), new Runnable() {
452+
public void run() {
453+
pipe.pipeClosed();
454+
//SimplePipeHelper.removePipe(key);
455+
}
456+
});
457+
}
458+
SimplePipeHelper.removePipe(key);
459+
}
460+
return end + 1;
461+
}
462+
if (PIPE_STATUS_OK == bytes[end]) {
463+
String key = new String(bytes, start, PIPE_KEY_LENGTH);
464+
SimplePipeRunnable runnable = SimplePipeHelper.getPipe(key);
465+
if (runnable != null) { // should always satisfy this condition
466+
runnable.lastPipeDataReceived = System.currentTimeMillis();
467+
}
468+
start = end + 1;
469+
if (start == bytes.length) {
470+
return start;
471+
}
472+
continue;
473+
}
474+
ss = SimpleSerializable.parseInstance(bytes, end);
475+
if (ss == null) {
476+
break;
477+
}
478+
if (ss == SimpleSerializable.ERROR) {
479+
return -1; // error
480+
}
481+
if (!ss.deserializeBytes(bytes, end)) {
482+
break;
483+
}
484+
String key = new String(bytes, start, PIPE_KEY_LENGTH);
485+
final SimplePipeRunnable runnable = SimplePipeHelper.getPipe(key);
486+
if (runnable != null) { // should always satisfy this condition
487+
runnable.lastPipeDataReceived = System.currentTimeMillis();
488+
if (ss != SimpleSerializable.UNKNOWN) {
489+
if (ss instanceof SimplePipeSequence) {
490+
long sequence = ((SimplePipeSequence) ss).sequence;
491+
if (sequence > runnable.pipeSequence) {
492+
runnable.pipeSequence = sequence;
493+
}
494+
} else {
495+
//runnable.deal(ss);
496+
final SimpleSerializable instance = ss;
497+
SWTHelper.syncExec(Display.getDefault(), new Runnable() {
498+
public void run() {
499+
runnable.deal(instance);
500+
}
501+
});
502+
}
503+
}
504+
}
505+
506+
start = restBytesIndex(bytes, start);
507+
}
508+
if (start != 0) {
509+
return start;
510+
}
511+
return 0;
512+
}
513+
396514
}

sources/net.sf.j2s.ajax/generator/net/sf/j2s/ajax/SimpleSource4Java.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
import net.sf.j2s.ajax.annotation.SimpleInOut;
3232
import net.sf.j2s.ajax.annotation.SimpleOut;
3333

34+
/**
35+
*
36+
* @author zhourenjian
37+
*
38+
* @j2sIgnore
39+
*/
3440
public class SimpleSource4Java {
3541

3642
static String folder = "Project";

sources/net.sf.j2s.ajax/generator/net/sf/j2s/ajax/SimpleSource4ObjectiveC.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
import net.sf.j2s.ajax.annotation.SimpleInOut;
3030
import net.sf.j2s.ajax.annotation.SimpleOut;
3131

32+
/**
33+
*
34+
* @author zhourenjian
35+
*
36+
* @j2sIgnore
37+
*/
3238
public class SimpleSource4ObjectiveC {
3339

3440
static String folder = "Project";

sources/net.sf.j2s.ajax/generator/net/sf/j2s/ajax/SourceUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
import java.util.regex.Matcher;
1515
import java.util.regex.Pattern;
1616

17+
/**
18+
*
19+
* @author zhourenjian
20+
*
21+
* @j2sIgnore
22+
*/
1723
public class SourceUtils {
1824

1925
public static void insertLineComment(StringBuilder source, String indent, int index, boolean blankLine) {

0 commit comments

Comments
 (0)