diff options
Diffstat (limited to 'src/IvyDaemon.java')
-rw-r--r-- | src/IvyDaemon.java | 63 |
1 files changed, 49 insertions, 14 deletions
diff --git a/src/IvyDaemon.java b/src/IvyDaemon.java index f99202d..40d0535 100644 --- a/src/IvyDaemon.java +++ b/src/IvyDaemon.java @@ -12,6 +12,10 @@ * (c) CENA * * changelog: + * 1.2.16 + * - now uses a Thread Pool Executor + * - now parses the messages: if the message is ".die IvyDaemon", quits the + * bus * 1.2.14 * - remove the Thread.start() from the constructor, to avoid mulithread issues * see * http://findbugs.sourceforge.net/bugDescriptions.html#SC_START_IN_CTOR @@ -35,14 +39,19 @@ import fr.dgac.ivy.* ; import java.io.*; import java.net.*; import java.util.Properties ; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; import gnu.getopt.Getopt; -public class IvyDaemon implements Runnable { +public class IvyDaemon implements Runnable, IvyApplicationListener { private ServerSocket serviceSocket; private static boolean debug = (System.getProperty("IVY_DEBUG")!=null) ; - private volatile Thread daemonThread;// volatile to ensure the quick communication + private Thread daemonThread;// volatile to ensure the quick communication + private volatile boolean keeprunning = false ;// volatile to ensure the quick communication private Ivy bus; + private ExecutorService pool = null; + private static volatile int serial = 0; public static final int DEFAULT_SERVICE_PORT = 3456 ; public static final String DEFAULTNAME = "IvyDaemon"; @@ -88,52 +97,56 @@ public class IvyDaemon implements Runnable { if (!quiet) System.out.println("broadcasting on "+Domain.domains(domain)); bus.start(domain); if (!quiet) System.out.println("listening on "+servicePort); - new IvyDaemon(bus,servicePort).doStart(); + new IvyDaemon(bus,servicePort); } public IvyDaemon(Ivy bus,int servicePort) throws IOException { this.bus=bus; + bus.addApplicationListener(this); serviceSocket = new ServerSocket(servicePort) ; - daemonThread=new Thread(this); - daemonThread.setName("Ivy Daemon tool thread"); + pool = Executors.newCachedThreadPool(); + keeprunning = true ; + pool.execute(this); } - protected void doStart() { - daemonThread.start(); - } /* * the service socket reader. * it could be a thread, but as long as we've got one .... */ public void run() { - Thread thisThread = Thread.currentThread(); + daemonThread = Thread.currentThread(); + daemonThread.setName("Ivy Daemon tool thread"); traceDebug("Thread started"); - while ( daemonThread==thisThread ) { + while ( keeprunning ) { + /* there is no way out of here, except ^C */ try { new SubReader(serviceSocket.accept()); } catch( IOException e ) { traceDebug("TCP socket reader caught an exception " + e.getMessage()); } } + System.out.println("outta here"); traceDebug("Thread stopped"); + pool.shutdown(); } - class SubReader extends Thread { + class SubReader implements Runnable { BufferedReader in; SubReader(Socket socket) throws IOException { in=new BufferedReader(new InputStreamReader(socket.getInputStream())); - SubReader.this.start(); + // setName("Subreader "+serial++); + pool.execute(SubReader.this); } public void run() { traceDebug("Subreader Thread started"); - String msg = null; try { while (true) { - msg=in.readLine(); + String msg=in.readLine(); if (msg==null) break; + if (msg.compareTo(".die IvyDaemon") == 0) break; try { bus.sendMsg(msg); } catch (IvyException ie) { @@ -146,9 +159,31 @@ public class IvyDaemon implements Runnable { throw new RuntimeException(); } traceDebug("Subreader Thread stopped"); + try { + in.close(); + } catch (IOException ioe) { + // do nothing + } + pool.shutdown(); + bus.stop(); + System.exit(0); } } + public void connect( IvyClient client ) { } + public void disconnect( IvyClient client ) { } + public void die( IvyClient client, int id, String msgarg) { + keeprunning = false; + if ( daemonThread != null ) daemonThread.interrupt(); + try { + serviceSocket.close(); + } catch (IOException ioe) { + // I don't care + } + } + + public void directMessage( IvyClient client, int id,String msgarg ) {} + private static void traceDebug(String s){ if (debug) System.out.println("-->IvyDaemon "+name+"<-- "+s); } |