HTTP service now listens but does nothing
This commit is contained in:
parent
e9c1f744ae
commit
4558b695f5
6 changed files with 163 additions and 31 deletions
|
|
@ -342,16 +342,16 @@ namespace OpenMetaverse
|
||||||
/// You will get no output on the console. This behavior can be overriden by creating
|
/// You will get no output on the console. This behavior can be overriden by creating
|
||||||
/// a logger configuration file for log4net
|
/// a logger configuration file for log4net
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>Attach avatar names to log messages</summary>
|
||||||
public bool LOG_NAMES = true;
|
public bool LOG_NAMES = false;
|
||||||
|
|
||||||
/// <summary>Log packet retransmission info</summary>
|
/// <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>
|
/// <summary>Log disk cache misses and other info</summary>
|
||||||
public bool LOG_DISKCACHE = true;
|
public bool LOG_DISKCACHE = false;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
#region Private Fields
|
#region Private Fields
|
||||||
|
|
|
||||||
|
|
@ -11,17 +11,12 @@ namespace SecondBotEvents.Config
|
||||||
{
|
{
|
||||||
filename = "http";
|
filename = "http";
|
||||||
settings.Add("enabled");
|
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);
|
return readSetting("host", "http://*:80");
|
||||||
if(result == false)
|
|
||||||
{
|
|
||||||
return 8080;
|
|
||||||
}
|
|
||||||
return port;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool getEnabled()
|
public bool getEnabled()
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,22 @@ namespace SecondBotEvents
|
||||||
public http HttpService = null;
|
public http HttpService = null;
|
||||||
public BotClient botClient = 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()
|
public string getVersion()
|
||||||
{
|
{
|
||||||
return AssemblyInfo.GetGitHash();
|
return AssemblyInfo.GetGitHash();
|
||||||
|
|
@ -46,6 +62,7 @@ namespace SecondBotEvents
|
||||||
}
|
}
|
||||||
restartServices();
|
restartServices();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void restartServices()
|
public void restartServices()
|
||||||
{
|
{
|
||||||
stopServices();
|
stopServices();
|
||||||
|
|
@ -97,4 +114,13 @@ namespace SecondBotEvents
|
||||||
return attrs.FirstOrDefault(a => a.Key == "GitHash")?.Value;
|
return attrs.FirstOrDefault(a => a.Key == "GitHash")?.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class botClientNotice
|
||||||
|
{
|
||||||
|
public bool isRestart = false;
|
||||||
|
public botClientNotice(bool asRestart=false)
|
||||||
|
{
|
||||||
|
isRestart = asRestart;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="EmbedIO" Version="3.4.3" />
|
||||||
<PackageReference Include="MSBuildGitHash" Version="2.0.2">
|
<PackageReference Include="MSBuildGitHash" Version="2.0.2">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
|
|
||||||
|
|
@ -25,21 +25,51 @@ namespace SecondBotEvents.Services
|
||||||
resetClient();
|
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)
|
if (client != null)
|
||||||
{
|
{
|
||||||
client.Network.Logout();
|
client.Network.Logout();
|
||||||
|
isRestart = true;
|
||||||
}
|
}
|
||||||
client = null;
|
client = null;
|
||||||
client = new GridClient();
|
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()
|
public void login()
|
||||||
{
|
|
||||||
if (client == null)
|
|
||||||
{
|
{
|
||||||
resetClient();
|
resetClient();
|
||||||
}
|
|
||||||
LoginParams loginParams = new LoginParams(
|
LoginParams loginParams = new LoginParams(
|
||||||
client,
|
client,
|
||||||
basicCfg.getFirstName(),
|
basicCfg.getFirstName(),
|
||||||
|
|
@ -54,5 +84,7 @@ namespace SecondBotEvents.Services
|
||||||
}
|
}
|
||||||
client.Network.BeginLogin(loginParams);
|
client.Network.BeginLogin(loginParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,8 @@
|
||||||
using SecondBotEvents.Config;
|
using EmbedIO;
|
||||||
|
using EmbedIO.Actions;
|
||||||
|
using OpenMetaverse;
|
||||||
|
using SecondBotEvents.Config;
|
||||||
|
using Swan.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
@ -7,27 +11,101 @@ namespace SecondBotEvents.Services
|
||||||
{
|
{
|
||||||
internal class http: service
|
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)
|
public http(EventsSecondBot setMaster) : base(setMaster)
|
||||||
{
|
{
|
||||||
myConfig = new http_config(master.fromEnv,master.fromFolder);
|
myConfig = new http_config(master.fromEnv,master.fromFolder);
|
||||||
}
|
if (myConfig.getEnabled() == false)
|
||||||
public override void start()
|
|
||||||
{
|
{
|
||||||
if (myConfig.getEnabled() == true)
|
Console.WriteLine(status());
|
||||||
{
|
|
||||||
Console.WriteLine("HTTP service [Enabled]");
|
|
||||||
return;
|
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()
|
public override void stop()
|
||||||
{
|
{
|
||||||
if (myConfig.getEnabled() == false)
|
if (myConfig.getEnabled() == false)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if(HTTPendpoint != null)
|
||||||
|
{
|
||||||
|
HTTPendpoint.Dispose();
|
||||||
|
HTTPendpoint = null;
|
||||||
Console.WriteLine("HTTP service [Stopping]");
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue