Permalink
Browse files

Convert warden-client to use warden-protocol

Change-Id: Ibaa5189381d7c22e9628d691aa8925c568e03d03
  • Loading branch information...
1 parent cb58664 commit 5e7be6ca2512849473b129db3df578725472da1d @pietern pietern committed Jun 28, 2012
View
@@ -3,6 +3,7 @@ source "http://rubygems.org"
gemspec
gem "rake"
+gem "warden-protocol", :path => "../warden-protocol"
group :spec do
gem "rspec"
@@ -1,5 +1,5 @@
require "socket"
-require "yajl"
+require "warden/protocol"
module Warden
@@ -34,36 +34,51 @@ def reconnect
connect
end
- def read
- line = @sock.gets
- if line.nil?
+ def io
+ rv = yield
+ if rv.nil?
disconnect
raise ::EOFError
end
- object = ::Yajl::Parser.parse(line)
- payload = object["payload"]
+ rv
+ end
+
+ def read
+ length = io { @sock.gets }
+ data = io { @sock.read(length.to_i) }
+
+ # Discard \r\n
+ io { @sock.read(2) }
+
+ wrapped_response = Warden::Protocol::WrappedResponse.decode(data)
+ response = wrapped_response.response
# Raise error replies
- if object["type"] == "error"
- raise Warden::Client::ServerError.new(payload)
+ if response.is_a?(Warden::Protocol::ErrorResponse)
+ raise Warden::Client::ServerError.new(response.message)
end
- payload
+ response
end
- def write(args)
- json = ::Yajl::Encoder.encode(args, :pretty => false)
- @sock.write(json + "\n")
+ def write(request)
+ data = request.wrap.encode.to_s
+ @sock.write data.length.to_s + "\r\n"
+ @sock.write data + "\r\n"
end
- def call(args)
- write(args)
+ def call(request)
+ write(request)
read
end
def method_missing(sym, *args, &blk)
- call([sym, *args])
+ klass_name = sym.to_s.gsub(/(^|_)([a-z])/) { $2.upcase }
+ klass_name += "Request"
+ klass = Warden::Protocol.const_get(klass_name)
+
+ call(klass.new(*args))
end
end
end
@@ -120,14 +120,16 @@ def connection_count
context "when connected" do
before(:each) do
- start_server do |session, args|
- case args.shift
+ start_server do |session, request|
+ next if request.nil?
+
+ case request.message
when "eof"
session.close
when "error"
- session.reply(:type => "error", :payload => "error")
- when "echo"
- session.reply(:payload => args)
+ session.respond(Warden::Protocol::ErrorResponse.new(:message => "error"))
+ else
+ session.respond(request.create_response(:message => request.message))
end
end
@@ -137,7 +139,7 @@ def connection_count
it "should raise EOFError on eof" do
expect do
- client.eof
+ client.echo(:message => "eof")
end.to raise_error(::EOFError)
# This should update the connection status
@@ -146,16 +148,16 @@ def connection_count
it "should raise Warden::Client::ServerError on error payloads" do
expect do
- client.error
+ client.echo(:message => "error")
end.to raise_error(Warden::Client::ServerError)
# This should not affect the connection
client.should be_connected
end
it "should return decoded payload for non-error replies" do
- reply = client.echo("hello")
- reply.should == ["hello"]
+ response = client.echo(:message => "hello")
+ response.message.should == "hello"
end
end
end
@@ -1,6 +1,6 @@
require "socket"
-require "yajl"
require "tempfile"
+require "warden/protocol"
class Session
@@ -9,11 +9,11 @@ def initialize(sock, handler = nil)
@handler = handler
# Post-initialization
- handle([nil])
+ handle(nil)
end
- def handle(args)
- @handler.call(self, args) if @handler
+ def handle(request)
+ @handler.call(self, request) if @handler
end
def close
@@ -22,15 +22,21 @@ def close
@sock = nil
end
- def reply(obj)
- data = ::Yajl::Encoder.encode(obj, :pretty => false) + "\n"
- @sock.write(data)
+ def respond(response)
+ data = response.wrap.encode.to_s
+ @sock.write data.length.to_s + "\r\n"
+ @sock.write data + "\r\n"
end
def run!
- while @sock && line = @sock.gets
- args = ::Yajl::Parser.parse(line)
- handle(args)
+ while @sock && length = @sock.gets
+ data = @sock.read(length.to_i)
+
+ # Discard \r\n
+ @sock.read(2)
+
+ wrapped_request = Warden::Protocol::WrappedRequest.decode(data)
+ handle(wrapped_request.request)
end
end
end
@@ -16,5 +16,5 @@ Gem::Specification.new do |s|
s.executables = []
s.require_paths = ["lib"]
- s.add_runtime_dependency "yajl-ruby"
+ s.add_runtime_dependency "warden-protocol"
end

0 comments on commit 5e7be6c

Please sign in to comment.