lib/dripdrop/handlers/http.rb in dripdrop-0.3.1 vs lib/dripdrop/handlers/http.rb in dripdrop-0.4.0

- old
+ new

@@ -1,10 +1,10 @@ require 'thin' require 'json' class DripDrop - class HTTPDeferrableBody + class HTTPDeferrableBody < BaseHandler include EventMachine::Deferrable def call(body) body.each do |chunk| @body_callback.call(chunk) @@ -13,17 +13,18 @@ def each(&blk) @body_callback = blk end - def send_message(msg) + def send_message(raw_msg) + msg = dd_messagify(raw_msg) if msg.class == DripDrop::Message json = msg.encode_json self.call([json]) self.succeed else - raise "Message Type not supported" + raise "Message Type '#{msg.class}' not supported" end end end class HTTPApp @@ -31,11 +32,10 @@ AsyncResponse = [-1, {}, []].freeze def initialize(msg_format,&block) @msg_format = msg_format @recv_cbak = block - super end def call(env) body = HTTPDeferrableBody.new @@ -43,58 +43,58 @@ env['async.callback'].call([200, {'Content-Type' => 'text/plain', 'Access-Control-Allow-Origin' => '*'}, body]) EM.next_tick do case @msg_format when :dripdrop_json msg = DripDrop::Message.decode_json(env['rack.input'].read) - msg.head[:http_env] = env - @recv_cbak.call(msg,body) + @recv_cbak.call(msg,body,env) else raise "Unsupported message type #{@msg_format}" end end end AsyncResponse end end - class HTTPServerHandler + class HTTPServerHandler < BaseHandler attr_reader :address, :opts - def initialize(address,opts={}) - @address = address + def initialize(uri,opts={}) + @uri = uri + @address = uri.to_s @opts = opts end def on_recv(msg_format=:dripdrop_json,&block) - #Rack middleware was not meant to be used this way... #Thin's error handling only rescues stuff w/o a backtrace begin - Thin::Logging.debug = false - Thin::Logging.trace = false - Thin::Server.start(@address.host, @address.port) do + Thin::Logging.silent = true + + Thin::Server.start(@uri.host, @uri.port) do map '/' do run HTTPApp.new(msg_format,&block) end end rescue Exception => e - puts e.message; puts e.backtrace.join("\n"); + puts "Error in Thin server: #{e.message}\n#{e.backtrace.join("\n")}" end end end - class HTTPClientHandler + class HTTPClientHandler < BaseHandler attr_reader :address, :opts - def initialize(address, opts={}) - @address = address + def initialize(uri, opts={}) + @uri = uri + @address = @uri.to_s @opts = opts end def send_message(msg,&block) if msg.class == DripDrop::Message req = EM::Protocols::HttpClient.request( - :host => address.host, :port => address.port, + :host => @uri.host, :port => @uri.port, :request => '/', :verb => 'POST', :contenttype => 'application/json', :content => msg.encode_json ) req.callback do |response|