- add_channel_open_handler
- allocate_channel_id
- channels
- do_channel_close
- do_channel_data
- do_channel_eof
- do_channel_extended_data
- do_channel_failure
- do_channel_open
- do_channel_open_confirmation
- do_channel_open_failure
- do_channel_request
- do_channel_success
- do_channel_window_adjust
- do_request_failure
- do_request_success
- global_request
- loop
- new
- open_channel
- ping!
- process
- reader_ready?
- register_data_request
- remove_channel
- remove_channel_open_handler
- send_message
Request | = | Struct.new( :type, :data, :callback ) |
A structure for representing global requests, as registered by the global_request method. | ||
DataRequest | = | Struct.new( :channel, :data, :type ) |
A structure for representing a data buffer that must be sent across a channel. | ||
MESSAGES | = | {} |
Create a new connection driver that communicates over the given transport session. log is the logger instance to write log messages to, buffers is a buffer factory, and channels is a factory that can return new channel instances.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 46 46: def initialize( session, log, buffers, factories ) 47: @session = session 48: @log = log 49: @buffers = buffers 50: @factories = factories 51: 52: @channel_id_mutex = Mutex.new 53: @next_channel_id = 0 54: 55: @channel_map = Hash.new 56: @request_queue = Array.new 57: @channel_open_handlers = Hash.new 58: 59: @data_requests = Array.new 60: @data_requests_mutex = Mutex.new 61: end
Add a callback to be invoked when a channel-open request is recieved for a channel of the given type. The handler-id is returned.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 90 90: def add_channel_open_handler( type, &block ) 91: ( @channel_open_handlers[ type ] ||= Array.new ).push block 92: @channel_open_handlers.length 93: end
Return the next available channel id for this connection. This method is thread-safe.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 103 103: def allocate_channel_id 104: @channel_id_mutex.synchronize do 105: @next_channel_id += 1 106: return @next_channel_id 107: end 108: end
Returns an array of active channels.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 84 84: def channels 85: @channel_map.values 86: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 317 317: def do_channel_close( response ) 318: local_id = response.read_long 319: @log.debug "CHANNEL_CLOSE recieved (#{local_id})" if @log.debug? 320: @channel_map[ local_id ].close false 321: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 287 287: def do_channel_data( response ) 288: local_id = response.read_long 289: data = response.read_string 290: 291: if @log.debug? 292: @log.debug "CHANNEL_DATA recieved (#{local_id}:#{data.inspect})" 293: end 294: 295: @channel_map[ local_id ].do_data data 296: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 311 311: def do_channel_eof( response ) 312: local_id = response.read_long 313: @log.debug "CHANNEL_EOF recieved (#{local_id})" if @log.debug? 314: @channel_map[ local_id ].do_eof 315: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 298 298: def do_channel_extended_data( response ) 299: local_id = response.read_long 300: data_type = response.read_long 301: data = response.read_string 302: 303: if @log.debug? 304: @log.debug "CHANNEL_EXTENDED_DATA recieved " + 305: "(#{local_id}:#{data_type}:#{data.inspect})" 306: end 307: 308: @channel_map[ local_id ].do_extended_data data_type, data 309: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 342 342: def do_channel_failure( response ) 343: local_id = response.read_long 344: @log.debug "CHANNEL_FAILURE recieved (#{local_id})" if @log.debug? 345: @channel_map[ local_id ].do_failure 346: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 214 214: def do_channel_open( response ) 215: ch_type = response.read_string 216: @log.debug "CHANNEL_OPEN recieved (#{ch_type})" if @log.debug? 217: handled = false 218: 219: sender_channel = response.read_long 220: window_size = response.read_long 221: packet_size = response.read_long 222: 223: channel = @factories[:create].call( ch_type, sender_channel, 224: window_size, packet_size ) 225: 226: ( @channel_open_handlers[ ch_type ] || [] ).each do |handler| 227: next unless handler 228: handled = true 229: handler.call( self, channel, response ) 230: end 231: 232: unless handled 233: raise Net::SSH::Exception, 234: "cannot handle request to open a channel of type '#{ch_type}'" 235: end 236: 237: @channel_map[channel.local_id] = channel 238: 239: writer = @buffers.writer 240: writer.write_byte CHANNEL_OPEN_CONFIRMATION 241: writer.write_long channel.remote_id 242: writer.write_long channel.local_id 243: writer.write_long 0x7FFFFFFF 244: writer.write_long 0x7FFFFFFF 245: @session.send_message writer 246: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 261 261: def do_channel_open_confirmation( response ) 262: local_id = response.read_long 263: remote_id = response.read_long 264: window_size = response.read_long 265: packet_size = response.read_long 266: 267: if @log.debug? 268: @log.debug "CHANNEL_OPEN_CONFIRMATION recieved (#{local_id})" 269: end 270: 271: channel = @channel_map[ local_id ] 272: channel.do_confirm_open remote_id, window_size, packet_size 273: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 248 248: def do_channel_open_failure( response ) 249: local_id = response.read_long 250: reason_code = response.read_long 251: reason = response.read_string 252: language = response.read_string 253: 254: @log.debug "CHANNEL_OPEN_FAILURE recieved (#{reason})" if @log.debug? 255: 256: channel = @channel_map[ local_id ] 257: @channel_map.delete local_id 258: channel.do_confirm_failed reason_code, reason, language 259: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 323 323: def do_channel_request( response ) 324: local_id = response.read_long 325: request = response.read_string 326: want_reply = response.read_bool 327: request_data = response.remainder_as_buffer 328: 329: if @log.debug? 330: @log.debug "CHANNEL_REQUEST recieved (#{local_id}:#{request})" 331: end 332: 333: @channel_map[ local_id ].do_request request, want_reply, request_data 334: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 336 336: def do_channel_success( response ) 337: local_id = response.read_long 338: @log.debug "CHANNEL_SUCCESS recieved (#{local_id})" if @log.debug? 339: @channel_map[ local_id ].do_success 340: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 275 275: def do_channel_window_adjust( response ) 276: local_id = response.read_long 277: bytes_to_add = response.read_long 278: 279: if @log.debug? 280: @log.debug "CHANNEL_WINDOW_ADJUST recieved " + 281: "(#{local_id}:#{bytes_to_add})" 282: end 283: 284: @channel_map[ local_id ].do_window_adjust( bytes_to_add ) 285: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 209 209: def do_request_failure( response ) 210: @log.debug "REQUEST_FAILURE received" if @log.debug? 211: process_request response, false 212: end
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 204 204: def do_request_success( response ) 205: @log.debug "REQUEST_SUCCESS received" if @log.debug? 206: process_request response, true 207: end
Send a global request packet to the server. This returns immediately. The given block will be invoked when the server responds.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 169 169: def global_request( type, data=nil, &block ) 170: writer = @buffers.writer 171: writer.write_byte GLOBAL_REQUEST 172: writer.write_string type.to_s 173: writer.write_bool true 174: writer.write data.to_s if data 175: @session.send_message writer 176: 177: @request_queue.push Request.new( type, data, block ) 178: self 179: end
Repeated call process for as long as the given block returns true. If no block is given, then the loop continues until there are no more open channels on this connection.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 136 136: def loop( &block ) 137: block ||= proc { not @channel_map.empty? } 138: process while block.call 139: end
Open and return a new channel. This returns immediately, before the server confirms that the channel was opened. When the server sends the confirmation, the on_confirm callback will be invoked.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 72 72: def open_channel( type, extra_data=nil, &on_confirm ) 73: channel = @factories[:open].call( type, extra_data ) 74: channel.on_confirm_open &on_confirm 75: @channel_map[ channel.local_id ] = channel 76: end
Sends an innocuous packet to the server to test the connection. Can be used to defeat timeouts on long-running commands.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 194 194: def ping! 195: @session.ping! 196: end
Wait for and dispatch a single event. If nonblock is false (the default) this will block until a message has been received. Otherwise, it will return immediately.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 144 144: def process( nonblock=false ) 145: process_data_requests 146: 147: if !nonblock || reader_ready? 148: type, response = @session.wait_for_message 149: 150: unless ( dispatcher = MESSAGES[ type ] ) 151: raise Net::SSH::Exception, 152: "Unexpected response type '#{type}', (#{response.inspect})" 153: end 154: 155: dispatcher[:method].bind( self ).call( response ) 156: end 157: 158: self 159: end
Delegates to the reader_ready method of the transport session.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 188 188: def reader_ready? 189: @session.reader_ready? 190: end
Register a data buffer (of an optional type) to be sent across the given channel at the next available opportunity.
This is used internally by channels to hide the window size and maximum packet size from the client. Clients should not call this method directly.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 116 116: def register_data_request( channel, data, type=nil ) 117: @data_requests_mutex.synchronize do 118: @data_requests << DataRequest.new( channel, data, type ) 119: end 120: 121: # make sure the new data request has a chance to be sent to the 122: # server... Otherwise, it cannot be sent until the next time #process 123: # is invoked, which can be unexpected in synchronous situations. 124: process_data_requests 125: end
Remove the given channel from the connection.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 79 79: def remove_channel( channel ) 80: @channel_map.delete channel.local_id 81: end
Remove a callback with the given id for channel-open requests of the given type.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 97 97: def remove_channel_open_handler( type, id ) 98: @channel_open_handlers[ type ][ id-1 ] = nil 99: end
A convenience method for sending messages.
[ show source ]
# File lib/net/ssh/connection/driver.rb, line 182 182: def send_message( msg ) 183: @session.send_message msg 184: self 185: end