Alter the interface presented by Connectors::Socket

We now provide a queue-like reader as well as a queue-lke writer.
Additional read queues can still be registered.
This commit is contained in:
Nick Thomas
2011-11-13 12:00:15 +00:00
parent 1688e55459
commit 6dc5c832f8
3 changed files with 19 additions and 20 deletions

View File

@@ -1,5 +1,4 @@
require 'mutex_m'
module QMPClient
module Connectors
@@ -61,15 +60,18 @@ module QMPClient
# block, and stops reading/writing the socket once the block returns.
# @param[IO] read_socket Socket to read data from
# @param[IO] write_socket Socket to write data to. Use read_socket if nil
# @yields[WriteProxy] writer Push messages to this to get them on the wire
# @yields[Queue, WriteProxy] reader, writer pop / Push messages
def run(read_socket, write_socket=nil, &blk)
write_socket ||= read_socket
readq = Queue.new
register_receive_queue(readq)
write_proxy = WriteProxy.new(write_socket, @serialiser)
begin
stop_r, stop_w = IO.pipe
r_thread = read_thread(read_socket, stop_r)
yield(write_proxy)
yield(readq, write_proxy)
ensure
unregister_receive_queue(readq) if defined?(readq)
stop_w.write("*") if stop_w # Stop signal for the r_thread
stop_w.close if stop_w && !stop_w.closed?
stop_r.close if stop_r && !stop_w.closed?
@@ -114,6 +116,7 @@ module QMPClient
break if buf.nil? # Dispatching raised an error of some sort!
end
end
end
# Converts each message in +buf+ into an instance, and pushes it to each