コード例 #1
0
 OptionSet GetConnectCommandOptions(ConnectCommandParams prms = null)
 {
     return(new OptionSet
     {
         { "address|a=", "Specifies the address to connect to", v =>
           {
               prms.Address = v;
           } },
         { "port|p=", "Specifies the port to connect to", v =>
           {
               int port;
               if (!int.TryParse(v, out port))
               {
                   throw new OptionException("The port must be an integer", "-p");
               }
               prms.Port = port;
           } },
     });
 }
コード例 #2
0
ファイル: ServerPlugin.cs プロジェクト: huoxudong125/ServerX
        public void Init(Application application)
        {
            application.RegisterCommand(new ConsoleCommand
            {
                Title = "Service Manager",
                CommandAliases = new [] { "service", "svc", "s" },
                ShortDescription = "Controls the active state of the service",
                HelpOptions = GetServiceCommandOptions().WriteOptionDescriptions(),
                HelpUsage = "svc {start|stop|status} [options...]",
                HelpDescription = "Starts or stops the service manager, or obtains its status.",
                HelpRemarks = "Note that if no port is specified, the port will default to " + DefaultPort + ".",
                Handler = (app, command, args) =>
                {
                    app.DisplayServerNotifications = true;
                    try
                    {
                        var prms = new ServiceCommandParams();
                        var options = GetServiceCommandOptions(prms);
                        try
                        {
                            options.Parse(args);
                        }
                        catch(OptionException e)
                        {
                            ColorConsole.WriteLine("%!" + e.Message + "%!");
                            return null;
                        }

                        switch(prms.Action)
                        {
                            case ServiceAction.Status:
                                break;

                            case ServiceAction.Install:
                                bool success = true;
                                if(!prms.Local && app.IsWindowsServiceRunning)
                                    ColorConsole.WriteLine("%!Service is already installed.%!");
                                else
                                {
                                    success = app.StartHost(prms.Port, prms.Local);
                                    ColorConsole.WriteLine(success ? "%~Service now running on port " + prms.Port + "." : "%~Unable to start service on port " + prms.Port + ".");
                                }
                                if(prms.AutoConnect && success)
                                {
                                    ColorConsole.WriteLine(app.Connect("localhost", prms.Port));
                                    if(prms.Watch)
                                        Watch(app);
                                }
                                break;

                            case ServiceAction.Uninstall:
                                //if(!prms.Local && !app.IsWindowsServiceRunning)
                                //    return "%!Can't uninstall; service is not installed.";
                                ColorConsole.WriteLine(app.UninstallService() ? "%~Service uninstalled successfully." : "%!Service uninstallation failed.");
                                break;
                        }

                        if(prms.Exit && !prms.Watch)
                            app.ExitRequested = true;

                        return null;
                    }
                    finally
                    {
                        app.DisplayServerNotifications = false;
                    }
                }
            });

            application.RegisterCommand(new ConsoleCommand
            {
                Title = "Connect to Service Manager",
                CommandAliases = new [] { "connect", "conn", "c" },
                ShortDescription = "Attempts to connect to an active service manager",
                HelpDescription = "Connects to the service manager.",
                HelpOptions = GetConnectCommandOptions().WriteOptionDescriptions(),
                HelpUsage = "connect [options...]",
                HelpRemarks = "Note that if no port is specified, the port will default to " + DefaultPort + ".",
                Handler = (app, command, args) =>
                {
                    var prms = new ConnectCommandParams();
                    var options = GetConnectCommandOptions(prms);
                    try
                    {
                        options.Parse(args);
                    }
                    catch(OptionException e)
                    {
                        ColorConsole.WriteLine("%!" + e.Message + "%!");
                        return null;
                    }

                    return app.Connect(prms.Address, prms.Port);
                }
            });

            application.RegisterCommand(new ConsoleCommand
            {
                Title = "Watch for Notifications",
                CommandAliases = new [] { "watch", "w" },
                ShortDescription = "Displays notifications as they are received",
                HelpDescription = "Puts the console into an idle state where it will display any server notifications received.",
                Handler = (app, command, args) =>
                {
                    Watch(app);
                    return null;
                }
            });
        }
コード例 #3
0
ファイル: ServerPlugin.cs プロジェクト: huoxudong125/ServerX
 OptionSet GetConnectCommandOptions(ConnectCommandParams prms = null)
 {
     return new OptionSet
     {
         { "address|a=", "Specifies the address to connect to", v =>
             {
                 prms.Address = v;
             }
         },
         { "port|p=", "Specifies the port to connect to", v =>
             {
                 int port;
                 if(!int.TryParse(v, out port))
                     throw new OptionException("The port must be an integer", "-p");
                 prms.Port = port;
             }
         },
     };
 }
コード例 #4
0
        public void Init(Application application)
        {
            application.RegisterCommand(new ConsoleCommand
            {
                Title            = "Service Manager",
                CommandAliases   = new [] { "service", "svc", "s" },
                ShortDescription = "Controls the active state of the service",
                HelpOptions      = GetServiceCommandOptions().WriteOptionDescriptions(),
                HelpUsage        = "svc {start|stop|status} [options...]",
                HelpDescription  = "Starts or stops the service manager, or obtains its status.",
                HelpRemarks      = "Note that if no port is specified, the port will default to " + DefaultPort + ".",
                Handler          = (app, command, args) =>
                {
                    app.DisplayServerNotifications = true;
                    try
                    {
                        var prms    = new ServiceCommandParams();
                        var options = GetServiceCommandOptions(prms);
                        try
                        {
                            options.Parse(args);
                        }
                        catch (OptionException e)
                        {
                            ColorConsole.WriteLine("%!" + e.Message + "%!");
                            return(null);
                        }

                        switch (prms.Action)
                        {
                        case ServiceAction.Status:
                            break;

                        case ServiceAction.Install:
                            bool success = true;
                            if (!prms.Local && app.IsWindowsServiceRunning)
                            {
                                ColorConsole.WriteLine("%!Service is already installed.%!");
                            }
                            else
                            {
                                success = app.StartHost(prms.Port, prms.Local);
                                ColorConsole.WriteLine(success ? "%~Service now running on port " + prms.Port + "." : "%~Unable to start service on port " + prms.Port + ".");
                            }
                            if (prms.AutoConnect && success)
                            {
                                ColorConsole.WriteLine(app.Connect("localhost", prms.Port));
                                if (prms.Watch)
                                {
                                    Watch(app);
                                }
                            }
                            break;

                        case ServiceAction.Uninstall:
                            //if(!prms.Local && !app.IsWindowsServiceRunning)
                            //    return "%!Can't uninstall; service is not installed.";
                            ColorConsole.WriteLine(app.UninstallService() ? "%~Service uninstalled successfully." : "%!Service uninstallation failed.");
                            break;
                        }

                        if (prms.Exit && !prms.Watch)
                        {
                            app.ExitRequested = true;
                        }

                        return(null);
                    }
                    finally
                    {
                        app.DisplayServerNotifications = false;
                    }
                }
            });

            application.RegisterCommand(new ConsoleCommand
            {
                Title            = "Connect to Service Manager",
                CommandAliases   = new [] { "connect", "conn", "c" },
                ShortDescription = "Attempts to connect to an active service manager",
                HelpDescription  = "Connects to the service manager.",
                HelpOptions      = GetConnectCommandOptions().WriteOptionDescriptions(),
                HelpUsage        = "connect [options...]",
                HelpRemarks      = "Note that if no port is specified, the port will default to " + DefaultPort + ".",
                Handler          = (app, command, args) =>
                {
                    var prms    = new ConnectCommandParams();
                    var options = GetConnectCommandOptions(prms);
                    try
                    {
                        options.Parse(args);
                    }
                    catch (OptionException e)
                    {
                        ColorConsole.WriteLine("%!" + e.Message + "%!");
                        return(null);
                    }

                    return(app.Connect(prms.Address, prms.Port));
                }
            });

            application.RegisterCommand(new ConsoleCommand
            {
                Title            = "Watch for Notifications",
                CommandAliases   = new [] { "watch", "w" },
                ShortDescription = "Displays notifications as they are received",
                HelpDescription  = "Puts the console into an idle state where it will display any server notifications received.",
                Handler          = (app, command, args) =>
                {
                    Watch(app);
                    return(null);
                }
            });
        }