Class Skates::XmppConnection
In: lib/skates/xmpp_connection.rb
Parent: EventMachine::Connection

This class is in charge of handling the network connection to the XMPP server.

Methods

Attributes

host  [RW] 
jid  [RW] 
port  [RW] 

Public Class methods

Connects the XmppConnection to the right host with the right port. It passes itself (as handler) and the configuration This can very well be overwritten by subclasses.

[Source]

    # File lib/skates/xmpp_connection.rb, line 54
54:     def self.connect(params, handler)
55:       if params["host"] =~ /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/ 
56:         params["port"] = params["port"] ? params["port"].to_i : 5222 
57:         _connect(params, handler)
58:       else
59:         resolve(params["host"]) do |host_info|
60:           if host_info
61:             begin
62:               _connect(params.merge(host_info), handler)
63:               true # connected! Yay!
64:             rescue NotConnected
65:               # It will try the next pair of ip/port
66:               false
67:             end
68:           else
69:             Skates.logger.error {
70:               "Sorry, we couldn't resolve #{srv_for_host(params["host"])} to any host that accept XMPP connections. Please provide a params[\"host\"]."
71:             }
72:             EM.stop_event_loop
73:           end
74:         end
75:       end
76:     end

Maximum Stanza size. Default is 65535

[Source]

    # File lib/skates/xmpp_connection.rb, line 40
40:     def self.max_stanza_size
41:       @@max_stanza_size
42:     end

Setter for Maximum Stanza size.

[Source]

    # File lib/skates/xmpp_connection.rb, line 46
46:     def self.max_stanza_size=(_size)
47:       @@max_stanza_size = _size
48:     end

Instantiate the Handler (called internally by EventMachine)

[Source]

     # File lib/skates/xmpp_connection.rb, line 105
105:     def initialize(params = {})
106:       @connected = false
107:       @jid       = params["jid"]
108:       @password  = params["password"]
109:       @host      = params["host"]
110:       @port      = params["port"]
111:       @handler   = params["handler"]
112:       @buffer    = "" 
113:     end

This will the host asynscrhonously and calls the block for each IP:Port pair. if the block returns true, no other record will be tried. If it returns false, the block will be called with the next pair.

[Source]

    # File lib/skates/xmpp_connection.rb, line 34
34:     def self.resolve(host, &block)
35:       block.call(false)
36:     end

Public Instance methods

Called when the connection is completed.

[Source]

    # File lib/skates/xmpp_connection.rb, line 80
80:     def connection_completed
81:       @connected = true
82:       Skates.logger.debug {
83:         "CONNECTED"
84:       } # Very low level Logging
85:     end

Attaches a new parser since the network connection has been established.

[Source]

     # File lib/skates/xmpp_connection.rb, line 117
117:     def post_init
118:       @parser = XmppParser.new(method(:receive_stanza))
119:     end

Called when a full stanza has been received and returns it to the central router to be sent to the corresponding controller.

[Source]

     # File lib/skates/xmpp_connection.rb, line 123
123:     def receive_stanza(stanza)
124:       Skates.logger.debug {
125:         "PARSED : #{stanza.to_xml}"
126:       }
127:       # If not handled by subclass (for authentication)
128:       case stanza.name
129:       when "stream:error"
130:         if !stanza.children.empty? and stanza.children.first.name == "xml-not-well-formed"
131:           Skates.logger.error {
132:             "DISCONNECTED DUE TO MALFORMED STANZA"
133:           }
134:           raise XmlNotWellFormed
135:         end
136:         # In any case, we need to close the connection.
137:         close_connection
138:       else
139:         begin
140:           @handler.on_stanza(stanza) if @handler and @handler.respond_to?("on_stanza")
141:         rescue
142:           Skates.logger.error {
143:             "on_stanza failed : #{$!}\n#{$!.backtrace.join("\n")}"
144:           }
145:         end
146:       end 
147:     end

Sends the Nokogiri::XML data (after converting to string) on the stream. Eventually it displays this data for debugging purposes.

[Source]

     # File lib/skates/xmpp_connection.rb, line 151
151:     def send_xml(xml)
152:       if xml.is_a? Nokogiri::XML::NodeSet
153:         xml.each do |element|
154:           send_chunk(element.to_s)
155:         end
156:       else
157:         send_chunk(xml.to_s)
158:       end
159:     end

Called when the connection is terminated and stops the event loop

[Source]

     # File lib/skates/xmpp_connection.rb, line 89
 89:     def unbind()
 90:       @connected = false
 91:       Skates.logger.debug {
 92:         "DISCONNECTED"
 93:       } # Very low level Logging
 94:       begin
 95:         @handler.on_disconnected() if @handler and @handler.respond_to?("on_disconnected")
 96:       rescue
 97:         Skates.logger.error {
 98:           "on_disconnected failed : #{$!}\n#{$!.backtrace.join("\n")}"
 99:         }
100:       end
101:     end

[Validate]