lib/em-ssh/connection/channel/interactive.rb
# Wait for the shell to send data containing the given string.
# @param [String, Regexp] strregex a string or regex to match the console output against.
# @param [Hash] opts
# @option opts [Fixnum] :timeout (Session::TIMEOUT) the maximum number of seconds to wait
# @return [String] the contents of the buffer or a TimeoutError
# @raise Disconnected
# @raise ClosedChannel
# @raise TimeoutError
Wait for the shell to send data containing the given string.
Wait for the shell to send data containing the given string .
def wait_for(strregex, opts = { })
            log = opts[:log] || NullLogger.new
            timeout_value = opts[:timeout].is_a?(Fixnum) ? opts[:timeout] : DEFAULT_TIMEOUT
            log.debug("wait_for(#{strregex.inspect}, :timeout => #{timeout_value})")
            opts          = { :timeout => timeout_value }.merge(opts)
            found         = nil
            f             = Fiber.current
            trace         = caller
            timer         = nil
            data_callback = nil
            matched       = false
            started       = Time.new
            timeout_proc = proc do
              data_callback && data_callback.cancel
              f.resume(TimeoutError.new("#{@host}: inactivity timeout (#{opts[:timeout]}) while waiting for #{strregex.inspect}; received: #{buffer.inspect}; waited total: #{Time.new - started}"))
            end
            data_callback = on(:data) do
              timer && timer.cancel
              if matched
                log.warn("data_callback invoked when already matched")
                next
              end
              if (matched = buffer.match(strregex))
                log.debug("matched #{strregex.inspect} on #{buffer.inspect}")
                data_callback.cancel
                @buffer = matched.post_match
                f.resume(matched.pre_match + matched.to_s)
              else
                timer = EM::Timer.new(opts[:timeout], &timeout_proc)
              end
            end
            EM::next_tick { data_callback.call() if buffer.length > 0 }
            timer = EM::Timer.new(opts[:timeout], &timeout_proc)
            Fiber.yield.tap do |res|
              if res.is_a?(Exception)
                res.set_backtrace(Array(res.backtrace) + trace)
                raise res
              end
              yield(res) if block_given?
            end
          end
def wait_for ( strregex , opts = { } ) log = opts [ :log ] || NullLogger . new timeout_value = opts [ :timeout ] . is_a? ( Fixnum ) ? opts [ :timeout ] : DEFAULT_TIMEOUT log . debug ( "wait_for(#{strregex.inspect}, :timeout => #{timeout_value})" ) opts = { :timeout => timeout_value } . merge ( opts ) found = nil f = Fiber . current trace = caller timer = nil data_callback = nil matched = false started = Time . new timeout_proc = proc do data_callback && data_callback . cancel f . resume ( TimeoutError . new ( "#{@host}: inactivity timeout (#{opts[:timeout]}) while waiting for #{strregex.inspect}; received: #{buffer.inspect}; waited total: #{Time.new - started}" ) ) end data_callback = on ( :data ) do timer && timer . cancel if matched log . warn ( "data_callback invoked when already matched" ) next end if ( matched = buffer . match ( strregex ) ) log . debug ( "matched #{strregex.inspect} on #{buffer.inspect}" ) data_callback . cancel @buffer = matched . post_match f . resume ( matched . pre_match + matched . to_s ) else timer = EM :: Timer . new ( opts [ :timeout ] , & timeout_proc ) end end EM :: next_tick { data_callback . call ( ) if buffer . length > 0 } timer = EM :: Timer . new ( opts [ :timeout ] , & timeout_proc ) Fiber . yield . tap do | res | if res . is_a? ( Exception ) res . set_backtrace ( Array ( res . backtrace ) + trace ) raise res end yield ( res ) if block_given? end end
Wait for the shell to send data containing the given string.
Wait for the shell to send data containing the given string .
### ### # Check against current buffer
the contents of the buffer or a TimeoutError
the contents of the buffer or a TimeoutError
String
None
None
None
strregex
<not_specific>
opts
:timeout (Session::TIMEOUT) the maximum number of seconds to wait
: timeout ( Session :: TIMEOUT ) the maximum number of seconds to wait
<not_specific>
None