Class: Pokan::Peer

Inherits:
Entity show all
Defined in:
lib/pokan/peer.rb

Overview

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

Direct Known Subclasses

Server

Instance Attribute Summary

Attributes inherited from Entity

#id

Instance Method Summary collapse

Methods inherited from Entity

#destroy, #digest, #keys, #match?, #merge, #newer, #older, #reload, #save, #timestamp, #value, #values

Constructor Details

#initializePeer

Returns a new instance of Peer.



9
10
11
12
13
# File 'lib/pokan/peer.rb', line 9

def initialize
  super
  store(:role, 'peer', 0)
  store(:status, 'alive', 0)
end

Instance Method Details

#act_as_seedObject

Turns the peer into a seed.



73
74
75
# File 'lib/pokan/peer.rb', line 73

def act_as_seed
  store(:role, 'seed')
end

#addressObject



20
21
22
# File 'lib/pokan/peer.rb', line 20

def address
  @address = id.split(':').first
end

#address=(address) ⇒ Object



15
16
17
18
# 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?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/pokan/peer.rb', line 109

def alive?
  status == 'alive'
end

#dead?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/pokan/peer.rb', line 113

def dead?
  status == 'dead'
end

#killObject

Kills the peer



104
105
106
107
# File 'lib/pokan/peer.rb', line 104

def kill
  store(:status, 'dead')
  self
end

#peer?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/pokan/peer.rb', line 81

def peer?
  value(:role) == 'peer'
end

#reviveObject

Revives the peer



98
99
100
# File 'lib/pokan/peer.rb', line 98

def revive
  store(:status, 'alive')
end

#roleObject



67
68
69
# File 'lib/pokan/peer.rb', line 67

def role
  value(:role)
end

#role=(role) ⇒ Object

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.



63
64
65
# File 'lib/pokan/peer.rb', line 63

def role=(role)
  store(:role, role)
end

#seed?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/pokan/peer.rb', line 77

def seed?
  value(:role) == 'seed'
end

#statusObject



92
93
94
# File 'lib/pokan/peer.rb', line 92

def status
  value(:status)
end

#status=(status) ⇒ Object

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.



88
89
90
# File 'lib/pokan/peer.rb', line 88

def status=(status)
  store(:status, status)
end

#store(key, value, timestamp = Time.now) ⇒ Object

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.



50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object

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'


128
129
130
131
132
133
134
# 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_portObject



37
38
39
# File 'lib/pokan/peer.rb', line 37

def tcp_port
  value(:tcp_port)
end

#tcp_port=(port) ⇒ Object



33
34
35
# File 'lib/pokan/peer.rb', line 33

def tcp_port=(port)
  store(:tcp_port, port)
end

#udp_portObject



29
30
31
# File 'lib/pokan/peer.rb', line 29

def udp_port
  @udp_port = id.split(':')[1]
end

#udp_port=(port) ⇒ Object



24
25
26
27
# 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