Class: Adhearsion::Rayo::Connection::Asterisk

Inherits:
GenericConnection show all
Defined in:
lib/adhearsion/rayo/connection/asterisk.rb

Defined Under Namespace

Classes: RubyAMIStreamProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from GenericConnection

#connected?, #not_ready!, #ready!

Constructor Details

#initialize(options = {}) ⇒ Asterisk

Returns a new instance of Asterisk



14
15
16
17
18
19
# File 'lib/adhearsion/rayo/connection/asterisk.rb', line 14

def initialize(options = {})
  @stream_options = options.values_at(:host, :port, :username, :password)
  @ami_client = new_ami_stream
  @translator = Translator::Asterisk.new @ami_client, self
  super()
end

Instance Attribute Details

#ami_clientObject (readonly)

Returns the value of attribute ami_client



11
12
13
# File 'lib/adhearsion/rayo/connection/asterisk.rb', line 11

def ami_client
  @ami_client
end

#event_handlerObject

Returns the value of attribute event_handler



12
13
14
# File 'lib/adhearsion/rayo/connection/asterisk.rb', line 12

def event_handler
  @event_handler
end

#translatorObject (readonly)

Returns the value of attribute translator



11
12
13
# File 'lib/adhearsion/rayo/connection/asterisk.rb', line 11

def translator
  @translator
end

Instance Method Details

#handle_event(event) ⇒ Object



39
40
41
# File 'lib/adhearsion/rayo/connection/asterisk.rb', line 39

def handle_event(event)
  event_handler.call event
end

#new_ami_streamObject



43
44
45
46
47
48
# File 'lib/adhearsion/rayo/connection/asterisk.rb', line 43

def new_ami_stream
  stream = RubyAMI::Stream.new(*@stream_options, ->(event) { translator.async.handle_ami_event event }, logger)
  client = (ami_client || RubyAMIStreamProxy.new(stream))
  client.stream = stream
  client
end

#new_call_uriObject



56
57
58
# File 'lib/adhearsion/rayo/connection/asterisk.rb', line 56

def new_call_uri
  Adhearsion.new_uuid
end

#runObject

Raises:



21
22
23
24
# File 'lib/adhearsion/rayo/connection/asterisk.rb', line 21

def run
  start_ami_client
  raise DisconnectedError
end

#send_message(*args) ⇒ Object



35
36
37
# File 'lib/adhearsion/rayo/connection/asterisk.rb', line 35

def send_message(*args)
  translator.send_message *args
end

#start_ami_clientObject



50
51
52
53
54
# File 'lib/adhearsion/rayo/connection/asterisk.rb', line 50

def start_ami_client
  @ami_client = new_ami_stream unless ami_client.alive?
  ami_client.async.run
  Celluloid::Actor.join(ami_client)
end

#stopObject



26
27
28
29
# File 'lib/adhearsion/rayo/connection/asterisk.rb', line 26

def stop
  translator.terminate
  ami_client.terminate
end

#write(command, options) ⇒ Object



31
32
33
# File 'lib/adhearsion/rayo/connection/asterisk.rb', line 31

def write(command, options)
  translator.async.execute_command command, options
end