Class: C3s::Component
- Inherits:
-
Jabber::Component
- Object
- Jabber::Component
- C3s::Component
- Defined in:
- lib/component.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#config ⇒ Object
Component configuration hash.
-
#model ⇒ Object
Active record main class associated with component.
Instance Method Summary collapse
-
#add_callbacks ⇒ Object
Adds callbacks to listen for provides a callback named handle_iq for especific components to use.
-
#connect ⇒ Object
Connects component to jabber server.
-
#handle_disco_info(iq) ⇒ Object
- Reply to discovery info IQ packet iq
- Jabber::IQ
-
the received IQ packet.
-
#handle_iq(iq) ⇒ Object
- Handles discovery, sets and gets iq
- Jabber::IQ
-
the received IQ packet.
- #handle_msg(msg) ⇒ Object
-
#handle_set(iq) ⇒ Object
- Handles set of information iq
- Jabber::IQ
-
the received IQ packet.
-
#initialize(config, model) ⇒ Component
constructor
Initializes the jabber component with a configuration hash read with ConfigReader.
-
#publish_to_pubsub(c3snode, data, changed) ⇒ Object
- Publishes information to pubsub server c3snode
- C3s::Node
- the pubsub node data
- String
- what to publish changed
- Boolean
-
the data is different from last set operation?.
-
#send!(msg) ⇒ Object
Tries to send the message.
-
#send_error(iq, err) ⇒ Object
Sends an error response to a received IQ packet.
-
#send_ok(iq) ⇒ Object
- Sends an OK response to a given iq iq
-
the iq to respond to.
Constructor Details
#initialize(config, model) ⇒ Component
Initializes the jabber component with a configuration hash read with ConfigReader
- config
- Hash
-
configuration hash
22 23 24 25 26 27 28 |
# File 'lib/component.rb', line 22 def initialize(config, model) super(config['jid']) self.config = config self.model = model Jabber::debug = config['debug'].to_s.eql?"true" config['pubsub'] = "pubsub.#{config['url']}" end |
Instance Attribute Details
#config ⇒ Object
Component configuration hash
11 12 13 |
# File 'lib/component.rb', line 11 def config @config end |
#model ⇒ Object
Active record main class associated with component
15 16 17 |
# File 'lib/component.rb', line 15 def model @model end |
Instance Method Details
#add_callbacks ⇒ Object
Adds callbacks to listen for provides a callback named handle_iq for especific components to use.
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/component.rb', line 48 def add_callbacks add_iq_callback do |iq| t1 = Thread.new do handle_iq(iq) end end do |msg| t2 = Thread.new do handle_msg(msg) end end end |
#connect ⇒ Object
Connects component to jabber server
Throws ClientAuthenticationFailure
34 35 36 37 38 39 40 41 42 |
# File 'lib/component.rb', line 34 def connect super(config['url'], config['port']) if is_connected? auth(config['password']) else raise Exception, "Can't connect" end end |
#handle_disco_info(iq) ⇒ Object
Reply to discovery info IQ packet
- iq
- Jabber::IQ
-
the received IQ packet
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/component.rb', line 150 def handle_disco_info(iq) reply = iq.answer if iq.type != :get send_error(iq, ['bad-request', nil]) return end reply.type = :result reply.query.add(Jabber::Discovery::Identity.new(config['category'], config['description'], config['type'])) reply.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoInfo.new.namespace)) #reply.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoItems.new.namespace)) send!(reply) end |
#handle_iq(iq) ⇒ Object
Handles discovery, sets and gets
- iq
- Jabber::IQ
-
the received IQ packet
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/component.rb', line 90 def handle_iq(iq) if iq.query.kind_of?(Jabber::Discovery::IqQueryDiscoInfo) handle_disco_info(iq) elsif iq.query.first_element(config['name']) # TODO assuming that pubsubnode = xml root handle_set(iq) if iq.type == :set handle_get(iq) if iq.type == :get end rescue Exception => e $LOG.error("#{e.inspect} #{e.backtrace.join("\n")}") send_error(iq, ['bad-request', nil]) end |
#handle_msg(msg) ⇒ Object
102 103 |
# File 'lib/component.rb', line 102 def handle_msg(msg) end |
#handle_set(iq) ⇒ Object
Handles set of information
- iq
- Jabber::IQ
-
the received IQ packet
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/component.rb', line 108 def handle_set(iq) stanza = iq.query.first_element(config['name']) # TODO assuming that pubsubnode = xml root data = {} # It's admin setting the information for someone adminJID = "admin@#{config['url']}" if iq.from..to_s.eql?(adminJID) if stanza.attributes['jid'] data = {:jid => stanza.attributes['jid']} end end # Build data for activerecord object. The jid is taken from # the 'jid' attribute on the main tag under the <query> data = {:jid => iq.from.to_s} if data.empty? self.model.new.nodes.each do |node| data.merge!(node => stanza.first_element_text(node)) end changed = true object = self.model.find_by_jid(data[:jid]) || self.model.new $LOG.info "Saving #{config['name']} context for #{data[:jid]} #{data.inspect}" object.update_attributes(data) changed = object.changed? object.save! if object.errors.count == 0 $LOG.info "Publishing data to node #{config['name']}:#{data[:jid]} (data changed: #{changed})" c3snode = C3s::Node.new("#{config['name']}:#{data[:jid]}") self.publish_to_pubsub(c3snode, stanza, changed) send_ok(iq) end rescue Exception => e $LOG.error("#{e.inspect} #{e.backtrace.join("\n")}") send_error(iq, ['bad-request', object.errors..first]) end |
#publish_to_pubsub(c3snode, data, changed) ⇒ Object
Publishes information to pubsub server
- c3snode
- C3s::Node
-
the pubsub node
- data
- String
-
what to publish
- changed
- Boolean
-
the data is different from last set operation?
188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/component.rb', line 188 def publish_to_pubsub(c3snode, data, changed) if changed $LOG.info "Information for node '#{c3snode.to_s}' changed. Publishing data..." pub = Publisher.new(self, config['pubsub']) pub.create_tree(c3snode) pub.publish(c3snode, data) end # TODO - not sending any confirmations #send_ok(iq) if iq.is_a?(Jabber::Iq) rescue Exception => e $LOG.error("#{e.inspect} #{e.backtrace.join("\n")}") end |
#send!(msg) ⇒ Object
Tries to send the message. It will repeat the process if sending the stanza fails, reconnecting the client. This should be used instead of the default send method.
- msg
-
the stanza to send
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/component.rb', line 67 def send!(msg) attempts = 0 begin attempts += 1 send(msg) rescue Errno::EPIPE, IOError => e sleep 1 disconnect reconnect retry unless attempts > 3 raise e rescue Errno::ECONNRESET => e sleep (attempts^2) * 60 + 60 disconnect reconnect retry unless attempts > 3 raise e end end |
#send_error(iq, err) ⇒ Object
Sends an error response to a received IQ packet.
- iq
- Jabber::IQ
-
the received IQ packet
- error
- Array
-
an array with error type and error message. The error types should be valid.
Look at XEP-0086 for explanation: www.xmpp.org/extensions/xep-0086.html
174 175 176 177 178 179 180 181 |
# File 'lib/component.rb', line 174 def send_error(iq, err) reply = iq.answer reply.type = :error error = Jabber::ErrorResponse.new(err.first, err.last) reply.add error send!(reply) if iq.type != :error end |
#send_ok(iq) ⇒ Object
Sends an OK response to a given iq
- iq
-
the iq to respond to
205 206 207 208 209 210 211 212 |
# File 'lib/component.rb', line 205 def send_ok(iq) reply = iq.answer reply.id = iq.id reply.to = iq.from reply.type = :result send!(reply) if iq.type != :error end |