Permalink
Browse files

woot! houston, we got data.

  • Loading branch information...
1 parent 1c07506 commit 73b458645426152eed4129b280a2284cd2f09ea8 @igrigorik committed Jan 10, 2011
Showing with 55 additions and 1 deletion.
  1. +1 −0 lib/em-handlersocket.rb
  2. +35 −0 lib/em-handlersocket/client.rb
  3. +19 −1 spec/client_spec.rb
View
@@ -1,4 +1,5 @@
require 'eventmachine'
+require 'em/protocols/line_protocol'
require 'em-handlersocket/client'
module EventMachine
@@ -2,6 +2,41 @@ module EventMachine
module HandlerSocket
class Client < EventMachine::Connection
+ include EventMachine::Deferrable
+ include EventMachine::Protocols::LineProtocol
+
+ def initialize
+ @deferrables = []
+ end
+
+ def connection_completed
+ succeed
+ end
+
+ def execute(*cmd, &blk)
+ callback { send(cmd) }
+ add_deferrable(&blk)
+ end
+
+
+ private
+
+ def send(data)
+ p [:sending, data.join("\t")]
+ send_data data.join("\t")+"\n"
+ end
+
+ def receive_line(line)
+ @deferrables.shift.succeed(line.chomp.split("\t"))
+ end
+
+ def add_deferrable(&blk)
+ df = EM::DefaultDeferrable.new
+ df.callback &blk
+
+ @deferrables.push(df)
+ df
+ end
end
end
View
@@ -20,4 +20,22 @@
}
end
-end
+ it "should open an index" do
+ EM.run {
+ c = EM::HandlerSocket.new
+
+ df = c.execute(['P', '0', 'widgets', 'user', 'PRIMARY', 'user_name,user_email,created'])
+
+ df.callback {|data|
+ p data
+
+ df = c.execute(['0', '=', '1', '1'])
+ df.callback { |data|
+ p data
+ EM.stop
+ }
+ }
+ }
+ end
+
+end

0 comments on commit 73b4586

Please sign in to comment.