Class: Net::SFTP::Request

Inherits:
Object
  • Object
show all
Includes:
Constants::PacketTypes
Defined in:
lib/net/sftp/request.rb

Overview

Encapsulates a single active SFTP request. This is instantiated automatically by the Net::SFTP::Session class when an operation is executed.

request = sftp.open("/path/to/file")
puts request.pending? #-> true
request.wait
puts request.pending? #-> false
result = request.response

Constant Summary

Constants included from Constants::PacketTypes

Constants::PacketTypes::FXP_ATTRS, Constants::PacketTypes::FXP_BLOCK, Constants::PacketTypes::FXP_CLOSE, Constants::PacketTypes::FXP_DATA, Constants::PacketTypes::FXP_EXTENDED, Constants::PacketTypes::FXP_EXTENDED_REPLY, Constants::PacketTypes::FXP_FSETSTAT, Constants::PacketTypes::FXP_FSTAT, Constants::PacketTypes::FXP_HANDLE, Constants::PacketTypes::FXP_INIT, Constants::PacketTypes::FXP_LINK, Constants::PacketTypes::FXP_LSTAT, Constants::PacketTypes::FXP_MKDIR, Constants::PacketTypes::FXP_NAME, Constants::PacketTypes::FXP_OPEN, Constants::PacketTypes::FXP_OPENDIR, Constants::PacketTypes::FXP_READ, Constants::PacketTypes::FXP_READDIR, Constants::PacketTypes::FXP_READLINK, Constants::PacketTypes::FXP_REALPATH, Constants::PacketTypes::FXP_REMOVE, Constants::PacketTypes::FXP_RENAME, Constants::PacketTypes::FXP_RMDIR, Constants::PacketTypes::FXP_SETSTAT, Constants::PacketTypes::FXP_STAT, Constants::PacketTypes::FXP_STATUS, Constants::PacketTypes::FXP_SYMLINK, Constants::PacketTypes::FXP_UNBLOCK, Constants::PacketTypes::FXP_VERSION, Constants::PacketTypes::FXP_WRITE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, type, id, &callback) ⇒ Request

Instantiate a new Request object, serviced by the given session, and being of the given type. The id is the packet identifier for this request.



42
43
44
45
46
# File 'lib/net/sftp/request.rb', line 42

def initialize(session, type, id, &callback) #:nodoc:
  @session, @id, @type, @callback = session, id, type, callback
  @response = nil
  @properties = {}
end

Instance Attribute Details

#callbackObject (readonly)

The callback (if any) associated with this request. When the response is recieved for this request, the callback will be invoked.



29
30
31
# File 'lib/net/sftp/request.rb', line 29

def callback
  @callback
end

#idObject (readonly)

The SFTP packet identifier for this request



22
23
24
# File 'lib/net/sftp/request.rb', line 22

def id
  @id
end

#propertiesObject (readonly)

The hash of properties associated with this request. Properties allow programmers to associate arbitrary data with a request, making state machines richer.



34
35
36
# File 'lib/net/sftp/request.rb', line 34

def properties
  @properties
end

#responseObject (readonly)

The response that was received for this request (see Net::SFTP::Response)



37
38
39
# File 'lib/net/sftp/request.rb', line 37

def response
  @response
end

#sessionObject (readonly)

The Net::SFTP session object that is servicing this request



19
20
21
# File 'lib/net/sftp/request.rb', line 19

def session
  @session
end

#typeObject (readonly)

The type of this request (e.g., :open, :symlink, etc.)



25
26
27
# File 'lib/net/sftp/request.rb', line 25

def type
  @type
end

Instance Method Details

#[](key) ⇒ Object

Returns the value of property with the given key. If key is not a symbol, it will be converted to a symbol before lookup.



50
51
52
# File 'lib/net/sftp/request.rb', line 50

def [](key)
  properties[key.to_sym]
end

#[]=(key, value) ⇒ Object

Sets the value of the property with name key to value. If key is not a symbol, it will be converted to a symbol before lookup.



56
57
58
# File 'lib/net/sftp/request.rb', line 56

def []=(key, value)
  properties[key.to_sym] = value
end

#pending?Boolean

Returns true if the request is still waiting for a response from the server, and false otherwise. The SSH event loop must be run in order for a request to be processed; see #wait.

Returns:

  • (Boolean)


63
64
65
# File 'lib/net/sftp/request.rb', line 63

def pending?
  session.pending_requests.key?(id)
end

#respond_to(packet) ⇒ Object

When the server responds to this request, the packet is passed to this method, which parses the packet and builds a Net::SFTP::Response object to encapsulate it. If a #callback has been provided for this request, the callback is invoked with the new response object.



82
83
84
85
86
87
88
# File 'lib/net/sftp/request.rb', line 82

def respond_to(packet) #:nodoc:
  data = session.protocol.parse(packet)
  data[:type] = packet.type
  @response = Response.new(self, data)

  callback.call(@response) if callback
end

#waitObject

Waits (blocks) until the server responds to this packet. If prior SFTP packets were also pending, they will be processed as well (since SFTP packets are processed in the order in which they are received by the server). Returns the request object itself.



71
72
73
74
# File 'lib/net/sftp/request.rb', line 71

def wait
  session.loop { pending? }
  self
end