Sha256: 20e8c32c90d3d65d65e600a2a6bab1128db1ba151204c4320bb312254ea90412

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

module Throttle
  class Pipe
    attr_accessor :id, :bandwidth, :rule_id

    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    

    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  
    
    def self.reset
      Pipe.all.each do |pipe|
        pipe.delete
      end  
    end  
    
    def initialize(options = {})
      @id        = options[:id]
      @bandwidth = options[:bandwidth]
    end
    
    def set
      unless rule_id
        ipfw("add pipe #{self.id} ip from any to any")
      end
        
      ipfw("pipe #{id} config bw #{self.bandwidth}")
    end  
    
    def bandwidth
      @bandwidth || "Inactive"
    end  
    
    def rule_id
      @rule_id ||= set_rule_id
    end  
    
    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  
    
    def delete
      delete_rule
      
      unless @bandwidth.nil?
        ipfw("pipe #{@id} delete")
      end
    end  
    
    def delete_rule
      if rule_id && rule_id != 0
        ipfw("delete #{rule_id}")
      end    
    end  
    
    private
        
    include Ipfw  
    extend  Ipfw
  end  
end
  

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
throttle-0.0.2 lib/throttle/pipe.rb
throttle-0.0.1 lib/throttle/pipe.rb