Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
make Session#connect execute/enqueue the callback even if the session…
… is already open/opening
  • Loading branch information
jamis committed Apr 26, 2008
1 parent c6de5bc commit d947cf5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
@@ -1,5 +1,7 @@
=== *unreleased*

* Allow Session#connect to be useful even in the open/opening states by invoking or queuing the callback block [Jamis Buck]

* Allow custom properties to be set on upload/download initiation via the :properties option [Jamis Buck]

* Custom properties on Download instances [Jamis Buck]
Expand Down
30 changes: 21 additions & 9 deletions lib/net/sftp/session.rb
Expand Up @@ -746,8 +746,11 @@ def opening?
end

# Attempts to establish an SFTP connection over the SSH session given when
# this object was instantiated. If the object is already open (or opening),
# this does nothing.
# this object was instantiated. If the object is already open, this will
# simply execute the given block (if any), passing the SFTP session itself
# as argument. If the session is currently being opened, this will add
# the given block to the list of callbacks, to be executed when the session
# is fully open.
#
# This method does not block, and will return immediately. If you pass a
# block to it, that block will be invoked when the connection has been
Expand All @@ -760,12 +763,20 @@ def opening?
# If you just want to block until the connection is ready, see the #connect!
# method.
def connect(&block)
return unless state == :closed
@state = :opening
@channel = session.open_channel(&method(:when_channel_confirmed))
@packet_length = nil
@protocol = nil
@on_ready = block
case state
when :open
block.call(self) if block
when :closed
@state = :opening
@channel = session.open_channel(&method(:when_channel_confirmed))
@packet_length = nil
@protocol = nil
@on_ready = Array(block)
else # opening
@on_ready << block if block
end

self
end

# Same as the #connect method, but blocks until the SFTP connection has
Expand Down Expand Up @@ -924,7 +935,8 @@ def do_version(packet)
@pending_requests = {}

@state = :open
@on_ready.call(self) if @on_ready
@on_ready.each { |callback| callback.call(self) }
@on_ready = nil
end

# Parses the packet, finds the associated Request instance, and tells
Expand Down

0 comments on commit d947cf5

Please sign in to comment.