[svn] commit: r35 - in /experiments/graff-ccapi/ruby/lib: cc.rb cc/message.rb cc/session.rb

BIND 10 source code commits bind10-changes at lists.isc.org
Fri Sep 25 20:38:06 UTC 2009


Author: mgraff
Date: Fri Sep 25 20:38:06 2009
New Revision: 35

Log:
add a single file to require, and a the session class

Added:
    experiments/graff-ccapi/ruby/lib/cc.rb
    experiments/graff-ccapi/ruby/lib/cc/session.rb
Modified:
    experiments/graff-ccapi/ruby/lib/cc/message.rb

Modified: experiments/graff-ccapi/ruby/lib/cc/message.rb
==============================================================================
--- experiments/graff-ccapi/ruby/lib/cc/message.rb (original)
+++ experiments/graff-ccapi/ruby/lib/cc/message.rb Fri Sep 25 20:38:06 2009
@@ -14,6 +14,10 @@
 # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
 class CC
+  class DecodeError < Exception ; end
+end
+
+class CC
 class Message
   PROTOCOL_VERSION = 0x536b616e
 
@@ -27,6 +31,21 @@
   ITEM_LENGTH_16   = 0x10
   ITEM_LENGTH_8    = 0x20
   ITEM_LENGTH_MASK = 0x30
+
+  def initialize(msg = nil)
+    @data = [PROTOCOL_VERSION].pack("N")
+    if msg.is_a?(Hash)
+      @data += CC::Message::encode_hash(msg)
+    elsif msg.is_a?(String)
+      @data = msg
+    else
+      raise ArgumentError, "initializer is not a Hash or String"
+    end
+  end
+
+  def to_wire
+    @data
+  end
 
   #
   # Encode a message.  The item passed in should be a hash, and can contain
@@ -38,13 +57,19 @@
   # encoded as a DATA item.
   #
   def self.to_wire(msg)
-    encode_hash(msg)
+    encoded = [PROTOCOL_VERSION].pack("N")
+    encoded += encode_hash(msg)
+    encoded
   end
 
   #
   # Decode a wire format message.
   #
   def self.from_wire(msg)
+    version, msg = msg.unpack("N a*")
+    unless version == PROTOCOL_VERSION
+      raise CC::DecodeError, "Incorrect protocol version"
+    end
     decode_hash(msg)
   end
 
@@ -108,7 +133,7 @@
 
   def self.encode_hash(msg)
     unless msg.is_a?Hash
-      raise "Should be a hash"
+      raise ArgumentError, "Should be a hash"
     end
     buffer = ""
     msg.each do |key, value|
@@ -120,7 +145,7 @@
 
   def self.encode_array(msg)
     unless msg.is_a?Array
-      raise "Should be an array"
+      raise ArgumentError, "Should be an array"
     end
     buffer = ""
     msg.each do |value|
@@ -163,7 +188,7 @@
       when ITEM_NULL
       value = nil
     else
-      raise "Unknown item type in decode: #{type}"
+      raise CC::DecodeError, "Unknown item type in decode: #{type}"
     end
 
     [value, msg]




More information about the bind10-changes mailing list