diff options
Diffstat (limited to 'src/IvyWatcher.java')
-rwxr-xr-x | src/IvyWatcher.java | 78 |
1 files changed, 41 insertions, 37 deletions
diff --git a/src/IvyWatcher.java b/src/IvyWatcher.java index dafa6f9..3ac5ef9 100755 --- a/src/IvyWatcher.java +++ b/src/IvyWatcher.java @@ -14,6 +14,9 @@ * thing. * * CHANGELOG: + * 1.2.6: + * - IOException now goes silent when we asked the bus to stop() + * - use a new buffer for each Datagram received, to prevent an old bug * 1.2.5: * - getDomain now sends IvyException for malformed broadcast addresses * - uses apache jakarta-regexp instead of gnu-regexp @@ -56,7 +59,6 @@ import java.lang.Thread; import java.net.*; import java.io.*; import java.util.StringTokenizer; -/* import gnu.regexp.*; GNURETOAPACHERE */ import org.apache.regexp.*; import java.util.Vector; import java.util.Enumeration; @@ -71,6 +73,8 @@ class IvyWatcher implements Runnable { private int port; private volatile Thread listenThread; private InetAddress group; + private static int serial=0; + private int myserial=serial++; /** * creates an Ivy watcher @@ -103,55 +107,57 @@ class IvyWatcher implements Runnable { * the behaviour of each thread watching the UDP socket. */ public void run() { - traceDebug("IvyWatcher Thread started"); // THREADDEBUG + traceDebug("Thread started"); // THREADDEBUG Thread thisThread=Thread.currentThread(); traceDebug("beginning of a watcher Thread"); - byte buf[] = new byte[256]; - DatagramPacket packet=new DatagramPacket(buf, 256); InetAddress remotehost=null; try { while( listenThread==thisThread ) { try { + byte buf[] = new byte[256]; + DatagramPacket packet=new DatagramPacket(buf,buf.length); broadcast.receive(packet); if (listenThread!=thisThread) break; // I was summoned to leave during the receive - String msg = new String(packet.getData()) ; - for (int i=0;i<buf.length;i++) { buf[i]=10; } - // clean up the buffer after each message - // BUGFIX ? I change 0 to 10 in order to avoid a bug - remotehost = packet.getAddress(); - traceDebug("BUSWATCHER Receive Broadcast from "+remotehost.getHostName()+":"+packet.getPort()); - // if ( !isInDomain( remotehost ) ) continue; + String msg = new String(buf,0,packet.getLength()); + String remotehostname=null; try { + remotehost = packet.getAddress(); + remotehostname = remotehost.getHostName(); RE re = new RE("([0-9]*) ([0-9]*)"); if (!(re.match(msg))) { - System.err.println("Ignoring bad format broadcast from "+remotehost); + System.err.println("Ignoring bad format broadcast from "+ + remotehostname+":"+packet.getPort()); continue; } int version = Integer.parseInt(re.getParen(1)); if ( version < bus.PROTOCOLMINIMUM ) { - System.err.println("Ignoring bad protocol version "+remotehost+" we need "+ bus.PROTOCOLMINIMUM+" minimum"); + System.err.println("Ignoring bad format broadcast from "+ + remotehostname+":"+packet.getPort() + +" protocol version "+remotehost+" we need "+bus.PROTOCOLMINIMUM+" minimum"); continue; } 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 a broadcast on my port number, it's *probably* me"); - continue; // it's me + traceDebug("ignoring a broadcast from "+ remotehostname+":"+packet.getPort() + +" on my port number ("+port+"), it's probably me"); + // TODO check better + // if bus.applicationPort=port + // parse the list of Watchers and check for each + // iw.broadcast.getInetAddress().equals(packet().getAddress() + // if one is true, "continue" ( ignore the broadcast ) + continue; } - traceDebug("Broadcast de " +packet.getAddress().getHostName() - +":"+packet.getPort()+" port "+port+" version "+version); - Socket socket = new Socket( remotehost, port ); - bus.addClient(socket,false,version); + traceDebug("broadcast accepted from " +remotehostname + +":"+packet.getPort()+", port:"+port+", protocol version:"+version); + new IvyClient(bus,new Socket(remotehost,port),port); } catch (RESyntaxException ree) { ree.printStackTrace(); System.exit(-1); } catch (NumberFormatException nfe) { - System.err.println("Ignoring bad format broadcast from "+remotehost); + System.err.println("Ignoring bad format broadcast from "+remotehostname); continue; } catch ( UnknownHostException e ) { - System.err.println("Unkonwn host "+remotehost + e.getMessage()); + System.err.println("Unkonwn host "+remotehost +","+e.getMessage()); } catch ( IOException e) { System.err.println("can't connect to "+remotehost+" port "+ port+e.getMessage()); } @@ -161,26 +167,24 @@ class IvyWatcher implements Runnable { } // while } catch (java.net.SocketException se ){ if (thisThread==listenThread) { - System.out.println("IvyWatcher error, continuing anyway"); - se.printStackTrace(); + traceDebug("socket exception, continuing anyway on other Ivy domains "+se); } } catch (IOException ioe ){ - System.out.println("IvyWatcher IO Exception, continuing anyway"); - ioe.printStackTrace(); + System.out.println("IO Exception, continuing anyway on other Ivy domains "+ioe); } - traceDebug("IvyWatcher Thread stopped"); // THREADDEBUG + traceDebug("Thread stopped"); // THREADDEBUG } /** * stops the thread waiting on the broadcast socket */ synchronized void stop() { - traceDebug("begining stopping an IvyWatcher"); + traceDebug("begining stopping"); Thread t = listenThread; listenThread=null; broadcast.close(); if (t!=null) { t.interrupt(); } // it might not even have been created - traceDebug("ending stopping an IvyWatcher"); + traceDebug("stopped"); } private class PacketSender implements Runnable { @@ -201,10 +205,11 @@ class IvyWatcher implements Runnable { e.printStackTrace(); traceDebug("IO interrupted during the broadcast. Do nothing"); } catch ( IOException e ) { - System.out.println("Broadcast Error" + e.getMessage()); - e.printStackTrace(); - // throw new IvyException("Broadcast error " + e.getMessage() ); - System.exit(0); + if (listenThread!=null) { + System.out.println("Broadcast Error " + e.getMessage()+" continuing anyway"); + // cannot throw new IvyException in a run ... + e.printStackTrace(); + } } traceDebug("PacketSender thread stopped"); // THREADDEBUG } @@ -274,9 +279,8 @@ class IvyWatcher implements Runnable { private void traceDebug(String s){ - if (debug) System.out.println("-->ivywatcher<-- "+s); + if (debug) System.out.println("-->IvyWatcher["+myserial+","+bus.getSerial()+"]<-- "+s); } } // class IvyWatcher -/* EOF */ |