From 4558b695f5568758d95ba6e3818a82f75ae01dcc Mon Sep 17 00:00:00 2001 From: Madpeter <57852827+Madpeterz@users.noreply.github.com> Date: Thu, 17 Mar 2022 00:21:30 +0000 Subject: [PATCH] HTTP service now listens but does nothing --- LibreMetaverse/Settings.cs | 8 +-- SecondBotEvents/Config/http_config.cs | 11 +-- SecondBotEvents/EventsSecondBot.cs | 26 +++++++ SecondBotEvents/SecondBotEvents.csproj | 1 + SecondBotEvents/Services/BotClient.cs | 52 +++++++++++--- SecondBotEvents/Services/http.cs | 96 +++++++++++++++++++++++--- 6 files changed, 163 insertions(+), 31 deletions(-) diff --git a/LibreMetaverse/Settings.cs b/LibreMetaverse/Settings.cs index f13b703..fcc04fe 100644 --- a/LibreMetaverse/Settings.cs +++ b/LibreMetaverse/Settings.cs @@ -342,16 +342,16 @@ namespace OpenMetaverse /// You will get no output on the console. This behavior can be overriden by creating /// a logger configuration file for log4net /// - public static Helpers.LogLevel LOG_LEVEL = Helpers.LogLevel.Debug; + public static Helpers.LogLevel LOG_LEVEL = Helpers.LogLevel.None; /// Attach avatar names to log messages - public bool LOG_NAMES = true; + public bool LOG_NAMES = false; /// Log packet retransmission info - public bool LOG_RESENDS = true; + public bool LOG_RESENDS = false; /// Log disk cache misses and other info - public bool LOG_DISKCACHE = true; + public bool LOG_DISKCACHE = false; #endregion #region Private Fields diff --git a/SecondBotEvents/Config/http_config.cs b/SecondBotEvents/Config/http_config.cs index de6dffc..cafe5f1 100644 --- a/SecondBotEvents/Config/http_config.cs +++ b/SecondBotEvents/Config/http_config.cs @@ -11,17 +11,12 @@ namespace SecondBotEvents.Config { filename = "http"; settings.Add("enabled"); - settings.Add("port"); + settings.Add("host"); } - public int getPort() + public string getHost() { - bool result = int.TryParse(readSetting("port", "8080"), out int port); - if(result == false) - { - return 8080; - } - return port; + return readSetting("host", "http://*:80"); } public bool getEnabled() diff --git a/SecondBotEvents/EventsSecondBot.cs b/SecondBotEvents/EventsSecondBot.cs index e25fc71..04d6e28 100644 --- a/SecondBotEvents/EventsSecondBot.cs +++ b/SecondBotEvents/EventsSecondBot.cs @@ -25,6 +25,22 @@ namespace SecondBotEvents public http HttpService = null; public BotClient botClient = null; + + private EventHandler botclient_eventNotices; + public event EventHandler botClientNoticeEvent + { + add { lock (botclient_event_attach) { botclient_eventNotices += value; } } + remove { lock (botclient_event_attach) { botclient_eventNotices -= value; } } + } + + private readonly object botclient_event_attach = new object(); + + public void triggerBotClientEvent(bool asRestart) + { + EventHandler handler = botclient_eventNotices; + handler?.Invoke(this, new botClientNotice(asRestart)); + } + public string getVersion() { return AssemblyInfo.GetGitHash(); @@ -46,6 +62,7 @@ namespace SecondBotEvents } restartServices(); } + public void restartServices() { stopServices(); @@ -97,4 +114,13 @@ namespace SecondBotEvents return attrs.FirstOrDefault(a => a.Key == "GitHash")?.Value; } } + + public class botClientNotice + { + public bool isRestart = false; + public botClientNotice(bool asRestart=false) + { + isRestart = asRestart; + } + } } diff --git a/SecondBotEvents/SecondBotEvents.csproj b/SecondBotEvents/SecondBotEvents.csproj index 27d98e9..cfdf9d8 100644 --- a/SecondBotEvents/SecondBotEvents.csproj +++ b/SecondBotEvents/SecondBotEvents.csproj @@ -10,6 +10,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/SecondBotEvents/Services/BotClient.cs b/SecondBotEvents/Services/BotClient.cs index 09aa854..4da2011 100644 --- a/SecondBotEvents/Services/BotClient.cs +++ b/SecondBotEvents/Services/BotClient.cs @@ -6,7 +6,7 @@ using System.Text; namespace SecondBotEvents.Services { - internal class BotClient: service + internal class BotClient : service { public GridClient client = null; public basic_config basicCfg = null; @@ -25,27 +25,57 @@ namespace SecondBotEvents.Services resetClient(); } - public void resetClient() + protected void botLoggedOut(object o, LoggedOutEventArgs e) { + Console.WriteLine("Client service ~ Logged out"); + } + + protected void botLoggedIn(object o, SimConnectedEventArgs e) + { + Console.WriteLine("Client service ~ Connected to sim: "+e.Simulator.Name); + } + + protected void botLoseSim(object o, SimDisconnectedEventArgs e) + { + Console.WriteLine("Client service ~ Disconnected from sim: " + e.Simulator.Name); + } + + protected void botDisconnected(object o, DisconnectedEventArgs e) + { + Console.WriteLine("Client service ~ Network lost: " + e.Message); + } + + protected void botLoginStatus(object o, LoginProgressEventArgs e) + { + Console.WriteLine("Client service ~ Login status: " + e.Status.ToString()); + } + + protected void resetClient() + { + bool isRestart = false; if (client != null) { client.Network.Logout(); + isRestart = true; } client = null; client = new GridClient(); + master.triggerBotClientEvent(isRestart); + client.Network.SimConnected += botLoggedIn; + client.Network.LoggedOut += botLoggedOut; + client.Network.Disconnected += botDisconnected; + client.Network.SimDisconnected += botLoseSim; + client.Network.LoginProgress += botLoginStatus; } public void login() { - if (client == null) - { - resetClient(); - } + resetClient(); LoginParams loginParams = new LoginParams( client, - basicCfg.getFirstName(), - basicCfg.getLastName(), - basicCfg.getPassword(), - "secondbot", + basicCfg.getFirstName(), + basicCfg.getLastName(), + basicCfg.getPassword(), + "secondbot", master.getVersion() ); if (basicCfg.getLoginURI() != "secondlife") @@ -54,5 +84,7 @@ namespace SecondBotEvents.Services } client.Network.BeginLogin(loginParams); } + + } } diff --git a/SecondBotEvents/Services/http.cs b/SecondBotEvents/Services/http.cs index 57deb04..ec0b536 100644 --- a/SecondBotEvents/Services/http.cs +++ b/SecondBotEvents/Services/http.cs @@ -1,4 +1,8 @@ -using SecondBotEvents.Config; +using EmbedIO; +using EmbedIO.Actions; +using OpenMetaverse; +using SecondBotEvents.Config; +using Swan.Logging; using System; using System.Collections.Generic; using System.Text; @@ -7,27 +11,101 @@ namespace SecondBotEvents.Services { internal class http: service { - http_config myConfig; + protected WebServer HTTPendpoint = null; + protected http_config myConfig; + protected bool acceptBotCommands = false; public http(EventsSecondBot setMaster) : base(setMaster) { myConfig = new http_config(master.fromEnv,master.fromFolder); - } - public override void start() - { - if (myConfig.getEnabled() == true) + if (myConfig.getEnabled() == false) { - Console.WriteLine("HTTP service [Enabled]"); + Console.WriteLine(status()); return; } - Console.WriteLine("HTTP service [Disabled]"); + master.botClientNoticeEvent += botClientRestart; } + + public string status() + { + if(myConfig == null) + { + return "HTTP service [Config status broken]"; + } + if (myConfig.getEnabled() == false) + { + return "HTTP service [- Not requested -]"; + } + // @todo HTTP server check here + if(HTTPendpoint == null) + { + return "HTTP endpoint not online"; + } + if (acceptBotCommands == false) + { + return "Active [Service]"; + } + return "Active [Commands]"; + } + + protected void botClientRestart(object o, botClientNotice e) + { + acceptBotCommands = false; + Console.WriteLine("HTTP service [Attached to new client]"); + master.botClient.client.Network.LoggedOut += botLoggedOut; + master.botClient.client.Network.SimConnected += botLoggedIn; + } + + protected void botLoggedOut(object o, LoggedOutEventArgs e) + { + acceptBotCommands = false; + master.botClient.client.Network.SimConnected += botLoggedIn; + Console.WriteLine("HTTP service [Bot commands disabled]"); + } + + protected void botLoggedIn(object o, SimConnectedEventArgs e) + { + master.botClient.client.Network.SimConnected -= botLoggedIn; + acceptBotCommands = true; + Console.WriteLine("HTTP service [Bot commands enabled]"); + } + + public override void start() + { + if (myConfig.getEnabled() == false) + { + Console.WriteLine("HTTP service [Disabled]"); + return; + } + stop(); + Console.WriteLine("HTTP service [Enabled]"); + CreateWebServer(); + } + public override void stop() { if (myConfig.getEnabled() == false) { return; } - Console.WriteLine("HTTP service [Stopping]"); + if(HTTPendpoint != null) + { + HTTPendpoint.Dispose(); + HTTPendpoint = null; + Console.WriteLine("HTTP service [Stopping]"); + } + + } + + private void CreateWebServer() + { + Swan.Logging.Logger.UnregisterLogger(); + HTTPendpoint = new WebServer(o => o + .WithUrlPrefix(myConfig.getHost()) + .WithMode(HttpListenerMode.EmbedIO)) + .WithCors() + .WithModule(new ActionModule("/", HttpVerbs.Any, ctx => ctx.SendDataAsync(new { Message = "Error" })) + ); + HTTPendpoint.RunAsync(); } } }