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