Class: Punchblock::Connection::Asterisk

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

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


11
12
13
14
15
16
# File 'lib/punchblock/connection/asterisk.rb', line 11

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


8
9
10
# File 'lib/punchblock/connection/asterisk.rb', line 8

def ami_client
  @ami_client
end

#event_handlerObject

Returns the value of attribute event_handler


9
10
11
# File 'lib/punchblock/connection/asterisk.rb', line 9

def event_handler
  @event_handler
end

#translatorObject (readonly)

Returns the value of attribute translator


8
9
10
# File 'lib/punchblock/connection/asterisk.rb', line 8

def translator
  @translator
end

Instance Method Details

#handle_event(event) ⇒ Object


36
37
38
# File 'lib/punchblock/connection/asterisk.rb', line 36

def handle_event(event)
  event_handler.call event
end

#new_ami_streamObject


40
41
42
43
44
45
# File 'lib/punchblock/connection/asterisk.rb', line 40

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

#new_call_uriObject


53
54
55
# File 'lib/punchblock/connection/asterisk.rb', line 53

def new_call_uri
  Punchblock.new_uuid
end

#runObject

Raises:


18
19
20
21
# File 'lib/punchblock/connection/asterisk.rb', line 18

def run
  start_ami_client
  raise DisconnectedError
end

#send_message(*args) ⇒ Object


32
33
34
# File 'lib/punchblock/connection/asterisk.rb', line 32

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

#start_ami_clientObject


47
48
49
50
51
# File 'lib/punchblock/connection/asterisk.rb', line 47

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

#stopObject


23
24
25
26
# File 'lib/punchblock/connection/asterisk.rb', line 23

def stop
  translator.terminate
  ami_client.terminate
end

#write(command, options) ⇒ Object


28
29
30
# File 'lib/punchblock/connection/asterisk.rb', line 28

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