Class: Punchblock::Connection::XMPP

Inherits:
Object
  • Object
show all
Includes:
Blather::DSL
Defined in:
lib/punchblock/connection/xmpp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ XMPP

Initialize the required connection attributes

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :username (String)

    client JID

  • :password (String)

    XMPP password

  • :rayo_domain (String)

    the domain on which Rayo is running

  • :wire_logger (Logger)

    to which all XMPP transactions will be logged

  • :auto_reconnect (Boolean, Optional)

    whether or not to auto reconnect

  • :write_timeout (Numeric, Optional)

    for which to wait on a command response

  • :ping_period (Numeric, nil, Optional)

    interval in seconds on which to ping the server. Nil or false to disable

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/punchblock/connection/xmpp.rb', line 26

def initialize(options = {})
  raise ArgumentError unless (@username = options[:username]) && options[:password]

  setup *[:username, :password, :host, :port, :certs].map { |key| options.delete key }

  @rayo_domain = options[:rayo_domain] || Blather::JID.new(@username).domain

  @callmap = {} # This hash maps call IDs to their XMPP domain.

  @auto_reconnect = !!options[:auto_reconnect]
  @reconnect_attempts = 0

  @ping_period = options.has_key?(:ping_period) ? options[:ping_period] : 60

  @event_handler = lambda { |event| raise 'No event handler set' }

  Blather.logger = options.delete(:wire_logger) if options.has_key?(:wire_logger)
end

Instance Attribute Details

#event_handlerObject

Returns the value of attribute event_handler.



12
13
14
# File 'lib/punchblock/connection/xmpp.rb', line 12

def event_handler
  @event_handler
end

Instance Method Details

#connectObject



77
78
79
80
81
82
83
# File 'lib/punchblock/connection/xmpp.rb', line 77

def connect
  begin
    EM.run { client.run }
  rescue Blather::SASLError, Blather::StreamError => e
    raise ProtocolError.new(e.class.to_s, e.message)
  end
end

#connected?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/punchblock/connection/xmpp.rb', line 90

def connected?
  client.connected?
end

#prep_command_for_execution(command, options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/punchblock/connection/xmpp.rb', line 57

def prep_command_for_execution(command, options = {})
  call_id, component_id = options.values_at :call_id, :component_id
  command.connection = self
  command.call_id = call_id
  jid = command.is_a?(Command::Dial) ? @rayo_domain : "#{call_id}@#{@callmap[call_id]}"
  jid << "/#{component_id}" if component_id
  create_iq(jid).tap do |iq|
    @logger.debug "Sending IQ ID #{iq.id} #{command.inspect} to #{jid}" if @logger
    iq << command
  end
end

#runObject

Fire up the connection



72
73
74
75
# File 'lib/punchblock/connection/xmpp.rb', line 72

def run
  register_handlers
  connect
end

#stopObject



85
86
87
88
# File 'lib/punchblock/connection/xmpp.rb', line 85

def stop
  @reconnect_attempts = nil
  client.close
end

#write(command, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/punchblock/connection/xmpp.rb', line 45

def write(command, options = {})
  iq = prep_command_for_execution command, options
  client.write_with_handler iq do |response|
    if response.result?
      handle_iq_result response, command
    elsif response.error?
      handle_error response, command
    end
  end
  command.request!
end