Class: Flic::Protocol::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/flic/protocol/connection.rb

Overview

A wrapper around a socket that allows sending/receiving commands/events

Defined Under Namespace

Classes: UnderlyingSocketClosedError, UnexpectedNilReturnValueFromRead

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Connection

Returns a new instance of Connection.



13
14
15
16
17
# File 'lib/flic/protocol/connection.rb', line 13

def initialize(socket)
  @socket = socket
  @read_semaphore = Mutex.new
  @write_semaphore = Mutex.new
end

Instance Attribute Details

#socketSocket (readonly)

Returns the underlying socket.

Returns:

  • (Socket)

    the underlying socket



11
12
13
# File 'lib/flic/protocol/connection.rb', line 11

def socket
  @socket
end

Instance Method Details

#recv_commandFlic::Protocol::Commands::Command

Note:

This method may block

Receives a command from the socket



28
29
30
# File 'lib/flic/protocol/connection.rb', line 28

def recv_command
  Protocol.parse_command(recv_packet)
end

#recv_eventFlic::Protocol::Events::Event

Note:

This method may block

Receives an event from the socket



41
42
43
# File 'lib/flic/protocol/connection.rb', line 41

def recv_event
  Protocol.parse_event(recv_packet)
end

#send_command(command) ⇒ Object

Sends a command over the socket

Parameters:



21
22
23
# File 'lib/flic/protocol/connection.rb', line 21

def send_command(command)
  send_packet Protocol.serialize_command(command)
end

#send_event(event) ⇒ Object

Sends an event over the socket

Parameters:



34
35
36
# File 'lib/flic/protocol/connection.rb', line 34

def send_event(event)
  send_packet Protocol.serialize_event(event)
end