Class: Librevox::Listener::Base

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

Direct Known Subclasses

Inbound, Outbound

Defined Under Namespace

Classes: CommandDelegate

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
# File 'lib/librevox/listener/base.rb', line 9

def initialize(connection)
  @connection = connection
  @reply_queue = Async::Queue.new
  @command_mutex = Async::Semaphore.new(1)
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



58
59
60
# File 'lib/librevox/listener/base.rb', line 58

def response
  @response
end

Class Method Details

.event(event, &block) ⇒ Object



20
21
22
# File 'lib/librevox/listener/base.rb', line 20

def event(event, &block)
  hooks[event] << block
end

.hooksObject



16
17
18
# File 'lib/librevox/listener/base.rb', line 16

def hooks
  @hooks ||= Hash.new {|hash, key| hash[key] = []}
end

Instance Method Details

#apiObject

Exposes an instance of CommandDelegate, which includes Commands.

Examples:

api.status
api.fsctl :pause
api.uuid_park "592567a2-1be4-11df-a036-19bfdab2092f"

See Also:



47
48
49
# File 'lib/librevox/listener/base.rb', line 47

def api
  @command_delegate ||= CommandDelegate.new(self)
end

#disconnectObject



87
88
89
# File 'lib/librevox/listener/base.rb', line 87

def disconnect
  @connection&.close
end

#handle_responseObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/librevox/listener/base.rb', line 65

def handle_response
  if response.reply?
    @reply_queue.push(response)
    return
  end

  if response.event?
    resp = response
    Async do
      on_event(resp)
      invoke_event_hooks(resp)
    end
  end
end

#on_event(event) ⇒ Object

override



81
82
# File 'lib/librevox/listener/base.rb', line 81

def on_event(event)
end

#receive_message(response) ⇒ Object



60
61
62
63
# File 'lib/librevox/listener/base.rb', line 60

def receive_message(response)
  @response = response
  handle_response
end

#run_sessionObject



84
85
# File 'lib/librevox/listener/base.rb', line 84

def run_session
end

#send_message(msg) ⇒ Object



51
52
53
54
55
56
# File 'lib/librevox/listener/base.rb', line 51

def send_message(msg)
  @command_mutex.acquire do
    @connection.send_message(msg)
    @reply_queue.dequeue
  end
end