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
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
Turns the peer into a seed.
# File lib/pokan/peer.rb, line 73 def act_as_seed store(:role, 'seed') end
# File lib/pokan/peer.rb, line 20 def address @address = id.split(':').first end
# File lib/pokan/peer.rb, line 15 def address=(address) @address = address self.id = "#{@address}:#{@udp_port}" if defined?(@address) && defined?(@udp_port) end
# File lib/pokan/peer.rb, line 109 def alive? status == 'alive' end
# File lib/pokan/peer.rb, line 113 def dead? status == 'dead' end
Kills the peer
# File lib/pokan/peer.rb, line 104 def kill store(:status, 'dead') self end
# File lib/pokan/peer.rb, line 81 def peer? value(:role) == 'peer' end
Revives the peer
# File lib/pokan/peer.rb, line 98 def revive store(:status, 'alive') end
# File lib/pokan/peer.rb, line 67 def role value(:role) end
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
# File lib/pokan/peer.rb, line 77 def seed? value(:role) == 'seed' end
# File lib/pokan/peer.rb, line 92 def status value(:status) end
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
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.
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
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
# File lib/pokan/peer.rb, line 37 def tcp_port value(:tcp_port) end
# File lib/pokan/peer.rb, line 33 def tcp_port=(port) store(:tcp_port, port) end
# File lib/pokan/peer.rb, line 29 def udp_port @udp_port = id.split(':')[1] end
# 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