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>();
Func<RPCContainer, RPCModule, MethodInfo, bool> checkAccessHook;
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(string moduleName,object moduleInstance)
@ -23,7 +30,7 @@ namespace ln.types.rpc
if (modules.ContainsKey(moduleName))
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);
modules.Add(moduleName, rpcModule);
}
@ -55,20 +62,26 @@ namespace ln.types.rpc
public class RPCModule
{
public RPCContainer Container { get; }
public String Name { get; }
public object ModuleInstance { get; }
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;
ModuleInstance = instance;
Initialize(ModuleInstance.GetType());
}
public RPCModule(string moduleName,Type type)
public RPCModule(RPCContainer container,string moduleName,Type type)
{
Container = container;
Name = moduleName;
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);
} catch (Exception e)