Enqueue() public method

public Enqueue ( object a ) : int
a object
return int
Example #1
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.Collections.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.Collections.Pair<object, bool>(my_entry, done));
    }
Example #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.Collections.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.Collections.Pair<object, bool>(retval, done));
    }
Example #3
0
 /*
  * Map method to add CachEntry to CacheList
  * @param map_arg [content,alpha,start,end]
  */
 public override void Map(Channel q, object map_arg) {
   IList arg = map_arg as IList;	    
   object input = arg[0];
   double alpha = (double)arg[1]; //replication factor
   string st = arg[2] as string;  // start brunet address of range
   string ed = arg[3] as string;  // end brunet address of range 
   AHAddress a = (AHAddress)AddressParser.Parse(st);
   AHAddress b = (AHAddress)AddressParser.Parse(ed);
   Entry ce = new Entry(input, alpha, a, b);
   int result = 0;   // if caching is successful, result will set to 1.
   int previous_count = _cl.Count;
   try {
     if( _cl.Insert(ce) ) {
       result = 1;
     }
     else {
       result = 0;
     }
   }
   catch {
     result = 0;
   }
   if (_cl.Count > previous_count) { result = 1; }
   IDictionary my_entry = new ListDictionary();
   my_entry["count"]=1;
   my_entry["height"]=1;
   my_entry["success"]=result;
   q.Enqueue(my_entry);
 }
Example #4
0
 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);
 }
Example #5
0
 /** 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.State.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)));
 }
Example #6
0
    /**
     * Generates tree for bounded broadcast. Algorithm works as follows:
     * The goal is to broadcast to all nodes in range [local_address, end).
     * Given a range [local_address, b), determine all connections that belong to this range.
     * Let the connections be b_1, b_2, ..... b_n.
     * To connection bi assign the range [b_i, b_{i+1}).
     * To the connection bn assign range [b_n, end).]
     */
    public override void GenerateTree(Channel q, MapReduceArgs mr_args) 
    {
      object gen_arg = mr_args.GenArg;
      string end_range = gen_arg as string;
      Log("generating child tree, range end: {0}.", end_range);
      AHAddress end_addr = (AHAddress) AddressParser.Parse(end_range);
      AHAddress start_addr = _node.Address as AHAddress;
      //we are at the start node, here we go:
      ConnectionTable tab = _node.ConnectionTable;
      ConnectionList structs = tab.GetConnections(ConnectionType.Structured);
      ArrayList retval = new ArrayList();

      if (structs.Count > 0) {
        Connection curr_con = structs.GetLeftNeighborOf(_node.Address);
        int curr_idx = structs.IndexOf(curr_con.Address);
        //keep going until we leave the range
        int count = 0;
        List<Connection> con_list = new List<Connection>();
        //ArrayList con_list = new ArrayList();
        while (count++ < structs.Count && ((AHAddress) curr_con.Address).IsBetweenFromLeft(start_addr, end_addr)) {
          con_list.Add(curr_con);
          //Log("adding connection: {0} to list.", curr_con.Address);
          curr_idx  = (curr_idx + 1)%structs.Count;
          curr_con = structs[curr_idx];
        }
        
        Log("{0}: {1}, number of child connections: {2}", 
            this.TaskName, _node.Address, con_list.Count);
        for (int i = 0; i < con_list.Count; i++) {
          MapReduceInfo mr_info = null;
          ISender sender = null;
          Connection con = (Connection) con_list[i];
          sender = con.State.Edge;
          //check if last connection
          if (i == con_list.Count - 1) {
            mr_info = new MapReduceInfo( (ISender) sender, 
                                         new MapReduceArgs(this.TaskName, 
                                                           mr_args.MapArg, //map argument
                                                           end_range, //generate argument
                                                           mr_args.ReduceArg //reduce argument
                                                           ));
            
            Log("{0}: {1}, adding address: {2} to sender list, range end: {3}", 
                this.TaskName, _node.Address, 
                con.Address, end_range);
            retval.Add(mr_info);
          }
          else {
            string child_end = ((Connection) con_list[i+1]).Address.ToString();
            mr_info = new MapReduceInfo( sender,
                                         new MapReduceArgs(this.TaskName,
                                                           mr_args.MapArg, 
                                                           child_end,
                                                           mr_args.ReduceArg));
            Log("{0}: {1}, adding address: {2} to sender list, range end: {3}", 
                this.TaskName, _node.Address, 
                con.Address, child_end);
            retval.Add(mr_info);
          }
        }
      }
      q.Enqueue( retval.ToArray(typeof(MapReduceInfo)));
    }
Example #7
0
 public override void Reduce(Channel q, object reduce_arg, object current_val, RpcResult child_r) {
   var rest = child_r.Result as IEnumerable;
   //If we get here, the child didn't throw an exception
   var result = new ArrayList();
   AddEnum(result, current_val as IEnumerable);
   AddEnum(result, rest);
   q.Enqueue(new Pair<object, bool>(result, false));
 }
Example #8
0
    /**
     * Generates tree for bounded broadcast. Algorithm works as follows:
     * The goal is to broadcast to all nodes in range (start, end).
     * Given a range (a, b), determine all connections that belong to this range.
     * Let the left connections be l_1, l_2, ..... l_n.
     * Let the right connections be r_1, r_2, ... , r_n.
     * To left connection l_i assign the range [b_{i-1}, b_i).
     * To right connection r_i assign the range [r_i, r_{i-1}]
     * To the connection ln assign range [l_{n-1}, end)
     * To the connection rn assign range (start, r_{n-1}]
     */
    public override void GenerateTree(Channel q, MapReduceArgs mr_args)  
    {
      ArrayList gen_list = mr_args.GenArg as ArrayList;
      string start_range = gen_list[0] as string;
      AHAddress start_addr = (AHAddress) AddressParser.Parse(start_range);
      AHAddress end_addr;
      string end_range;
      /// If users do not specify an end range, this method understands 
      /// that users intend to broadcasting the whole range.
      /// Thus, the address of end range is set to (start_address - 2), 
      /// the farthest address from the start_addr.
      if (gen_list.Count < 2)  {
        BigInteger start_int = start_addr.ToBigInteger();
        BigInteger end_int = start_int -2;
        end_addr = new AHAddress(end_int);
        end_range = end_addr.ToString();
      }
      else {
        end_range = gen_list[1] as string;
        end_addr = (AHAddress) AddressParser.Parse(end_range);
      }
      Log("generating child tree, range start: {0}, range end: {1}.", start_range, end_range);
      //we are at the start node, here we go:
      ConnectionTable tab = _node.ConnectionTable;
      ConnectionList structs = tab.GetConnections(ConnectionType.Structured);
      List<MapReduceInfo> retval = new List<MapReduceInfo>();

      if (InRange(_this_addr, start_addr, end_addr)) {
        if (structs.Count > 0) {
          //make connection list in the range.
          //left connection list is a list of neighbors which are in the range (this node, end of range)
          //right connection list is a list of neighbors which are in the range (start of range, this node)
          Brunet.Collections.Pair<List<Connection>,List<Connection>> cons = GetConnectionInfo(_this_addr, start_addr, end_addr, structs);
          List<Connection> left_cons =  cons.First as List<Connection>;
          List<Connection> right_cons = cons.Second as List<Connection>;
          //PrintConnectionList(left_cons);
          //PrintConnectionList(right_cons);
          retval = GenerateTreeInRange(start_addr, end_addr, left_cons, true, mr_args);
          List<MapReduceInfo> ret_right = GenerateTreeInRange(start_addr, end_addr, right_cons, false, mr_args);
          retval.AddRange(ret_right);
        }
        else {  //this node is a leaf node.
          //MapReduceInfo mr_info = null;
          //retval.Add(mr_info);
          //Console.WriteLine("no connection in the range: return null info");
        }
      }
      else { // _node is out of range. Just pass it to the closest to the middle of range.
        retval = GenerateTreeOutRange(start_addr, end_addr, mr_args);
      }
      q.Enqueue( retval.ToArray());
    }
Example #9
0
    /**
     * Reduce method
     * @param reduce_arg argument for reduce
     * @param current_result result of current map 
     * @param child_rpc results from children 
     * @param done if done is true, stop reducing and return result
     * return table of hop count, number of success, tree depth
     */
    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 = UNSET;
      try {
        child_result = child_rpc.Result;
      }
      catch(Exception x) {
        /*
        if (x.Equals("Child did not return") ) {
          //there might be at least one timeout from children.
	  //Start boundedcasting again
          IDictionary ht;
          string task_name = "Brunet.Services.Deetoo.MapReduceCache;
	  ht["task_name"] = task_name 
	  IDictionary map_arg;
	  IDictionary gen_arg;

	  = (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();
          }
	}
        */
      } 
      if( child_result == UNSET ) {
        //The child threw an exception
        q.Enqueue(new Brunet.Collections.Pair<object, bool> (current_result, done));
        return;
      } 
      //child result is a valid result
      if (current_result == null) {
        q.Enqueue(new Brunet.Collections.Pair<object, bool> (child_result, done));
        return;
      }
      //else combine them: 
      IDictionary my_entry = (IDictionary)current_result;
      IDictionary value = (IDictionary)child_result;
      int max_height = (int) my_entry["height"];
      int count = (int) my_entry["count"];
      my_entry["success"] = (int) my_entry["success"] + (int) value["success"];
      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.Collections.Pair<object, bool>(my_entry,done));
    }
Example #10
0
 public override void Map(Channel q, object map_arg) {
   IDictionary my_entry = new ListDictionary();
   my_entry["count"] = 1;
   my_entry["height"] = 1;
   q.Enqueue( my_entry );
 }
Example #11
0
  public void ChannelTests() {
    Channel c0 = new Channel();
    bool e_event_fired = false;
    c0.EnqueueEvent += delegate(object o, EventArgs arg) {
      e_event_fired = true;
    };
    c0.Enqueue(0);
    bool c_event_fired = false;
    c0.CloseEvent += delegate(object o, EventArgs arg) {
      c_event_fired = true;
    };
    c0.Close();
    Assert.IsTrue(c_event_fired, "CloseEvent");

    c0 = new Channel();
    c0.CloseAfterEnqueue();
    c_event_fired = false;
    c0.CloseEvent += delegate(object o, EventArgs arg) {
      c_event_fired = true;
    };
    c0.Enqueue(1); //This should close the channel:
    Assert.IsTrue(c_event_fired, "CloseEvent on Enqueue");
    Assert.IsTrue(c0.Closed, "Closed");
    
    c0 = new Channel(1);
    c_event_fired = false;
    c0.CloseEvent += delegate(object o, EventArgs arg) {
      c_event_fired = true;
    };
    c0.Enqueue(1); //This should close the channel:
    Assert.IsTrue(c_event_fired, "CloseEvent on Enqueue");
    Assert.IsTrue(c0.Closed, "Closed");
    //Try with different starting values:
    Random r = new Random();
    int en_count;
    for(int i = 0; i < 100; i++) {
      int max_enqueues = r.Next(1, 1000);
      c0 = new Channel(max_enqueues);
      c_event_fired = false;
      en_count = 0;
      c0.CloseEvent += delegate(object o, EventArgs arg) {
        c_event_fired = true;
      };
      c0.EnqueueEvent += delegate(object o, EventArgs arg) {
        en_count++;
      };
      for(int j = 0; j < max_enqueues; j++) {
        c0.Enqueue(j);
      }
      Assert.IsTrue(c_event_fired, "CloseEvent on Enqueue");
      Assert.AreEqual(en_count, max_enqueues, "EnqueueEvent count");
      Assert.IsTrue(c0.Closed, "Closed");
      try {
        c0.Enqueue(null);
        Assert.IsTrue(false, "Enqueue after close didn't fail");
      }
      catch {
        Assert.IsTrue(true, "Enqueue after close Got exception");
      }
    }

  }
Example #12
0
 /*
  * Map method to add CachEntry to CacheList
  * @param map_arg [pattern, query_type]
  */
 public override void Map(Channel q, object map_arg) {
   IList map_args = (IList)map_arg;
   string query_type = (string)(map_args[0]);
   Converter<object,QueryMatcher> qmf = null;
   if( _mut.State.QMFact.TryGetValue(query_type, out qmf) ) {
     QueryMatcher qm = qmf(map_args[1]); //I need to implement the code that map_args[1] includes x and y coordinate, map_args[1] will be Arraylist.
     var my_entry = new ListDictionary();
     var results = new ArrayList();
     foreach(Entry e in _cl.MState.State.Data) {
       object cont = e.Content;
       if(qm.Match(cont)) {
         results.Add(cont);
       }
     }
     my_entry["query_result"] = results;
     my_entry["count"] = 1;
     my_entry["height"] = 1;
     q.Enqueue(my_entry);
   }
   else {
     q.Enqueue(new AdrException(-32608, "No Deetoo match option with this name: " +  query_type) );
     return;
   }
 }
Example #13
0
 /**
  * Reduce method
  * @param reduce_args argument for reduce
  * @param current_result result of current map 
  * @param child_rpc results from children 
  * return table of hop count, tree depth, and query result
  */  
 public override void Reduce(Channel q, object reduce_args, 
                               object current_result, RpcResult child_rpc) {
   object child_result = null;
   child_result = child_rpc.Result;
   Converter<object,HitCombiner> hcf = null;
   IList args = (IList)reduce_args;
   string query_type = (string)(args[0]);
   object hc_arg = args[1];
   if( _mut.State.HCFact.TryGetValue(query_type, out hcf) ) {
     HitCombiner hc = hcf(hc_arg);
     if( current_result == null ) {
       IDictionary child_val = (IDictionary)child_result;
       //Initially, we are empty:
       var res = hc.Combine(new ArrayList(), (IList)child_val["query_result"]);
       var ret_val = new ListDictionary();
       ret_val["height"] = child_val["height"];
       ret_val["count"] = child_val["count"];
       ret_val["query_result"] = res.First;
       q.Enqueue(new Brunet.Collections.Pair<object, bool>(ret_val, res.Second));
     }
     else {
       IDictionary current_val = (IDictionary)current_result;
       IDictionary child_val = (IDictionary)child_result;
       
       var ret_val = new ListDictionary();
       ret_val["count"] = (int)current_val["count"] + (int)child_val["count"];
       ret_val["height"] = Math.Max((int)current_val["height"],
                                   1 + (int)child_val["height"]);
       
       var res = hc.Combine((IList)current_val["query_result"],
                              (IList)child_val["query_result"]);
       ret_val["query_result"] = res.First;
       q.Enqueue(new Brunet.Collections.Pair<object, bool>(ret_val, res.Second));
     }
   }
   else {
     q.Enqueue(new AdrException(-32608, "This query type {0} is not supported." + query_type) );
   }
 }
Example #14
0
        public void ChannelTests()
        {
            Channel c0            = new Channel();
            bool    e_event_fired = false;

            c0.EnqueueEvent += delegate(object o, EventArgs arg) {
                e_event_fired = true;
            };
            c0.Enqueue(0);
            bool c_event_fired = false;

            c0.CloseEvent += delegate(object o, EventArgs arg) {
                c_event_fired = true;
            };
            c0.Close();
            Assert.IsTrue(c_event_fired, "CloseEvent");

            c0 = new Channel();
            c0.CloseAfterEnqueue();
            c_event_fired  = false;
            c0.CloseEvent += delegate(object o, EventArgs arg) {
                c_event_fired = true;
            };
            c0.Enqueue(1); //This should close the channel:
            Assert.IsTrue(c_event_fired, "CloseEvent on Enqueue");
            Assert.IsTrue(c0.Closed, "Closed");

            c0             = new Channel(1);
            c_event_fired  = false;
            c0.CloseEvent += delegate(object o, EventArgs arg) {
                c_event_fired = true;
            };
            c0.Enqueue(1); //This should close the channel:
            Assert.IsTrue(c_event_fired, "CloseEvent on Enqueue");
            Assert.IsTrue(c0.Closed, "Closed");
            //Try with different starting values:
            Random r = new Random();
            int    en_count;

            for (int i = 0; i < 100; i++)
            {
                int max_enqueues = r.Next(1, 1000);
                c0             = new Channel(max_enqueues);
                c_event_fired  = false;
                en_count       = 0;
                c0.CloseEvent += delegate(object o, EventArgs arg) {
                    c_event_fired = true;
                };
                c0.EnqueueEvent += delegate(object o, EventArgs arg) {
                    en_count++;
                };
                for (int j = 0; j < max_enqueues; j++)
                {
                    c0.Enqueue(j);
                }
                Assert.IsTrue(c_event_fired, "CloseEvent on Enqueue");
                Assert.AreEqual(en_count, max_enqueues, "EnqueueEvent count");
                Assert.IsTrue(c0.Closed, "Closed");
                try {
                    c0.Enqueue(null);
                    Assert.IsTrue(false, "Enqueue after close didn't fail");
                }
                catch {
                    Assert.IsTrue(true, "Enqueue after close Got exception");
                }
            }
        }