class Flic::SimpleClient
Attributes
host[R]
port[R]
thread[R]
Public Class Methods
new(host = 'localhost', port = 5551)
click to toggle source
# File lib/flic/simple_client.rb, line 14 def initialize(host = 'localhost', port = 5551) @host, @port = host, port @blocker = Blocker.new @client = Client.new(host, port) @listen_queues_semaphore = Mutex.new @listen_queues = [] @thread = Thread.new do begin @client.enter_main_loop rescue Client::Shutdown nil ensure shutdown end end @is_shutdown = false end
Public Instance Methods
listen(button_bluetooth_address_or_latency_mode, *button_bluetooth_addresses) { |*params| ... }
click to toggle source
# File lib/flic/simple_client.rb, line 106 def listen(button_bluetooth_address_or_latency_mode, *button_bluetooth_addresses) if Symbol === button_bluetooth_address_or_latency_mode latency_mode = button_bluetooth_address_or_latency_mode else latency_mode = :normal button_bluetooth_addresses.unshift button_bluetooth_address_or_latency_mode end connection_channels = [] queue = Queue.new @listen_queues_semaphore.synchronize { @listen_queues << queue } begin button_bluetooth_addresses.each do |button_bluetooth_addresses| connection_channel = Client::ConnectionChannel.new(button_bluetooth_addresses, latency_mode) connection_channel.button_up_or_down do |click_type, latency| queue << [:button_interaction, button_bluetooth_addresses, click_type, latency] end connection_channel.button_single_click_or_double_click_or_hold do |click_type, latency| queue << [:button_interaction, button_bluetooth_addresses, click_type, latency] end connection_channel.removed do queue << [:connection_channel_removed, connection_channel] end connection_channels << connection_channel @client.add_connection_channel connection_channel end loop do event_type, *params = queue.pop case event_type when :button_interaction yield *params when :connection_channel_removed raise ConnectionChannelRemoved, 'A connection channel was removed' when :shutdown raise Shutdown, 'The client has shutdown' end end ensure connection_channels.each do |connection_channel| @client.remove_connection_channel connection_channel end @listen_queues_semaphore.synchronize { @listen_queues.delete queue unless @listen_queues.frozen? } end rescue Client::Shutdown raise Shutdown, 'The client has shutdown' end
shutdown()
click to toggle source
# File lib/flic/simple_client.rb, line 40 def shutdown @listen_queues_semaphore.synchronize do unless @listen_queues.frozen? @listen_queues.each { |queue| queue << :shutdown }.clear @listen_queues.freeze end end @blocker.unblock_all! Shutdown, 'The client has shutdown' @client.shutdown @thread.join unless Thread.current == @thread @is_shutdown = true end
shutdown?()
click to toggle source
# File lib/flic/simple_client.rb, line 36 def shutdown? @is_shutdown end