class Noam::Player

Public Class Methods

new(remote_host, remote_port) click to toggle source
# File lib/noam_lemma/player.rb, line 7
def initialize(remote_host, remote_port)
  begin
    @socket = TCPSocket.new(remote_host, remote_port)
  rescue Errno::ECONNREFUSED
    raise NoamPlayerException.new("Unable to connect to the Noam server at #{remote_host}:#{remote_port}. Is it running?")
  end

  @queue = Queue.new
  manage_queue_on_thread
end

Public Instance Methods

connected?() click to toggle source
# File lib/noam_lemma/player.rb, line 31
def connected?
  !@disconnected
end
put(message) click to toggle source
# File lib/noam_lemma/player.rb, line 18
def put(message)
  @queue.push(message)
end
stop() click to toggle source
# File lib/noam_lemma/player.rb, line 22
def stop
  put(:exit)
  @thread.join
end
stop!() click to toggle source
# File lib/noam_lemma/player.rb, line 27
def stop!
  @thread.exit
end

Private Instance Methods

exit?(message) click to toggle source
# File lib/noam_lemma/player.rb, line 58
def exit?(message)
  message == :exit
end
manage_queue_on_thread() click to toggle source
# File lib/noam_lemma/player.rb, line 37
def manage_queue_on_thread
  @thread = Thread.new do |t|
    begin
      loop do
        message = @queue.pop
        break if exit?(message)
        print_message(message)
      end
    rescue Errno::EPIPE
      @disconnected = true
    ensure
      @socket.close
    end
  end
end
print_message(message) click to toggle source