[svn] commit: r36 - 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 21:53:49 UTC 2009
Author: mgraff
Date: Fri Sep 25 21:53:49 2009
New Revision: 36
Log:
make it work with both ruby 1.8 and 1.9 -- minor changes, mostly dealing with default UTF-8 encoding in Ruby 1.9
Modified:
experiments/graff-ccapi/ruby/lib/cc.rb
experiments/graff-ccapi/ruby/lib/cc/message.rb
experiments/graff-ccapi/ruby/lib/cc/session.rb
Modified: experiments/graff-ccapi/ruby/lib/cc.rb
==============================================================================
--- experiments/graff-ccapi/ruby/lib/cc.rb (original)
+++ experiments/graff-ccapi/ruby/lib/cc.rb Fri Sep 25 21:53:49 2009
@@ -26,6 +26,20 @@
end
end
+class CC
+ def self.set_utf8(str) #nodoc
+ if str.respond_to?('force_encoding')
+ str.force_encoding(Encoding::UTF_8)
+ end
+ end
+
+ def self.set_binary(str) #nodoc
+ if str.respond_to?('force_encoding')
+ str.force_encoding(Encoding::BINARY)
+ end
+ end
+end
+
require_relative 'cc/message'
require_relative 'cc/session'
@@ -34,6 +48,5 @@
cc.sendmsg({ :type => :getlname })
-
puts "Our local name: #{cc.lname}"
end
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 21:53:49 2009
@@ -18,6 +18,21 @@
end
class CC
+ def self.set_utf8(str) #nodoc
+ if str.respond_to?('force_encoding')
+ str.force_encoding(Encoding::UTF_8)
+ end
+ end
+
+ def self.set_binary(str) #nodoc
+ if str.respond_to?('force_encoding')
+ str.force_encoding(Encoding::BINARY)
+ end
+ end
+end
+
+
+class CC
class Message
PROTOCOL_VERSION = 0x536b616e
@@ -44,6 +59,7 @@
end
def to_wire
+ CC::set_binary(@data)
@data
end
@@ -59,6 +75,7 @@
def self.to_wire(msg)
encoded = [PROTOCOL_VERSION].pack("N")
encoded += encode_hash(msg)
+ CC::set_binary(encoded)
encoded
end
@@ -66,6 +83,7 @@
# Decode a wire format message.
#
def self.from_wire(msg)
+ CC::set_binary(msg)
version, msg = msg.unpack("N a*")
unless version == PROTOCOL_VERSION
raise CC::DecodeError, "Incorrect protocol version"
@@ -155,17 +173,19 @@
end
def self.decode_tag(str)
- length = str[0]
+ length = str.unpack("C")[0]
tag, remainder = str.unpack("x a#{length} a*")
+ CC::set_utf8(tag)
[ tag, remainder ]
end
def self.decode_item(msg)
- type = msg[0] & ITEM_MASK
- length_format = msg[0] & ITEM_LENGTH_MASK
+ type_and_length_format = msg.unpack("C")[0]
+ type = type_and_length_format & ITEM_MASK
+ length_format = type_and_length_format & ITEM_LENGTH_MASK
if type == ITEM_NULL
- msg = msg.unpack("x a*").to_s
+ msg = msg.unpack("x a*")[0]
else
if length_format == ITEM_LENGTH_8
length, msg = msg.unpack("x C a*")
@@ -214,7 +234,9 @@
ret
end
-end
+
+end # class Message
+end # class CC
if $0 == __FILE__
target = {
@@ -228,7 +250,7 @@
}
wire = CC::Message.to_wire(target)
+ puts wire.inspect
puts CC::Message.from_wire(wire).inspect
-end # class Message
-end # class CC
+end
Modified: experiments/graff-ccapi/ruby/lib/cc/session.rb
==============================================================================
--- experiments/graff-ccapi/ruby/lib/cc/session.rb (original)
+++ experiments/graff-ccapi/ruby/lib/cc/session.rb Fri Sep 25 21:53:49 2009
@@ -29,6 +29,12 @@
# :port => port to connect to (defaults to 9913)
#
def initialize(args = {})
+ @socket = nil
+ @lname = nil
+ @recvbuffer = nil
+ @recvlength = nil
+ @sendbuffer = ""
+
options = {
:host => "127.0.0.1",
:port => 9913
@@ -45,8 +51,19 @@
if @lname.nil?
raise CC::ProtocolError, "Could not get local name"
end
+ CC::set_utf8(@lname)
end
+ #
+ # Send a message to the controller. The item to send can either be a
+ # CC::Message object, or a Hash. If a Hash, it will be internally
+ # converted to a CC::Message before transmitting.
+ #
+ # A return value of true means the entire message was not
+ # transmitted, and a call to send_pending will have to be
+ # made to send remaining data. This should only happen when
+ # the socket is in non-blocking mode.
+ #
def sendmsg(msg)
if msg.is_a?(Hash)
msg = CC::Message.new(msg)
@@ -57,14 +74,19 @@
end
wire = msg.to_wire
- sent = @socket.send([wire.length].pack("N"), 0)
- if sent != 4
- raise CC::SendError, "Could not send length bytes"
- end
- sent = @socket.send(wire, 0)
- if sent != wire.length
- raise CC::SendError, "Could not send bytes"
- end
+ @sendbuffer << [wire.length].pack("N")
+ @sendbuffer << wire
+
+ send_pending
+ end
+
+ #
+ # Send as much data as we can.
+ def send_pending
+ return false if @sendbuffer.length == 0
+ sent = @socket.send(@sendbuffer, 0)
+ @sendbuffer = @sendbuffer[sent .. -1]
+ @sendbuffer.length == 0 ? true : false
end
def recvmsg
More information about the bind10-changes
mailing list