/** * Handles RPC calls. * Note that AddXRHandler and RemoveXRHandler calls are only accepted when * they are made by local Brunet node. */ public void HandleRpc(ISender caller, string method, IList args, object rs) { if (method.Equals("AddXRHandler") || method.Equals("RemoveXRHandler")) { ReqrepManager.ReplyState s = (ReqrepManager.ReplyState)caller; ISender sender = s.ReturnPath; if (Object.ReferenceEquals(_node, sender)) { if (args.Count == 2) { if (method.Equals("AddXRHandler")) { this.AddXRHandler(args[0] as string, args[1] as string); } else { this.RemoveXRHandler(args[0] as string, args[1] as string); } _rpc.SendResult(rs, null); return; } else { throw new ArgumentException("2 arguments expected"); } } else { throw new AdrException(-32602, "This operation is only accessible for local calls"); } } else { object result = null; try { Type type = this.GetType(); MethodInfo mi = type.GetMethod(method); object[] arg_array = new object[args.Count]; args.CopyTo(arg_array, 0); result = mi.Invoke(this, arg_array); } catch (Exception e) { result = new AdrException(-32602, e); } _rpc.SendResult(rs, result); } }
// Provides a method for local apps to add certificates to Brunet without // being loaded with Brunet. public void HandleRpc(ISender caller, string method, IList args, object rs) { object result = null; try { if (method.Equals("AddCertificate")) { ReqrepManager.ReplyState rqrs = caller as ReqrepManager.ReplyState; if (rqrs == null || !(rqrs.ReturnPath is Node)) { throw new Exception("Call must be made locally for security reasons!"); } string path = (string)args[0]; result = _ch.AddCertificate(path); } else if (method.Equals("GetState")) { if (args.Count != 1) { throw new Exception("Not enough arguments"); } else if (!(args[0] is string)) { throw new Exception("Argument should be a string"); } Address addr = AddressParser.Parse(args[0] as string); SecurityAssociation sa = CheckForSecureSender(addr); if (sa == null) { result = "No SA"; } else { result = sa.ToString(); } } else { result = new Exception("Invalid method"); } } catch (Exception e) { result = e; } _node.Rpc.SendResult(rs, result); }
// Provides a method for local apps to add certificates to Brunet without // being loaded with Brunet. public void HandleRpc(ISender caller, string method, IList args, object rs) { object result = null; try { if (method.Equals("AddCertificate")) { ReqrepManager.ReplyState rqrs = caller as ReqrepManager.ReplyState; if (rqrs == null || !(rqrs.ReturnPath is Node)) { throw new Exception("Call must be made locally for security reasons!"); } string path = (string)args[0]; result = _ch.AddCertificate(path); } else { throw new Exception("Invalid method"); } } catch (Exception e) { result = new AdrException(-32602, e); } _node.Rpc.SendResult(rs, result); }
public void HandleRpc(ISender caller, string method, IList args, object rs) { if (LocalUseOnly) { try { ReqrepManager.ReplyState _rs = (ReqrepManager.ReplyState)caller; Node node = (Node)_rs.ReturnPath; if (node != _node) { throw new Exception(); } } catch { AdrException e = new AdrException(-32602, new Exception("Must send from local node!")); _node.Rpc.SendResult(rs, e); return; } } object result = null; try { switch (method) { case "Create": { // Needs to be Async so we don't deadlock! MemBlock key = MemBlock.Reference((byte[])args[0]); MemBlock value = MemBlock.Reference((byte[])args[1]); int ttl = (int)args[2]; Channel returns = new Channel(1); returns.CloseEvent += delegate(object o, EventArgs eargs) { _node.Rpc.SendResult(rs, returns.Dequeue()); }; _dht.AsyncCreate(key, value, ttl, returns); return; } case "Put": { // Needs to be Async so we don't deadlock! MemBlock key = MemBlock.Reference((byte[])args[0]); MemBlock value = MemBlock.Reference((byte[])args[1]); int ttl = (int)args[2]; Channel returns = new Channel(1); returns.CloseEvent += delegate(object o, EventArgs eargs) { _node.Rpc.SendResult(rs, returns.Dequeue()); }; _dht.AsyncPut(key, value, ttl, returns); return; } case "Get": { // Needs to be Async so we don't deadlock! MemBlock key = MemBlock.Reference((byte[])args[0]); Channel returns = new Channel(); returns.CloseEvent += delegate(object o, EventArgs eargs) { Hashtable [] results = new Hashtable[returns.Count]; int pos = 0; while (returns.Count > 0) { results[pos++] = (Hashtable)returns.Dequeue(); } _node.Rpc.SendResult(rs, results); }; _dht.AsyncGet(key, returns); return; } case "BeginGet": { MemBlock key = MemBlock.Reference((byte[])args[0]); result = BeginGet(key); break; } case "ContinueGet": { MemBlock token = MemBlock.Reference((byte[])args[0]); ContinueGet(token, rs); return; } case "EndGet": { MemBlock token = MemBlock.Reference((byte[])args[0]); EndGet(token); result = true; break; } } } catch (Exception e) { result = new AdrException(-32602, e); } _node.Rpc.SendResult(rs, result); }