Exemplo n.º 1
0
        public void HandleRpc(ISender caller, String method, IList arguments,
                              object request_state)
        {
            object result = new InvalidOperationException("Invalid method");

            if (method.Equals("Enable") || method.Equals("Disable"))
            {
                if (arguments.Count < 2)
                {
                    Rpc.SendResult(request_state, new Exception("Not enough arguments."));
                    return;
                }

                string option_type = arguments[0] as string;
                string option_name = arguments[1] as string;
                if (option_type == null || option_name == null)
                {
                    Rpc.SendResult(request_state, new Exception("Expected a string."));
                    return;
                }

                if (option_type.Equals("BooleanSwitch"))
                {
                    BooleanSwitch bs;
                    if (!TryGetBooleanSwitch(option_name, out bs))
                    {
                        Rpc.SendResult(request_state, new Exception("No such BooleanSwitch."));
                        return;
                    }

                    bs.Enabled = method.Equals("Enable");
                    result     = true;
                }
                else if (option_type.Equals("Trace") && option_name.Equals("Console"))
                {
                    if (method.Equals("Enable"))
                    {
                        Trace.Listeners.Add(Console);
                    }
                    else
                    {
                        Trace.Listeners.Remove(Console);
                    }
                    result = true;
                }
                else
                {
                    result = new InvalidOperationException("Invalid method");
                }
            }
            Rpc.SendResult(request_state, result);
        }
Exemplo n.º 2
0
            public bool HandleReply(ReqrepManager man, ReqrepManager.ReqrepType rt,
                                    int mid,
                                    PType prot,
                                    MemBlock payload, ISender returnpath,
                                    ReqrepManager.Statistics statistics,
                                    object state)
            {
                DateTime reply_time = DateTime.UtcNow;

                ListDictionary res_dict = new ListDictionary();
                AHSender       ah_rp    = returnpath as AHSender;

                if (ah_rp != null)
                {
                    res_dict["target"] = ah_rp.Destination.ToString();
                }
                //Here are the number of microseconds
                res_dict["musec"] = (int)(1000.0 * ((reply_time - _start_time).TotalMilliseconds));
                //Send the RPC result now;
                RpcManager my_rpc = System.Threading.Interlocked.Exchange(ref _rpc, null);

                if (my_rpc != null)
                {
                    //We have not sent any reply yet:
                    my_rpc.SendResult(_req_state, res_dict);
                }
                return(false);
            }
Exemplo n.º 3
0
        public void HandleRpc(ISender caller, string method, IList args, object rs)
        {
            object result = null;

            if (method.Equals("SeekTAs"))
            {
                if (args.Count != 1)
                {
                    throw new Exception("Not enough parameters");
                }
                else if (_realm.Equals(args[0]))
                {
                    result = LocalTAsToString(20);
                }
                else
                {
                    // We can't handle the request, let's send back an empty list
                    result = EMPTY_LIST;
                }
            }
            else
            {
                throw new Exception("Invalid method");
            }
            _rpc.SendResult(rs, result);
        }
Exemplo n.º 4
0
 public void HandleRpc(ISender caller, string meth, IList args, object state)
 {
     if (meth == "create")
     {
         Edge             calling_edge = (Edge)((ReqrepManager.ReplyState)caller).ReturnPath;
         string           remote_path  = (string)args[0];
         string           local_path   = (string)args[1];
         PathEdgeListener el           = _pel_map[local_path];
         if (el.IsStarted)
         {
             PathEdge npe = new PathEdge(this, calling_edge, local_path, remote_path);
             lock ( _sync ) {
                 //We don't announce yet, wait till we get some data, which
                 //verifies that the other side has seen it.
                 _unannounced[calling_edge] = npe;
             }
             //So the new Edge has been announced.
             Rpc.SendResult(state, true);
         }
         else
         {
             throw new Exception(
                       String.Format("PathEdgeListener({0}) not started", local_path));
         }
     }
     else
     {
         throw new AdrException(-32601, "No Handler for method: " + meth);
     }
 }
Exemplo n.º 5
0
 /**
  * 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);
     }
 }
Exemplo n.º 6
0
        /**
         * <summary>Implements IRpcHandler that is called by the RpcManager when an
         * Rpc method requests any method with Information.*.</summary>
         * <param name="caller">The remote caller</param>
         * <param name="method">The name of the method called.</param>
         * <param name="arguments">An IList of arguments supplied</param>
         * <param name="request_state">Provides a return path back to the caller.
         * </param>
         */
        public void HandleRpc(ISender caller, String method, IList arguments,
                              object request_state)
        {
            object result = new InvalidOperationException("Invalid method");

            if (method.Equals("Info"))
            {
                result = this.Info();
            }
            _rpc.SendResult(request_state, result);
        }
Exemplo n.º 7
0
            public void HandleError(ReqrepManager man, int message_number,
                                    ReqrepManager.ReqrepError err, ISender ret_path, object state)
            {
                _rrman.StopRequest(message_number, this);
                RpcManager my_rpc = System.Threading.Interlocked.Exchange(ref _rpc, null);

                if (my_rpc != null)
                {
                    //We have not sent any reply yet:
                    my_rpc.SendResult(_req_state,
                                      new Exception(String.Format("Error: {0} from: {1}", err, ret_path)));
                }
            }
Exemplo n.º 8
0
        public void HandleRpc(ISender caller, string method, IList args,
                              object req_state)
        {
            object result = null;

            try {
                switch (method)
                {
                case "Ping":
                    result = HandlePing((string)args[0], (string)args[1]);
                    break;

                case "AddCertRequest":
                    result = AddCertRequest((string)args[0], (string)args[1]);
                    break;

                case "AddCertReply":
                    result = AddCertReply((string)args[0], (string)args[1]);
                    break;

                case "SearchMapping":
                    result = _sdm.SearchMapping((string)args[0], (string)args[1],
                                                this);
                    break;

                case "AddTmpMapping":
                    result = _sdm.AddTmpMapping((string)args[0], (string)args[1]);
                    break;

                case "GetStats":
                    result = _ssm.GetStats();
                    break;

                case "GetNat":
                    result = _node.GetNatType();
                    break;

                default:
                    result = new InvalidOperationException("Invalid Method");
                    break;
                }
            } catch (Exception e) {
                result = e;
            }
            _rpc.SendResult(req_state, result);
        }
Exemplo n.º 9
0
        /**
         * Sends the result of the computation back.
         * @return true if this is the first time this has been called
         */
        protected bool SendResult(object result)
        {
            object old_res = Interlocked.CompareExchange(ref _result, result, State.DEFAULT_OBJ);

            if (old_res == State.DEFAULT_OBJ)
            {
                if (LogEnabled)
                {
                    ProtocolLog.Write(ProtocolLog.MapReduce,
                                      String.Format("MapReduce: {0}, sending back result: {1}.", _node.Address, result));
                }
                _rpc.SendResult(_mr_request_state, result);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 10
0
        public void HandleRpc(ISender caller, string method, IList args, object rs)
        {
            object result = null;

            try {
                if (method.Equals("GetInformation"))
                {
                    result = GetInformation();
                }
                else
                {
                    throw new Exception("Invalid method");
                }
            }
            catch (Exception e) {
                result = new AdrException(-32602, e);
            }
            _rpc.SendResult(rs, result);
        }
Exemplo n.º 11
0
        public void HandleRpc(ISender caller, string method, IList arguments,
                              object req_state)
        {
            object result = null;

            try {
                switch (method)
                {
                case "FriendPing":
                    result = FriendPingHandler((string)arguments[0]);
                    break;

                default:
                    result = new InvalidOperationException("Invalid Method");
                    break;
                }
            } catch (Exception e) {
                result = e;
                ProtocolLog.Write(SocialLog.SVPNLog, e.Message);
                ProtocolLog.Write(SocialLog.SVPNLog, "RPC HANDLER FAILURE: " +
                                  method);
            }
            _rpc.SendResult(req_state, result);
        }
Exemplo n.º 12
0
        /**
         * This dispatches the particular methods this class provides.
         * Currently, the only invokable method is:
         * "Start".
         */
        public void HandleRpc(ISender caller, string method, IList args, object req_state)
        {
            int part_idx = method.IndexOf(':');

            if (part_idx == -1)
            {
                if (method == "Start")
                {
                    IDictionary   ht        = (IDictionary)args[0];
                    MapReduceArgs mr_args   = new MapReduceArgs(ht);
                    string        task_name = mr_args.TaskName;
                    MapReduceTask task;
                    if (_name_to_task.TryGetValue(task_name, out task))
                    {
                        MapReduceComputation mr = new MapReduceComputation(_node, req_state, task, mr_args);
                        mr.Start();
                    }
                    else
                    {
                        throw new AdrException(-32608, "No mapreduce task with name: " + task_name);
                    }
                }
                else if (method == "AddHandler")
                {
                    //Make sure this is local:
                    ISender tmp_call = caller;
                    bool    islocal  = tmp_call is Node;
                    while (!islocal && tmp_call is IWrappingSender)
                    {
                        tmp_call = ((IWrappingSender)tmp_call).WrappedSender;
                        islocal  = tmp_call is Node;
                    }
                    if (!islocal)
                    {
                        throw new AdrException(-32601, "AddHandler only valid for local callers");
                    }
                    SubscribeTask(new RpcMapReduceTask(_node, (IDictionary)args[0]));
                    _rpc.SendResult(req_state, null);
                }
                else
                {
                    throw new AdrException(-32601, "No Handler for method: " + method);
                }
            }
            else
            {
                //This is a reference to a specific part of a task:
                string        part      = method.Substring(0, part_idx);
                string        task_name = method.Substring(part_idx + 1);
                MapReduceTask task;
                if (false == _name_to_task.TryGetValue(task_name, out task))
                {
                    throw new AdrException(-32608, "No mapreduce task with name: " + task_name);
                }
                if (part == "tree")
                {
                    var mra = new MapReduceArgs((IDictionary)args[0]);

                    var tree_res = new Channel(1, req_state);
                    tree_res.CloseEvent += this.HandleTree;
                    task.GenerateTree(tree_res, mra);
                }
                else if (part == "reduce")
                {
                    //Prepare the RpcResult:
                    var     rres_d = (IDictionary)args[2];
                    ISender send   = SenderFactory.CreateInstance(_node, (string)rres_d["sender"]);
                    var     rres   = new RpcResult(send, rres_d["result"]);

                    Channel reduce_res = new Channel(1, req_state);
                    reduce_res.CloseEvent += this.HandleReduce;
                    task.Reduce(reduce_res, args[0], args[1], rres);
                }
                else if (part == "map")
                {
                    Channel map_res = new Channel(1, req_state);
                    map_res.CloseEvent += this.HandleMap;
                    task.Map(map_res, args[0]);
                }
                else
                {
                    throw new AdrException(-32608,
                                           String.Format("No mapreduce task({0}) part with name: {1}", task_name, part));
                }
            }
        }
Exemplo n.º 13
0
 public void Start()
 {
     _rpc.SendResult(_rs, _result);
 }