Permalink
Browse files

Merge pull request #7 from nixterrimus/master

Check if Port is open in a platform independent way
  • Loading branch information...
2 parents 2813d80 + 573d725 commit 3381d74dfee23257068a9742896263395c4e8ca8 @sax sax committed Nov 21, 2011
Showing with 21 additions and 2 deletions.
  1. +21 −2 lib/fake_ftp/server.rb
View
@@ -1,5 +1,6 @@
require 'socket'
require 'thread'
+require 'timeout'
module FakeFtp
class Server
@@ -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
@@ -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.