Class Babylon::XmppConnection
In: lib/babylon/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/babylon/xmpp_connection.rb, line 47
47:     def self.connect(params, handler)
48:       Babylon.logger.debug {
49:         "CONNECTING TO #{params["host"]}:#{params["port"]} with #{handler.inspect} as connection handler" # Very low level Logging
50:       }
51:       begin
52:         EventMachine.connect(params["host"], params["port"], self, params.merge({"handler" => handler}))
53:       rescue RuntimeError
54:         Babylon.logger.error {
55:           "CONNECTION ERROR : #{$!.class} => #{$!}" # Very low level Logging
56:         }
57:         raise NotConnected
58:       end
59:     end

Maximum Stanza size. Default is 65535

[Source]

    # File lib/babylon/xmpp_connection.rb, line 33
33:     def self.max_stanza_size
34:       @@max_stanza_size
35:     end

Setter for Maximum Stanza size.

[Source]

    # File lib/babylon/xmpp_connection.rb, line 39
39:     def self.max_stanza_size=(_size)
40:       @@max_stanza_size = _size
41:     end

Instantiate the Handler (called internally by EventMachine)

[Source]

    # File lib/babylon/xmpp_connection.rb, line 88
88:     def initialize(params = {})
89:       @connected = false
90:       @jid       = params["jid"]
91:       @password  = params["password"]
92:       @host      = params["host"]
93:       @port      = params["port"]
94:       @handler   = params["handler"]
95:       @buffer    = "" 
96:     end

Public Instance methods

Called when the connection is completed.

[Source]

    # File lib/babylon/xmpp_connection.rb, line 63
63:     def connection_completed
64:       @connected = true
65:       Babylon.logger.debug {
66:         "CONNECTED"
67:       } # Very low level Logging
68:     end

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

[Source]

     # File lib/babylon/xmpp_connection.rb, line 100
100:     def post_init
101:       @parser = XmppParser.new(method(:receive_stanza))
102:     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/babylon/xmpp_connection.rb, line 106
106:     def receive_stanza(stanza)
107:       Babylon.logger.debug {
108:         "PARSED : #{stanza.to_xml}"
109:       }
110:       # If not handled by subclass (for authentication)
111:       case stanza.name
112:       when "stream:error"
113:         if !stanza.children.empty? and stanza.children.first.name == "xml-not-well-formed"
114:           Babylon.logger.error {
115:             "DISCONNECTED DUE TO MALFORMED STANZA"
116:           }
117:           raise XmlNotWellFormed
118:         end
119:         # In any case, we need to close the connection.
120:         close_connection
121:       else
122:         begin
123:           @handler.on_stanza(stanza) if @handler and @handler.respond_to?("on_stanza")
124:         rescue
125:           Babylon.logger.error {
126:             "on_stanza failed : #{$!}\n#{$!.backtrace.join("\n")}"
127:           }
128:         end
129:       end 
130:     end

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

[Source]

     # File lib/babylon/xmpp_connection.rb, line 134
134:     def send_xml(xml)
135:       if xml.is_a? Nokogiri::XML::NodeSet
136:         xml.each do |element|
137:           send_chunk(element.to_s)
138:         end
139:       else
140:         send_chunk(xml.to_s)
141:       end
142:     end

Called when the connection is terminated and stops the event loop

[Source]

    # File lib/babylon/xmpp_connection.rb, line 72
72:     def unbind()
73:       @connected = false
74:       Babylon.logger.debug {
75:         "DISCONNECTED"
76:       } # Very low level Logging
77:       begin
78:         @handler.on_disconnected() if @handler and @handler.respond_to?("on_disconnected")
79:       rescue
80:         Babylon.logger.error {
81:           "on_disconnected failed : #{$!}\n#{$!.backtrace.join("\n")}"
82:         }
83:       end
84:     end

[Validate]