aboutsummaryrefslogtreecommitdiff
path: root/src/IvyWatcher.java
diff options
context:
space:
mode:
authorjestin2002-03-07 14:13:30 +0000
committerjestin2002-03-07 14:13:30 +0000
commit61ac1763d6ae6455ac6962bcef931103c9298d61 (patch)
treeab427cbc53fe95deedb9c69824c2761ab18d899c /src/IvyWatcher.java
parent36739f8756462e6c6d6fbe0dd148b0d347775cd8 (diff)
downloadivy-java-61ac1763d6ae6455ac6962bcef931103c9298d61.zip
ivy-java-61ac1763d6ae6455ac6962bcef931103c9298d61.tar.gz
ivy-java-61ac1763d6ae6455ac6962bcef931103c9298d61.tar.bz2
ivy-java-61ac1763d6ae6455ac6962bcef931103c9298d61.tar.xz
*** empty log message ***
Diffstat (limited to 'src/IvyWatcher.java')
-rwxr-xr-xsrc/IvyWatcher.java100
1 files changed, 54 insertions, 46 deletions
diff --git a/src/IvyWatcher.java b/src/IvyWatcher.java
index 2fe0bfd..669287d 100755
--- a/src/IvyWatcher.java
+++ b/src/IvyWatcher.java
@@ -42,56 +42,59 @@ class IvyWatcher implements Runnable {
* 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() {
+ 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 {
- byte buf[] = new byte[256];
- DatagramPacket packet=new DatagramPacket(buf, 256);
- int port;
- broadcast.receive(packet);
- String msg = new String(packet.getData()) ;
- InetAddress remotehost = packet.getAddress();
- traceDebug("BUSWATCHER Receive Broadcast from "+
- remotehost.getHostName()+":"+packet.getPort());
- // check if remoteaddr is in our broadcast domain list
- // otherwise we ignore the broadcast
- if ( !isInDomain( remotehost ) ) continue;
- StringTokenizer st = new StringTokenizer(msg);
- if ( !st.hasMoreTokens()) {
- System.err.println("Bad format "+msg);
- continue;
- }
- int version = Integer.parseInt( st.nextToken() );
- if ( version != bus.PROCOCOLVERSION ) {
- System.err.println("Ignoring bad protocol version broadcast");
- continue;
- }
- if ( ! st.hasMoreTokens()) {
- System.err.println("Bad format "+msg);
- continue;
- }
- port = Integer.parseInt( st.nextToken() );
- if ( (bus.applicationPort == port) ) continue;
- traceDebug("BUSWATCHER Broadcast de "
- +packet.getAddress().getHostName()
- +":"+packet.getPort()+" port "+port+" version "+version);
- try {
- Socket socket = new Socket( remotehost, port );
- bus.addClient(socket,false);
- } catch ( UnknownHostException e ) {
- System.err.println("Unkonwn host "+remotehost + e.getMessage());
- } catch ( IOException e) {
- System.err.println("can't connect to "+remotehost+" port "+
- port+e.getMessage());
- }
- } catch ( IOException e ) {
- watcherrunning=false;
- traceDebug("broadcast listener crashed " + e.getMessage());
+ broadcast.receive(packet);
+ String msg = new String(packet.getData()) ;
+ InetAddress remotehost = packet.getAddress();
+ traceDebug("BUSWATCHER Receive Broadcast from "+
+ remotehost.getHostName()+":"+packet.getPort());
+ // check if remoteaddr is in our broadcast domain list otherwise we ignore the broadcast
+ if ( !isInDomain( remotehost ) ) continue;
+ StringTokenizer st = new StringTokenizer(msg);
+ if ( !st.hasMoreTokens()) {
+ System.err.println("Bad format "+msg);
+ continue;
+ }
+ int version = Integer.parseInt( st.nextToken() );
+ if ( version != bus.PROCOCOLVERSION ) {
+ System.err.println("Ignoring bad protocol version broadcast");
+ continue;
+ }
+ if ( ! st.hasMoreTokens()) {
+ System.err.println("Bad format "+msg);
+ continue;
+ }
+ port = Integer.parseInt( st.nextToken() );
+ if ( (bus.applicationPort == port) ) continue;
+ traceDebug("BUSWATCHER Broadcast de "
+ +packet.getAddress().getHostName()
+ +":"+packet.getPort()+" port "+port+" version "+version);
+ try {
+ Socket socket = new Socket( remotehost, port );
+ bus.addClient(socket,false);
+ } catch ( UnknownHostException e ) {
+ System.err.println("Unkonwn host "+remotehost + e.getMessage());
+ } catch ( IOException e) {
+ System.err.println("can't connect to "+remotehost+" port "+
+ port+e.getMessage());
+ }
+ } catch (java.io.InterruptedIOException jii ){
+ if (!watcherrunning) break;
+ } catch (java.io.IOException ioe ){
+ System.err.println("IvyWatcher IOException "+ ioe.getMessage() );
}
- broadcast.close();
- traceDebug("broadcast listener normal shutdown");
- }
+ broadcast.close();
+ traceDebug("broadcast listener normal shutdown");
+ } // while
/**
* stops the thread waiting on the broadcast socket
@@ -149,6 +152,11 @@ class IvyWatcher implements Runnable {
} catch ( IOException e ) {
throw new IvyException("IvyWatcher I/O error" + e );
}
+ try {
+ broadcast.setSoTimeout(100);
+ } catch ( java.net.SocketException jns ) {
+ throw new IvyException("IvyWatcher setSoTimeout error" + jns.getMessage() );
+ }
// starts a Thread listening on the socket
watcherrunning=true;
broadcastListener = new Thread(this);