require 'observer' require 'thread' require 'timeout' require 'net/http' module RubyForge module MirrorWatch class Watcher include Observable def initialize(host, url, size, interval = 60) @host, @url, @interval, @active = host, url, interval, false $THREADS << Thread.new do while true do response, e = nil, nil begin timeout(@timeout) { response = fetch @host, @url } rescue Timeout::Error => e rescue SocketError => e rescue Errno::EHOSTUNREACH => e rescue Errno::ETIMEDOUT => e rescue Errno::ECONNREFUSED => e rescue Errno::ECONNRESET => e end unless e.nil? and response.code == "200" and response.content_length == size changed and notify_observers(self, @active = false, response) if @active else changed and notify_observers(self, @active = true) if !@active end sleep @interval end end end def fetch(host, url) Net::HTTP.new(host, 80).get(url) end attr_reader :active, :host end end end