Esempio n. 1
0
        static void Main(string[] args)
        {
            ProgramTools.Setup();

            var config = new Config();

            ExecutableTools.LogArgs(new StderrWriteLine(), args, (arg) =>
            {
                ConfigTools.SetProperty(config, arg);
            });

            if (!config.Daemon)
            {
                Logger.TRACE = new StderrWriteLine();
            }

            Stdio.SetStatus("Ready");

            var line = Stdio.ReadLine();

            while (line != null)
            {
                Logger.Trace("> {0}", line);
                if (line == "ping")
                {
                    Stdio.WriteLine("pong");
                    Logger.Trace("< pong");
                }
                //throw Exception Message
                if (line.StartsWith("throw"))
                {
                    throw new Exception(line.Substring(6));
                }
                if (line == "exit!")
                {
                    return;
                }
                line = Stdio.ReadLine();
            }

            Logger.Trace("Stdin closed");

            Environment.Exit(0);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            ProgramTools.Setup();

            var config = new Config();

            //args will always log to stderr because trace flag not loaded yet
            ExecutableTools.LogArgs(new StderrWriteLine(), args, (arg) =>
            {
                ConfigTools.SetProperty(config, arg);
            });

            AssertTools.NotEmpty(config.EndPoint, "Missing EndPoint");
            AssertTools.NotEmpty(config.Root, "Missing Root");

            if (!config.Daemon)
            {
                Logger.TRACE = new StderrWriteLine();
            }

            var uri  = string.Format("http://{0}/", config.EndPoint);
            var http = new HttpListener();

            http.Prefixes.Add(uri);
            http.Start();
            var accepter = new Runner(new Runner.Args {
                ThreadName = "Accepter"
            });
            var handler = new Runner(new Runner.Args {
                ThreadName = "Handler"
            });

            accepter.Run(() =>
            {
                while (http.IsListening)
                {
                    var ctx = http.GetContext();
                    handler.Run(() =>
                    {
                        var request  = ctx.Request;
                        var response = ctx.Response;
                        var pass     = true;
                        var file     = ctx.Request.RawUrl.Substring(1); //remove leading /
                        if (!PathTools.IsChildPath(config.Root, file))
                        {
                            pass = false;
                        }
                        var path = PathTools.Combine(config.Root, file);
                        Logger.Trace("File {0} {1}", file, path);
                        if (!File.Exists(path))
                        {
                            pass = false;
                        }
                        if (ctx.Request.HttpMethod != "GET")
                        {
                            pass = false;
                        }
                        if (pass)
                        {
                            var fi = new FileInfo(path);
                            var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                            ctx.Response.StatusCode      = (int)HttpStatusCode.OK;
                            ctx.Response.ContentLength64 = fi.Length;
                            ctx.Response.ContentType     = "application/octet-stream";
                            var data  = new byte[1024];
                            var count = fs.Read(data, 0, data.Length);
                            while (count > 0)
                            {
                                ctx.Response.OutputStream.Write(data, 0, count);
                                count = fs.Read(data, 0, data.Length);
                            }
                        }
                        else
                        {
                            ctx.Response.StatusCode      = (int)HttpStatusCode.NotFound;
                            ctx.Response.ContentLength64 = 0;
                        }
                        ctx.Response.Close();
                    });
                }
            });

            Stdio.SetStatus("Listeninig on {0}", uri);

            using (var disposer = new Disposer())
            {
                disposer.Push(handler);
                disposer.Push(accepter);
                disposer.Push(http.Stop);

                var line = Stdio.ReadLine();
                while (line != null)
                {
                    line = Stdio.ReadLine();
                }
            }

            Logger.Trace("Stdin closed");

            Environment.Exit(0);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            ProgramTools.Setup();

            var config = new Config();

            config.IP      = "0.0.0.0";
            config.Port    = 22333;
            config.Delay   = 5000;
            config.Timeout = 5000;
            config.Root    = ExecutableTools.Relative("Root");

            //args will always log to stderr because daemon flag not loaded yet
            ExecutableTools.LogArgs(new StderrWriteLine(), args, (arg) =>
            {
                ConfigTools.SetProperty(config, arg);
            });

            AssertTools.NotEmpty(config.Root, "Missing Root path");
            AssertTools.NotEmpty(config.IP, "Missing IP");
            AssertTools.Ip(config.IP, "Invalid IP");

            var pid     = Process.GetCurrentProcess().Id;
            var writers = new WriteLineCollection();

            if (!config.Daemon)
            {
                writers.Add(new StderrWriteLine());
                Logger.TRACE = new TimedWriter(writers);
            }

            Logger.Trace("Root {0}", config.Root);

            var dbpath    = Path.Combine(config.Root, "SharpDaemon.LiteDb");
            var logpath   = Path.Combine(config.Root, "SharpDaemon.Log.txt");
            var eppath    = Path.Combine(config.Root, "SharpDaemon.Endpoint.txt");
            var downloads = Path.Combine(config.Root, "Downloads");

            Directory.CreateDirectory(downloads); //creates root as well

            //acts like mutex for the workspace
            var log = new StreamWriter(logpath, true);

            writers.Add(new TextWriterWriteLine(log));

            ExecutableTools.LogArgs(new TextWriterWriteLine(log), args);

            var instance = new Instance(new Instance.Args
            {
                DbPath       = dbpath,
                RestartDelay = config.Delay,
                Downloads    = downloads,
                EndPoint     = new IPEndPoint(IPAddress.Parse(config.IP), config.Port),
            });

            Stdio.SetStatus("Listening on {0}", instance.EndPoint);

            using (var disposer = new Disposer())
            {
                //wrap to add to disposable count
                disposer.Push(new Disposable.Wrapper(log));
                disposer.Push(instance);
                disposer.Push(() => Disposing(config.Timeout));

                if (config.Daemon)
                {
                    Logger.Trace("Stdin loop...");
                    var line = Stdio.ReadLine();
                    while (line != null)
                    {
                        Logger.Trace("> {0}", line);
                        if ("exit!" == line)
                        {
                            break;
                        }
                        line = Stdio.ReadLine();
                    }
                    Logger.Trace("Stdin closed");
                }
                else
                {
                    Logger.Trace("Stdin loop...");
                    var shell  = instance.CreateShell();
                    var stream = new ShellStream(new StdoutWriteLine(), new ConsoleReadLine());
                    //disposer.Push(stream); //stdin.readline wont return even after close/dispose call
                    var line = stream.ReadLine();
                    while (line != null)
                    {
                        if ("exit!" == line)
                        {
                            break;
                        }
                        Shell.ParseAndExecute(shell, stream, line);
                        line = stream.ReadLine();
                    }
                    Logger.Trace("Stdin closed");
                }
            }

            Environment.Exit(0);
        }