Class: Peasant::PeerInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/peasant/peer_info.rb

Instance Method Summary collapse

Constructor Details

#initialize(peer, args = {}) ⇒ PeerInfo

Returns a new instance of PeerInfo.



3
4
5
6
7
8
9
10
11
# File 'lib/peasant/peer_info.rb', line 3

def initialize peer, args={}
  @peer = peer
  @node = nil
  @requests = 0
  @node_expiration = 0

  @max_requests = args[:max_requests] || 3
  @node_timeout = args[:node_timeout] || 15
end

Instance Method Details

#inc_requestsObject



27
28
29
# File 'lib/peasant/peer_info.rb', line 27

def inc_requests
  @requests += 1
end

#nodeObject



13
14
15
# File 'lib/peasant/peer_info.rb', line 13

def node
  node_expired? ? nil : @node
end

#node=(n) ⇒ Object



17
18
19
20
21
# File 'lib/peasant/peer_info.rb', line 17

def node= n
  @node = n
  @node_expiration = Time.now.to_f + @node_timeout
  @requests = 0
end

#node_expired?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/peasant/peer_info.rb', line 23

def node_expired?
  @node_expiration < Time.now.to_f || @requests > @max_requests
end

#reset_node_expirationObject



31
32
33
# File 'lib/peasant/peer_info.rb', line 31

def reset_node_expiration
  @node_expiration = Time.now.to_f + @node_timeout
end