Class: Dovado::Router::Traffic

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/dovado/router/traffic.rb,
lib/dovado/router/traffic/amount.rb

Overview

Traffic Counters.

Since:

  • 1.0.3

Defined Under Namespace

Classes: Amount

Instance Method Summary collapse

Constructor Details

#initializeTraffic

Create a new Internet object.

Since:

  • 1.0.3



12
13
14
15
16
17
18
# File 'lib/dovado/router/traffic.rb', line 12

def initialize
  @data = ThreadSafe::Cache.new
  @client = Actor[:client] unless @client
  @last_update = nil
  @data[:down] = Traffic::Amount.new(0)
  @data[:up] = Traffic::Amount.new(0)
end

Instance Method Details

#Amount(value, base = DEFAULT_KILO_BASE) ⇒ Object

Create a new Amount object.

Parameters:

  • value (Numeric)

    initial value of the Amount.

  • base (Integer) (defaults to: DEFAULT_KILO_BASE)

    the base of a kilobyte.

Since:

  • 1.0.3



50
51
52
# File 'lib/dovado/router/traffic/amount.rb', line 50

def Amount(value, base=DEFAULT_KILO_BASE)
  Amount.new(value, base)
end

#downAmount

Data download traffic amount.

Returns:

  • (Amount)

    amount of downloaded data.

Since:

  • 1.0.3



31
32
33
34
# File 'lib/dovado/router/traffic.rb', line 31

def down
  update!
  @data[:down]
end

#upAmount

Data upload traffic amount.

Returns:

  • (Amount)

    amount of uploaded data.

Since:

  • 1.0.3



23
24
25
26
# File 'lib/dovado/router/traffic.rb', line 23

def up
  update!
  @data[:up]
end

#update!Object

Update this Dovado::Router::Traffic object.

Since:

  • 1.0.3



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dovado/router/traffic.rb', line 37

def update!
  unless valid?
    begin
      up, down = fetch_from_router
    rescue
      up, down = fetch_from_router_info
    end
    @data[:down] = Traffic::Amount.new(down)
    @data[:up] = Traffic::Amount.new(up)
    touch!
  end
end

#valid?Boolean

Determine if this traffic object is valid.

Returns:

  • (Boolean)

    true or false.

Since:

  • 1.0.3



53
54
55
56
# File 'lib/dovado/router/traffic.rb', line 53

def valid?
  return false if @last_update.nil?
  (@last_update + SecureRandom.random_number(9) + 1 <= Time.now.to_i)
end