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.

Returns the traffic counters from the router. The developer will have to check the return values to determine if there is traffic for multiple modems available, see #up and #down, this can be done by checking the type of the return value of these commands.

Examples:

Single modem

single_modem_amount = @router.traffic.down

Two modems

first_modem_amount = @router.traffic.down.first
second_modem_amount = @router.traffic.down.last

Checking the return value to see if there are multiple modems

traffic_down = @router.traffic.down
if traffic_down.is_a? Dovado::Router::Traffic::Amount
  return traffic_down.gigabytes
elsif traffic_down.is_a? Array
  return traffic_down.first.gb + traffic_down.last.gb
end

Since:

  • 1.0.3

Defined Under Namespace

Classes: Amount

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTraffic

Create a new Dovado::Router::Traffic object.

Since:

  • 1.0.3



32
33
34
35
36
37
38
# File 'lib/dovado/router/traffic.rb', line 32

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

Class Method Details

.setup_supervision!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.3



99
100
101
# File 'lib/dovado/router/traffic.rb', line 99

def self.setup_supervision!
  supervise as: :traffic, size: 1 unless Actor[:traffic]
end

Instance Method Details

#Amount(value = 0, base = DEFAULT_KILO_BASE) ⇒ Object

Create a new Amount object.

Parameters:

  • value (Numeric) (defaults to: 0)

    initial value of the Amount.

  • base (Integer) (defaults to: DEFAULT_KILO_BASE)

    the base of a kilobyte.

Since:

  • 1.0.3



77
78
79
# File 'lib/dovado/router/traffic/amount.rb', line 77

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

#downAmount+

Data download traffic amount.

If two modems are used, the returned value is an array with Amount objects.

Returns:

Since:

  • 1.0.3



57
58
59
60
# File 'lib/dovado/router/traffic.rb', line 57

def down
  update!
  @data[:down]
end

#down_totalAmount

Data download total traffic amount. Useful with multiple modems.

Returns:

  • (Amount)

    total amount of downloaded data.

Since:

  • 1.0.3



65
66
67
68
# File 'lib/dovado/router/traffic.rb', line 65

def down_total
  up, down = update_total_traffic_from_router_info
  Traffic::Amount.new down
end

#upAmount+

Data upload traffic amount.

If two modems are used, the returned value is an array with Amount objects.

Returns:

Since:

  • 1.0.3



46
47
48
49
# File 'lib/dovado/router/traffic.rb', line 46

def up
  update!
  @data[:up]
end

#up_totalAmount

Data upload total traffic amount. Useful with multiple modems.

Returns:

  • (Amount)

    total amount of uploaded data.

Since:

  • 1.0.3



73
74
75
76
# File 'lib/dovado/router/traffic.rb', line 73

def up_total
  up, down = update_total_traffic_from_router_info
  Traffic::Amount.new up
end

#update!Object

Update the data in this Dovado::Router::Traffic object.

Since:

  • 1.0.3



79
80
81
82
83
84
85
86
87
88
# File 'lib/dovado/router/traffic.rb', line 79

def update!
  unless valid?
    begin
      fetch_from_router
    rescue
      @data[:up], @data[:down] = fetch_from_router_info
    end
    touch!
  end
end

#valid?Boolean

Determine if this traffic object is valid.

Returns:

  • (Boolean)

    true or false.

Since:

  • 1.0.3



93
94
95
96
# File 'lib/dovado/router/traffic.rb', line 93

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