Skip to content

Commit

Permalink
Make Stream usable in a supervision tree
Browse files Browse the repository at this point in the history
Supervisors don't tolerate a secondary startup step.
  • Loading branch information
benlangfeld committed Jan 8, 2016
1 parent 3e62051 commit 5a6a19e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions README.md
Expand Up @@ -21,10 +21,6 @@ In order to setup a connection to listen for AMI events, one can do:
```ruby
require 'ruby_ami'

stream = RubyAMI::Stream.new '127.0.0.1', 5038, 'manager', 'password',
->(e) { handle_event e },
Logger.new(STDOUT), 10

def handle_event(event)
case event.name
when 'FullyBooted'
Expand All @@ -34,28 +30,32 @@ def handle_event(event)
end
end

stream.run # This will block until the actor is terminated elsewhere. stream.async.run is also available if you need to do other things in the main thread.
stream = RubyAMI::Stream.new '127.0.0.1', 5038, 'manager', 'password',
->(e) { handle_event e },
Logger.new(STDOUT), 10

Celluloid.join(stream) # This will block until the actor is terminated elsewhere. Otherwise, the actor will run in its own thread allowing other work to be done here.
```

It is also possible to execute actions in response to events:

```ruby
require 'ruby_ami'

$stream = RubyAMI::Stream.new '127.0.0.1', 5038, 'manager', 'password',
->(e) { handle_event e },
Logger.new(STDOUT), 10

def handle_event(event)
def handle_event(event, stream)
case event.name
when 'FullyBooted'
puts "The connection was successful. Originating a call."
response = $stream.send_action 'Originate', 'Channel' => 'SIP/foo'
response = stream.send_action 'Originate', 'Channel' => 'SIP/foo'
puts "The call origination resulted in #{response.inspect}"
end
end

$stream.run
stream = RubyAMI::Stream.new '127.0.0.1', 5038, 'manager', 'password',
->(e) { handle_event e },
Logger.new(STDOUT), 10

Celluloid.join(stream) # This will block until the actor is terminated elsewhere. Otherwise, the actor will run in its own thread allowing other work to be done here.
```

Executing actions does not strictly have to be done within the event handler, but it is not valid to send AMI events before receiving a `FullyBooted` event. If you attempt to execute an action prior to this, it may fail, and `RubyAMI::Stream` will not help you recover or queue the action until the connection is `FullyBooted`; you must manage this timing yourself. That said, assuming you take care of this, you may invoke `RubyAMI::Stream#send_action` from anywhere in your code and it will return the response of the action.
Expand Down
1 change: 1 addition & 0 deletions lib/ruby_ami/stream.rb
Expand Up @@ -29,6 +29,7 @@ def initialize(host, port, username, password, event_callback, logger = Logger,
@lexer = Lexer.new self
@sent_actions = {}
@causal_actions = {}
async.run
end

[:started, :stopped, :ready].each do |state|
Expand Down
1 change: 0 additions & 1 deletion spec/ruby_ami/stream_spec.rb
Expand Up @@ -34,7 +34,6 @@ def mocked_server(times = nil, fake_client = nil, &block)
mock_target.should_receive(:receive_data).send(*(times ? [:exactly, times] : [:at_least, 1])).with &block
s = ServerMock.new '127.0.0.1', server_port, mock_target
@stream = Stream.new '127.0.0.1', server_port, username, password, lambda { |m| client.message_received m }
@stream.async.run
fake_client.call if fake_client.respond_to? :call
Celluloid::Actor.join s
Timeout.timeout 5 do
Expand Down

0 comments on commit 5a6a19e

Please sign in to comment.