lib/cuboid/rpc/server/agent/node.rb in cuboid-0.2.2 vs lib/cuboid/rpc/server/agent/node.rb in cuboid-0.2.3
- old
+ new
@@ -36,30 +36,28 @@
@server = server
@url = @server.url
reroute_to_file( logfile ) if logfile
- print_status 'Initializing grid node...'
+ print_status 'Initializing node...'
@dead_nodes = Set.new
- @peers = Set.new
+ @peers = Set.new
@nodes_info_cache = []
if (peer = @options.agent.peer)
# Grab the peer's peers.
- connect_to_peer( peer ).peers do |urls|
- if urls.rpc_exception?
+ connect_to_peer( peer ).peers do |peers|
+ if peers.rpc_exception?
+ print_info "Peer seems dead: #{peer}"
add_dead_peer( peer )
- print_info "Neighbour seems dead: #{peer}"
- add_dead_peer( peer )
next
end
- # Add peer and announce it to everyone.
- add_peer( peer, true )
-
- urls.each { |url| @peers << url if url != @url }
+ peers << peer
+ peers.each { |url| add_peer url }
+ announce @url
end
end
print_status 'Node ready.'
@@ -90,39 +88,26 @@
# Adds a peer to the peer list.
#
# @param [String] node_url
# URL of a peering node.
- # @param [Boolean] propagate
- # Whether or not to announce the new node to the peers.
- def add_peer( node_url, propagate = false )
- # we don't want ourselves in the Set
- return false if node_url == @url
- return false if @peers.include?( node_url )
-
+ def add_peer( node_url )
print_status "Adding peer: #{node_url}"
@peers << node_url
log_updated_peers
- announce( node_url ) if propagate
-
- connect_to_peer( node_url ).add_peer( @url, propagate ) do |res|
- next if !res.rpc_exception?
- add_dead_peer( node_url )
- print_status "Neighbour seems dead: #{node_url}"
- end
true
end
def remove_peer( url )
@peers.delete url
@dead_nodes.delete url
nil
end
# @return [Array]
- # Neighbour/node/peer URLs.
+ # Peer/node/peer URLs.
def peers
@peers.to_a
end
def peers_with_info( &block )
@@ -134,11 +119,11 @@
@peers_cmp = peers.to_s
each = proc do |peer, iter|
connect_to_peer( peer ).info do |info|
if info.rpc_exception?
- print_info "Neighbour seems dead: #{peer}"
+ print_info "Peer seems dead: #{peer}"
add_dead_peer( peer )
log_updated_peers
iter.return( nil )
else
@@ -163,12 +148,12 @@
# * `url` -- This node's URL.
# * `name` -- Nickname
# * `peers` -- Array of peers.
def info
{
- 'url' => @url,
- 'name' => @options.agent.name,
+ 'url' => @url,
+ 'name' => @options.agent.name,
'peers' => @peers.to_a,
'unreachable_peers' => @dead_nodes.to_a
}
end
@@ -207,11 +192,11 @@
@dead_nodes.dup.each do |url|
peer = connect_to_peer( url )
peer.alive? do |res|
next if res.rpc_exception?
- print_status "Agent came back to life: #{url}"
+ print_status "Peer came back to life: #{url}"
([@url] | peers).each do |node|
peer.add_peer( node ){}
end
add_peer( url )
@@ -223,15 +208,13 @@
# Announces the node to the ones in the peer list
#
# @param [String] node
# URL
def announce( node )
- print_status "Advertising: #{node}"
+ print_status "Announcing: #{node}"
peers.each do |peer|
- next if peer == node
-
- print_info '---- to: ' + peer
+ print_info "---- to: #{peer}"
connect_to_peer( peer ).add_peer( node ) do |res|
add_dead_peer( peer ) if res.rpc_exception?
end
end
end