summaryrefslogtreecommitdiff
path: root/IvyDaemon/IvyDaemon.cs
diff options
context:
space:
mode:
authorfcolin2007-02-01 12:05:09 +0000
committerfcolin2007-02-01 12:05:09 +0000
commit12c926b30ac4c97773d93afbd9adadd3bc5251b4 (patch)
treeeb24ef3f5e519f0d05c6ecec2736feb47cb664af /IvyDaemon/IvyDaemon.cs
parent91afb5f2ead250cd371feb9882a5b31f8acc4ea4 (diff)
downloadivy-csharp-12c926b30ac4c97773d93afbd9adadd3bc5251b4.zip
ivy-csharp-12c926b30ac4c97773d93afbd9adadd3bc5251b4.tar.gz
ivy-csharp-12c926b30ac4c97773d93afbd9adadd3bc5251b4.tar.bz2
ivy-csharp-12c926b30ac4c97773d93afbd9adadd3bc5251b4.tar.xz
modification structure svn
Diffstat (limited to 'IvyDaemon/IvyDaemon.cs')
-rw-r--r--IvyDaemon/IvyDaemon.cs169
1 files changed, 169 insertions, 0 deletions
diff --git a/IvyDaemon/IvyDaemon.cs b/IvyDaemon/IvyDaemon.cs
new file mode 100644
index 0000000..8c0b069
--- /dev/null
+++ b/IvyDaemon/IvyDaemon.cs
@@ -0,0 +1,169 @@
+/// François-Régis Colin
+/// http://www.tls.cena.fr/products/ivy/
+/// *
+/// (C) CENA
+/// *
+namespace IvyDaemon
+{
+ using System;
+ using System.IO;
+ using System.Threading;
+ using System.Net;
+ using System.Net.Sockets;
+ using System.Configuration;
+ using System.Diagnostics;
+ using IvyBus;
+ /// <summary> IvyDaemon: simple TCP to Ivy relay.
+ /// </summary>
+ /// <remarks>
+ /// 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.
+ /// </remarks>
+ public class IvyDaemon
+ {
+
+
+ private TcpListener serviceSocket;
+ private static bool debug = Properties.Settings.Default.IvyDebug;
+ private volatile Thread clientThread; // volatile to ensure the quick communication
+ private Ivy bus;
+
+ public static int DEFAULT_SERVICE_PORT = 3456;
+ public const System.String DEFAULTNAME = "IvyDaemon";
+ public static System.String helpmsg = "usage: 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";
+ [STAThread]
+ public static void Main(System.String[] args)
+ {
+ Ivy bus;
+ int servicePort = DEFAULT_SERVICE_PORT;
+ System.String name = DEFAULTNAME;
+ bool quiet = false;
+ System.String domain = Ivy.GetDomain(null);
+/* Getopt opt = new Getopt("IvyDaemon", args, "n:b:dqp:h");
+ int c;
+ while ((c = opt.getopt()) != - 1)
+ {
+ switch (c)
+ {
+ case 'n':
+ name = opt.Optarg;
+ break;
+
+ case 'b':
+ domain = opt.Optarg;
+ break;
+
+ case 'q':
+ quiet = true;
+ break;
+
+ case 'd':
+ //UPGRADE_ISSUE: Method 'java.lang.System.getProperties' was not converted. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1000_javalangSystemgetProperties"'
+ //System.Configuration.AppSettingsReader sysProp = System.getProperties();
+ //SupportClass.PutElement(sysProp, "IVY_DEBUG", "yes");
+ break;
+
+ case 'p':
+ System.String s = "";
+ try
+ {
+ servicePort = System.Int32.Parse(s = opt.Optarg);
+ }
+ catch (System.FormatException nfe)
+ {
+ System.Console.Out.WriteLine("Invalid port number: " + s);
+ System.Environment.Exit(0);
+ }
+ break;
+
+ case 'h': default:
+ System.Console.Out.WriteLine(helpmsg);
+ System.Environment.Exit(0);
+ break;
+
+ }
+ }
+*/
+ bus = new Ivy(name, name + " ready");
+ if (!quiet)
+ System.Console.Out.WriteLine("broadcasting on " + Ivy.Domains(domain));
+ bus.Start(domain);
+ if (!quiet)
+ System.Console.Out.WriteLine("listening on " + servicePort);
+ IvyDaemon d = new IvyDaemon(bus, servicePort);
+ }
+
+ public IvyDaemon(Ivy bus, int servicePort)
+ {
+ this.bus = bus;
+ IPAddress localAddr = IPAddress.Any;
+ serviceSocket = new TcpListener(localAddr, servicePort);
+ clientThread = new Thread(new ThreadStart(this.Run));
+ clientThread.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;
+ traceDebug("Thread started");
+ while (clientThread == thisThread)
+ {
+ try
+ {
+ SubReader generatedAux2 = new SubReader(serviceSocket.AcceptTcpClient(),bus);
+ }
+ catch (IOException e)
+ {
+ traceDebug("TCP socket reader caught an exception " + e.Message);
+ }
+ }
+ traceDebug("Thread stopped");
+ }
+
+ internal class SubReader
+ {
+ internal StreamReader in_Renamed;
+ internal Thread looperThread;
+ internal Ivy bus;
+ internal SubReader(TcpClient socket, Ivy bus)
+ {
+ this.bus = bus;
+ in_Renamed = new StreamReader(socket.GetStream());
+ looperThread = new Thread(new ThreadStart(this.Run));
+ looperThread.Start();
+ }
+ public void Run()
+ {
+ traceDebug("Subreader Thread started");
+ String msg = null;
+ try
+ {
+ while (true)
+ {
+ msg = in_Renamed.ReadLine();
+ if (msg == null)
+ break;
+ bus.SendMsg(msg);
+ }
+ }
+ catch (IOException ioe)
+ {
+ traceDebug("Subreader exception:" + ioe.Message);
+ System.Environment.Exit(0);
+ }
+ traceDebug("Subreader Thread stopped");
+ }
+
+ }
+ [Conditional("DEBUG")]
+ private static void traceDebug(string s)
+ {
+ Trace.WriteLineIf(debug, "-->IvyDaemon<-- " + s);
+ }
+ }
+} \ No newline at end of file