Class: Net::SSH::Connection::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/evented-ssh/connection/session.rb

Instance Method Summary collapse

Constructor Details

#initialize(transport, options = {}) ⇒ Session

Returns a new instance of Session.



7
8
9
10
11
12
13
14
15
16
# File 'lib/evented-ssh/connection/session.rb', line 7

def initialize(transport, options = {})
    original_initialize(transport, options)

    # This processes the incoming packets
    # Replacing the IO select calls
    # Next tick so we don't block the current fiber
    transport.reactor.next_tick {
        loop { true }
    }
end

Instance Method Details

#closeObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/evented-ssh/connection/session.rb', line 18

def close
    info { "closing remaining channels (#{channels.length} open)" }
    waiting = channels.collect { |id, channel|
        channel.close
        channel.defer.promise
    }
    begin
        # We use promise resolution here instead of a loop
        ::Libuv.all(waiting).value if channels.any?
    rescue Net::SSH::Disconnect
        raise unless channels.empty?
    end
    transport.close
end

#original_initializeObject



6
# File 'lib/evented-ssh/connection/session.rb', line 6

alias_method :original_initialize, :initialize

#p_exec!(command, status: nil) ⇒ Object

similar to exec! however it returns a promise instead of blocking the flow of execution.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/evented-ssh/connection/session.rb', line 35

def p_exec!(command, status: nil)
    status ||= {}
    channel = exec(command, status: status) do |ch, type, data|
        ch[:result] ||= String.new
        ch[:result] << data
    end
    channel.promise.then do
        channel[:result] ||= ""
        channel[:result] &&= channel[:result].force_encoding("UTF-8")

        StringWithExitstatus.new(channel[:result], status[:exit_code])
    end
end