class Noam::Listener
Attributes
port[R]
Public Class Methods
new()
click to toggle source
# File lib/noam_lemma/listener.rb, line 7 def initialize @queue = Queue.new @server = TCPServer.new(0) @port = @server.addr[1] manage_queue_on_thread end
Public Instance Methods
connected?()
click to toggle source
# File lib/noam_lemma/listener.rb, line 24 def connected? !@disconnected end
stop()
click to toggle source
# File lib/noam_lemma/listener.rb, line 19 def stop @exit_requested = true @thread.join end
take()
click to toggle source
# File lib/noam_lemma/listener.rb, line 15 def take @queue.pop end
Private Instance Methods
exiting?()
click to toggle source
# File lib/noam_lemma/listener.rb, line 72 def exiting? return @exit_requested end
listen_for_connection()
click to toggle source
# File lib/noam_lemma/listener.rb, line 51 def listen_for_connection timeout_sec = 0.1 available_ios = IO.select([@server], nil, nil, timeout_sec) @server.accept if available_ios end
loop_listen()
click to toggle source
# File lib/noam_lemma/listener.rb, line 40 def loop_listen loop do if client = listen_for_connection read_from_client(client) client.close end break if exiting? end end
manage_queue_on_thread()
click to toggle source
# File lib/noam_lemma/listener.rb, line 30 def manage_queue_on_thread @thread = Thread.new do |t| begin loop_listen ensure @server.close end end end
read_from_client(client)
click to toggle source
# File lib/noam_lemma/listener.rb, line 57 def read_from_client(client) begin loop do message_length = client.read_nonblock(Message::MESSAGE_LENGTH_STRING_SIZE).to_i message_content = client.read_nonblock(message_length) @queue.push(Message::Heard.from_noam(message_content)) break if exiting? end rescue IO::WaitReadable retry unless exiting? rescue EOFError @disconnected = true end end