class Flic::Client

Constants

ServerInfo

Attributes

connection[R]
host[R]
port[R]
socket[R]

Public Class Methods

new(host = 'localhost', port = 5551) { |self| ... } click to toggle source
# File lib/flic/client.rb, line 44
def initialize(host = 'localhost', port = 5551)
  @host, @port = host, port
  @handle_next_event_semaphore = Mutex.new
  @socket = TCPSocket.new(host, port)
  @connection = Protocol::Connection.new(socket)
  @is_shutdown = false
  yield self if block_given?
end
open(*args) { |client| ... } click to toggle source
# File lib/flic/client.rb, line 28
def open(*args)
  client = new(*args)

  begin
    yield client
  ensure
    client.shutdown
  end
end

Public Instance Methods

enter_main_loop() click to toggle source
# File lib/flic/client.rb, line 74
def enter_main_loop
  loop { handle_next_event }
end
handle_next_event() click to toggle source
# File lib/flic/client.rb, line 62
def handle_next_event
  @handle_next_event_semaphore.synchronize do
    begin
      handle_event connection.recv_event
    rescue Protocol::Connection::UnderlyingSocketClosedError
      shutdown

      raise Shutdown, 'The connection has been closed'
    end
  end
end
shutdown() click to toggle source
# File lib/flic/client.rb, line 57
def shutdown
  socket.close
  @is_shutdown = true
end
shutdown?() click to toggle source
# File lib/flic/client.rb, line 53
def shutdown?
  @is_shutdown
end

Private Instance Methods

handle_event(event) click to toggle source
# File lib/flic/client.rb, line 88
def handle_event(event)
  case event
    when Protocol::Events::NewVerifiedButton
      new_button_verified event.bluetooth_address
    when Protocol::Events::BluetoothControllerStateChange
      bluetooth_controller_state_changed event.bluetooth_controller_state
    when Protocol::Events::NoSpaceForNewConnection
      connections_exhausted
    when Protocol::Events::GotSpaceForNewConnection
      connection_available
  end
end
send_command(command) click to toggle source
# File lib/flic/client.rb, line 80
def send_command(command)
  connection.send_command(command)
rescue Protocol::Connection::UnderlyingSocketClosedError
  shutdown

  raise Shutdown, 'The connection has been closed'
end