Develop RPCContainer

master
Harald Wolff 2019-11-24 15:13:38 +01:00
parent aaf7da496f
commit e780feaafd
1 changed files with 20 additions and 4 deletions

View File

@ -13,9 +13,16 @@ namespace ln.types.rpc
{ {
Dictionary<string, RPCModule> modules = new Dictionary<string, RPCModule>(); Dictionary<string, RPCModule> modules = new Dictionary<string, RPCModule>();
Func<RPCContainer, RPCModule, MethodInfo, bool> checkAccessHook;
public RPCContainer() public RPCContainer()
{ {
} }
public RPCContainer(Func<RPCContainer, RPCModule, MethodInfo, bool> checkAccessHook)
{
this.checkAccessHook = checkAccessHook;
}
public void Add(object moduleInstance) => Add(moduleInstance.GetType().Name, moduleInstance); public void Add(object moduleInstance) => Add(moduleInstance.GetType().Name, moduleInstance);
public void Add(string moduleName,object moduleInstance) public void Add(string moduleName,object moduleInstance)
@ -23,7 +30,7 @@ namespace ln.types.rpc
if (modules.ContainsKey(moduleName)) if (modules.ContainsKey(moduleName))
throw new ArgumentOutOfRangeException(nameof(moduleName), "Module with same name already added"); throw new ArgumentOutOfRangeException(nameof(moduleName), "Module with same name already added");
RPCModule rpcModule = new RPCModule(moduleName,moduleInstance); RPCModule rpcModule = new RPCModule(this,moduleName,moduleInstance);
Logging.Log(LogLevel.DEBUG, "ServiceContainer: Adding RPC Module: {0}", moduleName); Logging.Log(LogLevel.DEBUG, "ServiceContainer: Adding RPC Module: {0}", moduleName);
modules.Add(moduleName, rpcModule); modules.Add(moduleName, rpcModule);
} }
@ -55,20 +62,26 @@ namespace ln.types.rpc
public class RPCModule public class RPCModule
{ {
public RPCContainer Container { get; }
public String Name { get; } public String Name { get; }
public object ModuleInstance { get; } public object ModuleInstance { get; }
public Dictionary<string, MethodInfo> methodInfos = new Dictionary<string, MethodInfo>(); public Dictionary<string, MethodInfo> methodInfos = new Dictionary<string, MethodInfo>();
public RPCModule(string moduleName,object instance) public RPCModule(RPCContainer container,string moduleName,object instance)
{ {
Container = container;
Name = moduleName; Name = moduleName;
ModuleInstance = instance; ModuleInstance = instance;
Initialize(ModuleInstance.GetType()); Initialize(ModuleInstance.GetType());
} }
public RPCModule(string moduleName,Type type) public RPCModule(RPCContainer container,string moduleName,Type type)
{ {
Container = container;
Name = moduleName; Name = moduleName;
ModuleInstance = null; ModuleInstance = null;
@ -130,7 +143,10 @@ namespace ln.types.rpc
} }
} }
object result = methodInfo.Invoke(ModuleInstance, parameters);
object result = null;
if ((Container.checkAccessHook == null) || Container.checkAccessHook(Container,this,methodInfo))
result = methodInfo.Invoke(ModuleInstance, parameters);
return new RPCResult(call, result); return new RPCResult(call, result);
} catch (Exception e) } catch (Exception e)