Class: Punchblock::Connection::XMPP
- Inherits:
-
GenericConnection
- Object
- GenericConnection
- 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.
-
#root_domain ⇒ Object
Returns the value of attribute root_domain.
Instance Method Summary collapse
- #connect ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(options = {}) ⇒ XMPP
constructor
Initialize the required connection attributes.
- #new_call_uri ⇒ Object
- #not_ready! ⇒ Object
- #prep_command_for_execution(command, options = {}) ⇒ Object
- #ready! ⇒ Object
-
#run ⇒ Object
Fire up the connection.
- #send_message(call_id, domain, body, options = {}) ⇒ Object
- #stop ⇒ Object
- #write(command, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ XMPP
Initialize the required connection attributes
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( = {}) raise ArgumentError unless (@username = [:username]) && [:password] setup(*[:username, :password, :host, :port, :certs, :connection_timeout].map { |key| .delete key }) @root_domain = Blather::JID.new([:root_domain] || [:rayo_domain] || @username).domain @joined_mixers = [] @ping_period = .has_key?(:ping_period) ? [: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_handler ⇒ Object
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_domain ⇒ Object
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
#connect ⇒ Object
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.) end end |
#connected? ⇒ Boolean
98 99 100 |
# File 'lib/punchblock/connection/xmpp.rb', line 98 def connected? client.connected? end |
#new_call_uri ⇒ Object
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, = {}) command.connection = self command.target_call_id ||= [:call_id] command.target_mixer_name ||= [:mixer_name] command.domain ||= [:domain] command.component_id ||= [: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 |
#run ⇒ Object
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 (call_id, domain, body, = {}) jid = Blather::JID.new(call_id, domain || root_domain).to_s = Blather::Stanza::Message.new(jid, body, :normal) .subject = [:subject] client.write end |
#stop ⇒ Object
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, = {}) iq = prep_command_for_execution command, 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 |