Sha256: e33e5139c4d931dfa1283af050f7df8b46b4ea817efd0fb91c98a336da594c91

Contents?: true

Size: 1.44 KB

Versions: 6

Compression:

Stored size: 1.44 KB

Contents

# -*- encoding: binary -*-
# helpers for server_settings lookups
module Regurgitator::ServerSettings

  # :stopdoc:
  SETTINGS_LOOKUP = 'SELECT value FROM server_settings WHERE field = ? LIMIT 1'
  # :startdoc:

  def server_settings_init
    @zone_cache_mtime = Time.at(0)
    @zone_cache = nil
    @zone_cache_lock = Mutex.new
  end

  def self.extended(obj)
    obj.server_settings_init
  end

  def refresh_zone(force = false) # :nodoc:
    @zone_cache_lock.synchronize { refresh_zone_unlocked(force) }
  end

  def refresh_zone_unlocked(force)
    return @zone_cache if ! force && ((Time.now - @zone_cache_mtime) < 60)
    tmp = Patricia.new
    begin
      row = @db[SETTINGS_LOOKUP, 'network_zones'].first or return tmp
      row[:value].split(/\s*,\s*/).each do |zone|
        row = @db[SETTINGS_LOOKUP, "zone_#{zone}"].first or next
        begin
          tmp.add(row[:value], zone)
        rescue ArgumentError
        end
      end
    ensure
      @zone_cache_mtime = Time.now
      return @zone_cache = tmp
    end
  end

  # If +skip_local+ is true, it may return +:local+ if +addr+ is the address
  # of the host this method is running on
  # Returns the zone (a String) for a given +addr+, if there is one
  # Returns +nil+ if zone information is unknown.
  def zone_for(addr, skip_local = false)
    return :local if skip_local && Regurgitator::Local.include?(addr)
    zone = refresh_zone.search_best(addr) and return zone.data # String
    nil
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
regurgitator-0.6.0 lib/regurgitator/server_settings.rb
regurgitator-0.5.0 lib/regurgitator/server_settings.rb
regurgitator-0.4.0 lib/regurgitator/server_settings.rb
regurgitator-0.3.0 lib/regurgitator/server_settings.rb
regurgitator-0.2.0 lib/regurgitator/server_settings.rb
regurgitator-0.1.0 lib/regurgitator/server_settings.rb