예제 #1
1
 public override void Map(Channel q, object map_arg) {
   IList retval = new ArrayList();
   IDictionary my_entry = new ListDictionary();
   my_entry["node"] = _node.Address.ToString();
   retval.Add(my_entry);
   q.Enqueue(retval);
 }
예제 #2
0
    public override void Reduce(Channel q, object reduce_arg, 
                                  object current_result, RpcResult child_rpc) {

      bool done = false;
      ISender child_sender = child_rpc.ResultSender;
      //the following can throw an exception, will be handled by the framework
      object child_result = child_rpc.Result;
      

      //child result is a valid result
      if (current_result == null) {
        q.Enqueue(new Brunet.Util.Pair<object, bool>(child_result, done));
        return;
      }
      

      ArrayList retval = current_result as ArrayList;
      IDictionary my_entry = (IDictionary) retval[0];
      my_entry["next_con"] = child_sender.ToUri();
      retval.AddRange((IList) child_result);
      
      if (LogEnabled) {
        ProtocolLog.Write(ProtocolLog.MapReduce, 
                          String.Format("{0}: {1}, reduce list count: {2}.", this.TaskName, _node.Address, retval.Count));
      }
      q.Enqueue(new Brunet.Util.Pair<object, bool>(retval, done));
    }
    public override void Reduce(Channel q, object reduce_arg, 
                                  object current_result, RpcResult child_rpc) {

      bool done = false;
      //ISender child_sender = child_rpc.ResultSender;
      //the following can throw an exception, will be handled by the framework
      object child_result = child_rpc.Result;
      
      //child result is a valid result
      if (current_result == null) {
        q.Enqueue(new Brunet.Util.Pair<object, bool>(child_result, done));
        return;
      }
      
      IDictionary my_entry = current_result as IDictionary;
      IDictionary value = child_result as IDictionary;
      int max_height = (int) my_entry["height"];
      int count = (int) my_entry["count"];

      int y = (int) value["count"];
      my_entry["count"] = count + y;
      int z = (int) value["height"] + 1;
      if (z > max_height) {
        my_entry["height"] = z; 
      }
      q.Enqueue(new Brunet.Util.Pair<object, bool>(my_entry, done));
    }
예제 #4
0
    override public void Invoke(ISender target, Channel q, string method,
                                params object[] args)
    {
      IRpcHandler handler = null;
      string mname = null;
      lock( _sync ) {
        object[] info = (object[]) _method_cache[method];
        if( info == null ) {
          string[] parts = method.Split('.');
          string hname = parts[0];
          mname = parts[1];
          
          handler = (IRpcHandler)_method_handlers[ hname ];
          if( handler == null ) {
            //No handler for this.
            throw new AdrException(-32601, "No Handler for method: " + method);
          }
          info = new object[2];
          info[0] = handler;
          info[1] = mname;
          _method_cache[ method ] = info;
        }
        else {
          handler = (IRpcHandler)info[0];
          mname = (string)info[1];
        }
      }

      handler.HandleRpc(target, mname, args, q);
    }
 /** Greedy routing.  gen_arg is the Address of the destination
  */
 public override void GenerateTree(Channel q, MapReduceArgs mr_args) {
   object gen_arg = mr_args.GenArg;
   Log("{0}: {1}, greedy generator called, arg: {2}.", 
       this.TaskName, _node.Address, gen_arg);
   string address = gen_arg as string;
   AHAddress a =  (AHAddress) AddressParser.Parse(address);
   ArrayList retval = new ArrayList();
   ConnectionTable tab = _node.ConnectionTable;
   ConnectionList structs = tab.GetConnections(ConnectionType.Structured);
   Connection next_closest = structs.GetNearestTo((AHAddress) _node.Address, a);
   if (next_closest != null) {
     //arguments do not change at all
     MapReduceInfo mr_info = new MapReduceInfo(next_closest.Edge, mr_args);
     retval.Add(mr_info);
   }
   
   Log("{0}: {1}, greedy generator returning: {2} senders.", 
       this.TaskName, _node.Address, retval.Count);
   //Send the result:
   q.Enqueue(retval.ToArray(typeof(MapReduceInfo)));
 }
예제 #6
0
 /**
  * @return true if we successfully started the next reduce
  */
 protected bool TryNextReduce(State new_s, State old_s, RpcResult v, bool cont) {
   if( new_s.Done ) {
     SendResult( new_s.ReduceResult );
     return false;
   }
   bool start_red = new_s.Reducing && (cont || (false == old_s.Reducing));
   if (LogEnabled) {
     ProtocolLog.Write(ProtocolLog.MapReduce,        
                       String.Format("MapReduce: {0}, TryNextReduce: {1}.",
                       _node.Address, start_red));
   }
   if( start_red ) {
     Channel r_chan = new Channel(1, v);
     r_chan.CloseEvent += this.ReduceHandler;
     object startval = new_s.ReduceResult == State.DEFAULT_OBJ ? null : new_s.ReduceResult;
     if (LogEnabled) {
       ProtocolLog.Write(ProtocolLog.MapReduce,        
                       String.Format("MapReduce: {0} abt to Reduce({1},{2},{3})",
                         _node.Address, _mr_args.ReduceArg, startval, v));
     }
     try {
       _mr_task.Reduce(r_chan, _mr_args.ReduceArg, startval, v);
     }
     catch(Exception x) {
       //Reduce is where we do error handling, if that doesn't work, oh well:
       if (LogEnabled) {
         ProtocolLog.Write(ProtocolLog.MapReduce,        
                       String.Format("MapReduce: {0}, reduce threw: {1}.", _node.Address, x));
       }
       SendResult(x);
       return false;
     }
   }
   return start_red;
 }
예제 #7
0
파일: SocialNode.cs 프로젝트: j3g/socialvpn
        /**
         * Add local certificate to the DHT.
         */
        public void PublishCertificate()
        {
            byte[] key_bytes = Encoding.UTF8.GetBytes(_local_user.DhtKey);
              MemBlock keyb = MemBlock.Reference(key_bytes);
              MemBlock valueb = MemBlock.Reference(_local_cert.X509.RawData);

              Channel q = new Channel();
              q.CloseAfterEnqueue();
              q.CloseEvent += delegate(Object o, EventArgs eargs) {
            try {
              bool success = (bool) (q.Dequeue());
              if(success) {
            _cert_published = true;
            ProtocolLog.WriteIf(SocialLog.SVPNLog,
                                String.Format("PUBLISH CERT SUCCESS: {0} {1}",
                                DateTime.Now.TimeOfDay, _local_user.DhtKey));
              }
            } catch (Exception e) {
              ProtocolLog.WriteIf(SocialLog.SVPNLog,e.Message);
              ProtocolLog.WriteIf(SocialLog.SVPNLog,
                              String.Format("PUBLISH CERT FAILURE: {0} {1}",
                              DateTime.Now.TimeOfDay, _local_user.DhtKey));
            }
              };
              this.Dht.AsyncPut(keyb, valueb, DHTTTL, q);
        }
예제 #8
0
파일: SystemTest.cs 프로젝트: xujyan/brunet
 // Each nodes dht places a piece of data into the same dht key
 private static void dht_put() {
   foreach(DictionaryEntry de in nodes) {
     NodeMapping nm = (NodeMapping) de.Value;
     Dht dht = nm.Dht;
     Node node = nm.Node;
     if(!dht.Activated)
       continue;
     Channel returns = new Channel();
     dht.AsPut("tester", node.Address.ToString(), 2 * time_interval * dht_put_interval, returns);
   }
 }
예제 #9
0
  /**
   * This is how you invoke a method on a remote host.
   * Results are put into the Channel.
   * 
   * If you want to have an Event based approach, listen to the EnqueueEvent
   * on the Channel you pass for the results.  That will be fired
   * immediately from the thread that gets the result.
   *
   * When a result comes back, we put and RpcResult into the Channel.
   * When you have enough responses, Close the queue (please).  The code
   * will stop sending requests after the queue is closed.  If you never close
   * the queue, this will be wasteful of resources.
   *
   * @param target the sender to use when making the RPC call
   * @param q the Channel into which the RpcResult objects will be placed.
   *            q may be null if you don't care about the response.
   * @param method the Rpc method to call
   *
   * @throw Exception if we cannot send the request for some reason.
   */
  virtual public void Invoke(ISender target, Channel q, string method,
                              params object[] args)
  {
    //build state for the RPC call
    RpcRequestState rs = new RpcRequestState();
    rs.Results = q;
    rs.RpcTarget = target;

    object[] rpc_call = new object[2];
    rpc_call[0] = method;
    if( args != null ) {
      rpc_call[1] = args;
    }
    else {
      //There are no args, which we represent as a zero length list
      rpc_call[1] = new object[0];
    }
    
    AdrCopyable req_copy = new AdrCopyable(rpc_call);
#if RPC_DEBUG
    Console.Error.WriteLine("[RpcClient: {0}] Invoking method: {1} on target: {2}",
                     _rrman.Info, method, target);
#endif
    ICopyable rrpayload = new CopyList( PType.Protocol.Rpc, req_copy ); 
    int reqid = _rrman.SendRequest(target, ReqrepManager.ReqrepType.Request,
                                   rrpayload, this, rs);
  
    //Make sure we stop this request when the queue is closed.
    if( q != null ) {
      try {
        q.CloseEvent += delegate(object qu, EventArgs eargs) {
          _rrman.StopRequest(reqid, this);
       };
      }
      catch {
        if(q.Closed) {
          _rrman.StopRequest(reqid, this);
        }
        else {
          throw;
        }
      }
    }
  }
    /**
     * Compute candidate scores for a shortcut connection.
     * @param start address computed by the SCO.
     * @param range nunber of candidate nodes.
     * @param cb callback function when candidate scores are available. 
     * @param current current selection of the optimal in the provided range.
     */
    public override void ComputeCandidates(Address start, int range, TargetSelectorDelegate cb, Address current) {
      Channel q = null;
      RequestState rs = null;
      lock(_sync) {
#if VTS_DEBUG
        Console.Error.WriteLine("VTS local: {0}, start: {1}, range: {2}, count: {3}", _node.Address, start, range, _num_requests);
#endif
        if (_num_requests == MAX_REQUESTS) {
          return; //do nothing and return;
        }
        _num_requests++;
        q = new Channel();
        rs = new RequestState(start, range, cb, current);
        _channel_to_state[q] = rs;        
      }
      
      //create a new request state
      ISender s = new ForwardingSender(_node, 
                                       start, 
                                       AHHeader.Options.Greedy, 
                                       new DirectionalAddress(DirectionalAddress.Direction.Left),
                                       (short) range,
                                       AHHeader.Options.Path
                                       );

      q.EnqueueEvent += new EventHandler(EnqueueHandler);
      q.CloseEvent += new EventHandler(CloseHandler);
      RpcManager rpc = RpcManager.GetInstance(_node);
      rpc.Invoke(s, q, "ncserver.EchoVivaldiState", new object[]{});
    }
예제 #11
0
    public override void Start() {
      //Make sure the Node is listening to this node
      try {
        //This will throw an exception if _e is already closed:
        _e.CloseEvent += this.CloseHandler; 
        //_e must not be closed, let's start listening to it:
        _e.Subscribe(_node, _e);
        /* Make the call */
        Channel results = new Channel();
        results.CloseAfterEnqueue();
        results.CloseEvent += this.LinkCloseHandler;
        RpcManager rpc = RpcManager.GetInstance(_node);
	if(ProtocolLog.LinkDebug.Enabled) {
	  ProtocolLog.Write(ProtocolLog.LinkDebug, 
			    String.Format("LPS target: {0} Invoking Start() over edge: {1}", _linker.Target, _e));
	}
        rpc.Invoke(_e, results, "sys:link.Start", MakeLM().ToDictionary() );
      }
      catch (Exception e) {
        //The Edge must have closed, move on to the next TA
	if(ProtocolLog.LinkDebug.Enabled) {
	  ProtocolLog.Write(ProtocolLog.LinkDebug, 
			    String.Format("LPS target: {0} Start() over edge: {1}, hit exception: {2}", 
					  _linker.Target, _e, e));
	}
        Finish(Result.MoveToNextTA);
      }
    }
예제 #12
0
 /** 
  * reduce function.  This reduces the local and children results into one.
  * This is also the error handling function.  Any exceptions must be
  * handled by this method, if not, the computation stops immediately and
  * sends the exception back up the tree.
  * 
  * @param q the Channel into which a Brunet.Util.Pair<object, bool> is enqueued,
  * if the second item is true, we stop querying nodes
  * @param reduce_arg arguments for the reduce
  * @param current_result accumulated result of reductions
  * @param child_rpc result from child computation
  */
 public virtual void Reduce(Channel q, object reduce_arg, object current_result, RpcResult child_rpc) {
   throw new NotImplementedException();
 }
 private static void dht_get() {
   for(int i = 0; i < network_size / 25; i++) {
     int index = rand.Next(0, network_size);
     Node node = (Node) nodes.GetByIndex(index);
     Dht dht = (Dht) dhts[node];
     Channel returns = new Channel();
     dht.AsyncGet(Encoding.UTF8.GetBytes("tester"), returns);
   }
 }
 private static void dht_put() {
   foreach(DictionaryEntry de in nodes) {
     Node node = (Node)de.Value;
     Dht dht = (Dht) dhts[node];
     Channel returns = new Channel();
     dht.AsyncPut(Encoding.UTF8.GetBytes("tester"),
         Encoding.UTF8.GetBytes(node.Address.ToString()),
         2 * base_time * dht_put_interval, returns);
   }
 }
예제 #15
0
파일: MapReduce.cs 프로젝트: xujyan/brunet
    /** Starts the computation. */
    public void Start() {
      //invoke map
      try {
        _map_result = _mr_task.Map(_mr_args.MapArg);
      } 
      catch(Exception x) {
        if (ProtocolLog.MapReduce.Enabled) {
          ProtocolLog.Write(ProtocolLog.MapReduce, 
                            String.Format("MapReduce: {0}, map exception: {1}.", _node.Address, x));        
        }
        _finished = true;
        SendResult(x);
        return;
      }

      if (LogEnabled) {
        ProtocolLog.Write(ProtocolLog.MapReduce,
                          String.Format("MapReduce: {0}, map result: {1}.", _node.Address, _map_result));
      }

      //do an initial reduction and see if we can terminate
      try {
        bool done; //out parameter
        _reduce_result = _mr_task.Reduce(_mr_args.ReduceArg, null, new RpcResult(null, _map_result), out done);

        if (LogEnabled) {
          ProtocolLog.Write(ProtocolLog.MapReduce,
                            String.Format("MapReduce: {0}, initial reduce result: {1}.", _node.Address, _reduce_result));
        }

        if (done) {
          _finished = true;
          SendResult(_reduce_result);
          return;
        }
      } catch(Exception x) {
        if (ProtocolLog.MapReduce.Enabled) {
          ProtocolLog.Write(ProtocolLog.MapReduce, 
                            String.Format("MapReduce: {0}, initial reduce exception: {1}.", _node.Address, x));
        }
        _finished = true;
        SendResult(x);
        return;
      }

      //compute the list of child targets
      MapReduceInfo[] child_mr_info = null;
      try {
        child_mr_info = _mr_task.GenerateTree(_mr_args);
      } catch (Exception x) {
        if (ProtocolLog.MapReduce.Enabled) {
          child_mr_info = new MapReduceInfo[0];
          ProtocolLog.Write(ProtocolLog.MapReduce,         
                            String.Format("MapReduce: {0}, generate tree exception: {1}.", _node.Address, x));
        }
      }
      
      if (LogEnabled) {
        ProtocolLog.Write(ProtocolLog.MapReduce,
                          String.Format("MapReduce: {0}, child senders count: {1}.", _node.Address, child_mr_info.Length));
      }

      if (child_mr_info.Length > 0) {
        foreach ( MapReduceInfo mr_info in child_mr_info) {
          Channel child_q = new Channel(1);
          //so far this is thread-safe
          _queue_to_child[child_q] = mr_info;
        }
        
        foreach (DictionaryEntry de in _queue_to_child) {
          Channel child_q = (Channel) de.Key;
          MapReduceInfo mr_info = (MapReduceInfo) de.Value;

          //the following will prevent the current object from going out of scope. 
          child_q.EnqueueEvent += new EventHandler(ChildCallback);
          try {
            _rpc.Invoke(mr_info.Sender, child_q,  "mapreduce.Start", mr_info.Args.ToHashtable());
          } catch(Exception) {
            ChildCallback(child_q, null);
          }
        }
      } else {
        // did not generate any child computations, return rightaway
        _finished = true;
        SendResult(_reduce_result);
        return;
      }
    }
예제 #16
0
파일: Simulator.cs 프로젝트: xujyan/brunet
      protected void CrawlNext(Address addr) {
        bool finished = false;
        if(_log && _crawled.Count < _count) {
          Console.WriteLine("Current address: " + addr);
        }
        if(_crawled.Contains(addr)) {
          finished = true;
        } else {
          _crawled.Add(addr, true);
          try {
            ISender sender = new AHGreedySender(_node, addr);
            Channel q = new Channel(1);
            q.CloseEvent += CrawlHandler;
            _node.Rpc.Invoke(sender, q, "sys:link.GetNeighbors");
          } catch(Exception e) {
            if(_log) {
              Console.WriteLine("Crawl failed" + e);
            }
            finished = true;
          }
        }

        if(finished) {
          Interlocked.Exchange(ref _done, 1);
          if(_log) {
            Console.WriteLine("Crawl stats: {0}/{1}", _crawled.Count, _count);
            Console.WriteLine("Consistency: {0}/{1}", _consistency, _crawled.Count);
            Console.WriteLine("Finished in: {0}", (DateTime.UtcNow - _start));
          }
        }
      }
예제 #17
0
파일: Simulator.cs 프로젝트: xujyan/brunet
      public void Start() {
        foreach(NodeMapping nm_from in _nodes.Values) {
          foreach(NodeMapping nm_to in _nodes.Values) {
            if(nm_from == nm_to) {
              continue;
            }

            ISender sender = new AHGreedySender(nm_from.Node, nm_to.Node.Address);
            Channel q = new Channel(1);
            q.CloseEvent += Callback;
            try {
              nm_from.Node.Rpc.Invoke(sender, q, "sys:link.Ping", 0);
              _count++;
              _waiting_on++;
            } catch(Exception e) {
              Console.WriteLine(e);
            }
          }
        }
      }
예제 #18
0
 protected void GenTreeHandler(object chano, EventArgs args) {
   if (LogEnabled) {
           ProtocolLog.Write(ProtocolLog.MapReduce,        
                       String.Format("MapReduce: {0}, GenTreeHandler", _node.Address));
   }
   Channel gtchan = (Channel)chano;
   MapReduceInfo[] children;
   object gtchan_result = null;
   try {
     gtchan_result = gtchan.Dequeue();
     children = (MapReduceInfo[])gtchan_result;
   }
   catch(Exception x) {
     //We could fail to return (queue is empty), or get a bad result
     Exception rx = gtchan_result as Exception;
     if( rx != null) {
       x = rx; //x should have been a bad cast
     }
     if (LogEnabled) {
         ProtocolLog.Write(ProtocolLog.MapReduce,        
                       String.Format("MapReduce: {0}, GenTreeHandler exception ({1})", _node.Address, x));
     }
     HandleException(null, x);
     return;
   }
   //The usual transactional bit:
   State state = _state;
   State old_state;
   State new_state;
   do {
     old_state = state;
     new_state = old_state.UpdateTree(children);
     state = Interlocked.CompareExchange<State>(ref _state, new_state, old_state);
   }
   while( state != old_state);
   if( new_state.Done ) {
     //We don't need to start calling our children
     if (LogEnabled) {
       ProtocolLog.Write(ProtocolLog.MapReduce,        
                     String.Format("MapReduce: {0}, done on GenTreeHandler", _node.Address));
     }
     SendResult( new_state.ReduceResult );
     return;
   }
   //Now we need to start calling our children:
   foreach(MapReduceInfo mri in children) {
     Channel child_q = new Channel(1, mri);
     child_q.CloseEvent += this.ChildCallback;
     try {
       if (LogEnabled) {
         ProtocolLog.Write(ProtocolLog.MapReduce,        
                     String.Format("MapReduce: {0}, calling child ({1})", _node.Address, mri.Sender.ToUri()));
       }
       _rpc.Invoke(mri.Sender, child_q,  "mapreduce.Start", mri.Args.ToHashtable());
     }
     catch(Exception x) {
       if (LogEnabled) {
         ProtocolLog.Write(ProtocolLog.MapReduce,        
                     String.Format("MapReduce: {0}, child ({1}) call threw: {2}.", _node.Address, mri.Sender.ToUri(), x));
       }
       HandleException(mri.Sender, x);
     }
   }
 }
예제 #19
0
 /** map function.
  * @param q the Channel into which the Map result is Enqueue 'd
  * @param map_arg the argument for the map function
  * */
 public virtual void Map(Channel q, object map_arg) {
   throw new NotImplementedException();
 }
예제 #20
0
 /** tree generator function.
  * @param q The channel into which to put exactly one IList<MapReduceInfo>
  * @param args the MapReduceArgs for this call
  */
 public virtual void GenerateTree(Channel q, MapReduceArgs args) {
   throw new NotImplementedException();
 }
예제 #21
0
    /**
     * When we get a response to the sys:link method, this handled
     * is called
     */
    protected void LinkCloseHandler(object q, EventArgs args) {
      try {
        Channel resq = (Channel)q;
        //If the Channel is empty this will throw an exception:
        RpcResult res = (RpcResult)resq.Dequeue();
        /* Here's the LinkMessage response */
        LinkMessage lm = new LinkMessage( (IDictionary)res.Result );
        /**
         * This will set our LinkMessageReply variable.  It can
         * only be set once, so all future sets will fail.  It
         * will also make sure we have the lock on the target.
         * If we don't, that will throw an exception
         */
        SetAndCheckLinkReply(lm);
        //If we got here, we have our response and the Lock on _target_address
        StatusMessage sm = _node.GetStatus(_contype, lm.Local.Address);
        /* Make the call */
        Channel results = new Channel();
        results.CloseAfterEnqueue();
        results.CloseEvent += this.StatusCloseHandler;
        RpcManager rpc = RpcManager.GetInstance(_node);
	if (ProtocolLog.LinkDebug.Enabled) {
	      ProtocolLog.Write(ProtocolLog.LinkDebug, 
                String.Format(
                  "LPS target: {0} Invoking GetStatus() over edge: {1}",
                  _linker.Target, _e));
	}
        /*
         * This could throw an exception if the Edge is closed
         */
        rpc.Invoke(_e, results, "sys:link.GetStatus", sm.ToDictionary() );
      }
      catch(AdrException x) {
        /*
         * This happens when the RPC call has some kind of issue,
         * first we check for common error conditions:
         */
        _x.Value = x;
        Finish( GetResultForErrorCode(x.Code) );
      }
      catch(ConnectionExistsException x) {
        /* We already have a connection */
        _x.Value = x;
        Finish( Result.ProtocolError );
      }
      catch(CTLockException x) {
        //This is thrown when ConnectionTable cannot lock.  Lets try again:
        _x.Value = x;
        Finish( Result.RetryThisTA );
      }
      catch(LinkException x) {
        _x.Value = x;
        if( x.IsCritical ) { Finish( Result.MoveToNextTA ); }
        else { Finish( Result.RetryThisTA ); }
      }
      catch(InvalidOperationException) {
        //The queue never got anything
        Finish(Result.MoveToNextTA);
      }
      catch(EdgeException) {
        //The Edge is goofy, let's move on:
        Finish(Result.MoveToNextTA);
      }
      catch(Exception x) {
        //The protocol was not followed correctly by the other node, fail
        _x.Value = x;
        Finish( Result.RetryThisTA );
      } 
    }
예제 #22
0
 public override void Map(Channel q, object map_arg) {
   Channel result = new Channel(1, q);
   result.CloseEvent += this.MapHandler;
   _node.Rpc.Invoke(_map.First, result, _map.Second, map_arg);
 }
예제 #23
0
 /**
  * Used to request other nodes existence
  */
 protected void Announce()
 {
   Channel queue = new Channel();
   queue.EnqueueEvent += HandleGetInformation;
   try {
     ISender mcs = _node.IPHandler.CreateMulticastSender();
     _rpc.Invoke(mcs, queue, "LocalCO.GetInformation");
   }
   catch(SendException) {
     /*
      * On planetlab, it is not uncommon to have a node that
      * does not allow Multicast, and it will throw an exception
      * here.  We just ignore this information for now.  If we don't
      * the heartbeatevent in the node will not execute properly.
      */ 
   }
 }
예제 #24
0
 public override void GenerateTree(Channel q, MapReduceArgs args) {
   Channel result = new Channel(1, q);
   result.CloseEvent += this.TreeHandler;
   _node.Rpc.Invoke(_tree.First, result, _tree.Second, args.ToHashtable());
 }
예제 #25
0
 private static void dht_put() {
   foreach(DictionaryEntry de in nodes) {
     Node node = (Node)de.Value;
     Dht dht = (Dht) dhts[node];
     if(!dht.Activated)
       continue;
     Channel returns = new Channel();
     dht.AsPut("tester", node.Address.ToString(), 2 * base_time * dht_put_interval, returns);
   }
 }
예제 #26
0
 public override void Reduce(Channel q, object reduce_arg, object current_result, RpcResult child_rpc) {
   Channel result = new Channel(1, q);
   result.CloseEvent += this.ReduceHandler;
   var childrpc_ht = new Hashtable();
   ISender rsend = child_rpc.ResultSender;
   childrpc_ht["sender"] = rsend != null ? rsend.ToUri() : "sender:localnode";
   try {
     //If this is an exception, this will throw
     childrpc_ht["result"] = child_rpc.Result; 
   }
   catch(Exception x) {
     childrpc_ht["result"] = x;
   }
   _node.Rpc.Invoke(_reduce.First, result, _reduce.Second, reduce_arg, current_result, childrpc_ht);
 }
예제 #27
0
파일: SystemTest.cs 프로젝트: xujyan/brunet
 // Performs network_size / 25 random dht gets
 private static void dht_get() {
   for(int i = 0; i < network_size / 25; i++) {
     int index = rand.Next(0, network_size);
     Dht dht = ((NodeMapping) nodes.GetByIndex(index)).Dht;
     if(!dht.Activated)
       continue;
     Channel returns = new Channel();
     dht.AsGet("tester", returns);
   }
 }
예제 #28
0
 /** Starts the computation. */
 public void Start() {
   //invoke map
   try {
     Channel map_res = new Channel(1);
     map_res.CloseEvent += this.MapHandler;
     if (LogEnabled) {
       ProtocolLog.Write(ProtocolLog.MapReduce,        
                       String.Format("MapReduce: {0}, about to call Map", _node.Address));
     }
     _mr_task.Map(map_res, _mr_args.MapArg);
   } 
   catch(Exception x) {
     //Simulate the above except with the Exception enqueued
     if (LogEnabled) {
       ProtocolLog.Write(ProtocolLog.MapReduce,        
                       String.Format("MapReduce: {0}, Exception in Map: {1}", _node.Address, x));
     }
     HandleException(null, x);
   }
   if( _state.Done ) { return; }
   /* Our local Map was not enough
    * to finish the computation, look
    * for children
    */
   try {
     Channel gentree_res = new Channel(1);
     gentree_res.CloseEvent += this.GenTreeHandler;
     if (LogEnabled) {
       ProtocolLog.Write(ProtocolLog.MapReduce,        
                       String.Format("MapReduce: {0}, about to call GenerateTree", _node.Address));
     }
     _mr_task.GenerateTree(gentree_res, _mr_args);
   }
   catch(Exception x) {
     if (LogEnabled) {
       ProtocolLog.Write(ProtocolLog.MapReduce,        
                       String.Format("MapReduce: {0}, Exception in GenerateTree: {1}", _node.Address, x));
     }
     HandleException(null, x);
   }
 }
예제 #29
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));          
        }
      }
    }
예제 #30
0
파일: SocialNode.cs 프로젝트: j3g/socialvpn
 /**
  * Add friend by retreiving certificate from DHT.
  * @param key the DHT key for friend's certificate.
  * @param access determines to give user network access.
  */
 public void AddDhtFriend(string key, bool access)
 {
     if(key != _local_user.DhtKey && !_friends.ContainsKey(key) &&
      key.Length >= 45 ) {
     ProtocolLog.WriteIf(SocialLog.SVPNLog,
                     String.Format("ADD DHT FETCH: {0} {1}",
                     DateTime.Now.TimeOfDay, key));
     Channel q = new Channel();
     q.CloseAfterEnqueue();
     q.CloseEvent += delegate(Object o, EventArgs eargs) {
       try {
     Hashtable result = (Hashtable) q.Dequeue();
     byte[] certData = (byte[]) result["value"];
     string tmp_key = SocialUtils.GetHashString(certData);
     tmp_key = SocialUser.DHTPREFIX + tmp_key;
     if(key == tmp_key) {
       ProtocolLog.WriteIf(SocialLog.SVPNLog,
                           String.Format("ADD DHT SUCCESS: {0} {1}",
                           DateTime.Now.TimeOfDay, key));
       if(access) {
         _queue.Enqueue(new QueueItem(
                        QueueItem.Actions.AddCertTrue, certData));
       }
       else {
         _queue.Enqueue(new QueueItem(
                        QueueItem.Actions.AddCertFalse, certData));
       }
     }
       } catch (Exception e) {
     ProtocolLog.WriteIf(SocialLog.SVPNLog,e.Message);
     ProtocolLog.WriteIf(SocialLog.SVPNLog,
                         String.Format("ADD DHT FAILURE: {0} {1}",
                         DateTime.Now.TimeOfDay, key));
       }
     };
     byte[] key_bytes = Encoding.UTF8.GetBytes(key);
     MemBlock keyb = MemBlock.Reference(key_bytes);
     this.Dht.AsyncGet(keyb, q);
       }
 }