aboutsummaryrefslogtreecommitdiff
path: root/src/IvyClient.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/IvyClient.java')
-rwxr-xr-xsrc/IvyClient.java34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/IvyClient.java b/src/IvyClient.java
index 85edfc8..5e71ccb 100755
--- a/src/IvyClient.java
+++ b/src/IvyClient.java
@@ -10,6 +10,8 @@
* created for each remote client.
*
* CHANGELOG:
+ * 1.2.12
+ * - Ping and Pong are back ...
* 1.2.8
* - no CheckRegexp anymore
* - synchronized(regexps) pour le match et le getParen():
@@ -90,6 +92,8 @@ public class IvyClient implements Runnable {
final static int SchizoToken = 6; /* avoid race condition in concurrent connexions, aka BeginRegexp in other implementations */
final static int DirectMsg = 7;/* the peer sends a direct message */
final static int Die = 8; /* the peer wants us to quit */
+ final static int Ping = 9; // from outer space
+ final static int Pong = 10;
final static String MESSAGE_TERMINATOR = "\n"; /* the next protocol will use \r */
final static String StartArg = "\u0002";/* begin of arguments */
final static String EndArg = "\u0003"; /* end of arguments */
@@ -101,6 +105,7 @@ public class IvyClient implements Runnable {
// private variables
private static Integer csMutex=new Integer(0);
private static int clientSerial=0; /* an unique ID for each IvyClient */
+ private Stack PCHStack = new Stack();
private String messages_classes[] = null;
private Ivy bus;
@@ -234,6 +239,16 @@ public class IvyClient implements Runnable {
sendString(Die,0,message);
}
+ /**
+ * triggers a Ping, and executes the callback
+ * @param pc the callback that will be triggerred (once) when the ponc is
+ * received
+ */
+ public void ping(PingCallback pc) throws IvyException {
+ PCHStack.push(new PingCallbackHolder(pc));
+ sendString(Ping,0,"");
+ }
+
///////////////////////////////////////////////////
//
// PROTECTED METHODS
@@ -471,6 +486,12 @@ public class IvyClient implements Runnable {
throw new IvyException(ioe.getMessage());
}
break;
+ case Pong:
+ ((PingCallbackHolder)PCHStack.pop()).run();
+ break;
+ case Ping:
+ sendString(Pong,0,"");
+ break;
case AddRegexp:
String regexp=s.substring(from,b.length);
if ( bus.CheckRegexp(regexp) ) {
@@ -569,4 +590,17 @@ public class IvyClient implements Runnable {
traceDebug(s);
}
+ private class PingCallbackHolder {
+ PingCallback pc;
+ long epoch;
+ PingCallbackHolder(PingCallback pc) {
+ this.pc=pc;
+ epoch=System.currentTimeMillis();
+ PCHStack.push(this);
+ }
+ void run() {
+ pc.pongReceived(IvyClient.this,(int)(System.currentTimeMillis()-epoch));
+ }
+ }
+
}