lib/noam_lemma/player.rb in noam_lemma-0.2.1.2 vs lib/noam_lemma/player.rb in noam_lemma-0.2.1.3

- old
+ new

@@ -1,6 +1,6 @@ -require 'thread' +require 'thread' module Noam class NoamPlayerException < Exception; end class Player @@ -18,61 +18,45 @@ def put(message) @queue.push(message) end def stop - put(:soft_exit) + put(:exit) @thread.join end def stop! - put(:hard_exit) - @thread.join + @thread.exit end + def connected? + !@disconnected + end + private def manage_queue_on_thread @thread = Thread.new do |t| begin loop do message = @queue.pop break if exit?(message) - process(message) + print_message(message) end + rescue Errno::EPIPE + @disconnected = true ensure @socket.close end end end - def process(message) - case message - when :soft_exit - finish_queue - when :hard_exit - else - @socket.print(message.noam_encode) - @socket.flush - end + def print_message(message) + @socket.print(message.noam_encode) + @socket.flush end def exit?(message) - message == :hard_exit || message == :soft_exit - end - - def finish_queue - queue_to_array.each do |message| - @socket.print(message.noam_encode) - @socket.flush - end - end - - def queue_to_array - result = [] - while(@queue.size > 0) do - result << @queue.pop - end - result + message == :exit end end end