From 4826af957608a54e4492815551f39ac543258fd1 Mon Sep 17 00:00:00 2001 From: jestin Date: Mon, 6 Aug 2001 14:33:34 +0000 Subject: Adding multicast ( IvyWatcher ), adding the new IvyDaemon program, removing the debug code in Probe, and who knows what I've done in IvyClient ? --- src/IvyClient.java | 15 ++++---- src/IvyDaemon.java | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/IvyWatcher.java | 18 +++++++--- src/Probe.java | 4 +-- 4 files changed, 125 insertions(+), 13 deletions(-) create mode 100644 src/IvyDaemon.java (limited to 'src') diff --git a/src/IvyClient.java b/src/IvyClient.java index e99574c..069a45b 100755 --- a/src/IvyClient.java +++ b/src/IvyClient.java @@ -31,8 +31,9 @@ public class IvyClient extends Thread { final static int SchizoToken = 6; /* avoid race condition in concurrent connexions */ final static int DirectMsg = 7;/* the peer sends a direct message */ final static int Die = 8; /* the peer wants us to quit */ - final static String StartArg = "\002";/* begin of arguments */ - final static String EndArg = "\003"; /* end of arguments */ + /* test pour jdk1.3 */ + final static String StartArg = "\u0002";/* begin of arguments */ + final static String EndArg = "\u0003"; /* end of arguments */ private static boolean debug = (System.getProperty("IVY_DEBUG")!=null) ; private Ivy bus; @@ -150,7 +151,7 @@ public class IvyClient extends Thread { */ StringTokenizer st = new StringTokenizer(msg); if(!st.hasMoreTokens()){close("Bad format no type '"+msg+"'");break;} - token=st.nextToken(); + token=st.nextToken().trim(); if (token.length()==0){close("Bad format no type '"+msg+"'");break;} try { msgtype = Integer.parseInt(token); @@ -159,7 +160,9 @@ public class IvyClient extends Thread { break; } if(!st.hasMoreTokens()){close("Bad format no id '"+msg+"'");break;} - token=st.nextToken(StartArg); + /* IST */ + + token=st.nextToken(StartArg).trim(); /* * TODO * this doesn't work on jdk1.3 !!! @@ -172,7 +175,7 @@ public class IvyClient extends Thread { break; } String msgarg=""; - if (st.hasMoreTokens()) msgarg=st.nextToken("\n"); + if (st.hasMoreTokens()) msgarg=st.nextToken("\n").trim(); /* * second stage: process the message */ @@ -266,7 +269,7 @@ public class IvyClient extends Thread { String buffer = type+" "+id+StartArg; // Start at 1 because group 0 represent entire matching for(int sub = 1; sub <= nbsub; sub++) { - if (result.getSubStartIndex(sub) > -1) { + if (result.getStartIndex(sub) > -1) { buffer += result.toString(sub)+EndArg; } } diff --git a/src/IvyDaemon.java b/src/IvyDaemon.java new file mode 100644 index 0000000..09367c6 --- /dev/null +++ b/src/IvyDaemon.java @@ -0,0 +1,101 @@ +/** + * sample implementation of an Ivy Daemon, like ivyd. + * @author Yannick Jestin + * @author http://www.tls.cena.fr/products/ivy/ + */ +package fr.dgac.ivy ; + +import java.io.*; +import java.net.*; +import gnu.getopt.Getopt; + +/** + * toy tool to send anonymous messages to an Ivy bus through a simple tcp + * socket. + * IvyDaemon runs in the background and forwards all the incoming trafic to + * the bus, line by line. The default port is 3456. + * + * @author Yannick Jestin + * @author http://www.tls.cena.fr/products/ivy/ + */ +class IvyDaemon implements Runnable { + + public static int DEFAULT_SERVICE_PORT = 3456 ; + ServerSocket serviceSocket; + boolean isRunning=false; + Ivy bus; + + public static void main(String[] args) { + Getopt opt = new Getopt("IvyDaemon",args,"b:dp:"); + int c; + int servicePort = DEFAULT_SERVICE_PORT; + String domain=Ivy.DEFAULT_DOMAIN; + while ((c = opt.getopt()) != -1) switch (c) { + case 'b': + domain=opt.getOptarg(); + break; + case 'd': + System.setProperty("IVY_DEBUG","yes"); + break; + case 'p': + String s=""; + try { + servicePort = Integer.parseInt(s=opt.getOptarg()); + } catch (NumberFormatException nfe) { + System.out.println("Invalid port number: " + s + + "switching to default port "+servicePort); + } + break; + default: + } + IvyDaemon d = new IvyDaemon(domain,servicePort); + } + + IvyDaemon(String domain,int servicePort) { + // connexion to the Bus + try { + bus=new Ivy("IvyDaemon","IvyDaemon ready",null); + System.out.println("broadcasting on "+domain); + bus.start(domain); + serviceSocket = new ServerSocket(servicePort) ; + System.out.println("listening on "+servicePort); + isRunning=true; + (new Thread(this)).start(); // loops on the service Socket, awaiting connexion + } catch (IvyException ie) { + System.out.println("Caught an exception. quitting. "+ie.getMessage()); + } catch (IOException ioe) { + System.out.println("Caught an IOexception. quitting. "+ioe.getMessage()); + } + } + + /* + * the service socket reader. + * it could be a thread, but as long as we've got one .... + */ + public void run() { + while(isRunning){ + try { + Socket socket = serviceSocket.accept(); + new SubReader( + new BufferedReader(new InputStreamReader(socket.getInputStream()))); + } catch( IOException e ) { + System.out.println("DEBUG TCP socket reader caught an exception " + e.getMessage()); + } + } + } + + class SubReader extends Thread { + BufferedReader in; + SubReader(BufferedReader in) {this.in=in;start();} + public void run() { + String msg = null; + try { + while ( ((msg=in.readLine()) != null )) { bus.sendMsg(msg); } + } catch (IOException ioe) { + System.out.println("exception ..." + ioe.getMessage()); + } + } + } // subclass SubReader + +} // class IvyDaemon +// EOF diff --git a/src/IvyWatcher.java b/src/IvyWatcher.java index bae387a..2fe0bfd 100755 --- a/src/IvyWatcher.java +++ b/src/IvyWatcher.java @@ -24,10 +24,11 @@ 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 Thread 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 @@ -135,12 +136,19 @@ class IvyWatcher implements Runnable { port = Integer.parseInt( domain.substring( sep_index +1 )); domainaddr = domain.substring(0,sep_index); } - // create the UDP socket + // Handling of multicast address try { - broadcast = new MulticastSocket(port ); + InetAddress group = InetAddress.getByName(domainaddr); + if (group.isMulticastAddress()) { + isMulticastAddress = true; + broadcast = new MulticastSocket(port ); // create the UDP socket + ((MulticastSocket)broadcast).joinGroup(group); + } else { + broadcast = new MulticastSocket(port ); // create the UDP socket + } } catch ( IOException e ) { - throw new IvyException("IvyWatcher erreur I/O" + e ); - } + throw new IvyException("IvyWatcher I/O error" + e ); + } // starts a Thread listening on the socket watcherrunning=true; broadcastListener = new Thread(this); diff --git a/src/Probe.java b/src/Probe.java index 5152cda..84e282f 100644 --- a/src/Probe.java +++ b/src/Probe.java @@ -37,8 +37,8 @@ class Probe implements IvyApplicationListener, IvyMessageListener { System.out.println("broadcasting on "+domain); bus.start(domain); String s; - DataInputStream in = - new DataInputStream(new BufferedInputStream(System.in)); + BufferedReader in = + new BufferedReader(new InputStreamReader(System.in)); // infinite loop on keyboard input try { while ((s=in.readLine()).length()!=0) -- cgit v1.1