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 * * @author François-Régis Colin * @author Yannick Jestin * @author http://www.tls.cena.fr/products/ivy/ * * 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 * that the broadcast is done using the same socket, which is not a good * thing. * * CHANGELOG: * 1.0.10: * - isInDomain() is wrong in multicast. I've removed it * - there was a remanence effect in the datagrampacket buffer. I clean it up after each message * - cleaned up the getDomain() and getPort() code * - close message sends an interruption on all threads for a clean exit * - removed the timeout bug eating all the CPU resources * - now handles a Vector of broadcast listeners */ class IvyWatcher implements Runnable { private static boolean debug = (System.getProperty("IVY_DEBUG")!=null); //private Vector domainaddrList; private boolean watcherrunning = false; private boolean isMulticastAddress = false; private Vector broadcastListener ; private Ivy bus; /* master bus controler */ private DatagramSocket broadcast; /* supervision socket */ // it can also be a MulticastSocket, which inherits from the previous /** * creates an Ivy watcher. * @param bus the bus */ IvyWatcher(Ivy bus) throws IvyException { this.bus = bus; //domainaddrList = new Vector(); } /** * the behaviour of the thread watching the UDP socket. * this thread will stop either when the bus stops or when the * watcherrunning will be set to false * * TODO: better handling of exceptions, because we juste System.err.println * here, run cannot throw IvyException ... */ public void run() { byte buf[] = new byte[256]; DatagramPacket packet=new DatagramPacket(buf, 256); int port; traceDebug("IvyWatcher waiting for Broadcast"); while( watcherrunning && bus.ivyRunning ) try { broadcast.receive(packet); String msg = new String(packet.getData()) ; // clean up the buffer after each message for (int i=0;iivywatcher<-- "+s); } } // class IvyWatcher /* EOF */