aboutsummaryrefslogtreecommitdiff
path: root/src/IvyWatcher.java
diff options
context:
space:
mode:
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);