aboutsummaryrefslogtreecommitdiff
path: root/src/IvyClient.java
diff options
context:
space:
mode:
authorjestin2004-07-27 16:21:38 +0000
committerjestin2004-07-27 16:21:38 +0000
commitfd9c3e8ea4c7093bff92ec2162ca0be338024fa2 (patch)
treee4f6ea91828a3780310f0381cebf17f20ab9d705 /src/IvyClient.java
parent9ab98e8b79688821f85649fa3a04137f83467b73 (diff)
downloadivy-java-fd9c3e8ea4c7093bff92ec2162ca0be338024fa2.zip
ivy-java-fd9c3e8ea4c7093bff92ec2162ca0be338024fa2.tar.gz
ivy-java-fd9c3e8ea4c7093bff92ec2162ca0be338024fa2.tar.bz2
ivy-java-fd9c3e8ea4c7093bff92ec2162ca0be338024fa2.tar.xz
see upstream Changelog and each file's header for detail ...
Diffstat (limited to 'src/IvyClient.java')
-rwxr-xr-xsrc/IvyClient.java120
1 files changed, 53 insertions, 67 deletions
diff --git a/src/IvyClient.java b/src/IvyClient.java
index b69c8d3..d3deb8b 100755
--- a/src/IvyClient.java
+++ b/src/IvyClient.java
@@ -1,5 +1,5 @@
/**
- * A private Class for the the peers on the bus.
+ * the peers on the bus.
*
* @author Yannick Jestin
* @author <a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a>
@@ -10,6 +10,14 @@
* created for each remote client.
*
* CHANGELOG:
+ * 1.2.6
+ * - now sends back an error message when an incorrect regexp is sent
+ * the message is supposed to be readable
+ * 1.2.5:
+ * - no more java ping ...
+ * 1.2.5:
+ * - use org.apache.regexp instead of gnu-regexp
+ * http://jakarta.apache.org/regexp/apidocs/
* 1.2.4:
* - sendBuffer goes synchronized
* - sendMsg now has a async parameter, allowing the use of threads to
@@ -50,7 +58,8 @@ import java.lang.Thread;
import java.net.*;
import java.io.*;
import java.util.*;
-import gnu.regexp.*;
+/* import gnu.regexp.*; GNURETOAPACHERE */
+import org.apache.regexp.*;
public class IvyClient implements Runnable {
@@ -62,11 +71,9 @@ public class IvyClient implements Runnable {
final static int DelRegexp = 4;/* the peer removes one of his regex */
final static int EndRegexp = 5;/* no more regexp in the handshake */
final static int SchizoToken = 6; /* avoid race condition in concurrent connexions */
- /* SchizoToken is aka BeginRegexp in other implementations */
+ /* SchizoToken is 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; /* checks the presence of the other */
- final static int Pong = 10; /* checks the presence of the other */
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 */
@@ -85,10 +92,6 @@ public class IvyClient implements Runnable {
private boolean peerCalling;
private volatile Thread clientThread;// volatile to ensure the quick communication
private Integer clientKey ;
- private static boolean doping = (System.getProperty("IVY_PING")!=null) ;
- private final static int PINGTIMEOUT = 5000;
- private PINGER pinger;
- private volatile Thread pingerThread;
private boolean discCallbackPerformed = false;
// protected variables
@@ -115,22 +118,17 @@ public class IvyClient implements Runnable {
// the ID is the couple "host name,application Port", the host name
// information is in the socket itself, the port is not known if we
// initiate the connexion
- send(SchizoToken,bus.applicationPort,bus.appName);
+ sendString(SchizoToken,bus.applicationPort,bus.appName);
// sends our regexps to the peer
for (Enumeration e = tosend.keys(); e.hasMoreElements(); ) {
Integer ikey = (Integer)e.nextElement();
sendRegexp(ikey.intValue(),(String)tosend.get(ikey));
}
- send( EndRegexp,0,"");
+ sendString( EndRegexp,0,"");
// spawns a thread to manage the incoming traffic on this
// socket. We should be ready to receive messages now.
clientThread = new Thread(this);
- clientThread .start();
- if (doping) {
- pinger = new PINGER();
- pingerThread=new Thread(pinger);
- pingerThread.start();
- }
+ clientThread.start();
}
public String toString() { return "IvyClient "+bus.appName+":"+appName; }
@@ -147,7 +145,7 @@ public class IvyClient implements Runnable {
* allow the notification of regexp addition and deletion
* The content is not modifyable because String are not mutable, and cannot
* be modified once they are create.
- * @see getRegexpsArray to get a String[] result
+ * @see IvyClient#getRegexpsArray() getRegexpsArray to get a String[] result
*/
public Enumeration getRegexps() { return regexpsText.elements(); }
@@ -172,14 +170,13 @@ public class IvyClient implements Runnable {
public void sendDirectMsg(int id,String message) throws IvyException {
if ( (message.indexOf(IvyClient.newLineChar)!=-1)||(message.indexOf(IvyClient.endArgChar)!=-1))
throw new IvyException("newline character not allowed in Ivy messages");
- send(DirectMsg,id,message);
+ sendString(DirectMsg,id,message);
}
/* closes the connexion to the peer */
protected void close(boolean notify) throws IOException {
bus.waitForAll();
traceDebug("closing connexion to "+appName);
- if (doping&&(pinger!=null)) { pinger.stopPinging(); }
if (notify) sendBye("hasta la vista");
stopListening();
socket.close();
@@ -189,7 +186,7 @@ public class IvyClient implements Runnable {
* asks the remote client to leave the bus.
* @param message the message that will be carried
*/
- public void sendDie(String message) { send(Die,0,message); }
+ public void sendDie(String message) { sendString(Die,0,message); }
/**
* checks the "validity" of a regular expression.
@@ -213,12 +210,13 @@ public class IvyClient implements Runnable {
// PROTECTED METHODS
//
///////////////////////////////////////////////////
+
static String decode(String s) { return s.replace(escapeChar,'\n'); }
static String encode(String s) { return s.replace('\n',escapeChar); }
Integer getClientKey() { return clientKey ; }
int getAppPort() { return appPort ; }
- void sendRegexp(int id,String regexp) {send(AddRegexp,id,regexp);}
- void delRegexp(int id) {send( DelRegexp,id,"");}
+ void sendRegexp(int id,String regexp) {sendString(AddRegexp,id,regexp);}
+ void delRegexp(int id) {sendString(DelRegexp,id,"");}
int sendMsg(String message,boolean async) {
if (async) {
@@ -232,11 +230,18 @@ public class IvyClient implements Runnable {
for (Enumeration e = regexps.keys();e.hasMoreElements();) {
Integer key = (Integer)e.nextElement();
RE regexp = (RE)regexps.get(key);
+ /*
+ * GNURETOAPACHERE
int nb = regexp.getNumSubs();
REMatch result = regexp.getMatch(message);
if (result==null) continue; // no match
count++; // match
send(Msg,key,regexp.getNumSubs(),result);
+ *
+ */
+ if (!regexp.match(message)) continue; // no match
+ count++; // match
+ sendResult(Msg,key,regexp);
}
return count;
}
@@ -280,7 +285,6 @@ public class IvyClient implements Runnable {
try {
if ((msg=in.readLine()) != null ) {
if (clientThread!=thisThread) break; // early stop during readLine()
- if (doping && (pingerThread!=null)) pingerThread.interrupt();
if (!newParseMsg(msg)) {
close(true);
break;
@@ -290,6 +294,7 @@ public class IvyClient implements Runnable {
break;
}
} catch (IvyException ie) {
+ traceDebug("IvyClient caught an exception");
ie.printStackTrace();
} catch (InterruptedIOException ioe) {
traceDebug("I have been interrupted. I'm about to leave my thread loop");
@@ -326,7 +331,7 @@ public class IvyClient implements Runnable {
}
}
- private void send(int type, int id, String arg) {
+ private void sendString(int type, int id, String arg) {
try {
sendBuffer(type+" "+id+StartArg+arg);
} catch (IvyException ie ) {
@@ -335,10 +340,16 @@ public class IvyClient implements Runnable {
}
}
+ /* GNURETOAPACHERE
private void send(int type, Integer id, int nbsub, REMatch result) {
+ */
+
+ private void sendResult(int type,Integer id, RE regexp) {
String buffer = type+" "+id+StartArg;
+ /*
for(int sub = 1; sub <= nbsub; sub++) if (result.getStartIndex(sub) > -1)
- buffer += result.toString(sub)+EndArg;
+ */
+ for(int i=1;i<regexp.getParenCount();i++) buffer+=regexp.getParen(i)+EndArg;
try {
sendBuffer(buffer);
} catch (IvyException ie ) {
@@ -375,25 +386,25 @@ public class IvyClient implements Runnable {
while ((to<b.length)&&(b[to]!=' ')) to++;
// return false au lieu de throw
if (to>=b.length) {
- System.out.println("protocol error from "+appName);
+ System.out.println("Ivy protocol error from "+appName);
return false;
}
try {
msgType = Integer.parseInt(s.substring(from,to));
} catch (NumberFormatException nfe) {
- System.out.println("protocol error on msgType from "+appName);
+ System.out.println("Ivy protocol error on msgType from "+appName);
return false;
}
from=to+1;
while ((to<b.length)&&(b[to]!=2)) to++;
if (to>=b.length) {
- System.out.println("protocol error from "+appName);
+ System.out.println("Ivy protocol error from "+appName);
return false;
}
try {
msgId = new Integer(s.substring(from,to));
} catch (NumberFormatException nfe) {
- System.out.println("protocol error from "+appName+" "+s.substring(from,to)+" is not a number");
+ System.out.println("Ivy protocol error from "+appName+" "+s.substring(from,to)+" is not a number");
return false;
}
from=to+1;
@@ -434,10 +445,10 @@ public class IvyClient implements Runnable {
regexps.put(msgId,new RE(regexp));
regexpsText.put(msgId,regexp);
bus.regexpReceived(this,msgId.intValue(),regexp);
- } catch (REException e) {
+ } catch (RESyntaxException e) {
// the remote client sent an invalid regexp !
- System.out.println("invalid regexp sent by " +appName+" ("+regexp+"), I will ignore this regexp");
- return true;
+ traceDebug("invalid regexp sent by " +appName+" ("+regexp+"), I will ignore this regexp");
+ sendBuffer(Error+e.toString());
}
} else {
throw new IvyException("regexp Warning exp='"+regexp+"' can't match removing from "+appName);
@@ -467,23 +478,11 @@ public class IvyClient implements Runnable {
}
}
String[] tab = new String[v.size()];
- for (int i=0;i<v.size();i++) {
- tab[i]=(String)v.elementAt(i);
- // for developpemnt purposes
- // System.out.println(" *"+tab[i]+"* "+(tab[i]).length());
- }
+ for (int i=0;i<v.size();i++) tab[i]=(String)v.elementAt(i);
+ // for developpemnt purposes
+ traceDebug(tab);
bus.selfIvyClient.callCallback(this,msgId,tab);
break;
- case Pong:
- String paramPong=s.substring(from,b.length);
- traceDebug("Ping msg from "+appName+" : "+paramPong);
- break;
- case Ping:
- // I receive a ping. I can answer a pong.
- String param=s.substring(from,b.length);
- traceDebug("Ping msg from "+appName+" : "+param);
- sendPong(param);
- break;
case Error:
String error=s.substring(from,b.length);
traceDebug("Error msg "+msgId+" "+error);
@@ -511,10 +510,8 @@ public class IvyClient implements Runnable {
return true;
}
- protected void sendPong(String s) {send(Pong,0,s);}
- protected void sendPing(String s) {send(Ping,0,s);}
- private void sendBye() {send(Bye,0,"");}
- private void sendBye(String message) {send(Bye,0,message);}
+ private void sendBye() {sendString(Bye,0,"");}
+ private void sendBye(String message) {sendString(Bye,0,message);}
private InetAddress getRemoteAddress() { return socket.getInetAddress(); }
@@ -522,21 +519,10 @@ public class IvyClient implements Runnable {
if (debug) System.out.println("-->IvyClient "+bus.appName+":"+appName+"<-- "+s);
}
- class PINGER implements Runnable {
- boolean isPinging = false;
- public void run() {
- isPinging=true;
- traceDebug("Pinger Thread started");
- while (isPinging) {
- try {
- Thread.sleep(PINGTIMEOUT);
- sendPing("are you here ?");
- } catch (InterruptedException ie) {
- }
- }
- traceDebug("Pinger Thread stopped");
- }
- public void stopPinging() { isPinging=false; pingerThread.interrupt();}
+ private void traceDebug(String[] tab){
+ String s = "String array " + tab.length + " elements: ";
+ for (int i=0;i<tab.length;i++) s+="("+tab[i]+") ";
+ if (debug) System.out.println("-->IvyClient "+bus.appName+":"+appName+"<-- "+s);
}
// a class to perform the threaded execution of each new message