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
# 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

  @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

#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

#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



86
87
88
89
90
91
92
# File 'lib/punchblock/connection/xmpp.rb', line 86

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)


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

def connected?
  client.connected?
end

#new_call_uriObject



112
113
114
# File 'lib/punchblock/connection/xmpp.rb', line 112

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

#not_ready!Object



107
108
109
110
# File 'lib/punchblock/connection/xmpp.rb', line 107

def not_ready!
  send_presence :dnd
  super
end

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



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

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



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

def ready!
  send_presence :chat
  super
end

#runObject

Fire up the connection



82
83
84
# File 'lib/punchblock/connection/xmpp.rb', line 82

def run
  connect
end

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



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

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



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

def stop
  client.close if client.connected?
end

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



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

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