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
# File 'lib/peasant/peer_info.rb', line 3

def initialize peer, args={}
  @peer = peer
  reset
  @max_requests = args[:max_requests] || 3
  @node_timeout = args[:node_timeout] || 15
end

Instance Method Details

#inc_requestsObject



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

def inc_requests
  @requests += 1
end

#nodeObject



10
11
12
# File 'lib/peasant/peer_info.rb', line 10

def node
  node_expired? ? nil : @node
end

#node=(n) ⇒ Object



14
15
16
17
18
# File 'lib/peasant/peer_info.rb', line 14

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

#node_expired?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/peasant/peer_info.rb', line 20

def node_expired?
  expired = @node_expiration < Time.now.to_f || @requests > @max_requests
  reset if expired
  expired
end

#resetObject



34
35
36
37
38
# File 'lib/peasant/peer_info.rb', line 34

def reset
  @node = nil
  @requests = 0
  @node_expiration = 0
end

#reset_node_expirationObject



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

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