Exemplo n.º 1
0
        public void End()
        {
            if (!_ended)
            {
                _ended = true;

                // remove this session.
                lock (ActiveSessions) {
                    ActiveSessions.Remove(this);
                }

                if (!HasActiveSessions)
                {
                    Task.Factory.StartNew(() => {
                        Thread.Sleep(61 * 60 * 1000); // 11 minutes
                        if (!HasActiveSessions && DateTime.Now.Subtract(LastActivity) > new TimeSpan(0, 60, 0))
                        {
                            // no active sessions
                            // more than 60 minutes since last one.
                            // nighty-night!
                            EngineServiceManager.TryToStopService();
                            Logger.Message("Service getting sleepy. Going nighty-night");
                        }
                    });
                }

                Logger.Message("Ending Client: [{0}]-[{1}]".format(_clientId, _sessionId));

                // end any outstanding tasks as gracefully as we can.
                _cancellationTokenSource.Cancel();

                // drop all our local session data.
                _sessionData = null;

                // close and clean up the pipes.
                Disconnect();

                GC.Collect();
            }
        }
Exemplo n.º 2
0
        private int main(IEnumerable <string> args)
        {
            try {
                Environment.CurrentDirectory = Environment.GetEnvironmentVariable("tmp");
                var options    = args.Switches();
                var parameters = args.Parameters();

                Console.CancelKeyPress += (x, y) => {
                    Console.WriteLine("Stopping CoAppService.");
                    Engine.RequestStop();
                };

                #region Parse Options

                foreach (var arg in from arg in options.Keys select arg)
                {
                    var argumentParameters = options[arg];
                    switch (arg)
                    {
                    case "load-config":
                        break;

                    case "auto-install":
                        RequiresAdmin("--auto-install");
                        Environment.Exit(CoAppService.AutoInstall());
                        break;

                    case "start":
                        _start   = true;
                        _install = true;
                        break;

                    case "restart":
                        _stop    = true;
                        _start   = true;
                        _install = true;
                        break;

                    case "stop":
                        _stop = true;
                        break;

                    case "install":
                        _install = true;
                        break;

                    case "uninstall":
                        _stop      = true;
                        _uninstall = true;
                        break;

                    case "username":
                        UseUserAccount = true;
                        _username      = argumentParameters.LastOrDefault();
                        break;

                    case "password":
                        _password = argumentParameters.LastOrDefault();
                        break;

                    case "status":
                        _status = true;
                        break;

                    case "interactive":
                        if (EngineServiceManager.IsServiceRunning)
                        {
                            Console.WriteLine("Shutting down running assembly.");
                            EngineServiceManager.TryToStopService();

                            while (EngineServiceManager.IsServiceRunning)
                            {
                                Console.Write(".");
                                Thread.Sleep(100);
                            }
                        }
                        foreach (var proc in Process.GetProcessesByName("coapp.service").Where(each => each.Id != Process.GetCurrentProcess().Id).ToArray())
                        {
                            try {
                                Console.WriteLine("Killing Process... {0}", proc.Id);
                                proc.Kill();
                            } catch {
                            }
                        }

                        _interactive = true;
                        break;

                    case "help":
                        return(Help());

                    default:
                        Fail("Unrecognized switch [--{0}]", arg);
                        return(Help());
                    }
                }

                #endregion

                Logo();

                if (_interactive)
                {
                    RequiresAdmin("--interactive");
                    if (EngineServiceManager.IsServiceRunning)
                    {
                        throw new ConsoleException(
                                  "The CoApp Service can not be running.\r\nYou must stop it with --stop before using the service interactively.");
                    }
                    Console.WriteLine("Launching CoApp Service interactively.\r\nUse ctrl-c to stop.");

                    var task = Engine.Start(true);

                    Console.WriteLine("[CoApp Interactive -- Press escape to stop.]");

                    // wait for user to cancel task, or when it's actually closed
                    while (!task.Wait(1000))
                    {
                        Console.Write(".");
                        while (Console.KeyAvailable)
                        {
                            if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                            {
                                Engine.RequestStop();
                            }
                        }
                    }
                    return(0);
                }

                if (_stop)
                {
                    RequiresAdmin("--stop");
                    Console.Write("Stopping service:");
                    if (EngineServiceManager.IsServiceInstalled)
                    {
                        EngineServiceManager.TryToStopService();
                    }

                    while (EngineServiceManager.IsServiceRunning)
                    {
                        Console.Write(".");
                        Thread.Sleep(100);
                    }
                    Console.WriteLine(" [Stopped]");
                }

                if (_uninstall)
                {
                    RequiresAdmin("--uninstall");
                    CoAppService.Uninstall();
                    return(0);
                }

                if (_install)
                {
                    RequiresAdmin("--install");
                    CoAppService.Install(_username, _password);
                }

                if (_start)
                {
                    RequiresAdmin("--start");

                    if (EngineServiceManager.IsServiceInstalled)
                    {
                        Console.Write("Starting service:");
                        EngineServiceManager.TryToStartService();

                        while (!EngineServiceManager.IsServiceRunning)
                        {
                            Console.Write(".");
                            Thread.Sleep(100);
                        }
                        Console.WriteLine(" [Started]");
                    }
                    else
                    {
                        throw new ConsoleException("CoApp.Service is not installed.");
                    }
                }

                if (!options.Any() && EngineServiceManager.IsServiceInstalled && parameters.FirstOrDefault() == null)
                {
                    // this lets us run the service
                    ServiceBase.Run(new CoAppService());
                    return(0);
                }

                if (_status)
                {
                    Console.WriteLine("Service installed: {0}", EngineServiceManager.IsServiceInstalled);
                    Console.WriteLine("Service running: {0}", EngineServiceManager.IsServiceRunning);
                    return(0);
                }

                if (!options.Any())
                {
                    throw new ConsoleException("Missing CoApp.Service command. Use --help for information");
                }
            } catch (ConsoleException e) {
                return(Fail(e.Message));
            } catch (Exception ex) {
                return(Fail("{0}\r\n{1}", ex.Message, ex.StackTrace));
            }
            return(0);
        }