Skip to content
This repository has been archived by the owner on Mar 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from nixterrimus/master
Browse files Browse the repository at this point in the history
Check if Port is open in a platform independent way
  • Loading branch information
sax committed Nov 21, 2011
2 parents 2813d80 + 573d725 commit 3381d74
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/fake_ftp/server.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'socket'
require 'thread'
require 'timeout'

module FakeFtp
class Server
Expand Down Expand Up @@ -71,8 +72,7 @@ def stop
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
Expand Down Expand Up @@ -225,5 +225,24 @@ def _user(name = '')
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

0 comments on commit 3381d74

Please sign in to comment.