Class: KingKong::Ping
- Inherits:
-
Object
- Object
- KingKong::Ping
- Defined in:
- lib/kingkong/ping.rb
Overview
Encaspulates and calculates latency.
Direct Known Subclasses
Defined Under Namespace
Classes: Deferrable, Sequencer
Constant Summary collapse
- NotStartedError =
Class.new(RuntimeError)
Instance Attribute Summary collapse
-
#end_time ⇒ Object
Returns the value of attribute end_time.
-
#start_time ⇒ Object
Returns the value of attribute start_time.
-
#ttl ⇒ Object
Returns the value of attribute ttl.
Class Method Summary collapse
-
.default_ttl ⇒ Object
Default TTL.
-
.sequencer ⇒ Object
Generates ids for pings.
Instance Method Summary collapse
-
#active? ⇒ Boolean
Is this ping still on its journy? Will it make it back! It hasn’t yet…
-
#completed? ⇒ Boolean
Did this get ponged yet?.
-
#id ⇒ Object
Lazily grab a id.
-
#initialize(ttl = self.class.default_ttl, sequencer = self.class.sequencer) ⇒ Ping
constructor
A new instance of Ping.
-
#latency ⇒ Object
How long did it take to clear the message?.
-
#not_started? ⇒ Boolean
Nothing happened ye.
-
#start ⇒ Object
(also: #ping)
Start the ping and set a start time.
-
#status ⇒ Object
Figure out the state of this ping.
-
#stop ⇒ Object
(also: #pong)
Stop the ping and set a stop time.
-
#timed_out? ⇒ Boolean
Did we not receive a pong?.
-
#to_hash ⇒ Object
Bust out a hash so that we can encode it into JSON and make some magic happen.
- #to_s ⇒ Object
Constructor Details
#initialize(ttl = self.class.default_ttl, sequencer = self.class.sequencer) ⇒ Ping
Returns a new instance of Ping.
10 11 12 13 |
# File 'lib/kingkong/ping.rb', line 10 def initialize(ttl=self.class.default_ttl,sequencer=self.class.sequencer) # Time out is when the ping should give up! @ttl, @sequencer = ttl, sequencer end |
Instance Attribute Details
#end_time ⇒ Object
Returns the value of attribute end_time.
8 9 10 |
# File 'lib/kingkong/ping.rb', line 8 def end_time @end_time end |
#start_time ⇒ Object
Returns the value of attribute start_time.
8 9 10 |
# File 'lib/kingkong/ping.rb', line 8 def start_time @start_time end |
#ttl ⇒ Object
Returns the value of attribute ttl.
8 9 10 |
# File 'lib/kingkong/ping.rb', line 8 def ttl @ttl end |
Class Method Details
.default_ttl ⇒ Object
Default TTL. Override this method if you want a different default.
82 83 84 |
# File 'lib/kingkong/ping.rb', line 82 def self.default_ttl 30 # 30 seconds that is! end |
Instance Method Details
#active? ⇒ Boolean
Is this ping still on its journy? Will it make it back! It hasn’t yet…
58 59 60 |
# File 'lib/kingkong/ping.rb', line 58 def active? status == :active end |
#completed? ⇒ Boolean
Did this get ponged yet?
53 54 55 |
# File 'lib/kingkong/ping.rb', line 53 def completed? status == :completed end |
#id ⇒ Object
Lazily grab a id
16 17 18 |
# File 'lib/kingkong/ping.rb', line 16 def id @id ||= @sequencer.next end |
#latency ⇒ Object
How long did it take to clear the message?
35 36 37 |
# File 'lib/kingkong/ping.rb', line 35 def latency end_time.to_f - start_time.to_f if end_time end |
#not_started? ⇒ Boolean
Nothing happened ye
68 69 70 |
# File 'lib/kingkong/ping.rb', line 68 def not_started? status == :not_started end |
#start ⇒ Object Also known as: ping
Start the ping and set a start time
21 22 23 |
# File 'lib/kingkong/ping.rb', line 21 def start @start_time ||= current_time end |
#status ⇒ Object
Figure out the state of this ping.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/kingkong/ping.rb', line 40 def status if !end_time and !start_time :not_started elsif start_time and !end_time and start_time + ttl < current_time :timed_out elsif start_time and !end_time :active elsif start_time and end_time :completed end end |
#stop ⇒ Object Also known as: pong
Stop the ping and set a stop time
27 28 29 30 31 |
# File 'lib/kingkong/ping.rb', line 27 def stop raise Ping::NotStartedError.new("The ping must be started to stop") unless @start_time # We have a start time? Cool! Lets stop this thing then @end_time ||= current_time end |
#timed_out? ⇒ Boolean
Did we not receive a pong?
63 64 65 |
# File 'lib/kingkong/ping.rb', line 63 def timed_out? status == :timed_out end |
#to_hash ⇒ Object
Bust out a hash so that we can encode it into JSON and make some magic happen.
87 88 89 90 91 92 93 94 95 |
# File 'lib/kingkong/ping.rb', line 87 def to_hash { 'status' => status, 'latency' => latency, 'start_time' => start_time.iso8601, 'end_time' => end_time.iso8601, 'ttl' => ttl } end |
#to_s ⇒ Object
72 73 74 |
# File 'lib/kingkong/ping.rb', line 72 def to_s "Ping(#{id}, :#{status}#{", #{latency}s" if completed?})" end |