Class: Librevox::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/librevox/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler, endpoint, **options) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/librevox/client.rb', line 7

def initialize(handler, endpoint, **options)
  @handler = handler
  @endpoint = endpoint
  @options = options
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



13
14
15
# File 'lib/librevox/client.rb', line 13

def endpoint
  @endpoint
end

Instance Method Details

#connect(socket) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/librevox/client.rb', line 15

def connect(socket)
  stream = IO::Stream(socket)
  connection = Protocol::Connection.new(stream)

  listener = @handler.new(connection, @options)

  session_task = Async { listener.run_session }
  connection.read_loop { |msg| listener.receive_message(msg) }
ensure
  session_task&.stop
  connection.close
end

#runObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/librevox/client.rb', line 28

def run
  loop do
    @endpoint.connect do |socket|
      connect(socket)
    end
  rescue IOError, Errno::ECONNREFUSED, Errno::ECONNRESET => e
    Librevox.logger.error "Connection lost: #{e.message}. Reconnecting in 1s."
    sleep 1
  end
end