Class: Punchblock::Connection::XMPP
- Inherits:
-
Object
- Object
- Punchblock::Connection::XMPP
- Includes:
- Blather::DSL
- Defined in:
- lib/punchblock/connection/xmpp.rb
Instance Attribute Summary collapse
-
#event_handler ⇒ Object
Returns the value of attribute event_handler.
Instance Method Summary collapse
- #connect ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(options = {}) ⇒ XMPP
constructor
Initialize the required connection attributes.
- #prep_command_for_execution(command, options = {}) ⇒ Object
-
#run ⇒ Object
Fire up the connection.
- #stop ⇒ Object
- #write(command, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ XMPP
Initialize the required connection attributes
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( = {}) raise ArgumentError unless (@username = [:username]) && [:password] setup *[:username, :password, :host, :port, :certs].map { |key| .delete key } @rayo_domain = [:rayo_domain] || Blather::JID.new(@username).domain @callmap = {} # This hash maps call IDs to their XMPP domain. @auto_reconnect = !![:auto_reconnect] @reconnect_attempts = 0 @ping_period = .has_key?(:ping_period) ? [:ping_period] : 60 @event_handler = lambda { |event| raise 'No event handler set' } Blather.logger = .delete(:wire_logger) if .has_key?(:wire_logger) end |
Instance Attribute Details
#event_handler ⇒ Object
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
#connect ⇒ Object
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.) end end |
#connected? ⇒ 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, = {}) call_id, component_id = .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 |
#run ⇒ Object
Fire up the connection
72 73 74 75 |
# File 'lib/punchblock/connection/xmpp.rb', line 72 def run register_handlers connect end |
#stop ⇒ Object
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, = {}) iq = prep_command_for_execution command, 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 |