aboutsummaryrefslogtreecommitdiff
path: root/src/IvyWatcher.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/IvyWatcher.java')
-rwxr-xr-xsrc/IvyWatcher.java60
1 files changed, 37 insertions, 23 deletions
diff --git a/src/IvyWatcher.java b/src/IvyWatcher.java
index 7698632..dafa6f9 100755
--- a/src/IvyWatcher.java
+++ b/src/IvyWatcher.java
@@ -16,10 +16,13 @@
* CHANGELOG:
* 1.2.5:
* - getDomain now sends IvyException for malformed broadcast addresses
+ * - uses apache jakarta-regexp instead of gnu-regexp
+ * - throws an IvyException if the broadcast domain cannot be resolved
* 1.2.4:
* - sends the broadcast before listening to the other's broadcasts.
- * TODO wait for all the broadcast to be sent before starting the listen
- * mode
+ * I can't wait for all the broadcast to be sent before starting the listen
+ * mode, otherwise another agent behaving likewise could be started
+ * meanwhile, and one would not "see" each other.
* - (REMOVED) allows the connexion from a remote host with the same port number
* it's too complicated to know if the packet is from ourselves...
* - deals with the protocol errors in a more efficient way. The goal is not
@@ -53,7 +56,8 @@ import java.lang.Thread;
import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
-import gnu.regexp.*;
+/* import gnu.regexp.*; GNURETOAPACHERE */
+import org.apache.regexp.*;
import java.util.Vector;
import java.util.Enumeration;
@@ -99,7 +103,7 @@ class IvyWatcher implements Runnable {
* the behaviour of each thread watching the UDP socket.
*/
public void run() {
- // System.out.println("IvyWatcher Thread started"); // THREADDEBUG
+ traceDebug("IvyWatcher Thread started"); // THREADDEBUG
Thread thisThread=Thread.currentThread();
traceDebug("beginning of a watcher Thread");
byte buf[] = new byte[256];
@@ -116,32 +120,31 @@ class IvyWatcher implements Runnable {
// BUGFIX ? I change 0 to 10 in order to avoid a bug
remotehost = packet.getAddress();
traceDebug("BUSWATCHER Receive Broadcast from "+remotehost.getHostName()+":"+packet.getPort());
- // TODO if ( !isInDomain( remotehost ) ) continue;
+ // if ( !isInDomain( remotehost ) ) continue;
try {
RE re = new RE("([0-9]*) ([0-9]*)");
- REMatch result = re.getMatch(msg);
- if (result==null) {
+ if (!(re.match(msg))) {
System.err.println("Ignoring bad format broadcast from "+remotehost);
continue;
}
- int version = Integer.parseInt(result.toString(1));
+ int version = Integer.parseInt(re.getParen(1));
if ( version < bus.PROTOCOLMINIMUM ) {
System.err.println("Ignoring bad protocol version "+remotehost+" we need "+ bus.PROTOCOLMINIMUM+" minimum");
continue;
}
- int port = Integer.parseInt(result.toString(2));
+ int port = Integer.parseInt(re.getParen(2));
// allows the connexion from a remote host with the same port number
// if ( ( (remotehost.equals(localhost)) || (remotehost.equals(loopback)) )
// && (bus.applicationPort==port)) {
if (bus.applicationPort==port) {
- traceDebug("ignoring my own broadcast. OK");
+ traceDebug("ignoring a broadcast on my port number, it's *probably* me");
continue; // it's me
}
traceDebug("Broadcast de " +packet.getAddress().getHostName()
+":"+packet.getPort()+" port "+port+" version "+version);
Socket socket = new Socket( remotehost, port );
bus.addClient(socket,false,version);
- } catch (REException ree) {
+ } catch (RESyntaxException ree) {
ree.printStackTrace();
System.exit(-1);
} catch (NumberFormatException nfe) {
@@ -157,12 +160,15 @@ class IvyWatcher implements Runnable {
}
} // while
} catch (java.net.SocketException se ){
- if (thisThread==listenThread) { se.printStackTrace(); }
+ if (thisThread==listenThread) {
+ System.out.println("IvyWatcher error, continuing anyway");
+ se.printStackTrace();
+ }
} catch (IOException ioe ){
+ System.out.println("IvyWatcher IO Exception, continuing anyway");
ioe.printStackTrace();
}
- traceDebug("end of a watcher thread");
- // System.out.println("IvyWatcher Thread stopped"); // THREADDEBUG
+ traceDebug("IvyWatcher Thread stopped"); // THREADDEBUG
}
/**
@@ -206,14 +212,18 @@ class IvyWatcher implements Runnable {
synchronized void start() throws IvyException {
String hello = bus.PROTOCOLVERSION + " " + bus.applicationPort + "\n";
+ if (broadcast==null) throw new IvyException("IvyWatcher PacketSender null broadcast address");
new PacketSender(hello); // notifies our arrival on each domain: protocol version + port
listenThread.start();
}
/*
- * deprecated since we use Multicast. How to check when we are in UDP
- * broadcast ?
private boolean isInDomain( InetAddress host ){
+ return true;
+ // TODO check if this function is useful. for now, it always returns true
+ // deprecated since we use Multicast. How to check when we are in UDP
+ // broadcast ?
+ //
byte rem_addr[] = host.getAddress();
for ( int i = 0 ; i < domainaddrList.size(); i++ ) {
byte addr[] = ((InetAddress)domainaddrList.elementAt(i)).getAddress();
@@ -228,22 +238,26 @@ class IvyWatcher implements Runnable {
traceDebug( "host " + host + " Not in domain\n" );
return false;
}
- */
+ */
- // TODO this is buggy :-\ try it on a named multicast address just to see
static String getDomain(String net) throws IvyException {
// System.out.println("debug: net=[" + net+ "]");
int sep_index = net.lastIndexOf( ":" );
if ( sep_index != -1 ) { net = net.substring(0,sep_index); }
try {
+ RE numbersPoint = new RE("([0-9]|\\.)+");
+ if (!numbersPoint.match(net)) {
+ // traceDebug("should only have numbers and point ? I won't add anything... " + net);
+ return net;
+ }
net += ".255.255.255";
RE exp = new RE( "^(\\d+\\.\\d+\\.\\d+\\.\\d+).*");
- net = exp.substitute( net , "$1" );
- if (net==null) {
+ if (!exp.match(net)) {
System.out.println("Bad broascat addr " + net);
throw new IvyException("bad broadcast addr");
}
- } catch ( REException e ){
+ net=exp.getParen(1);
+ } catch ( RESyntaxException e ){
System.out.println(e);
System.exit(0);
}
@@ -251,9 +265,9 @@ class IvyWatcher implements Runnable {
return net;
}
- static int getPort(String net) {
+ static int getPort(String net) { // returns 0 if no port is set
int sep_index = net.lastIndexOf( ":" );
- int port= ( sep_index == -1 ) ? Ivy.DEFAULT_PORT :Integer.parseInt( net.substring( sep_index +1 ));
+ int port= ( sep_index == -1 ) ? 0 :Integer.parseInt( net.substring( sep_index +1 ));
// System.out.println("net: ["+net+"]\nsep_index: "+sep_index+"\nport: "+port);
return port;
}