Skip to content

Commit 340d5d9

Browse files
author
zhourenjian
committed
Improve Simple/Compound Pipe's stability.
Add detection of pipe's status. Add priority to pipe data by implementing IPipePriority to identify each SimpleSerializable object. Support multiple Compound Pipe by using #configure("abcdefg", ..., ..., ..., ...); #weave("abcdefg", pipe); instead of #configure(..., ..., ..., ...); #weave(pipe); in which, "abcdefg" is Compound Pipe's id.
1 parent 628d354 commit 340d5d9

File tree

8 files changed

+594
-282
lines changed

8 files changed

+594
-282
lines changed

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

Lines changed: 105 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,11 @@
44

55
public class CompoundPipeRequest extends SimplePipeRequest {
66

7-
static CompoundPipeRunnable pipe;
7+
static CompoundPipeRunnable[] pipes = new CompoundPipeRunnable[3];
88

9-
static int count = 0;
10-
11-
static long lastTried = 0;
12-
13-
static String pipeMethod = "GET";
14-
15-
static String rpcMethod = "POST";
16-
17-
static String pipeURL = "simplepipe";
18-
19-
static String rpcURL = "piperpc";
20-
21-
22-
public static void weave(CompoundPipeSession p) {
23-
if (pipe == null || !pipe.isPipeLive()) {
24-
pipe = new CompoundPipeRunnable() {
25-
26-
@Override
27-
public void ajaxOut() {
28-
super.ajaxOut();
29-
for (int i = 0; i < pipes.length; i++) {
30-
if (pipes[i] != null) {
31-
pipes[i].pipeKey = pipe.pipeKey;
32-
SimpleRPCRequest.request(pipes[i]);
33-
if (pipe.status < 2) {
34-
pipe.status = 2; // requested
35-
}
36-
}
37-
}
38-
}
39-
40-
@Override
41-
public void ajaxFail() {
42-
CompoundPipeRequest.pipeFailed(this);
43-
}
44-
45-
};
9+
public static void weave(String id, CompoundPipeSession p) {
10+
final CompoundPipeRunnable pipe = retrievePipe(id, true);
11+
if (pipe.status == 0 || !pipe.isPipeLive()) {
4612
pipe.weave(p);
4713
pipe.updateStatus(true);
4814
SimplePipeRequest.pipe(pipe);
@@ -61,14 +27,14 @@ public void ajaxFail() {
6127

6228
static void pipeFailed(CompoundPipeRunnable pipe) {
6329
long now = new Date().getTime();
64-
if (now - lastTried > 5 * 60 * 1000) { // five minutes
65-
count = 0;
30+
if (now - pipe.lastSetupRetried > 5 * 60 * 1000) { // five minutes
31+
pipe.setupFailedRetries = 0;
6632
}
67-
count++;
68-
if (count <= 3) {
33+
pipe.setupFailedRetries++;
34+
if (pipe.setupFailedRetries <= 3) {
6935
// take another trys
7036
pipe.updateStatus(true);
71-
lastTried = now;
37+
pipe.lastSetupRetried = now;
7238
SimplePipeRequest.pipe(pipe);
7339
} else {
7440
for (int i = 0; i < pipe.pipes.length; i++) {
@@ -77,22 +43,112 @@ static void pipeFailed(CompoundPipeRunnable pipe) {
7743
}
7844
pipe.pipes[i] = null;
7945
}
80-
pipe = null;
46+
unregisterPipe(pipe.id);
8147
}
8248
}
8349

84-
public static void configure(String pipeURL, String pipeMethod, String rpcURL, String rpcMethod) {
50+
public static void configure(String id, String pipeURL, String pipeMethod,
51+
String rpcURL, String rpcMethod) {
52+
CompoundPipeRunnable cfg = retrievePipe(id, true);
8553
if (pipeURL != null) {
86-
CompoundPipeRequest.pipeURL = pipeURL;
54+
cfg.pipeURL = pipeURL;
8755
}
8856
if (pipeMethod != null) {
89-
CompoundPipeRequest.pipeMethod = pipeMethod;
57+
cfg.pipeMethod = pipeMethod;
9058
}
9159
if (rpcURL != null) {
92-
CompoundPipeRequest.rpcURL = rpcURL;
60+
cfg.rpcURL = rpcURL;
9361
}
9462
if (rpcMethod != null) {
95-
CompoundPipeRequest.rpcMethod = rpcMethod;
63+
cfg.rpcMethod = rpcMethod;
64+
}
65+
}
66+
67+
static CompoundPipeRunnable retrievePipe(String id, boolean createNew) {
68+
CompoundPipeRunnable[] allPipes = pipes;
69+
synchronized (allPipes) {
70+
for (int i = 0; i < allPipes.length; i++) {
71+
if (allPipes[i] != null && allPipes[i].id.equals(id)) {
72+
return allPipes[i];
73+
}
74+
}
75+
if (!createNew) {
76+
return null;
77+
}
78+
79+
CompoundPipeRunnable pipe = createPipe(id);
80+
addPipe(pipe);
81+
return pipe;
82+
}
83+
}
84+
85+
private static CompoundPipeRunnable createPipe(String id) {
86+
CompoundPipeRunnable pipe = new CompoundPipeRunnable() {
87+
88+
@Override
89+
public void ajaxOut() {
90+
super.ajaxOut();
91+
for (int i = 0; i < pipes.length; i++) {
92+
if (pipes[i] != null) {
93+
pipes[i].pipeKey = pipeKey;
94+
SimpleRPCRequest.request(pipes[i]);
95+
if (status < 2) {
96+
status = 2; // requested
97+
}
98+
}
99+
}
100+
}
101+
102+
@Override
103+
public void ajaxFail() {
104+
CompoundPipeRequest.pipeFailed(this);
105+
}
106+
107+
};
108+
109+
pipe.id = id;
110+
return pipe;
111+
}
112+
113+
private static void addPipe(CompoundPipeRunnable pipe) {
114+
CompoundPipeRunnable[] allPipes = pipes;
115+
for (int i = 0; i < allPipes.length; i++) {
116+
if (allPipes[i] == null) {
117+
allPipes[i] = pipe;
118+
return;
119+
}
120+
}
121+
CompoundPipeRunnable[] newPipes = new CompoundPipeRunnable[allPipes.length + 100];
122+
System.arraycopy(allPipes, 0, newPipes, 0, allPipes.length);
123+
newPipes[allPipes.length] = pipe;
124+
pipes = newPipes;
125+
}
126+
127+
static CompoundPipeRunnable registerPipe(CompoundPipeRunnable pipe) {
128+
if (pipe == null) return null;
129+
String id = pipe.id;
130+
CompoundPipeRunnable[] allPipes = pipes;
131+
synchronized (allPipes) {
132+
for (int i = 0; i < allPipes.length; i++) {
133+
if (allPipes[i] != null && allPipes[i].id.equals(id)) {
134+
return allPipes[i];
135+
}
136+
}
137+
addPipe(pipe);
138+
return pipe;
139+
}
140+
}
141+
142+
static CompoundPipeRunnable unregisterPipe(String id) {
143+
CompoundPipeRunnable[] allPipes = pipes;
144+
synchronized (allPipes) {
145+
for (int i = 0; i < allPipes.length; i++) {
146+
if (allPipes[i] != null && allPipes[i].id.equals(id)) {
147+
allPipes[i] = null;
148+
return allPipes[i];
149+
}
150+
}
151+
return null;
96152
}
97153
}
98154
}

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,24 @@ private static String nextSessionKey() {
1616

1717
int status;
1818

19+
String id; // id for CompoundPipeRequest
20+
String pipeMethod;
21+
String rpcMethod;
22+
String pipeURL;
23+
String rpcURL;
24+
25+
int setupFailedRetries;
26+
long lastSetupRetried;
27+
1928
public CompoundPipeRunnable() {
2029
pipes = new CompoundPipeSession[100];
2130
status = 0; // starting
31+
setupFailedRetries = 0;
32+
lastSetupRetried = 0;
33+
pipeMethod = "GET";
34+
rpcMethod = "POST";
35+
pipeURL = "simplepipe";
36+
rpcURL = "piperpc";
2237
}
2338

2439
protected CompoundPipeSession getSession(String session) {
@@ -121,6 +136,7 @@ public void weave(CompoundPipeSession pipe) {
121136
newPipes[pipes.length] = pipe;
122137
}
123138
}
139+
pipe.parent = this;
124140
while (pipe.session == null) {
125141
String key = nextSessionKey();
126142
boolean isKeyOK = true;
@@ -188,22 +204,22 @@ public boolean deal(SimpleSerializable ss) {
188204

189205
@Override
190206
public String getHttpURL() {
191-
return CompoundPipeRequest.rpcURL;
207+
return rpcURL;
192208
}
193209

194210
@Override
195211
public String getHttpMethod() {
196-
return CompoundPipeRequest.rpcMethod;
212+
return rpcMethod;
197213
}
198214

199215
@Override
200216
public String getPipeURL() {
201-
return CompoundPipeRequest.pipeURL;
217+
return pipeURL;
202218
}
203219

204220
@Override
205221
public String getPipeMethod() {
206-
return CompoundPipeRequest.pipeMethod;
222+
return pipeMethod;
207223
}
208224

209225
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ private static class PipeSessionClosedEvent extends CompoundSerializable {
88

99
public String session;
1010

11+
CompoundPipeRunnable parent;
12+
1113
@Override
1214
public void ajaxRun() {
1315
SimplePipeRunnable pipe = SimplePipeHelper.getPipe(pipeKey);
@@ -129,22 +131,22 @@ public boolean deal(PipeSessionClosedEvent evt) {
129131

130132
@Override
131133
public String getHttpURL() {
132-
return CompoundPipeRequest.rpcURL;
134+
return parent.getHttpURL();
133135
}
134136

135137
@Override
136138
public String getHttpMethod() {
137-
return CompoundPipeRequest.rpcMethod;
139+
return parent.getHttpMethod();
138140
}
139141

140142
@Override
141143
public String getPipeURL() {
142-
return CompoundPipeRequest.pipeURL;
144+
return parent.getPipeURL();
143145
}
144146

145147
@Override
146148
public String getPipeMethod() {
147-
return CompoundPipeRequest.pipeMethod;
149+
return parent.getHttpMethod();
148150
}
149151

150152
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009 java2script.org and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Zhou Renjian - initial API and implementation
10+
*******************************************************************************/
11+
12+
package net.sf.j2s.ajax;
13+
14+
/**
15+
*
16+
* @author Zhou Renjian (http://zhourenjian.com)
17+
*
18+
* Feb 26, 2009
19+
*/
20+
public interface ISimplePipePriority {
21+
22+
public final int IMPORTANT = 32;
23+
24+
public final int NORMAL = 8;
25+
26+
public final int TRIVIAL = 1;
27+
28+
public int getPriority();
29+
30+
}

0 commit comments

Comments
 (0)