Class: Jacha::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/jacha/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jid, password, pool = nil) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/jacha/connection.rb', line 8

def initialize(jid, password, pool=nil)
  @password = password
  @jabber = Jabber::Client.new "#{jid}/#{Time.now.to_f}"
  @pool = pool
  @jabber.on_exception do |ex, stream, place|
    unless broken?
      broken!
      logger.warn "#{Time.now}: broken XmppConnection: #{self}: #{ex} at #{place}"
      destroy
      @pool.respawn if @pool
    end
  end
end

Instance Attribute Details

#jabberObject (readonly)

Returns the value of attribute jabber.



6
7
8
# File 'lib/jacha/connection.rb', line 6

def jabber
  @jabber
end

#poolObject (readonly)

Returns the value of attribute pool.



6
7
8
# File 'lib/jacha/connection.rb', line 6

def pool
  @pool
end

Instance Method Details

#broken!Object



82
83
84
# File 'lib/jacha/connection.rb', line 82

def broken!
  @broken = true
end

#broken?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/jacha/connection.rb', line 86

def broken?
  @broken
end

#connect!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jacha/connection.rb', line 22

def connect!
  jabber_connect!
  @pinger = Thread.new do
    while true
      if connected?
        sleep 180
        online!
      else
        jabber_connect!
      end
    end
  end
  online!
end

#connected?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/jacha/connection.rb', line 43

def connected?
  @jabber.is_connected?
end

#destroyObject



76
77
78
79
80
# File 'lib/jacha/connection.rb', line 76

def destroy
  broken!
  @pinger.kill if @pinger
  @jabber.close if @jabber
end

#jabber_connect!Object



94
95
96
97
# File 'lib/jacha/connection.rb', line 94

def jabber_connect!
  @jabber.connect
  @jabber.auth @password
end

#loggerObject



90
91
92
# File 'lib/jacha/connection.rb', line 90

def logger
  @pool && @pool.logger || (@logger ||= Logger.new(STDOUT))
end

#online!(&block) ⇒ Object



37
38
39
40
41
# File 'lib/jacha/connection.rb', line 37

def online!(&block)
  packet = Jabber::Presence.new
  packet.from = @jabber.jid
  @jabber.send packet, &block
end

#online?(jid, timeout = 1.5) ⇒ Boolean

Returns:

  • (Boolean)


47
48
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
# File 'lib/jacha/connection.rb', line 47

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