Sha256: ee71000fbb890e25affd3486e673ab2b337154f99c2e0758815e703416049e12

Contents?: true

Size: 1.57 KB

Versions: 10

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8

require 'ruby_ami'

module Punchblock
  module Connection
    class Asterisk < GenericConnection
      attr_reader :ami_client, :translator
      attr_accessor :event_handler

      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

      def run
        start_ami_client
        raise DisconnectedError
      end

      def stop
        translator.terminate
        ami_client.terminate
      end

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

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

      def handle_event(event)
        event_handler.call event
      end

      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

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

      def new_call_uri
        Punchblock.new_uuid
      end
    end

    class RubyAMIStreamProxy
      attr_accessor :stream

      def initialize(ami)
        @stream = ami
      end

      def method_missing(method, *args, &block)
        stream.__send__(method, *args, &block)
      end

    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
punchblock-2.7.5 lib/punchblock/connection/asterisk.rb
punchblock-2.7.3 lib/punchblock/connection/asterisk.rb
punchblock-2.7.2 lib/punchblock/connection/asterisk.rb
punchblock-2.7.1 lib/punchblock/connection/asterisk.rb
punchblock-2.7.0 lib/punchblock/connection/asterisk.rb
punchblock-2.6.0 lib/punchblock/connection/asterisk.rb
punchblock-2.5.3 lib/punchblock/connection/asterisk.rb
punchblock-2.5.2 lib/punchblock/connection/asterisk.rb
punchblock-2.5.1 lib/punchblock/connection/asterisk.rb
punchblock-2.5.0 lib/punchblock/connection/asterisk.rb