Skip to content

Commit

Permalink
[FEATURE] Support for dial requests with a URI on Asterisk
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed Feb 28, 2014
1 parent 818fdef commit 0ad92c1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
@@ -1,5 +1,5 @@
# [develop](https://github.com/adhearsion/punchblock)
* Feature: Add support for URI attribute to Dial command
* Feature: Add support for requesting calls with a specific URI
* Feature: Allow generation of a random call URI for a client

# [v2.3.1](https://github.com/adhearsion/punchblock/compare/v2.3.0...v2.3.1) - [2014-02-13](https://rubygems.org/gems/punchblock/versions/2.3.1)
Expand Down
10 changes: 7 additions & 3 deletions lib/punchblock/translator/asterisk.rb
Expand Up @@ -140,9 +140,13 @@ def execute_global_command(command)
register_component component
component.execute
when Punchblock::Command::Dial
call = Call.new command.to, current_actor, ami_client, connection
register_call call
call.dial command
if call = call_with_id(command.uri)
command.response = ProtocolError.new.setup(:conflict, 'Call ID already in use')
else
call = Call.new command.to, current_actor, ami_client, connection, nil, command.uri
register_call call
call.dial command
end
else
command.response = ProtocolError.new.setup 'command-not-acceptable', "Did not understand command"
end
Expand Down
5 changes: 3 additions & 2 deletions lib/punchblock/translator/asterisk/call.rb
Expand Up @@ -25,10 +25,11 @@ class Call
HANGUP_CAUSE_TO_END_REASON[22] = :reject
HANGUP_CAUSE_TO_END_REASON[102] = :timeout

def initialize(channel, translator, ami_client, connection, agi_env = nil)
def initialize(channel, translator, ami_client, connection, agi_env = nil, id = nil)
@channel, @translator, @ami_client, @connection = channel, translator, ami_client, connection
@agi_env = agi_env || {}
@id, @components = Punchblock.new_uuid, {}
@id = id || Punchblock.new_uuid
@components = {}
@answered = false
@pending_joins = {}
@progress_sent = false
Expand Down
39 changes: 38 additions & 1 deletion spec/punchblock/translator/asterisk_spec.rb
Expand Up @@ -7,7 +7,7 @@ module Punchblock
module Translator
describe Asterisk do
let(:ami_client) { double 'RubyAMI::Client' }
let(:connection) { double 'Connection::Asterisk', handle_event: nil }
let(:connection) { Connection::Asterisk.new }

let(:translator) { Asterisk.new ami_client, connection }

Expand All @@ -16,6 +16,10 @@ module Translator
its(:ami_client) { should be ami_client }
its(:connection) { should be connection }

before do
connection.event_handler = ->(*) {}
end

after { translator.terminate if translator.alive? }

describe '#execute_command' do
Expand Down Expand Up @@ -210,6 +214,39 @@ module Translator
mock_call.should_receive(:dial).once.with command
subject.execute_global_command command
end

context 'when requesting a specific URI' do
let(:requested_uri) { connection.new_call_uri }

before do
command.uri = requested_uri
end

it "should assign the requested URI to the call" do
subject.execute_global_command command
subject.call_with_id(requested_uri).id.should == requested_uri
end

context 'and the requested URI already represents a known call' do
before do
earlier_command = Command::Dial.new to: 'SIP/1234', uri: requested_uri
earlier_command.request!

subject.execute_global_command earlier_command

@first_call = subject.call_with_id(requested_uri)
end

it "should set the command response to a conflict error" do
subject.execute_global_command command
command.response(0.1).should == ProtocolError.new.setup(:conflict, 'Call ID already in use')
end

it "should not replace the original call in the registry" do
subject.call_with_id(requested_uri).should be @first_call
end
end
end
end

context 'with an AMI action' do
Expand Down

0 comments on commit 0ad92c1

Please sign in to comment.