|
@@ -0,0 +1,106 @@
|
|
|
+using System.Diagnostics;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using JsonRpcLite.InProcess;
|
|
|
+using JsonRpcLite.Network;
|
|
|
+using JsonRpcLite.Rpc;
|
|
|
+using WingCloudServer;
|
|
|
+using WingServerCommon.Service;
|
|
|
+
|
|
|
+namespace src
|
|
|
+{
|
|
|
+ internal class WingServer
|
|
|
+ {
|
|
|
+ private JsonRpcServer _rpcHttpServer;
|
|
|
+ private JsonRpcServer _rpcInProcessServer;
|
|
|
+ public WingServer(int port)
|
|
|
+ {
|
|
|
+ _rpcInProcessServer = new JsonRpcServer();
|
|
|
+ var inProcessEngine = new JsonRpcInProcessEngine();
|
|
|
+ _rpcInProcessServer.UseEngine(inProcessEngine);
|
|
|
+
|
|
|
+
|
|
|
+ _rpcHttpServer = new JsonRpcServer();
|
|
|
+ var jsonRpcHttpServerEngine = new JsonRpcHttpServerEngine($"http://*:{port}/");
|
|
|
+ _rpcHttpServer.UseEngine(jsonRpcHttpServerEngine);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Start server to load all service
|
|
|
+ /// </summary>
|
|
|
+ internal void Start(){
|
|
|
+
|
|
|
+ InitializeServices();
|
|
|
+ _rpcInProcessServer.Start();
|
|
|
+ _rpcHttpServer.Start();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Stop server
|
|
|
+ /// </summary>
|
|
|
+ internal void Stop()
|
|
|
+ {
|
|
|
+ //TODO dispose service
|
|
|
+ _rpcHttpServer.Stop();
|
|
|
+ _rpcInProcessServer.Stop();
|
|
|
+ }
|
|
|
+
|
|
|
+ void InitializeServices()
|
|
|
+ {
|
|
|
+ //initiaize rpc client pool
|
|
|
+ var remoteRpcHttpServices = new RemoteServiceInfo[0]; //TODO from settings
|
|
|
+ var rpcClientPool = new JsonRpcClientPool();
|
|
|
+ rpcClientPool.Initialize(remoteRpcHttpServices.ToArray());
|
|
|
+
|
|
|
+ var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Services"); //TODO from settings
|
|
|
+ var inProcessServices = new List<string>(){"WingEmailModule", "WingMongoDBModule","WingSessionModule", "WingSMSModule" }; //TODO from settings
|
|
|
+ var rpcHttpServices = new List<string>(){"WingManagementModule", "WingStorageModule","WingUserModule"}; //TODO from settings
|
|
|
+
|
|
|
+ if (!Directory.Exists(folder))
|
|
|
+ {
|
|
|
+ throw new NotSupportedException($"Folder {folder} not exist");
|
|
|
+ }
|
|
|
+
|
|
|
+ //InProcess service load and register to InPrcess rpc server
|
|
|
+ foreach (var service in inProcessServices)
|
|
|
+ {
|
|
|
+ var wingServiceType = LoadService(folder, service);
|
|
|
+ var instance = Activator.CreateInstance(wingServiceType);
|
|
|
+ var method = wingServiceType.GetMethod("Load");
|
|
|
+ method.Invoke(instance, new object[] { rpcClientPool});
|
|
|
+ _rpcInProcessServer.RegisterService(wingServiceType, instance); //TODO register one service or multi ?
|
|
|
+ }
|
|
|
+
|
|
|
+ //Rpc http service load and register to rpc http server
|
|
|
+ foreach (var service in rpcHttpServices)
|
|
|
+ {
|
|
|
+ var wingServiceType = LoadService(folder, service);
|
|
|
+ var instance = Activator.CreateInstance(wingServiceType);
|
|
|
+ var method = wingServiceType.GetMethod("Load");
|
|
|
+ method.Invoke(instance, new object[] { rpcClientPool});
|
|
|
+ _rpcHttpServer.RegisterService(wingServiceType, instance); //TODO register one service or multi ?
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Type LoadService(string folder, object serviceName)
|
|
|
+ {
|
|
|
+ var file = Path.Combine(folder, $"{serviceName}.dll");
|
|
|
+ if (!Directory.Exists(folder))
|
|
|
+ {
|
|
|
+ throw new NotSupportedException($"File {file} not exist");
|
|
|
+ }
|
|
|
+ var assemblyLoader = new AssemblyLoader();
|
|
|
+ var assembly = assemblyLoader.LoadAssemblyDependencies(file);
|
|
|
+ var types = assembly.GetTypes();
|
|
|
+ var wingServiceType = types.FirstOrDefault(x=>typeof(JsonRpcService).IsAssignableFrom(x) && x.IsClass && !x.IsAbstract);
|
|
|
+ if (wingServiceType == null)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException($"WingService not find in {file}");
|
|
|
+ }
|
|
|
+
|
|
|
+ return wingServiceType;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|