Class: Adhearsion::Rayo::Connection::XMPP

Inherits:
GenericConnection show all
Includes:
Blather::DSL
Defined in:
lib/adhearsion/rayo/connection/xmpp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ XMPP

Initialize the required connection attributes

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)

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

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

  @joined_mixers = []

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

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

  register_handlers

  super()
end

Instance Attribute Details

#event_handlerObject

Returns the value of attribute event_handler


16
17
18
# File 'lib/adhearsion/rayo/connection/xmpp.rb', line 16

def event_handler
  @event_handler
end

#root_domainObject

Returns the value of attribute root_domain


16
17
18
# File 'lib/adhearsion/rayo/connection/xmpp.rb', line 16

def root_domain
  @root_domain
end

Instance Method Details

#connectObject


88
89
90
91
92
93
94
# File 'lib/adhearsion/rayo/connection/xmpp.rb', line 88

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


100
101
102
# File 'lib/adhearsion/rayo/connection/xmpp.rb', line 100

def connected?
  client.connected?
end

#new_call_uriObject


114
115
116
# File 'lib/adhearsion/rayo/connection/xmpp.rb', line 114

def new_call_uri
  "xmpp:#{Adhearsion.new_uuid}@#{root_domain}"
end

#not_ready!Object


109
110
111
112
# File 'lib/adhearsion/rayo/connection/xmpp.rb', line 109

def not_ready!
  send_presence :dnd
  super
end

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


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

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.domain            ||= options[:domain]
  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), command.request_id).tap do |iq|
    command.to_rayo(iq)
  end
end

#ready!Object


104
105
106
107
# File 'lib/adhearsion/rayo/connection/xmpp.rb', line 104

def ready!
  send_presence :chat
  super
end

#runObject

Fire up the connection


84
85
86
# File 'lib/adhearsion/rayo/connection/xmpp.rb', line 84

def run
  connect
end

#send_message(call_id, domain, body, options = {}) ⇒ Object


74
75
76
77
78
79
# File 'lib/adhearsion/rayo/connection/xmpp.rb', line 74

def send_message(call_id, domain, body, options = {})
  jid = Blather::JID.new(call_id, domain || root_domain).to_s
  message = Blather::Stanza::Message.new(jid, body, :normal)
  message.subject = options[:subject]
  client.write message
end

#stopObject


96
97
98
# File 'lib/adhearsion/rayo/connection/xmpp.rb', line 96

def stop
  client.close if client.connected?
end

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


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

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