Class: Throttle::Pipe

Inherits:
Object
  • Object
show all
Extended by:
Ipfw
Includes:
Ipfw
Defined in:
lib/throttle/pipe.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ipfw

ipfw, parse_pipe_id

Constructor Details

#initialize(options = {}) ⇒ Pipe

Returns a new instance of Pipe.



44
45
46
47
# File 'lib/throttle/pipe.rb', line 44

def initialize(options = {})
  @id        = options[:id]
  @bandwidth = options[:bandwidth]
end

Instance Attribute Details

#bandwidthObject

Returns the value of attribute bandwidth.



3
4
5
# File 'lib/throttle/pipe.rb', line 3

def bandwidth
  @bandwidth
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/throttle/pipe.rb', line 3

def id
  @id
end

#rule_idObject

Returns the value of attribute rule_id.



3
4
5
# File 'lib/throttle/pipe.rb', line 3

def rule_id
  @rule_id
end

Class Method Details

.allObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/throttle/pipe.rb', line 5

def self.all
  pipes      = []
  pipe_lines = []
  
  ipfw('list').split("\n").each do |line|
    if /^\d{5} pipe.*$/.match(line)
      pipe_lines << Regexp.last_match(0)
    end  
  end  
  
  pipe_lines.each do |line|
    id        = parse_pipe_id(line)
    bandwidth = Pipe.bandwidth(id)
    
    pipes << Pipe.new({
      :id        => id,
      :bandwidth => bandwidth
    })
  end  
  
  return pipes
end

.bandwidth(id) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/throttle/pipe.rb', line 28

def self.bandwidth(id)
  output = ipfw("pipe #{id} show")
  
  if /^\d{5}:\W*(\d*\.\d* \S*).*$/.match(output)
    Regexp.last_match(1)
  else
    nil
  end    
end

.resetObject



38
39
40
41
42
# File 'lib/throttle/pipe.rb', line 38

def self.reset
  Pipe.all.each do |pipe|
    pipe.delete
  end  
end

Instance Method Details

#deleteObject



73
74
75
76
77
78
79
# File 'lib/throttle/pipe.rb', line 73

def delete
  delete_rule
  
  unless @bandwidth.nil?
    ipfw("pipe #{@id} delete")
  end
end

#delete_ruleObject



81
82
83
84
85
# File 'lib/throttle/pipe.rb', line 81

def delete_rule
  if rule_id && rule_id != 0
    ipfw("delete #{rule_id}")
  end    
end

#setObject



49
50
51
52
53
54
55
# File 'lib/throttle/pipe.rb', line 49

def set
  unless rule_id
    ipfw("add pipe #{self.id} ip from any to any")
  end
    
  ipfw("pipe #{id} config bw #{self.bandwidth}")
end

#set_rule_idObject



65
66
67
68
69
70
71
# File 'lib/throttle/pipe.rb', line 65

def set_rule_id
  regex = "^(\d{5}) pipe #{self.id}.*$"
  
  if /^(\d{5}) pipe #{self.id}.*$/.match(ipfw("list"))
    Regexp.last_match(1)
  end  
end