lib/fake_ftp/server.rb in fake_ftp-0.0.7 vs lib/fake_ftp/server.rb in fake_ftp-0.0.8

- old
+ new

@@ -1,7 +1,8 @@ require 'socket' require 'thread' +require 'timeout' module FakeFtp class Server attr_accessor :port, :passive_port @@ -69,12 +70,11 @@ @data_server.close if @data_server @data_server = nil end def is_running?(tcp_port = nil) - service = `lsof -w -n -i tcp:#{tcp_port || port}` - !service.nil? && service != '' + tcp_port.nil? ? port_is_open?(port) : port_is_open?(tcp_port) end private def respond_with(stuff) @@ -222,8 +222,27 @@ (name.to_s == 'anonymous') ? '230 logged in' : '331 send your password' end def active? @mode == :active + end + + private + + def port_is_open?(port) + begin + Timeout::timeout(1) do + begin + s = TCPSocket.new("127.0.0.1", port) + s.close + return true + rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH + return false + end + end + rescue Timeout::Error + end + + return false end end end