class Pokan::Peer

Peer is a Entity that takes care of the peer semantics. It controls the status, id and role attributes.

Public Class Methods

new() click to toggle source
Calls superclass method Pokan::Entity::new
# File lib/pokan/peer.rb, line 9
def initialize
  super
  store(:role, 'peer', 0)
  store(:status, 'alive', 0)
end

Public Instance Methods

act_as_seed() click to toggle source

Turns the peer into a seed.

# File lib/pokan/peer.rb, line 73
def act_as_seed
  store(:role, 'seed')
end
address() click to toggle source
# File lib/pokan/peer.rb, line 20
def address
  @address = id.split(':').first
end
address=(address) click to toggle source
# File lib/pokan/peer.rb, line 15
def address=(address)
  @address = address
  self.id = "#{@address}:#{@udp_port}"  if defined?(@address) && defined?(@udp_port)
end
alive?() click to toggle source
# File lib/pokan/peer.rb, line 109
def alive?
  status == 'alive'
end
dead?() click to toggle source
# File lib/pokan/peer.rb, line 113
def dead?
  status == 'dead'
end
kill() click to toggle source

Kills the peer

# File lib/pokan/peer.rb, line 104
def kill
  store(:status, 'dead')
  self
end
peer?() click to toggle source
# File lib/pokan/peer.rb, line 81
def peer?
  value(:role) == 'peer'
end
revive() click to toggle source

Revives the peer

# File lib/pokan/peer.rb, line 98
def revive
  store(:status, 'alive')
end
role() click to toggle source
# File lib/pokan/peer.rb, line 67
def role
  value(:role)
end
role=(role) click to toggle source

Changes the role of the peer. The role must be peer or seed. Otherwise, the role will stay as it was and no exception will be thrown.

# File lib/pokan/peer.rb, line 63
def role=(role)
  store(:role, role)
end
seed?() click to toggle source
# File lib/pokan/peer.rb, line 77
def seed?
  value(:role) == 'seed'
end
status() click to toggle source
# File lib/pokan/peer.rb, line 92
def status
  value(:status)
end
status=(status) click to toggle source

Changes the status of the peer. The status must be alive or dead. Otherwise, the role will stay as it was and no exception will be thrown.

# File lib/pokan/peer.rb, line 88
def status=(status)
  store(:status, status)
end
store(key, value, timestamp = Time.now) click to toggle source

Stores the value and the timestamp for a given key. If the key, which must be a symbol, already exists, it will be updated if the timestamp is greater. The timestamp defaults to Time.now.

If the key is ‘:role’, the values have to be ‘peer’ or ‘seed’. In the same way, if the key is ‘:status’, the value have to be ‘alive’ or ‘dead’. Otherwise, the value will not be set.

Calls superclass method Pokan::Entity#store
# File lib/pokan/peer.rb, line 50
def store(key, value, timestamp = Time.now)
  case key
  when :role
    super(:role, value, timestamp)  if ['peer', 'seed'].include?(value)
  when :status
    super(:status, value, timestamp)  if ['alive', 'dead'].include?(value)
  else
    super(key, value, timestamp)
  end
end
sync_with(host, port) click to toggle source

Gossips with the given seed and replicates its local storage.

If the host or port is invalid, the local storage will continue empty.

Usage

peer = Pokan::Peer.new
peer.address = '127.0.0.2'
peer.port = '1234'
peer.value(:key_not_present) # => nil
peer.gossip_with('127.0.0.2', '8888')
peer.value(:key_not_present) # => 'the value of the key'
# File lib/pokan/peer.rb, line 128
def sync_with(host, port)
  db = Connection.redis
  db.slaveof(host, port)
  sleep(3) #while db.info['master_sync_in_progress'].to_i == 0
  @seed = host
  db.slaveof('no', 'one')
end
tcp_port() click to toggle source
# File lib/pokan/peer.rb, line 37
def tcp_port
  value(:tcp_port)
end
tcp_port=(port) click to toggle source
# File lib/pokan/peer.rb, line 33
def tcp_port=(port)
  store(:tcp_port, port)
end
udp_port() click to toggle source
# File lib/pokan/peer.rb, line 29
def udp_port
  @udp_port = id.split(':')[1]
end
udp_port=(port) click to toggle source
# File lib/pokan/peer.rb, line 24
def udp_port=(port)
  @udp_port = port
  self.id = "#{@address}:#{@udp_port}"  if defined?(@address) && defined?(@udp_port)
end