Utilize HttpRouter, Implement WSHello
parent
af12fc21d4
commit
0742e45be7
|
@ -25,6 +25,7 @@ namespace ln.application
|
||||||
|
|
||||||
public ArgumentContainer Arguments { get; protected set; }
|
public ArgumentContainer Arguments { get; protected set; }
|
||||||
public HTTPServer HttpServer { get; private set; }
|
public HTTPServer HttpServer { get; private set; }
|
||||||
|
bool usesDedicatedHttpServer = false;
|
||||||
|
|
||||||
public ServiceContainer ServiceContainer { get; }
|
public ServiceContainer ServiceContainer { get; }
|
||||||
|
|
||||||
|
@ -39,8 +40,10 @@ namespace ln.application
|
||||||
|
|
||||||
MappingBTree<Guid, ApplicationSession> applicationSessions = new MappingBTree<Guid, ApplicationSession>((x)=>x.SessionID);
|
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;
|
IdentityProvider = identityProvider;
|
||||||
RPCContainer = rpcContainer ?? new RPCContainer(CheckRPCAccess);
|
RPCContainer = rpcContainer ?? new RPCContainer(CheckRPCAccess);
|
||||||
|
|
||||||
|
@ -59,8 +62,8 @@ namespace ln.application
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Application() :this(null,null) { }
|
public Application() :this(null,null,null) { }
|
||||||
public Application(IIdentityProvider identityProvider) : this(identityProvider,null) { }
|
public Application(IIdentityProvider identityProvider) : this(null,identityProvider,null) { }
|
||||||
|
|
||||||
bool CheckRPCAccess(RPCContainer container, RPCContainer.RPCModule module, System.Reflection.MethodInfo method)
|
bool CheckRPCAccess(RPCContainer container, RPCContainer.RPCModule module, System.Reflection.MethodInfo method)
|
||||||
{
|
{
|
||||||
|
@ -98,15 +101,18 @@ namespace ln.application
|
||||||
PrepareStart();
|
PrepareStart();
|
||||||
|
|
||||||
/* HTTP Server */
|
/* HTTP Server */
|
||||||
Logging.Log(LogLevel.INFO, "Start: HTTPServer on {0}:{1}", Arguments['l'].Value, Arguments['p'].IntegerValue);
|
if (HttpServer == null)
|
||||||
HttpServer = new HTTPServer();
|
{
|
||||||
HttpServer.AddEndpoint(new System.Net.IPEndPoint(
|
Logging.Log(LogLevel.INFO, "Start: HTTPServer on {0}:{1}", Arguments['l'].Value, Arguments['p'].IntegerValue);
|
||||||
IPAddress.Parse(Arguments['l'].Value),
|
HttpServer = new HTTPServer(new System.Net.IPEndPoint(
|
||||||
Arguments['p'].IntegerValue
|
IPAddress.Parse(Arguments['l'].Value),
|
||||||
));
|
Arguments['p'].IntegerValue
|
||||||
|
),
|
||||||
HttpServer.Start();
|
HttpRouter
|
||||||
HttpServer.DefaultApplication = this;
|
);
|
||||||
|
HttpServer.Start();
|
||||||
|
usesDedicatedHttpServer = true;
|
||||||
|
}
|
||||||
|
|
||||||
/* Application Services */
|
/* Application Services */
|
||||||
|
|
||||||
|
@ -115,7 +121,6 @@ namespace ln.application
|
||||||
if (serviceDefinition.AutoStart && !serviceDefinition.IsAlive)
|
if (serviceDefinition.AutoStart && !serviceDefinition.IsAlive)
|
||||||
ServiceContainer.Start(serviceDefinition);
|
ServiceContainer.Start(serviceDefinition);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
|
@ -133,8 +138,11 @@ namespace ln.application
|
||||||
}
|
}
|
||||||
|
|
||||||
/* HTTP Server */
|
/* HTTP Server */
|
||||||
Logging.Log(LogLevel.INFO, "Stopping HTTP server");
|
if (usesDedicatedHttpServer)
|
||||||
HttpServer.Stop();
|
{
|
||||||
|
Logging.Log(LogLevel.INFO, "Stopping HTTP server");
|
||||||
|
HttpServer.Stop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual ApplicationSession CreateApplicationSession() => new ApplicationSession(this);
|
public virtual ApplicationSession CreateApplicationSession() => new ApplicationSession(this);
|
||||||
|
@ -154,9 +162,10 @@ namespace ln.application
|
||||||
public ApplicationSession GetApplicationSession() => GetApplicationSession(Guid.Empty);
|
public ApplicationSession GetApplicationSession() => GetApplicationSession(Guid.Empty);
|
||||||
public ApplicationSession GetApplicationSession(Guid contextID)
|
public ApplicationSession GetApplicationSession(Guid contextID)
|
||||||
{
|
{
|
||||||
ApplicationSession applicationSession;
|
ApplicationSession applicationSession = null;
|
||||||
if (Guid.Empty.Equals(contextID))
|
|
||||||
{
|
if (!applicationSessions.TryGet(contextID,ref applicationSession))
|
||||||
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
applicationSession = CreateApplicationSession();
|
applicationSession = CreateApplicationSession();
|
||||||
|
@ -167,13 +176,6 @@ namespace ln.application
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!applicationSessions.ContainsKey(contextID))
|
|
||||||
throw new KeyNotFoundException();
|
|
||||||
|
|
||||||
applicationSession = applicationSessions[contextID];
|
|
||||||
}
|
|
||||||
return applicationSession;
|
return applicationSession;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,10 @@ namespace ln.application
|
||||||
public static ApplicationSession CurrentSession => currentApplicationSession.Value;
|
public static ApplicationSession CurrentSession => currentApplicationSession.Value;
|
||||||
|
|
||||||
public Guid SessionID { get; }
|
public Guid SessionID { get; }
|
||||||
public DateTime Created { get; }
|
|
||||||
|
public Guid Secret { get; }
|
||||||
|
|
||||||
|
public DateTime Created { get; }
|
||||||
public DateTime LastAccess { get; protected set; }
|
public DateTime LastAccess { get; protected set; }
|
||||||
|
|
||||||
public TimeSpan Age => LastAccess - Created;
|
public TimeSpan Age => LastAccess - Created;
|
||||||
|
@ -28,6 +31,8 @@ namespace ln.application
|
||||||
public ApplicationSession(Application application)
|
public ApplicationSession(Application application)
|
||||||
{
|
{
|
||||||
SessionID = Guid.NewGuid();
|
SessionID = Guid.NewGuid();
|
||||||
|
Secret = Guid.NewGuid();
|
||||||
|
|
||||||
Created = DateTime.Now;
|
Created = DateTime.Now;
|
||||||
LastAccess = Created;
|
LastAccess = Created;
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,8 @@ using System.Collections.Generic;
|
||||||
using ln.application.slots;
|
using ln.application.slots;
|
||||||
using ln.json.mapping;
|
using ln.json.mapping;
|
||||||
using ln.identities;
|
using ln.identities;
|
||||||
|
using ln.application.messages;
|
||||||
|
using ln.http.resources;
|
||||||
namespace ln.application
|
namespace ln.application
|
||||||
{
|
{
|
||||||
public class ApplicationWebSocket : WebSocket
|
public class ApplicationWebSocket : WebSocket
|
||||||
|
@ -17,11 +19,13 @@ namespace ln.application
|
||||||
static public Func<JSONValue, object> FindConverter(string messageType) => messageConverters[messageType];
|
static public Func<JSONValue, object> FindConverter(string messageType) => messageConverters[messageType];
|
||||||
|
|
||||||
public Application Application { get; }
|
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()
|
static ApplicationWebSocket()
|
||||||
{
|
{
|
||||||
|
RegisterMessageConverter("WSHello", (JSONValue json) => JSONMapper.DefaultMapper.FromJson(json, typeof(WSHello)));
|
||||||
RegisterMessageConverter("RPCCall", (JSONValue json) => JSONMapper.DefaultMapper.FromJson(json, typeof(RPCCall)));
|
RegisterMessageConverter("RPCCall", (JSONValue json) => JSONMapper.DefaultMapper.FromJson(json, typeof(RPCCall)));
|
||||||
RegisterMessageConverter("SlotRequest", (JSONValue json) => JSONMapper.DefaultMapper.FromJson(json, typeof(SlotRequest)));
|
RegisterMessageConverter("SlotRequest", (JSONValue json) => JSONMapper.DefaultMapper.FromJson(json, typeof(SlotRequest)));
|
||||||
RegisterMessageConverter("AuthenticationProve", (JSONValue json) => JSONMapper.DefaultMapper.FromJson(json, typeof(AuthenticationProve)));
|
RegisterMessageConverter("AuthenticationProve", (JSONValue json) => JSONMapper.DefaultMapper.FromJson(json, typeof(AuthenticationProve)));
|
||||||
|
@ -81,14 +85,33 @@ namespace ln.application
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
object result = ApplicationSession?.ProcessMessage(message);
|
object result;
|
||||||
if (result is null)
|
|
||||||
|
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);
|
SendMessage(messageId, result);
|
||||||
|
|
|
@ -44,6 +44,7 @@
|
||||||
<Compile Include="slots\SlotAllocation.cs" />
|
<Compile Include="slots\SlotAllocation.cs" />
|
||||||
<Compile Include="json\JSONIdentityMapping.cs" />
|
<Compile Include="json\JSONIdentityMapping.cs" />
|
||||||
<Compile Include="ApplicationRPC.cs" />
|
<Compile Include="ApplicationRPC.cs" />
|
||||||
|
<Compile Include="messages\WSHello.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\ln.types\ln.types.csproj">
|
<ProjectReference Include="..\ln.types\ln.types.csproj">
|
||||||
|
@ -76,6 +77,7 @@
|
||||||
<Folder Include="www\" />
|
<Folder Include="www\" />
|
||||||
<Folder Include="slots\" />
|
<Folder Include="slots\" />
|
||||||
<Folder Include="json\" />
|
<Folder Include="json\" />
|
||||||
|
<Folder Include="messages\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="www\ln.application.js">
|
<None Include="www\ln.application.js">
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
using System;
|
||||||
|
namespace ln.application.messages
|
||||||
|
{
|
||||||
|
public class WSHello
|
||||||
|
{
|
||||||
|
public Guid ApplicationSessionID { get; set; }
|
||||||
|
|
||||||
|
public WSHello()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue