[svn] commit: r317 - in /branches/parkinglot/src/lib/cc: cpp/data.cc python/ISC/CC/Message.py python/test.py ruby/lib/cc/message.rb

BIND 10 source code commits bind10-changes at lists.isc.org
Mon Nov 23 09:35:36 UTC 2009


Author: jelte
Date: Mon Nov 23 09:35:36 2009
New Revision: 317

Log:
int type for wireformat in python, ruby and C

Modified:
    branches/parkinglot/src/lib/cc/cpp/data.cc
    branches/parkinglot/src/lib/cc/python/ISC/CC/Message.py
    branches/parkinglot/src/lib/cc/python/test.py
    branches/parkinglot/src/lib/cc/ruby/lib/cc/message.rb

Modified: branches/parkinglot/src/lib/cc/cpp/data.cc
==============================================================================
--- branches/parkinglot/src/lib/cc/cpp/data.cc (original)
+++ branches/parkinglot/src/lib/cc/cpp/data.cc Mon Nov 23 09:35:36 2009
@@ -19,6 +19,7 @@
 const unsigned char ITEM_LIST = 0x03;
 const unsigned char ITEM_NULL = 0x04;
 const unsigned char ITEM_BOOL = 0x05;
+const unsigned char ITEM_INT  = 0x06;
 const unsigned char ITEM_UTF8 = 0x08;
 const unsigned char ITEM_MASK = 0x0f;
 
@@ -567,6 +568,12 @@
 }
 
 ElementPtr
+decode_int(std::stringstream& in, int& item_length)
+{
+    return from_stringstream_int_or_double(in);
+}
+
+ElementPtr
 decode_blob(std::stringstream& in, int& item_length)
 {
     char *buf = new char[item_length + 1];
@@ -667,6 +674,9 @@
     switch (type) {
     case ITEM_BOOL:
         element = decode_bool(in, item_length);
+        break;
+    case ITEM_INT:
+        element = decode_int(in, item_length);
         break;
     case ITEM_BLOB:
         element = decode_blob(in, item_length);
@@ -765,7 +775,7 @@
 
     text << str();
     int length = text.str().length();
-    ss << encode_length(length, ITEM_UTF8) << text.str();
+    ss << encode_length(length, ITEM_INT) << text.str();
 
     return ss.str();
 }

Modified: branches/parkinglot/src/lib/cc/python/ISC/CC/Message.py
==============================================================================
--- branches/parkinglot/src/lib/cc/python/ISC/CC/Message.py (original)
+++ branches/parkinglot/src/lib/cc/python/ISC/CC/Message.py Mon Nov 23 09:35:36 2009
@@ -28,6 +28,7 @@
 _ITEM_LIST = 0x03
 _ITEM_NULL = 0x04
 _ITEM_BOOL = 0x05
+_ITEM_INT  = 0x06
 _ITEM_UTF8 = 0x08
 _ITEM_MASK = 0x0f
 
@@ -73,6 +74,10 @@
     """Pack a bool and its type/length prefix."""
     return (_encode_length_and_type(_encode_bool(item), _ITEM_BOOL))
 
+def _pack_int(item):
+    """Pack an integer and its type/length prefix."""
+    return (_encode_length_and_type(bytes(str(item), 'utf-8'), _ITEM_INT))
+
 def _pack_array(item):
     """Pack a list (array) and its type/length prefix."""
     return (_encode_length_and_type(_encode_array(item), _ITEM_LIST))
@@ -100,6 +105,8 @@
         return (_pack_nil())
     elif type(item) == bool:
         return (_pack_bool(item))
+    elif type(item) == int:
+        return (_pack_int(item))
     elif type(item) == dict:
         return (_pack_hash(item))
     elif type(item) == list:
@@ -186,6 +193,8 @@
         value = item
     elif item_type == _ITEM_BOOL:
         value = _decode_bool(item)
+    elif item_type == _ITEM_INT:
+        value = _decode_int(item)
     elif item_type == _ITEM_UTF8:
         value = str(item, 'utf-8')
     elif item_type == _ITEM_HASH:
@@ -202,6 +211,9 @@
 def _decode_bool(data):
     return data == b'0x01'
 
+def _decode_int(data):
+    return int(str(data, 'utf-8'))
+
 def _decode_hash(data):
     ret = {}
     while len(data) > 0:

Modified: branches/parkinglot/src/lib/cc/python/test.py
==============================================================================
--- branches/parkinglot/src/lib/cc/python/test.py (original)
+++ branches/parkinglot/src/lib/cc/python/test.py Mon Nov 23 09:35:36 2009
@@ -33,7 +33,7 @@
 
     def test_to_wire_of_hash(self):
         wire = ISC.CC.Message.to_wire({ "simple" : { "string" : 1 }})
-        self.assertEqual(wire, b'Skan\x06simple"\n\x06string(\x011')
+        self.assertEqual(wire, b'Skan\x06simple"\n\x06string&\x011')
 
     def test_from_wire_of_hash(self):
         wire = b'Skan\x06simple"\n\x06string(\x011'
@@ -94,5 +94,14 @@
         decoded = ISC.CC.Message.from_wire(wire)
         self.assertEqual(decoded["bool"], False)
 
+    def test_to_wire_of_int(self):
+        wire = ISC.CC.Message.to_wire({ "number": 123 })
+        self.assertEqual(wire, b'Skan\x06number&\x03123')
+
+    def test_from_wire_of_int(self):
+        wire = b'Skan\x06number&\x03123'
+        decoded = ISC.CC.Message.from_wire(wire)
+        self.assertEqual(decoded["number"], 123)
+    
 if __name__ == '__main__':
     unittest.main()

Modified: branches/parkinglot/src/lib/cc/ruby/lib/cc/message.rb
==============================================================================
--- branches/parkinglot/src/lib/cc/ruby/lib/cc/message.rb (original)
+++ branches/parkinglot/src/lib/cc/ruby/lib/cc/message.rb Mon Nov 23 09:35:36 2009
@@ -26,6 +26,7 @@
   ITEM_LIST = 0x03
   ITEM_NULL = 0x04
   ITEM_BOOL = 0x05
+  ITEM_INT  = 0x06
   ITEM_UTF8 = 0x08
   ITEM_MASK = 0x0f
 
@@ -110,6 +111,10 @@
 
   def self.pack_bool(bool)
     encode_length_and_type(encode_bool(bool), ITEM_BOOL)
+  end
+
+  def self.pack_int(int)
+    encode_length_and_type(encode_int(int), ITEM_INT)
   end
 
   def self.pack_blob(str)
@@ -154,6 +159,8 @@
       ret = pack_bool(item)
     when TrueClass
       ret = pack_bool(item)
+    when Integer
+      ret = pack_int(item)
     else
       ret = pack_blob(item.to_s)
     end
@@ -183,7 +190,10 @@
       [0x00].pack("C")
     end
   end
-    
+
+  def self.encode_int(int)
+    int.to_s.encode('binary')
+  end
 
   def self.encode_array(msg)
     unless msg.is_a?Array
@@ -249,6 +259,8 @@
       value = item.encode('utf-8')
     when ITEM_BOOL
       value = decode_bool(item)
+    when ITEM_INT
+      value = decode_int(item)
     when ITEM_HASH
       value = decode_hash(item)
     when ITEM_LIST
@@ -265,7 +277,11 @@
   def self.decode_bool(msg)
     return msg == [0x01].pack("C")
   end
-    
+
+  def self.decode_int(msg)
+    return Integer(msg.encode('utf-8'))
+  end
+  
   def self.decode_hash(msg)
     ret = {}
     while msg.length > 0




More information about the bind10-changes mailing list