aboutsummaryrefslogtreecommitdiff
path: root/src/IvyClient.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/IvyClient.java')
-rwxr-xr-xsrc/IvyClient.java32
1 files changed, 27 insertions, 5 deletions
diff --git a/src/IvyClient.java b/src/IvyClient.java
index b0c1200..af42342 100755
--- a/src/IvyClient.java
+++ b/src/IvyClient.java
@@ -103,10 +103,11 @@ public class IvyClient implements Runnable {
final static char newLineChar = '\n';
// private variables
+ private final static int MAXPONGCALLBACKS = 10;
private static int pingSerial = 0;
private static Integer csMutex=new Integer(0);
private static int clientSerial=0; /* an unique ID for each IvyClient */
- private Stack PCHStack = new Stack();
+ private Hashtable PingCallbacksTable = new Hashtable();
private String messages_classes[] = null;
private Ivy bus;
@@ -246,7 +247,7 @@ public class IvyClient implements Runnable {
* received
*/
public void ping(PingCallback pc) throws IvyException {
- PCHStack.push(new PingCallbackHolder(pc));
+ PCHadd(pingSerial,pc);
sendString(Ping,pingSerial++,"");
}
@@ -488,7 +489,7 @@ public class IvyClient implements Runnable {
}
break;
case Pong:
- ((PingCallbackHolder)PCHStack.pop()).run();
+ PCHget(msgId);
break;
case Ping:
sendString(Pong,msgId.intValue(),"");
@@ -591,16 +592,37 @@ public class IvyClient implements Runnable {
traceDebug(s);
}
+ void PCHadd(int serial,PingCallback pc) {
+ PingCallbacksTable.put(new Integer(serial),new PingCallbackHolder(pc));
+ if (PingCallbacksTable.size()>MAXPONGCALLBACKS) {
+ // more than MAXPONGCALLBACKS callbacks, we ought to limit to prevent a
+ // memory leak
+ // TODO remove the first
+ Integer smallest=(Integer)new TreeSet(PingCallbacksTable.keySet()).first();
+ PingCallbackHolder pch = (PingCallbackHolder)PingCallbacksTable.remove(smallest);
+ System.err.println("no response from "+getApplicationName()+" to ping "+smallest+" after "+pch.age()+" ms, discarding");
+ }
+ }
+
+ void PCHget(Integer serial) {
+ PingCallbackHolder pc = (PingCallbackHolder)PingCallbacksTable.remove(serial);
+ if (pc==null) {
+ System.err.println("warning: pong received for a long lost callback");
+ return;
+ }
+ pc.run();
+ }
+
private class PingCallbackHolder {
PingCallback pc;
long epoch;
+ int age() { return (int)(System.currentTimeMillis()-epoch); }
PingCallbackHolder(PingCallback pc) {
this.pc=pc;
epoch=System.currentTimeMillis();
- PCHStack.push(this);
}
void run() {
- pc.pongReceived(IvyClient.this,(int)(System.currentTimeMillis()-epoch));
+ pc.pongReceived(IvyClient.this,age());
}
}