diff options
Diffstat (limited to 'src/IvyWatcher.java')
-rwxr-xr-x | src/IvyWatcher.java | 64 |
1 files changed, 43 insertions, 21 deletions
diff --git a/src/IvyWatcher.java b/src/IvyWatcher.java index b54a08f..98956ea 100755 --- a/src/IvyWatcher.java +++ b/src/IvyWatcher.java @@ -1,20 +1,12 @@ -package fr.dgac.ivy ; - -import java.lang.Thread; -import java.net.*; -import java.io.*; -import java.util.StringTokenizer; -import gnu.regexp.*; -import java.util.Vector; -import java.util.Enumeration; - /** - * A private Class for the Ivy rendezvous + * IvyWatcher, A private Class for the Ivy rendezvous * - * @author François-Régis Colin * @author Yannick Jestin + * @author François-Régis Colin * @author <a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a> * + * (C) CENA + * * right now, the rendez vous is either an UDP socket or a TCP multicast. * The watcher will answer to * each peer advertising its arrival on the bus. The intrinsics of Unix are so @@ -23,9 +15,13 @@ import java.util.Enumeration; * * CHANGELOG: * 1.2.3: + * - the packet sending is done in its own thread from now on (PacketSender) + * I don't care stopping it, since it can't be blocked. * - checks whether I have been interrupted just after the receive (start() * then stop() immediately). * 1.2.1: + * - can be Interrupted during the broadcast Send. I catch the + * and do nothing with it. InterruptedIOException * - changed the fill character from 0 to 10, in order to prevent a nasty bug * on Windows XP machines * - fixed a NullPointerException while trying to stop a Thread before having @@ -41,6 +37,14 @@ import java.util.Enumeration; * - removed the timeout bug eating all the CPU resources * - now handles a Vector of broadcast listeners */ +package fr.dgac.ivy ; +import java.lang.Thread; +import java.net.*; +import java.io.*; +import java.util.StringTokenizer; +import gnu.regexp.*; +import java.util.Vector; +import java.util.Enumeration; class IvyWatcher implements Runnable { private static boolean debug = (System.getProperty("IVY_DEBUG")!=null); @@ -143,7 +147,7 @@ class IvyWatcher implements Runnable { /** * stops the thread waiting on the broadcast socket */ - void stop() { + synchronized void stop() { traceDebug("begining stopping an IvyWatcher"); Thread t = listenThread; listenThread=null; @@ -152,19 +156,37 @@ class IvyWatcher implements Runnable { traceDebug("ending stopping an IvyWatcher"); } - private void sendBroadcast(String data, String domain, int port) throws IvyException { - try { - DatagramPacket packet = new DatagramPacket( data.getBytes(), data.length(), group, port ); - broadcast.send(packet); - } catch ( IOException e ) { - throw new IvyException("Broadcast error " + e.getMessage() ); + private class PacketSender implements Runnable { + DatagramPacket packet; + String data; + public PacketSender(String data) { + this.data=data; + packet=new DatagramPacket(data.getBytes(),data.length(),group,port); + new Thread((PacketSender.this)).start(); + } + public void run() { + traceDebug("PacketSender thread started"); // THREADDEBUG + try { + broadcast.send(packet); + } catch (InterruptedIOException e) { + // somebody interrupts my IO. Thread, do nothing. + System.out.println(e.bytesTransferred+" bytes transferred anyway, out of " + data.length()); + 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); + } + traceDebug("PacketSender thread stopped"); // THREADDEBUG } } - void start() throws IvyException { + synchronized void start() throws IvyException { String hello = bus.PROCOCOLVERSION + " " + bus.applicationPort + "\n"; listenThread.start(); - sendBroadcast(hello,domainaddr,port); // notifies our arrival on each domain: protocol version + port + new PacketSender(hello); // notifies our arrival on each domain: protocol version + port } /* |