Class: Librevox::Listener::Base
- Inherits:
-
Object
- Object
- Librevox::Listener::Base
- Defined in:
- lib/librevox/listener/base.rb
Defined Under Namespace
Classes: CommandDelegate
Instance Attribute Summary collapse
-
#response ⇒ Object
Returns the value of attribute response.
Class Method Summary collapse
Instance Method Summary collapse
-
#api ⇒ Object
Exposes an instance of CommandDelegate, which includes Commands.
- #disconnect ⇒ Object
- #handle_response ⇒ Object
-
#initialize(connection) ⇒ Base
constructor
A new instance of Base.
-
#on_event(event) ⇒ Object
override.
- #receive_message(response) ⇒ Object
- #run_session ⇒ Object
- #send_message(msg) ⇒ Object
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
#response ⇒ Object
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 |
.hooks ⇒ Object
16 17 18 |
# File 'lib/librevox/listener/base.rb', line 16 def hooks @hooks ||= Hash.new {|hash, key| hash[key] = []} end |
Instance Method Details
#api ⇒ Object
Exposes an instance of CommandDelegate, which includes Commands.
47 48 49 |
# File 'lib/librevox/listener/base.rb', line 47 def api @command_delegate ||= CommandDelegate.new(self) end |
#disconnect ⇒ Object
87 88 89 |
# File 'lib/librevox/listener/base.rb', line 87 def disconnect @connection&.close end |
#handle_response ⇒ Object
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 (response) @response = response handle_response end |
#run_session ⇒ Object
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 (msg) @command_mutex.acquire do @connection.(msg) @reply_queue.dequeue end end |