Class: RGossip::Node

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

Constant Summary collapse

@@lifetime =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_list, dead_list, address, data, timestamp, callback = nil) ⇒ Node

Returns a new instance of Node.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rgossip/node.rb', line 12

def initialize(node_list, dead_list, address, data, timestamp, callback = nil)
  @node_list = node_list
  @dead_list = dead_list
  @address = address
  @data = data
  @timestamp = timestamp || ""
  @callback = callback

  @timer = Timer.new(@@lifetime) do
    RGossip.log("Timeout Node: address=#{@address}")

    @node_list.synchronize {
      @node_list.reject! do |i|
        i.address == @address
      end
    }

    @dead_list.synchronize {
      @dead_list << self
    }

    @callback.call([:delete, address, timestamp, data]) if @callback
  end
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



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

def address
  @address
end

#callbackObject

Returns the value of attribute callback.



6
7
8
# File 'lib/rgossip/node.rb', line 6

def callback
  @callback
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/rgossip/node.rb', line 5

def data
  @data
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

Class Method Details

.lifetimeObject



9
# File 'lib/rgossip/node.rb', line 9

def self.lifetime; @@lifetime; end

.lifetime=(v) ⇒ Object



10
# File 'lib/rgossip/node.rb', line 10

def self.lifetime=(v); @@lifetime = v; end

Instance Method Details

#reset_timerObject



52
53
54
55
56
# File 'lib/rgossip/node.rb', line 52

def reset_timer
  RGossip.log("Reset Timer: address=#{@address} lifetime=#{@@lifetime}")

  @timer.reset
end

#start_timerObject



46
47
48
49
50
# File 'lib/rgossip/node.rb', line 46

def start_timer
  RGossip.log("Start Timer: address=#{@address} lifetime=#{@@lifetime}")

  @timer.start
end

#to_aObject



42
43
44
# File 'lib/rgossip/node.rb', line 42

def to_a
  [@address, @timestamp, @data]
end

#update_timestampObject



37
38
39
40
# File 'lib/rgossip/node.rb', line 37

def update_timestamp
  now = Time.now
  @timestamp = "#{now.tv_sec}#{now.tv_usec}"
end