From 72b6197ff0c668d89d5d219e37353fdf879671a7 Mon Sep 17 00:00:00 2001 From: Andy Wu Date: Mon, 20 May 2024 18:06:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E9=A1=B9=E7=9B=AE=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=BD=92=E7=B1=BBCommandHandler=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Test.cs} | 10 ++--- Zero.Agent/CommandHandler/TransferCall.cs | 37 +++++++++++++++++++ .../WebWatchDog.cs} | 10 ++--- 3 files changed, 45 insertions(+), 12 deletions(-) rename Zero.Agent/{TestCommandHandler.cs => CommandHandler/Test.cs} (89%) create mode 100644 Zero.Agent/CommandHandler/TransferCall.cs rename Zero.Agent/{WebWatchDogCommandHandler.cs => CommandHandler/WebWatchDog.cs} (81%) diff --git a/Zero.Agent/TestCommandHandler.cs b/Zero.Agent/CommandHandler/Test.cs similarity index 89% rename from Zero.Agent/TestCommandHandler.cs rename to Zero.Agent/CommandHandler/Test.cs index 60722e6..18b3433 100644 --- a/Zero.Agent/TestCommandHandler.cs +++ b/Zero.Agent/CommandHandler/Test.cs @@ -4,15 +4,15 @@ using NewLife.Agent; using NewLife.Agent.Command; -namespace Zero.Agent +namespace Zero.Agent.CommandHandler { - public class TestCommandHandler : BaseCommandHandler + public class Test : BaseCommandHandler { public override String Cmd { get; set; } = "-test"; public override String Description { get; set; } = "测试自定义菜单"; public override Char? ShortcutKey { get; set; } = 't'; - public TestCommandHandler(ServiceBase service) : base(service) + public Test(ServiceBase service) : base(service) { //本功能主要是用于演示如何定义自己的命令处理器,所有命令处理器必需指定Cmd和Description, //ShortcutKey用于定义快捷键,如果不需要快捷键,可以不指定,将不会显示在菜单中 @@ -35,17 +35,13 @@ public override void Process(String[] args) for (var i = 1; i <= 9; i++) { for (var j = 1; j <= i; j++) - { Console.Write($"{j}x{i}={i * j}\t"); - } Console.WriteLine(); Thread.Sleep(200); } if (args.Contains("-showme")) - { Console.WriteLine("这是[测试自定义菜单]处理程序,显示了自定义参数 -showme"); - } } } } diff --git a/Zero.Agent/CommandHandler/TransferCall.cs b/Zero.Agent/CommandHandler/TransferCall.cs new file mode 100644 index 0000000..f23595f --- /dev/null +++ b/Zero.Agent/CommandHandler/TransferCall.cs @@ -0,0 +1,37 @@ +using System; +using System.Linq; +using System.Threading; +using NewLife.Agent; +using NewLife.Agent.Command; + +namespace Zero.Agent.CommandHandler +{ + public class TransferCall : BaseCommandHandler + { + public override String Cmd { get; set; } = "-TransferCall"; + public override String Description { get; set; } = "测试转调用其他命令"; + public override Char? ShortcutKey { get; set; } = 'w'; + + public TransferCall(ServiceBase service) : base(service) + { + //本功能主要是用于演示如何转调用其他命令处理器,并且可以传入自定义参数 + } + + public override Boolean IsShowMenu() + { + return true; + } + + public override void Process(String[] args) + { + Console.WriteLine("这是[测试转调用其他命令]处理程序"); + Thread.Sleep(1000); + Console.WriteLine("以下开始转调用 [测试自定义菜单],还可以传入自定义参数"); + Service.Command.Handle("-test", args.Concat(new[] { "-showme" }).ToArray()); + Thread.Sleep(1000); + Console.WriteLine("以下开始转调用 [显示状态]"); + Service.Command.Handle(CommandConst.ShowStatus, args); + Console.WriteLine("转调用 [测试自定义菜单] 已执行完毕。"); + } + } +} diff --git a/Zero.Agent/WebWatchDogCommandHandler.cs b/Zero.Agent/CommandHandler/WebWatchDog.cs similarity index 81% rename from Zero.Agent/WebWatchDogCommandHandler.cs rename to Zero.Agent/CommandHandler/WebWatchDog.cs index 90c46af..7aeadb0 100644 --- a/Zero.Agent/WebWatchDogCommandHandler.cs +++ b/Zero.Agent/CommandHandler/WebWatchDog.cs @@ -1,18 +1,18 @@ using System; using System.Threading; using NewLife.Agent; -using NewLife.Agent.Command; +using NewLife.Agent.CommandHandler; -namespace Zero.Agent +namespace Zero.Agent.CommandHandler { /// /// WEB服务看门狗命令处理器 /// - public class WebWatchDogCommandHandler : WatchDogCommandHandler + public class WebWatchDog : WatchDog { public override String Description { get; set; } = "WEB看门狗保护服务"; - public WebWatchDogCommandHandler(ServiceBase service) : base(service) + public WebWatchDog(ServiceBase service) : base(service) { //本功能主要是用于演示如何覆盖默认的看门狗保护服务,实现自己的看门狗逻辑,其他需要覆盖基础命令处理器也可以参照这个类实现 } @@ -20,7 +20,7 @@ public WebWatchDogCommandHandler(ServiceBase service) : base(service) public override void Process(String[] args) { Console.WriteLine("这是[WEB看门狗保护服务]处理程序,开始检查WEB服务是否可以正常访问"); - this.CheckWatchDog(); + CheckWatchDog(); } public override void CheckWatchDog()