Class: Punchblock::Connection::XMPP

Inherits:
GenericConnection 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

  • :write_timeout (Numeric, Optional)

    for which to wait on a command response

  • :connection_timeout (Numeric, Optional)

    for which to wait on a connection being established

  • :ping_period (Numeric, nil, Optional)

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

Raises:

  • (ArgumentError)


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

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

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

  @root_domain    = Blather::JID.new(options[:root_domain] || options[:rayo_domain] || @username).domain
  @calls_domain   = options[:calls_domain]  || "calls.#{@root_domain}"
  @mixers_domain  = options[:mixers_domain] || "mixers.#{@root_domain}"

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

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

  Blather.logger = pb_logger
  Blather.default_log_level = :trace if Blather.respond_to? :default_log_level

  register_handlers

  super()
end

Instance Attribute Details

#calls_domainObject

Returns the value of attribute calls_domain.



14
15
16
# File 'lib/punchblock/connection/xmpp.rb', line 14

def calls_domain
  @calls_domain
end

#event_handlerObject

Returns the value of attribute event_handler.



14
15
16
# File 'lib/punchblock/connection/xmpp.rb', line 14

def event_handler
  @event_handler
end

#mixers_domainObject

Returns the value of attribute mixers_domain.



14
15
16
# File 'lib/punchblock/connection/xmpp.rb', line 14

def mixers_domain
  @mixers_domain
end

#root_domainObject

Returns the value of attribute root_domain.



14
15
16
# File 'lib/punchblock/connection/xmpp.rb', line 14

def root_domain
  @root_domain
end

Instance Method Details

#connectObject



81
82
83
84
85
86
87
# File 'lib/punchblock/connection/xmpp.rb', line 81

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

#connected?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/punchblock/connection/xmpp.rb', line 93

def connected?
  client.connected?
end

#not_ready!Object



102
103
104
105
# File 'lib/punchblock/connection/xmpp.rb', line 102

def not_ready!
  send_presence :dnd
  super
end

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



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/punchblock/connection/xmpp.rb', line 61

def prep_command_for_execution(command, options = {})
  command.connection    = self
  command.target_call_id    ||= options[:call_id]
  command.target_mixer_name ||= options[:mixer_name]
  command.component_id      ||= options[:component_id]
  if command.is_a?(Command::Join) && command.mixer_name
    @joined_mixers << command.mixer_name
  end
  create_iq(jid_for_command(command)).tap do |iq|
    iq << command
  end
end

#ready!Object



97
98
99
100
# File 'lib/punchblock/connection/xmpp.rb', line 97

def ready!
  send_presence :chat
  super
end

#runObject

Fire up the connection



77
78
79
# File 'lib/punchblock/connection/xmpp.rb', line 77

def run
  connect
end

#stopObject



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

def stop
  client.close if client.connected?
end

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



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/punchblock/connection/xmpp.rb', line 49

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