Class: KingKong::Ping

Inherits:
Object
  • Object
show all
Defined in:
lib/kingkong/ping.rb

Overview

Encaspulates and calculates latency.

Direct Known Subclasses

Deferrable

Defined Under Namespace

Classes: Deferrable, Sequencer

Constant Summary collapse

NotStartedError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_timeObject

Returns the value of attribute end_time.



8
9
10
# File 'lib/kingkong/ping.rb', line 8

def end_time
  @end_time
end

#start_timeObject

Returns the value of attribute start_time.



8
9
10
# File 'lib/kingkong/ping.rb', line 8

def start_time
  @start_time
end

#ttlObject

Returns the value of attribute ttl.



8
9
10
# File 'lib/kingkong/ping.rb', line 8

def ttl
  @ttl
end

Class Method Details

.default_ttlObject

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

.sequencerObject

Generates ids for pings



77
78
79
# File 'lib/kingkong/ping.rb', line 77

def self.sequencer
  @sequencer ||= Sequencer.new
end

Instance Method Details

#active?Boolean

Is this ping still on its journy? Will it make it back! It hasn’t yet…

Returns:

  • (Boolean)


58
59
60
# File 'lib/kingkong/ping.rb', line 58

def active?
  status == :active
end

#completed?Boolean

Did this get ponged yet?

Returns:

  • (Boolean)


53
54
55
# File 'lib/kingkong/ping.rb', line 53

def completed?
  status == :completed
end

#idObject

Lazily grab a id



16
17
18
# File 'lib/kingkong/ping.rb', line 16

def id
  @id ||= @sequencer.next
end

#latencyObject

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

Returns:

  • (Boolean)


68
69
70
# File 'lib/kingkong/ping.rb', line 68

def not_started?
  status == :not_started
end

#startObject 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

#statusObject

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

#stopObject 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?

Returns:

  • (Boolean)


63
64
65
# File 'lib/kingkong/ping.rb', line 63

def timed_out?
  status == :timed_out
end

#to_hashObject

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 if start_time),
    'end_time' => (end_time.iso8601 if end_time),
    'ttl' => ttl
  }
end

#to_sObject



72
73
74
# File 'lib/kingkong/ping.rb', line 72

def to_s
  "Ping(#{id}, :#{status}#{", #{latency}s" if completed?})"
end