Class: Babylon::XmppConnection
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- Babylon::XmppConnection
- Defined in:
- lib/babylon/xmpp_connection.rb
Overview
This class is in charge of handling the network connection to the XMPP server.
Direct Known Subclasses
Constant Summary collapse
- @@max_stanza_size =
65535
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#jid ⇒ Object
Returns the value of attribute jid.
-
#port ⇒ Object
Returns the value of attribute port.
Class Method Summary collapse
-
.connect(params, handler) ⇒ Object
Connects the XmppConnection to the right host with the right port.
-
.max_stanza_size ⇒ Object
Maximum Stanza size.
-
.max_stanza_size=(_size) ⇒ Object
Setter for Maximum Stanza size.
Instance Method Summary collapse
-
#connection_completed ⇒ Object
Called when the connection is completed.
-
#initialize(params = {}) ⇒ XmppConnection
constructor
Instantiate the Handler (called internally by EventMachine).
-
#post_init ⇒ Object
Attaches a new parser since the network connection has been established.
-
#receive_stanza(stanza) ⇒ Object
Called when a full stanza has been received and returns it to the central router to be sent to the corresponding controller.
-
#send_xml(xml) ⇒ Object
Sends the Nokogiri::XML data (after converting to string) on the stream.
-
#unbind ⇒ Object
Called when the connection is terminated and stops the event loop.
Constructor Details
#initialize(params = {}) ⇒ XmppConnection
Instantiate the Handler (called internally by EventMachine)
88 89 90 91 92 93 94 95 96 |
# File 'lib/babylon/xmpp_connection.rb', line 88 def initialize(params = {}) @connected = false @jid = params["jid"] @password = params["password"] @host = params["host"] @port = params["port"] @handler = params["handler"] @buffer = "" end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
27 28 29 |
# File 'lib/babylon/xmpp_connection.rb', line 27 def host @host end |
#jid ⇒ Object
Returns the value of attribute jid.
27 28 29 |
# File 'lib/babylon/xmpp_connection.rb', line 27 def jid @jid end |
#port ⇒ Object
Returns the value of attribute port.
27 28 29 |
# File 'lib/babylon/xmpp_connection.rb', line 27 def port @port end |
Class Method Details
.connect(params, handler) ⇒ Object
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.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/babylon/xmpp_connection.rb', line 47 def self.connect(params, handler) Babylon.logger.debug { "CONNECTING TO #{params["host"]}:#{params["port"]} with #{handler.inspect} as connection handler" # Very low level Logging } begin EventMachine.connect(params["host"], params["port"], self, params.merge({"handler" => handler})) rescue RuntimeError Babylon.logger.error { "CONNECTION ERROR : #{$!.class} => #{$!}" # Very low level Logging } raise NotConnected end end |
.max_stanza_size ⇒ Object
Maximum Stanza size. Default is 65535
33 34 35 |
# File 'lib/babylon/xmpp_connection.rb', line 33 def self.max_stanza_size @@max_stanza_size end |
.max_stanza_size=(_size) ⇒ Object
Setter for Maximum Stanza size.
39 40 41 |
# File 'lib/babylon/xmpp_connection.rb', line 39 def self.max_stanza_size=(_size) @@max_stanza_size = _size end |
Instance Method Details
#connection_completed ⇒ Object
Called when the connection is completed.
63 64 65 66 67 68 |
# File 'lib/babylon/xmpp_connection.rb', line 63 def connection_completed @connected = true Babylon.logger.debug { "CONNECTED" } # Very low level Logging end |
#post_init ⇒ Object
Attaches a new parser since the network connection has been established.
100 101 102 |
# File 'lib/babylon/xmpp_connection.rb', line 100 def post_init @parser = XmppParser.new(method(:receive_stanza)) end |
#receive_stanza(stanza) ⇒ Object
Called when a full stanza has been received and returns it to the central router to be sent to the corresponding controller.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/babylon/xmpp_connection.rb', line 106 def receive_stanza(stanza) Babylon.logger.debug { "PARSED : #{stanza.to_xml}" } # If not handled by subclass (for authentication) case stanza.name when "stream:error" if !stanza.children.empty? and stanza.children.first.name == "xml-not-well-formed" Babylon.logger.error { "DISCONNECTED DUE TO MALFORMED STANZA" } raise XmlNotWellFormed end # In any case, we need to close the connection. close_connection else begin @handler.on_stanza(stanza) if @handler and @handler.respond_to?("on_stanza") rescue Babylon.logger.error { "on_stanza failed : #{$!}\n#{$!.backtrace.join("\n")}" } end end end |
#send_xml(xml) ⇒ Object
Sends the Nokogiri::XML data (after converting to string) on the stream. Eventually it displays this data for debugging purposes.
134 135 136 137 138 139 140 141 142 |
# File 'lib/babylon/xmpp_connection.rb', line 134 def send_xml(xml) if xml.is_a? Nokogiri::XML::NodeSet xml.each do |element| send_chunk(element.to_s) end else send_chunk(xml.to_s) end end |
#unbind ⇒ Object
Called when the connection is terminated and stops the event loop
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/babylon/xmpp_connection.rb', line 72 def unbind() @connected = false Babylon.logger.debug { "DISCONNECTED" } # Very low level Logging begin @handler.on_disconnected() if @handler and @handler.respond_to?("on_disconnected") rescue Babylon.logger.error { "on_disconnected failed : #{$!}\n#{$!.backtrace.join("\n")}" } end end |