aboutsummaryrefslogtreecommitdiff
path: root/src/IvyDaemon.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/IvyDaemon.java')
-rw-r--r--src/IvyDaemon.java140
1 files changed, 82 insertions, 58 deletions
diff --git a/src/IvyDaemon.java b/src/IvyDaemon.java
index 72c83ec..56019c1 100644
--- a/src/IvyDaemon.java
+++ b/src/IvyDaemon.java
@@ -1,79 +1,92 @@
/**
- * sample implementation of an Ivy Daemon, like ivyd.
- * @author Yannick Jestin
- * @author <a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a>
- */
-package fr.dgac.ivy ;
-import java.io.*;
-import java.net.*;
-import java.util.Properties ;
-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.
+ * IvyDaemon: simple TCP to Ivy relay.
*
* @author Yannick Jestin
* @author <a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a>
*
+ * This is a sample implementation of an Ivy Daemon, like ivyd
+ * sends anonymous messages to an Ivy bus through a simple tcp socket,
+ * line by line. The default port is 3456.
+ *
+ * (c) CENA
+ *
* changelog:
+ * 1.2.3
+ * - adds the traceDebug
+ * - uses the clientThread paradigm to programm the thread sync
+ * - invalid port number as a command line argument now stops the program
+ * - cleans up the code
+ * - adds a "quiet" option on the command line
* 1.2.2
- * changes the setProperty to a backward compatible construct
+ * - changes the setProperty to a backward compatible construct
* 1.0.12
- * - class goes public access !
+ * - class goes public access !
*/
+package fr.dgac.ivy ;
+import java.io.*;
+import java.net.*;
+import java.util.Properties ;
+import gnu.getopt.Getopt;
public 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:");
+ private ServerSocket serviceSocket;
+ private boolean isRunning=false;
+ private static boolean debug = (System.getProperty("IVY_DEBUG")!=null) ;
+ private volatile Thread clientThread;// volatile to ensure the quick communication
+ private Ivy bus;
+
+ public static int DEFAULT_SERVICE_PORT = 3456 ;
+ public static final String DEFAULTNAME = "IvyDaemon";
+ public static final String helpmsg = "usage: java fr.dgac.ivy.IvyDaemon [options]\n\t-b BUS\tspecifies the Ivy bus domain\n\t-p\tport number, default "+DEFAULT_SERVICE_PORT+"\n\t-n ivyname (default "+DEFAULTNAME+")\n\t-q\tquiet, no tty output\n\t-d\tdebug\n\t-h\thelp\nListens on the TCP port, and sends each line read on the Ivy bus. It is useful to launch one Ivy Daemon and let scripts send their message on the bus.\n";
+ public static void main(String[] args) throws IvyException, IOException {
+ Ivy bus;
+ Getopt opt = new Getopt("IvyDaemon",args,"n:b:dqp:h");
int c;
int servicePort = DEFAULT_SERVICE_PORT;
- String domain=Ivy.DEFAULT_DOMAIN;
+ String name = DEFAULTNAME;
+ boolean quiet = false;
+ String domain=Ivy.getDomain(null);
while ((c = opt.getopt()) != -1) switch (c) {
+ case 'n':
+ name=opt.getOptarg();
+ break;
case 'b':
domain=opt.getOptarg();
break;
+ case 'q':
+ quiet=true;
+ break;
case 'd':
Properties sysProp = System.getProperties();
sysProp.put("IVY_DEBUG","yes");
- //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);
+ System.out.println("Invalid port number: " + s );
+ System.exit(0);
}
break;
+ case 'h':
default:
+ System.out.println(helpmsg);
+ System.exit(0);
}
- IvyDaemon d = new IvyDaemon(domain,servicePort);
+ bus=new Ivy(name,name+" ready",null);
+ if (!quiet) System.out.println("broadcasting on "+bus.domains(domain));
+ bus.start(domain);
+ if (!quiet) System.out.println("listening on "+servicePort);
+ IvyDaemon d = new IvyDaemon(bus,servicePort);
}
- public 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());
- }
+ public IvyDaemon(Ivy bus,int servicePort) throws IOException {
+ this.bus=bus;
+ serviceSocket = new ServerSocket(servicePort) ;
+ clientThread=new Thread(this);
+ clientThread.start();
}
/*
@@ -81,33 +94,44 @@ public class IvyDaemon implements Runnable {
* it could be a thread, but as long as we've got one ....
*/
public void run() {
- // System.out.println("IvyDaemon Thread started"); // THREADDEBUG
- while(isRunning){
+ Thread thisThread = Thread.currentThread();
+ traceDebug("Thread started");
+ while ( clientThread==thisThread ) {
try {
- Socket socket = serviceSocket.accept();
- new SubReader(
- new BufferedReader(new InputStreamReader(socket.getInputStream())));
+ new SubReader(serviceSocket.accept());
} catch( IOException e ) {
- System.out.println("IvyDaemon DEBUG TCP socket reader caught an exception " + e.getMessage());
+ traceDebug("TCP socket reader caught an exception " + e.getMessage());
}
}
- // System.out.println("IvyDaemon Thread stopped"); // THREADDEBUG
+ traceDebug("Thread stopped");
}
class SubReader extends Thread {
BufferedReader in;
- SubReader(BufferedReader in) {this.in=in;start();}
+ SubReader(Socket socket) throws IOException {
+ in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ start();
+ }
public void run() {
- // System.out.println("Subreader Thread started"); // THREADDEBUG
+ traceDebug("Subreader Thread started");
String msg = null;
try {
- while ( ((msg=in.readLine()) != null )) { bus.sendMsg(msg); }
+ while (true) {
+ msg=in.readLine();
+ if (msg==null) break;
+ bus.sendMsg(msg);
+ }
} catch (IOException ioe) {
- System.out.println("exception ..." + ioe.getMessage());
+ traceDebug("Subreader exception ...");
+ ioe.printStackTrace();
+ System.exit(0);
}
- // System.out.println("Subreader Thread stopped"); // THREADDEBUG
+ traceDebug("Subreader Thread stopped");
}
- } // subclass SubReader
+ }
+
+ private void traceDebug(String s){
+ if (debug) System.out.println("-->IvyDaemon "+bus.appName+"<-- "+s);
+ }
-} // class IvyDaemon
-// EOF
+}