Class: RGossip2::Node
- Inherits:
-
Object
- Object
- RGossip2::Node
- 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
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#data ⇒ Object
Returns the value of attribute data.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Instance Method Summary collapse
-
#initialize(context, node_list, dead_list, address, data, timestamp) ⇒ Node
constructor
クラスの生成・初期化はContextクラスからのみ行う addressはユニークであること.
- #reset_timer ⇒ Object
-
#serialize ⇒ Object
ノード情報のシリアライズ ただし、シリアライズ後の長さを調べるだけで 実際のデータ送信には使われない.
- #start_timer ⇒ Object
- #stop_timer ⇒ Object
-
#to_a ⇒ Object
(also: #to_ary)
Arrayへの変換.
-
#update_timestamp ⇒ Object
Nodeのタイムスタンプを更新.
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, ) @context = context @node_list = node_list @dead_list = dead_list @address = address @data = data @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
#address ⇒ Object (readonly)
Returns the value of attribute address.
26 27 28 |
# File 'lib/rgossip2/node.rb', line 26 def address @address end |
#data ⇒ Object
Returns the value of attribute data.
28 29 30 |
# File 'lib/rgossip2/node.rb', line 28 def data @data end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
27 28 29 |
# File 'lib/rgossip2/node.rb', line 27 def @timestamp end |
Instance Method Details
#reset_timer ⇒ Object
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 |
#serialize ⇒ Object
ノード情報のシリアライズ ただし、シリアライズ後の長さを調べるだけで 実際のデータ送信には使われない
93 94 95 |
# File 'lib/rgossip2/node.rb', line 93 def serialize self.to_a.to_msgpack end |
#start_timer ⇒ Object
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_timer ⇒ Object
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_a ⇒ Object Also known as: to_ary
Arrayへの変換
68 69 70 |
# File 'lib/rgossip2/node.rb', line 68 def to_a [@address, @timestamp, @data] end |
#update_timestamp ⇒ Object
Nodeのタイムスタンプを更新
62 63 64 65 |
# File 'lib/rgossip2/node.rb', line 62 def now = Time.now @timestamp = "#{now.tv_sec}#{now.tv_usec}" end |