Skip to content

Commit ce0fbe0

Browse files
author
zhourenjian@gmail.com
committed
Pipe data is allocated in pipe object itself.
Support more protected pipe API to control pipe data
1 parent 46dea78 commit ce0fbe0

4 files changed

Lines changed: 152 additions & 48 deletions

File tree

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

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public static interface IPipeClosing {
4242
@J2SIgnore
4343
public static int MAX_ITEMS_PER_QUERY = 100;
4444

45-
@J2SIgnore
46-
private static Map<String, List<SimpleSerializable>> pipeMap = null;
45+
// @J2SIgnore
46+
// private static Map<String, List<SimpleSerializable>> pipeMap = null;
4747

4848
@J2SIgnore
4949
private static boolean monitored = false;
@@ -98,15 +98,18 @@ synchronized static String registerPipe(SimplePipeRunnable pipe) {
9898
}
9999
pipes.put(key, pipe);
100100

101-
if (pipeMap == null) {
102-
pipeMap = Collections.synchronizedMap(new HashMap<String, List<SimpleSerializable>>());
103-
}
104-
List<SimpleSerializable> list = pipeMap.get(key);
105-
if (list == null) {
106-
list = new Vector<SimpleSerializable>();
107-
pipeMap.put(key, list);
108-
}
101+
// if (pipeMap == null) {
102+
// pipeMap = Collections.synchronizedMap(new HashMap<String, List<SimpleSerializable>>());
103+
// }
104+
// List<SimpleSerializable> list = pipeMap.get(key);
105+
// if (list == null) {
106+
// list = new Vector<SimpleSerializable>();
107+
// pipeMap.put(key, list);
108+
// }
109109

110+
if (pipe.pipeData == null) {
111+
pipe.pipeData = new Vector<SimpleSerializable>();
112+
}
110113
return key;
111114
}
112115

@@ -142,15 +145,19 @@ public static void removePipe(String key) {
142145
pipe = pipes.remove(key);
143146
if (pipe != null) {
144147
pipe.pipeAlive = false;
145-
}
146-
}
147-
if (pipeMap != null) {
148-
if (pipeMap.remove(key) != null && pipe != null) {
148+
pipe.pipeClearData();
149149
synchronized (pipe) {
150150
pipe.notifyAll();
151151
}
152152
}
153153
}
154+
// if (pipeMap != null) {
155+
// if (pipeMap.remove(key) != null && pipe != null) {
156+
// synchronized (pipe) {
157+
// pipe.notifyAll();
158+
// }
159+
// }
160+
// }
154161
}
155162

156163
@J2SNative({
@@ -177,18 +184,18 @@ public static boolean isPipeHashOK(String key, long hash) {
177184
return true;
178185
}
179186

180-
@J2SIgnore
181-
public static List<SimpleSerializable> getPipeDataList(String key) {
182-
if (pipeMap == null) {
183-
return null;
184-
}
185-
return pipeMap.get(key);
186-
}
187+
// @J2SIgnore
188+
// public static List<SimpleSerializable> getPipeDataList(String key) {
189+
// if (pipeMap == null) {
190+
// return null;
191+
// }
192+
// return pipeMap.get(key);
193+
// }
187194

188195
@J2SIgnore
189196
public static void pipeIn(String key, SimpleSerializable[] ss) {
190197
SimplePipeRunnable pipe = getPipe(key);
191-
List<SimpleSerializable> list = getPipeDataList(key);
198+
List<SimpleSerializable> list = pipe != null ? pipe.pipeData : null; //getPipeDataList(key);
192199
if (pipe == null || list == null) {
193200
System.out.println("There are no pipe listening?!!!!");
194201
return; // throw exception?
@@ -287,12 +294,12 @@ public static String printStatistics2() {
287294
buffer.append("Pipe monitor<br />\r\n");
288295
if (pipes != null) {
289296
buffer.append("Totoal pipe count: " + pipes.size() + "<br />\r\n");
290-
buffer.append("Totoal pipe map count: " + pipeMap.size() + "<br />\r\n");
291-
Object[] keys = pipeMap.keySet().toArray();
297+
// buffer.append("Totoal pipe map count: " + pipeMap.size() + "<br />\r\n");
298+
Object[] keys = pipes.keySet().toArray();
292299
for (int i = 0; i < keys.length; i++) {
293300
String key = (String) keys[i];
294-
List<SimpleSerializable> list = pipeMap.get(key);
295301
SimplePipeRunnable p = pipes.get(key);
302+
List<SimpleSerializable> list = p != null ? p.pipeData : null; //pipeMap.get(key);
296303
if (p instanceof CompoundPipeRunnable) {
297304
CompoundPipeRunnable cp = (CompoundPipeRunnable) p;
298305
int activeCount = 0;
@@ -326,12 +333,12 @@ public static String printStatistics() {
326333
buffer.append("Pipe monitor<br />\r\n");
327334
if (pipes != null) {
328335
buffer.append("Totoal pipe count: " + pipes.size() + "<br />\r\n");
329-
buffer.append("Totoal pipe map count: " + pipeMap.size() + "<br />\r\n");
330-
Object[] keys = pipeMap.keySet().toArray();
336+
// buffer.append("Totoal pipe map count: " + pipeMap.size() + "<br />\r\n");
337+
Object[] keys = pipes.keySet().toArray();
331338
for (int i = 0; i < keys.length; i++) {
332339
String key = (String) keys[i];
333-
List<SimpleSerializable> list = pipeMap.get(key);
334340
SimplePipeRunnable p = pipes.get(key);
341+
List<SimpleSerializable> list = p != null ? p.pipeData : null; //pipeMap.get(key);
335342
if (p instanceof CompoundPipeRunnable) {
336343
CompoundPipeRunnable cp = (CompoundPipeRunnable) p;
337344
buffer.append(i + "Pipe " + cp.pipeKey + " status=" + cp.status + " pipeAlive=" + cp.isPipeLive() + " created=" + new Date(cp.lastSetup) + "<br />\r\n");

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,8 @@ protected void doPipe(final HttpServletResponse resp, String key, String type, S
227227
List<SimpleSerializable> list = null;
228228
int priority = 0;
229229
long lastLiveDetected = System.currentTimeMillis();
230-
SimplePipeRunnable pipe = SimplePipeHelper.getPipe(key);
231-
long waitClosingInterval = pipe == null ? 5000 : pipe.pipeWaitClosingInterval();
232-
while ((list = SimplePipeHelper.getPipeDataList(key)) != null
230+
SimplePipeRunnable pipe = null;
231+
while ((pipe = SimplePipeHelper.getPipe(key)) != null && (list = pipe.getPipeData()) != null
233232
/* && SimplePipeHelper.isPipeLive(key) */ // check it!
234233
&& !writer.checkError()) {
235234
StringBuffer buffer = new StringBuffer();
@@ -270,6 +269,7 @@ protected void doPipe(final HttpServletResponse resp, String key, String type, S
270269
}
271270
writer.flush();
272271
if (!SimplePipeHelper.isPipeLive(key)) {
272+
long waitClosingInterval = pipe == null ? 5000 : pipe.pipeWaitClosingInterval();
273273
if (System.currentTimeMillis() - lastLiveDetected > waitClosingInterval) {
274274
// break out while loop so pipe connection will be closed
275275
break;
@@ -296,7 +296,7 @@ protected void doPipe(final HttpServletResponse resp, String key, String type, S
296296
}
297297

298298
now = System.currentTimeMillis();
299-
if (SimplePipeHelper.getPipeDataList(key) != null // may be broken down already!!
299+
if (pipe != null && pipe.getPipeData()/*SimplePipeHelper.getPipeDataList(key)*/ != null // may be broken down already!!
300300
&& (pipeMaxItemsPerQuery <= 0 || items < pipeMaxItemsPerQuery || isContinuum)
301301
&& (isContinuum || (isScripting && now - beforeLoop < pipeScriptBreakout)
302302
|| (priority < ISimplePipePriority.IMPORTANT && now - beforeLoop < pipeQueryTimeout))) {
@@ -312,8 +312,9 @@ protected void doPipe(final HttpServletResponse resp, String key, String type, S
312312
}
313313
} // end of while
314314
} // else pips is already closed or in other statuses
315-
if (SimplePipeHelper.getPipeDataList(key) == null
316-
|| !SimplePipeHelper.isPipeLive(key)) { // pipe is tore down!
315+
SimplePipeRunnable pipe = SimplePipeHelper.getPipe(key);
316+
if (pipe == null || pipe.getPipeData() /*SimplePipeHelper.getPipeDataList(key)*/ == null
317+
|| !pipe.isPipeLive() /*!SimplePipeHelper.isPipeLive(key)*/) { // pipe is tore down!
317318
//SimplePipeHelper.notifyPipeStatus(key, false); // Leave for pipe monitor to destroy it
318319
SimplePipeHelper.removePipe(key);
319320
try {

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ public void run() {
213213
@J2SIgnore
214214
static void keepPipeLive(final SimplePipeRunnable runnable) {
215215
runnable.updateStatus(true);
216-
/*
216+
if (getRequstMode() != MODE_LOCAL_JAVA_THREAD) {
217+
return;
218+
}
219+
//*
217220
ThreadUtils.runTask(new Runnable() {
218221

219222
public void run() {
@@ -784,8 +787,10 @@ protected IXHRReceiving initializeReceivingMonitor() {
784787
last = created;
785788
}
786789
if (now - last > 3 * spr.pipeLiveNotifyInterval) {
787-
runnable.pipeAlive = false;
788-
runnable.pipeClosed();
790+
if (key == runnable.pipeKey) {
791+
runnable.pipeAlive = false;
792+
runnable.pipeClosed();
793+
}
789794
sph.removePipe(key);
790795
spr.pipeIFrameClean (key);
791796
} else {
@@ -798,6 +803,7 @@ protected IXHRReceiving initializeReceivingMonitor() {
798803
window.setTimeout (fun, spr.pipeLiveNotifyInterval);
799804
*/
800805
static void pipeContinuum(final SimplePipeRunnable runnable) {
806+
final String pipeKey = runnable.pipeKey;
801807
HttpRequest pipeRequest = getRequestWithMonitor(new HttpRequest.IXHRReceiving() {
802808
public boolean receiving(ByteArrayOutputStream baos, byte b[], int off, int len) {
803809
runnable.updateStatus(true);
@@ -836,17 +842,19 @@ public void onReceiving() {
836842

837843
@Override
838844
public void onLoaded() { // on case that no destroy event is sent to client
839-
String pipeKey = runnable.pipeKey;
840-
if (SimplePipeHelper.getPipe(pipeKey) != null) {
845+
String key = runnable.pipeKey;
846+
if (pipeKey != null && pipeKey.equals(key)) {
841847
runnable.pipeClosed(); // may set runnable.pipeKey = null;
848+
SimplePipeHelper.removePipe(key);
849+
} else { // broken, reconnected and already assigned another pipe key
850+
// remove old pipe from pipe pool
842851
SimplePipeHelper.removePipe(pipeKey);
843852
}
844853
}
845854

846855
});
847856
pipeRequest.setCometConnection(true);
848857

849-
String pipeKey = runnable.pipeKey;
850858
String pipeMethod = runnable.getPipeMethod();
851859
String pipeURL = runnable.getPipeURL();
852860

@@ -922,8 +930,10 @@ public static String parseReceived(final String string) {
922930
String key = string.substring(start, end);
923931
SimplePipeRunnable pipe = SimplePipeHelper.getPipe(key);
924932
if (pipe != null) {
925-
pipe.pipeAlive = false;
926-
pipe.pipeClosed();
933+
if (key.equals(pipe.pipeKey)) {
934+
pipe.pipeAlive = false;
935+
pipe.pipeClosed();
936+
}
927937
SimplePipeHelper.removePipe(key);
928938
}
929939
return string.substring(end + destroyedKey.length());
@@ -1115,8 +1125,10 @@ public void run() {
11151125
runnable.received = runnable.lastPipeDataReceived;
11161126
if (runnable.queryFailedRetries >= 3
11171127
|| now - last > 3 * spr.pipeLiveNotifyInterval) {
1118-
runnable.pipeAlive = false;
1119-
runnable.pipeClosed();
1128+
if (key == runnable.pipeKey) {
1129+
runnable.pipeAlive = false;
1130+
runnable.pipeClosed();
1131+
}
11201132
sph.removePipe(key);
11211133
spr.pipeIFrameClean (key);
11221134
} else {
@@ -1142,8 +1154,10 @@ public void run() {
11421154
}
11431155
if (runnable.queryFailedRetries >= 3
11441156
|| now - last > 3 * pipeLiveNotifyInterval) {
1145-
runnable.pipeAlive = false;
1146-
runnable.pipeClosed();
1157+
if (key.equals(runnable.pipeKey)) {
1158+
runnable.pipeAlive = false;
1159+
runnable.pipeClosed();
1160+
}
11471161
SimplePipeHelper.removePipe(key);
11481162
return;
11491163
}
@@ -1374,8 +1388,10 @@ public void run() {
13741388
document.domain = p.parentDomain;
13751389
var key = p.key;
13761390
with (window.parent) {
1377-
runnable.pipeAlive = false;
1378-
runnable.pipeClosed ();
1391+
if (runnable.pipeKey == key) {
1392+
runnable.pipeAlive = false;
1393+
runnable.pipeClosed ();
1394+
}
13791395
net.sf.j2s.ajax.SimplePipeHelper.removePipe (key);
13801396
net.sf.j2s.ajax.SimplePipeRequest.pipeIFrameClean (key);
13811397
}

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

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

1313
import java.lang.reflect.Method;
14+
import java.util.Iterator;
15+
import java.util.List;
16+
import java.util.Vector;
1417

1518
import net.sf.j2s.ajax.SimpleRPCRunnable;
1619
import net.sf.j2s.ajax.SimpleSerializable;
@@ -52,6 +55,14 @@ public abstract class SimplePipeRunnable extends SimpleRPCRunnable {
5255
@J2SIgnore
5356
long lastHash;
5457

58+
@J2SIgnore
59+
List<SimpleSerializable> pipeData;
60+
61+
@J2SIgnore
62+
public List<SimpleSerializable> getPipeData() {
63+
return pipeData;
64+
}
65+
5566
@J2SIgnore
5667
public void setPipeHelper(SimplePipeHelper.IPipeThrough helper) {
5768
pipeManaged = true;
@@ -109,6 +120,74 @@ public void ajaxOut() {
109120
*/
110121
public abstract boolean pipeSetup();
111122

123+
/**
124+
* Clear existed pipe data, if any.
125+
*
126+
* For server side only.
127+
*/
128+
@J2SIgnore
129+
protected void pipeClearData() {
130+
if (pipeData != null) {
131+
pipeData = null;
132+
}
133+
}
134+
135+
/**
136+
* Check if there is any pipe data left.
137+
* @return
138+
*/
139+
@J2SIgnore
140+
protected boolean hasPipeData() {
141+
if (pipeData == null) {
142+
return false;
143+
}
144+
return !pipeData.isEmpty();
145+
}
146+
147+
@J2SIgnore
148+
protected void pipeCloneData(SimplePipeRunnable anotherPipe, SimpleFilter filter) {
149+
if (anotherPipe == this) {
150+
return;
151+
}
152+
if (anotherPipe != null && anotherPipe.pipeData != null && anotherPipe.pipeData.size() > 0) {
153+
if (pipeData == null) {
154+
List<SimpleSerializable> data = new Vector<SimpleSerializable>(anotherPipe.pipeData.size());
155+
synchronized (anotherPipe.pipeData) {
156+
if (filter == null) {
157+
data.addAll(anotherPipe.pipeData);
158+
} else {
159+
for (Iterator<SimpleSerializable> itr = anotherPipe.pipeData.iterator(); itr.hasNext();) {
160+
SimpleSerializable event = (SimpleSerializable) itr.next();
161+
if (filter.accept(event.getClass().getName())) {
162+
data.add(event);
163+
}
164+
}
165+
}
166+
}
167+
pipeData = data;
168+
} else {
169+
synchronized (pipeData) {
170+
synchronized (anotherPipe.pipeData) {
171+
if (filter == null) {
172+
pipeData.addAll(anotherPipe.pipeData);
173+
} else {
174+
for (Iterator<SimpleSerializable> itr = anotherPipe.pipeData.iterator(); itr.hasNext();) {
175+
SimpleSerializable event = (SimpleSerializable) itr.next();
176+
if (filter.accept(event.getClass().getName())) {
177+
pipeData.add(event);
178+
}
179+
}
180+
}
181+
anotherPipe.pipeData.clear();
182+
}
183+
}
184+
}
185+
synchronized (this) {
186+
this.notifyAll();
187+
}
188+
}
189+
}
190+
112191
/**
113192
* Destroy the pipe and remove listeners.
114193
* After pipe is destroyed, {@link #isPipeLive()} must be false
@@ -123,6 +202,7 @@ public boolean pipeDestroy() {
123202
SimplePipeHelper.removePipe(pipeKey);
124203
pipeKey = null;
125204
}
205+
pipeClearData();
126206
return true;
127207
}
128208

0 commit comments

Comments
 (0)