Class: Jacha::Connection
- Inherits:
-
Object
- Object
- Jacha::Connection
- Defined in:
- lib/jacha/connection.rb
Instance Attribute Summary collapse
-
#jabber ⇒ Object
readonly
Returns the value of attribute jabber.
-
#pool ⇒ Object
Returns the value of attribute pool.
Instance Method Summary collapse
- #broken! ⇒ Object
- #broken? ⇒ Boolean
- #connect! ⇒ Object
- #connected? ⇒ Boolean
- #destroy ⇒ Object
-
#initialize(jid, password) ⇒ Connection
constructor
A new instance of Connection.
- #logger ⇒ Object
- #online!(&block) ⇒ Object
- #online?(jid, timeout = 1.5) ⇒ Boolean
Constructor Details
#initialize(jid, password) ⇒ Connection
Returns a new instance of Connection.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/jacha/connection.rb', line 9 def initialize(jid, password) @password = password @jabber = Jabber::Client.new "#{jid}/#{Time.now.to_f}" @jabber.on_exception do unless broken? broken! logger.warn "#{Time.now}: broken XmppConnection: #{self}" destroy pool.respawn end end connect! @pinger = Thread.new do while true if connected? sleep 180 online! else connect! end end end end |
Instance Attribute Details
#jabber ⇒ Object (readonly)
Returns the value of attribute jabber.
6 7 8 |
# File 'lib/jacha/connection.rb', line 6 def jabber @jabber end |
#pool ⇒ Object
Returns the value of attribute pool.
7 8 9 |
# File 'lib/jacha/connection.rb', line 7 def pool @pool end |
Instance Method Details
#broken! ⇒ Object
84 85 86 |
# File 'lib/jacha/connection.rb', line 84 def broken! @broken = true end |
#broken? ⇒ Boolean
88 89 90 |
# File 'lib/jacha/connection.rb', line 88 def broken? @broken end |
#connect! ⇒ Object
33 34 35 36 37 |
# File 'lib/jacha/connection.rb', line 33 def connect! @jabber.connect @jabber.auth @password online! end |
#connected? ⇒ Boolean
45 46 47 |
# File 'lib/jacha/connection.rb', line 45 def connected? @jabber.is_connected? end |
#destroy ⇒ Object
78 79 80 81 82 |
# File 'lib/jacha/connection.rb', line 78 def destroy broken! @pinger.kill @jabber.close end |
#logger ⇒ Object
92 93 94 |
# File 'lib/jacha/connection.rb', line 92 def logger pool.logger end |
#online!(&block) ⇒ Object
39 40 41 42 43 |
# File 'lib/jacha/connection.rb', line 39 def online!(&block) packet = Jabber::Presence.new packet.from = @jabber.jid @jabber.send packet, &block end |
#online?(jid, timeout = 1.5) ⇒ Boolean
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/jacha/connection.rb', line 49 def online?(jid, timeout=1.5) # Only works if our bot is authorized by the given JID # see Presence with type :subscribe for more details # Also, bot should be online by itself jid = Jabber::JID.new(jid) pinger = Thread.new do pinger[:online] = nil packet = Jabber::Presence.new packet.to = jid packet.from = @jabber.jid packet.type = :probe @jabber.send(packet) do |presence| from = Jabber::JID.new presence.from if from.node == jid.node && from.domain == jid.domain if presence.type.nil? pinger[:online] = true pinger.stop elsif presence.type == :error pinger.stop end end end end pinger.join timeout result = pinger[:online] pinger.kill result end |