summaryrefslogtreecommitdiff
path: root/lib/net/ssh/gateway.rb
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2009-01-31 21:37:28 -0700
committerJamis Buck <jamis@37signals.com>2009-01-31 21:37:28 -0700
commiteb1f1e4465bc3358e63d6b9ce05da22ba158492a (patch)
tree54700be13e3f155803d478b0e8d295b754e5de91 /lib/net/ssh/gateway.rb
parent5da8a7c4a93ed3a595e7955c73641f7ac1a4b50a (diff)
downloadnet-ssh-gateway-eb1f1e4465bc3358e63d6b9ce05da22ba158492a.tar.gz
Allow local port to be specified to Gateway#open
Diffstat (limited to 'lib/net/ssh/gateway.rb')
-rw-r--r--lib/net/ssh/gateway.rb21
1 files changed, 13 insertions, 8 deletions
diff --git a/lib/net/ssh/gateway.rb b/lib/net/ssh/gateway.rb
index 897291f..9742db9 100644
--- a/lib/net/ssh/gateway.rb
+++ b/lib/net/ssh/gateway.rb
@@ -111,25 +111,28 @@ class Net::SSH::Gateway
# port = gateway.open('host', 80)
# # ...
# gateway.close(port)
- def open(host, port)
+ #
+ # If +local_port+ is not specified, the next available port will be used.
+ def open(host, port, local_port=nil)
ensure_open!
- local_port = next_port
+ actual_local_port = local_port || next_port
@session_mutex.synchronize do
- @session.forward.local(local_port, host, port)
+ @session.forward.local(actual_local_port, host, port)
end
if block_given?
begin
- yield local_port
+ yield actual_local_port
ensure
- close(local_port)
+ close(actual_local_port)
end
else
- return local_port
+ return actual_local_port
end
rescue Errno::EADDRINUSE
+ raise if local_port # if a local port was explicitly requested, bubble the error up
retry
end
@@ -186,7 +189,9 @@ class Net::SSH::Gateway
@thread = Thread.new do
while @active
- @session_mutex.synchronize { @session.process(0.1) }
+ @session_mutex.synchronize do
+ @session.process(0.1)
+ end
end
end
end
@@ -200,4 +205,4 @@ class Net::SSH::Gateway
port
end
end
-end \ No newline at end of file
+end