Class: RGossip2::Node

Inherits:
Object
  • Object
show all
Includes:
ContextHelper
Defined in:
lib/rgossip2/node.rb

Overview

class Node ノード情報を格納するクラス タイムアウトすると破棄される(=デッドリストに追加される)

------------ -------- ----------------------- | NodeList |<>—---| Node |<>—---| @node_list:NodeList | ------------ | -------- | ----------------------- ------------ | | ----------------------- | Receiver |<>—+ ---| @dead_list:NodeList | ------------ | | ----------------------- ------------ | | --------- | Gossiper |<>—+ ---| Timer | ------------ ---------

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, node_list, dead_list, address, data, timestamp) ⇒ Node

クラスの生成・初期化はContextクラスからのみ行う addressはユニークであること



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rgossip2/node.rb', line 32

def initialize(context, node_list, dead_list, address, data, timestamp)
  @context = context

  @node_list = node_list
  @dead_list = dead_list
  @address = address
  @data = data
  @timestamp = timestamp || ''

  # node_lifetimeの時間内に更新されない場合
  # TimerがNodeを破棄する
  @timer = Timer.new(@context.node_lifetime) do
    debug("Node timed out: address=#{@address}")

    # ノードリストからNodeを削除
    @node_list.synchronize {
      @node_list.delete(@address)
    }

    # デッドリストにNodeを追加
    @dead_list.synchronize {
      @dead_list[@address] = self
    }

    # 破棄時の処理をコールバック
    callback(:delete, @address, @timestamp, @data, @data)
  end
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



26
27
28
# File 'lib/rgossip2/node.rb', line 26

def address
  @address
end

#dataObject

Returns the value of attribute data.



28
29
30
# File 'lib/rgossip2/node.rb', line 28

def data
  @data
end

#timestampObject

Returns the value of attribute timestamp.



27
28
29
# File 'lib/rgossip2/node.rb', line 27

def timestamp
  @timestamp
end

Instance Method Details

#reset_timerObject



78
79
80
81
82
83
# File 'lib/rgossip2/node.rb', line 78

def reset_timer
  # 意図的にコメントアウト
  #debug("Node timer is reset: address=#{@address}")
  @timer.timeout = @context.node_lifetime
  @timer.reset
end

#serializeObject

ノード情報のシリアライズ ただし、シリアライズ後の長さを調べるだけで 実際のデータ送信には使われない



93
94
95
# File 'lib/rgossip2/node.rb', line 93

def serialize
  self.to_a.to_msgpack
end

#start_timerObject



73
74
75
76
# File 'lib/rgossip2/node.rb', line 73

def start_timer
  debug("Node timer is started: address=#{@address}")
  @timer.start
end

#stop_timerObject



85
86
87
88
# File 'lib/rgossip2/node.rb', line 85

def stop_timer
  debug("Node timer is suspended: address=#{@address}")
  @timer.stop
end

#to_aObject Also known as: to_ary

Arrayへの変換



68
69
70
# File 'lib/rgossip2/node.rb', line 68

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

#update_timestampObject

Nodeのタイムスタンプを更新



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

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