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
36
37
38
39
# 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.reject! do |i|
        i.address == @address
      end

      @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



56
57
58
59
60
# File 'lib/rgossip/node.rb', line 56

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

  @timer.reset
end

#start_timerObject



50
51
52
53
54
# File 'lib/rgossip/node.rb', line 50

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

  @timer.start
end

#stop_timerObject



62
63
64
65
66
# File 'lib/rgossip/node.rb', line 62

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

  @timer.stop
end

#to_aObject



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

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

#update_timestampObject



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

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