From 0742e45be72da6e6f4d237ae9652bc4470632d27 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Tue, 26 Nov 2019 12:22:09 +0100 Subject: [PATCH] Utilize HttpRouter, Implement WSHello --- Application.cs | 52 +++++++++++++++++++++-------------------- ApplicationSession.cs | 7 +++++- ApplicationWebSocket.cs | 35 ++++++++++++++++++++++----- ln.application.csproj | 2 ++ messages/WSHello.cs | 12 ++++++++++ 5 files changed, 76 insertions(+), 32 deletions(-) create mode 100644 messages/WSHello.cs diff --git a/Application.cs b/Application.cs index 3eeb28e..5fb9564 100644 --- a/Application.cs +++ b/Application.cs @@ -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 applicationSessions = new MappingBTree((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; } diff --git a/ApplicationSession.cs b/ApplicationSession.cs index 74b1f48..aae74fd 100644 --- a/ApplicationSession.cs +++ b/ApplicationSession.cs @@ -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; diff --git a/ApplicationWebSocket.cs b/ApplicationWebSocket.cs index 09f0e39..978c731 100644 --- a/ApplicationWebSocket.cs +++ b/ApplicationWebSocket.cs @@ -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 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); diff --git a/ln.application.csproj b/ln.application.csproj index c296d0e..c2dc1e1 100644 --- a/ln.application.csproj +++ b/ln.application.csproj @@ -44,6 +44,7 @@ + @@ -76,6 +77,7 @@ + diff --git a/messages/WSHello.cs b/messages/WSHello.cs new file mode 100644 index 0000000..24e16b2 --- /dev/null +++ b/messages/WSHello.cs @@ -0,0 +1,12 @@ +using System; +namespace ln.application.messages +{ + public class WSHello + { + public Guid ApplicationSessionID { get; set; } + + public WSHello() + { + } + } +}