HTTP service now listens but does nothing

This commit is contained in:
Madpeter 2022-03-17 00:21:30 +00:00
parent e9c1f744ae
commit 4558b695f5
6 changed files with 163 additions and 31 deletions

View file

@ -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
/// </summary>
public static Helpers.LogLevel LOG_LEVEL = Helpers.LogLevel.Debug;
public static Helpers.LogLevel LOG_LEVEL = Helpers.LogLevel.None;
/// <summary>Attach avatar names to log messages</summary>
public bool LOG_NAMES = true;
public bool LOG_NAMES = false;
/// <summary>Log packet retransmission info</summary>
public bool LOG_RESENDS = true;
public bool LOG_RESENDS = false;
/// <summary>Log disk cache misses and other info</summary>
public bool LOG_DISKCACHE = true;
public bool LOG_DISKCACHE = false;
#endregion
#region Private Fields

View file

@ -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()

View file

@ -25,6 +25,22 @@ namespace SecondBotEvents
public http HttpService = null;
public BotClient botClient = null;
private EventHandler<botClientNotice> botclient_eventNotices;
public event EventHandler<botClientNotice> 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<botClientNotice> 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;
}
}
}

View file

@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EmbedIO" Version="3.4.3" />
<PackageReference Include="MSBuildGitHash" Version="2.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View file

@ -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);
}
}
}

View file

@ -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<ConsoleLogger>();
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();
}
}
}