net-ssh/net-ssh — pull request #174 by jkeiser (2014-07-18 03:04:51+00:00)
Title: Add callback to directly find out remote forwarding response

When Net::SSH is used to ask for multiple remote forwardings at the same time, the typical polling loop (check if active_remotes has grown, assume the new active_remote is the response to our query) is insufficient. This adds a callback that will be called when a remote forwarding request is complete, so that you can record the response specifically to that request. The documentation says it well:

You may pass a block that will be called when the the port forward request receives a response. This block will be passed the remote_port that was actually bound to, or nil if the binding failed. If the block returns :no_exception, the "failed binding" exception will not be thrown.

If you want to block until the port is active, you could do something like this:

```ruby
got_remote_port = nil
remote(port, host, remote_port, remote_host) do |actual_remote_port|
got_remote_port = actual_remote_port || :error
:no_exception # will yield the exception (if any) on my own thread
end
session.loop { !got_remote_port }
if got_remote_port == :error
raise Net::SSH::Exception, "remote forwarding request failed"
end
```

I have included tests for the new functionality.