aboutsummaryrefslogtreecommitdiff
path: root/src/IvyDaemon.java
diff options
context:
space:
mode:
authorjestin2001-08-06 14:33:34 +0000
committerjestin2001-08-06 14:33:34 +0000
commit4826af957608a54e4492815551f39ac543258fd1 (patch)
tree4d52af9139d265dd5b8a7a68523f3c352665d447 /src/IvyDaemon.java
parent8fef6a76959160bf73064be48d4aa36b48f27412 (diff)
downloadivy-java-4826af957608a54e4492815551f39ac543258fd1.zip
ivy-java-4826af957608a54e4492815551f39ac543258fd1.tar.gz
ivy-java-4826af957608a54e4492815551f39ac543258fd1.tar.bz2
ivy-java-4826af957608a54e4492815551f39ac543258fd1.tar.xz
Adding multicast ( IvyWatcher ), adding the new IvyDaemon program, removing
the debug code in Probe, and who knows what I've done in IvyClient ?
Diffstat (limited to 'src/IvyDaemon.java')
-rw-r--r--src/IvyDaemon.java101
1 files changed, 101 insertions, 0 deletions
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 <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 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 <a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a>
+ */
+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