Example #1
0
  public bool HandleReply(ReqrepManager man, ReqrepManager.ReqrepType rt,
		   int mid,
		   PType prot,
		   MemBlock payload,
		   ISender from,
		   Brunet.ReqrepManager.Statistics s,
		   object state)
  {
    Console.WriteLine("{0} got our message", from);
    return false;
  }
Example #2
0
 static SenderReceiver() {
   testing = new PType("g");
   byte[] end_of_send = new byte[10];
   for(int i = 0; i < end_of_send.Length; i++) {
     end_of_send[i] = 254;
   }
   eos_data = MemBlock.Reference(end_of_send);
   eos = new CopyList(testing, eos_data);
 }
Example #3
0
  /**
   * Implements the IReplyHandler (also provides some light-weight statistics)
   */
  public bool HandleReply(ReqrepManager man, ReqrepManager.ReqrepType rt,
			  int mid, PType prot, MemBlock payload, ISender ret_path,
			  ReqrepManager.Statistics statistics, object state)
  {
    RpcRequestState rs = (RpcRequestState) state;
    //ISender target = rs.RpcTarget;
    Channel bq = rs.Results;
    if( bq != null ) {
      object data = AdrConverter.Deserialize(payload);
      RpcResult res = new RpcResult(ret_path, data, statistics);
      bq.Enqueue(res);
      //Keep listening unless the queue is closed
      return (!bq.Closed);
    }
    else {
      //If they didn't even pass us a queue, I guess they didn't want to
      //listen too long
      return false;
    }
  }
Example #4
0
 static Protocol() {
   //Here are all the defined protocols
   PType[] prots = new PType[]{ 
                             Linking,
                             AH,
                             Connection,
                             Forwarding,
                             Tunneling,
                             Echo,
                             IP,
                             ReqRep,
                             Rpc
                             };
   foreach(PType p in prots) {
     PType.AddToTable(p);
   }
 }
Example #5
0
 protected static void AddToTable(PType p) {
   if (0 <= p.TypeNumber && p.TypeNumber < _table.Length) {
     _table[ p.TypeNumber ] = p;
   }
 }
Example #6
0
 static PType() {
   //Initialize the _table:
   _table = new PType[ ASCII_UPPER_BOUND ];
 }
Example #7
0
  public void Test() {
    
    System.Random r = new System.Random();
    //Test numeric type codes:
    for(int i = 1; i < 32; i++ ) {
      PType p = new PType(i);
      MemBlock b = p.ToMemBlock();
      
      byte[] buf = new byte[100];
      r.NextBytes(buf); //Get some junk:
      MemBlock junk = MemBlock.Reference(buf);
      MemBlock b1 = MemBlock.Concat(b, junk);
      MemBlock rest = null;
      PType pp = PType.Parse(b1, out rest);
      
      byte[] buf2 = new byte[1];
      buf2[0] = (byte)i;
      MemBlock b2 = MemBlock.Reference(buf2);
      
      Assert.AreEqual(p, pp, System.String.Format("Round trip int: {0}", i));
      Assert.AreEqual( b, b2, System.String.Format("Convert to MemBlock int: {0}", i) );
      Assert.AreEqual(i, pp.TypeNumber, "Typenumber equality");
      Assert.AreEqual(rest, junk, "rest in int PType");
    }

    //Test string types:
    for(int i = 0; i < 1000; i++) {
      //Make a random string:
      //
      byte[] buf = new byte[ r.Next(1, 100) ];
      r.NextBytes(buf);
      string s = Base32.Encode(buf);
      PType p1 = new PType(s);
      r.NextBytes(buf); //Get some junk:
      MemBlock b = MemBlock.Copy(buf);
      MemBlock combine = MemBlock.Concat( p1.ToMemBlock(), b);
      MemBlock b2 = null;
      PType p2 = PType.Parse(combine, out b2);

      Assert.AreEqual( p1, p2, "Round trip string: " + s);
      Assert.AreEqual( b, b2, "Round trip rest" );
      Assert.AreEqual( s, p2.ToString(), "Round trip to string");
      Assert.AreEqual( s, p1.ToString(), "Round trip to string");
      Assert.AreEqual( p1.TypeNumber, p2.TypeNumber, "RT: TypeNumber test");
    }
    //Test all one byte ascii strings:
    for(byte b = 32; b < ASCII_UPPER_BOUND; b++) {
      MemBlock raw = MemBlock.Reference( new byte[]{ b, 0 } );
      MemBlock rest;
      PType p1 = PType.Parse(raw, out rest);
      Assert.AreEqual(rest, MemBlock.Null, "Rest is null");
      PType p2 = PType.Parse(raw, out rest);
      Assert.AreEqual(rest, MemBlock.Null, "Rest is null");
      Assert.IsTrue(p1 == p2, "reference equality of single byte type");
      Assert.AreEqual(p1, p2, "equality of single byte type");
      Assert.AreEqual(p1, new PType(p1.ToString()), "Round trip string");
    }
    //Test TypeNumber of string types:
    for(int i = 0; i < 100; i++) {
      byte[] buf = new byte[20];
      r.NextBytes(buf);
      for( int j = 1; j < 4; j++) {
        string s = Base32.Encode(buf).Substring(0, j);
        PType p1 = new PType(s);
        byte[] buf2 = System.Text.Encoding.UTF8.GetBytes(s);
        int t = 0;
        for(int k = 0; k < buf2.Length; k++) {
          t = t | buf2[k];
          t <<= 8;
        }
        Assert.AreEqual(t, p1.TypeNumber, System.String.Format("String type number: {0}, s={1}", t, s) );
      }
    }
    //Console.Error.WriteLine("Tested PType");
  }
Example #8
0
 /**
  * Parse the PType starting at mb, and return all of mb <b>after</b>
  * the PType.
  */
 public static PType Parse(MemBlock mb, out MemBlock rest) {
   PType result = null;
   byte fb = mb[0];
   bool is_v_n = IsValidNumeric( (int)fb );
   /**
    * Since ptypes must be valid UTF8 strings,
    * if the second byte is null, the first byte is an ascii character
    * and hence has a value less than ASCII_UPPER_BOUND 
    */
   bool store_in_tbl = ( is_v_n || (mb[1] == 0) );
   if( store_in_tbl ) {
     //This is stored in our table:
     result = _table[ fb ];
     if( result != null ) {
       if( is_v_n ) {
         //There is no null
         rest = mb.Slice(1);
       }
       else {
         //Skip the null
         rest = mb.Slice(2);
       }
       return result;
     }
   }
   //Otherwise we have to make it:
   MemBlock raw_data = null;
   result = new PType();
   if( is_v_n ) {
     /*
      * Don't set the raw_data since it is only one byte and we may not need
      * it
      */
     rest = mb.Slice(1);
     result._type_num = (int)fb;
   }
   else {
     int null_pos = mb.IndexOf(0);
     if( null_pos > 0 ) {
       //Include the "null", but make a copy so we don't keep some data in
       //scope for ever
       raw_data = MemBlock.Copy( (ICopyable)mb.Slice(0, null_pos + 1) );
       rest = mb.Slice(null_pos + 1); 
     }
     else {
       //There is no terminating Null, panic!!
       throw new ParseException(
         System.String.Format("PType not null terminated: {0}", mb.ToBase16String()));
     }
     result._type_num = -2;
     result._raw_data = raw_data;
   }
   if( store_in_tbl ) {
     //Make sure we don't have to create an object like this again
     _table[ fb ] = result;
   }
   return result;
 }
    /// <summary>This better be a SecureControl message!</summary>
    public bool HandleReply(ReqrepManager man, ReqrepManager.ReqrepType rt,
         int mid,
         PType prot,
         MemBlock payload, ISender returnpath,
         ReqrepManager.Statistics statistics,
         object state)
    {
      if(!prot.Equals(SecureControl)) {
        return true;
      }

      bool done = false;
      try {
        HandleControl(payload, returnpath);
      } catch(Exception e) {
        ProtocolLog.WriteIf(ProtocolLog.Security, e.ToString());
        done = true;
      }

      return done;
    }