Utilize HttpRouter, Implement WSHello

master
Harald Wolff 2019-11-26 12:22:09 +01:00
parent af12fc21d4
commit 0742e45be7
5 changed files with 76 additions and 32 deletions

View File

@ -25,6 +25,7 @@ namespace ln.application
public ArgumentContainer Arguments { get; protected set; }
public HTTPServer HttpServer { get; private set; }
bool usesDedicatedHttpServer = false;
public ServiceContainer ServiceContainer { get; }
@ -39,8 +40,10 @@ namespace ln.application
MappingBTree<Guid, ApplicationSession> applicationSessions = new MappingBTree<Guid, ApplicationSession>((x)=>x.SessionID);
public Application(IIdentityProvider identityProvider,RPCContainer rpcContainer)
public Application(HTTPServer httpServer, IIdentityProvider identityProvider,RPCContainer rpcContainer)
{
HttpServer = httpServer;
IdentityProvider = identityProvider;
RPCContainer = rpcContainer ?? new RPCContainer(CheckRPCAccess);
@ -59,8 +62,8 @@ namespace ln.application
}
public Application() :this(null,null) { }
public Application(IIdentityProvider identityProvider) : this(identityProvider,null) { }
public Application() :this(null,null,null) { }
public Application(IIdentityProvider identityProvider) : this(null,identityProvider,null) { }
bool CheckRPCAccess(RPCContainer container, RPCContainer.RPCModule module, System.Reflection.MethodInfo method)
{
@ -98,15 +101,18 @@ namespace ln.application
PrepareStart();
/* HTTP Server */
Logging.Log(LogLevel.INFO, "Start: HTTPServer on {0}:{1}", Arguments['l'].Value, Arguments['p'].IntegerValue);
HttpServer = new HTTPServer();
HttpServer.AddEndpoint(new System.Net.IPEndPoint(
IPAddress.Parse(Arguments['l'].Value),
Arguments['p'].IntegerValue
));
HttpServer.Start();
HttpServer.DefaultApplication = this;
if (HttpServer == null)
{
Logging.Log(LogLevel.INFO, "Start: HTTPServer on {0}:{1}", Arguments['l'].Value, Arguments['p'].IntegerValue);
HttpServer = new HTTPServer(new System.Net.IPEndPoint(
IPAddress.Parse(Arguments['l'].Value),
Arguments['p'].IntegerValue
),
HttpRouter
);
HttpServer.Start();
usesDedicatedHttpServer = true;
}
/* Application Services */
@ -115,7 +121,6 @@ namespace ln.application
if (serviceDefinition.AutoStart && !serviceDefinition.IsAlive)
ServiceContainer.Start(serviceDefinition);
}
}
public void Stop()
{
@ -133,8 +138,11 @@ namespace ln.application
}
/* HTTP Server */
Logging.Log(LogLevel.INFO, "Stopping HTTP server");
HttpServer.Stop();
if (usesDedicatedHttpServer)
{
Logging.Log(LogLevel.INFO, "Stopping HTTP server");
HttpServer.Stop();
}
}
public virtual ApplicationSession CreateApplicationSession() => new ApplicationSession(this);
@ -154,9 +162,10 @@ namespace ln.application
public ApplicationSession GetApplicationSession() => GetApplicationSession(Guid.Empty);
public ApplicationSession GetApplicationSession(Guid contextID)
{
ApplicationSession applicationSession;
if (Guid.Empty.Equals(contextID))
{
ApplicationSession applicationSession = null;
if (!applicationSessions.TryGet(contextID,ref applicationSession))
{
while (true)
{
applicationSession = CreateApplicationSession();
@ -167,13 +176,6 @@ namespace ln.application
}
}
}
else
{
if (!applicationSessions.ContainsKey(contextID))
throw new KeyNotFoundException();
applicationSession = applicationSessions[contextID];
}
return applicationSession;
}

View File

@ -16,7 +16,10 @@ namespace ln.application
public static ApplicationSession CurrentSession => currentApplicationSession.Value;
public Guid SessionID { get; }
public DateTime Created { get; }
public Guid Secret { get; }
public DateTime Created { get; }
public DateTime LastAccess { get; protected set; }
public TimeSpan Age => LastAccess - Created;
@ -28,6 +31,8 @@ namespace ln.application
public ApplicationSession(Application application)
{
SessionID = Guid.NewGuid();
Secret = Guid.NewGuid();
Created = DateTime.Now;
LastAccess = Created;

View File

@ -8,6 +8,8 @@ using System.Collections.Generic;
using ln.application.slots;
using ln.json.mapping;
using ln.identities;
using ln.application.messages;
using ln.http.resources;
namespace ln.application
{
public class ApplicationWebSocket : WebSocket
@ -17,11 +19,13 @@ namespace ln.application
static public Func<JSONValue, object> FindConverter(string messageType) => messageConverters[messageType];
public Application Application { get; }
public ApplicationSession ApplicationSession => Application.GetApplicationSession();
public ApplicationSession ApplicationSession => (Hello != null) ? Application.GetApplicationSession(Hello.ApplicationSessionID) : null;
public WSHello Hello { get; set; }
static ApplicationWebSocket()
{
RegisterMessageConverter("WSHello", (JSONValue json) => JSONMapper.DefaultMapper.FromJson(json, typeof(WSHello)));
RegisterMessageConverter("RPCCall", (JSONValue json) => JSONMapper.DefaultMapper.FromJson(json, typeof(RPCCall)));
RegisterMessageConverter("SlotRequest", (JSONValue json) => JSONMapper.DefaultMapper.FromJson(json, typeof(SlotRequest)));
RegisterMessageConverter("AuthenticationProve", (JSONValue json) => JSONMapper.DefaultMapper.FromJson(json, typeof(AuthenticationProve)));
@ -81,14 +85,33 @@ namespace ln.application
try
{
object result = ApplicationSession?.ProcessMessage(message);
if (result is null)
object result;
if (message is WSHello hello)
{
result = Application.ProcessMessage(message);
if (Hello == null)
{
Hello = hello;
hello.ApplicationSessionID = ApplicationSession.SessionID;
result = hello;
}
else
{
throw new ArgumentException("WSHello already received!");
}
}
if (result is null)
else
{
throw new NotImplementedException();
result = ApplicationSession?.ProcessMessage(message);
if (result is null)
{
result = Application.ProcessMessage(message);
}
if (result is null)
{
throw new NotImplementedException();
}
}
SendMessage(messageId, result);

View File

@ -44,6 +44,7 @@
<Compile Include="slots\SlotAllocation.cs" />
<Compile Include="json\JSONIdentityMapping.cs" />
<Compile Include="ApplicationRPC.cs" />
<Compile Include="messages\WSHello.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ln.types\ln.types.csproj">
@ -76,6 +77,7 @@
<Folder Include="www\" />
<Folder Include="slots\" />
<Folder Include="json\" />
<Folder Include="messages\" />
</ItemGroup>
<ItemGroup>
<None Include="www\ln.application.js">

View File

@ -0,0 +1,12 @@
using System;
namespace ln.application.messages
{
public class WSHello
{
public Guid ApplicationSessionID { get; set; }
public WSHello()
{
}
}
}