Class: RGossip::Nodes

Inherits:
Array
  • Object
show all
Defined in:
lib/rgossip/nodes.rb

Instance Method Summary collapse

Constructor Details

#initialize(ary = []) ⇒ Nodes

Returns a new instance of Nodes.



3
4
5
6
# File 'lib/rgossip/nodes.rb', line 3

def initialize(ary = [])
  super(ary)
  @mutex = Mutex.new
end

Instance Method Details

#choice_without(node) ⇒ Object



12
13
14
15
# File 'lib/rgossip/nodes.rb', line 12

def choice_without(node)
  node_list = self.select {|i| i.address != node.address }
  node_list.empty? ? nil : node_list[rand(node_list.size)]
end

#serialize_to_chunksObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rgossip/nodes.rb', line 17

def serialize_to_chunks
  chunks = []
  nodes = []
  datasum = ''

  self.sort_by { rand }.each do |node|
    packed = node.to_a.to_msgpack

    if (datasum + packed).length <= RGossip.bufsiz
      nodes << node
      datasum << packed
    else
      chunks << nodes.map {|i| i.to_a }.to_msgpack
      nodes.clear
      datasum.replace('')
    end
  end

  unless nodes.empty?
    chunks << nodes.map {|i| i.to_a }.to_msgpack
  end

  return chunks
end

#synchronizeObject



8
9
10
# File 'lib/rgossip/nodes.rb', line 8

def synchronize
  @mutex.synchronize { yield }
end