class Bitcourier::Peer

Attributes

ip[RW]
last_seen_at[RW]
next_connection_at[RW]
port[RW]

Public Class Methods

from_a(a) click to toggle source
# File lib/bitcourier/peer.rb, line 42
def self.from_a a
  new(a[0], a[1].to_i, a[2] && Time.at(a[2].to_i).utc, a[3] && Time.at(a[3].to_i).utc)
end
new(ip, port, last_seen_at = nil, next_connection_at = Time.now.utc) click to toggle source
# File lib/bitcourier/peer.rb, line 5
def initialize ip, port, last_seen_at = nil, next_connection_at = Time.now.utc
  self.ip                 = ip
  self.port               = port
  self.last_seen_at       = last_seen_at
  self.next_connection_at = next_connection_at
end

Public Instance Methods

can_connect?(time = Time.now.utc) click to toggle source
# File lib/bitcourier/peer.rb, line 21
def can_connect?(time = Time.now.utc)
  next_connection_at < time
end
retry_in(delay) click to toggle source
# File lib/bitcourier/peer.rb, line 25
def retry_in delay
  self.next_connection_at = Time.now + delay
end
same?(other) click to toggle source
# File lib/bitcourier/peer.rb, line 38
def same?(other)
  other && (ip == other.ip) && (port == other.port)
end
to_a() click to toggle source
# File lib/bitcourier/peer.rb, line 29
def to_a
  [
      ip,
      port,
      last_seen_at && last_seen_at.to_i,
      next_connection_at && next_connection_at.to_i
  ]
end
touch(timestamp = Time.now.utc) click to toggle source
# File lib/bitcourier/peer.rb, line 12
def touch(timestamp = Time.now.utc)
  self.last_seen_at = [last_seen_at || Time.at(0).utc, timestamp || Time.now.utc].max
end
update(peer) click to toggle source
# File lib/bitcourier/peer.rb, line 16
def update(peer)
  touch(peer.last_seen_at)
  self.next_connection_at = peer.next_connection_at
end