module PryRemoteEm::Client

Attributes

opts[R]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/pry-remote-em/client.rb, line 30
def initialize(opts = {})
  @opts = opts
  if (a = opts[:auth])
    if a.respond_to?(:call)
      @auth = a
    else
      @auth = lambda { a }
    end
  end
end
start(host = nil, port = nil, opts = {}) { || ... } click to toggle source
# File lib/pry-remote-em/client.rb, line 17
def start(host = nil, port = nil, opts = {})
  EM.connect(host || PryRemoteEm::DEFAULT_SERVER_HOST, port || PryRemoteEm::DEFAULT_SERVER_PORT, PryRemoteEm::Client, opts) do |c|
    c.callback { yield if block_given? }
    c.errback do |e|
      Kernel.puts "[pry-remote-em] connection failed\n#{e}"
      yield(e) if block_given?
    end
  end
end

Public Instance Methods

add_to_history(line) click to toggle source
# File lib/pry-remote-em/client.rb, line 216
def add_to_history(line)
  if defined?(Readline) && @input == Readline
    Readline::HISTORY.push(line)
  end
  # Nothing to do with Coolline, it just works
end
authenticate() click to toggle source
# File lib/pry-remote-em/client.rb, line 141
def authenticate
  return fail('[pry-remote-em] authentication required') unless @auth
  return fail("[pry-remote-em] can't authenticate before negotiation complete") unless @negotiated
  user, pass = @auth.call
  return fail("[pry-remote-em] expected #{@auth} to return a user and password") unless user && pass
  send_auth([user, pass])
end
auto_complete(word) click to toggle source
# File lib/pry-remote-em/client.rb, line 149
def auto_complete(word)
  word = word.completed_word if defined?(Coolline) && word.kind_of?(Coolline)

  @waiting = Thread.current
  EM.next_tick { send_completion(word) }
  sleep
  c = Thread.current[:completion]
  Thread.current[:completion] = nil
  c
end
pager() click to toggle source

TODO detect if the old pager behavior of Pry is supported and use it through Pry.pager. If it's not then use the SimplePager.

# File lib/pry-remote-em/client.rb, line 126
def pager
  pager_class = ENV['PRYEMNOPAGER'] ? Pry::Pager::NullPager : @opts[:pager] || Pry::Pager::SimplePager
  @pager ||= pager_class.new(Pry::Output.new(Pry))
end
post_init() click to toggle source
# File lib/pry-remote-em/client.rb, line 41
def post_init
  @input = if defined?(PryCoolline)
    PryCoolline.make_coolline
  else
    Pry.history.load if Pry.config.history.should_load
    Readline
  end
  @input.completion_proc = method(:auto_complete)
end
readline(prompt = @last_prompt) click to toggle source
# File lib/pry-remote-em/client.rb, line 171
def readline(prompt = @last_prompt)
  @last_prompt = prompt
  if @negotiated && !@unbound
    operation = proc do
      thread = Thread.current
      old_trap = Signal.trap(:INT) { thread.raise Interrupt }
      begin
        @input.readline(prompt)
      rescue Interrupt
        send_clear_buffer
        puts
        :ignore_me
      ensure
        Signal.trap(:INT, old_trap)
      end
    end

    callback  = proc do |l|
      next if l == :ignore_me

      add_to_history(l) unless l.nil? || l.empty?

      if l.nil?
        readline
      elsif '^^' == l[0..1]
        send_msg_bcast(l[2..-1])
      elsif '^' == l[0]
        send_msg(l[1..-1])
      elsif '.' == l[0]
        send_shell_cmd(l[1..-1])
        @keyboard = EM.open_keyboard(Keyboard, self)
      elsif 'reset' == l.strip
        # TODO work with 'bundle exec pry-remote-em ...'
        # TODO work with 'ruby -I lib bin/pry-remote-em ...'
        Kernel.puts "\033[1m#{$0} #{ARGV.join(' ')}\033[0m"
        exec("#{$0} #{ARGV.join(' ')}")
      else
        send_raw(l)
      end
    end

    EM.defer(operation, callback)
  end
end
receive_auth(a) click to toggle source
# File lib/pry-remote-em/client.rb, line 96
def receive_auth(a)
  return fail a if a.is_a?(String)
  return authenticate if a == false
  @authenticated = true if a == true
end
receive_banner(name, version, scheme) click to toggle source
# File lib/pry-remote-em/client.rb, line 71
def receive_banner(name, version, scheme)
  # Client::Generic#receive_banner
  if super(name, version, scheme)
    start_tls if @opts[:tls]
  end
end
receive_completion(c) click to toggle source
# File lib/pry-remote-em/client.rb, line 160
def receive_completion(c)
  return unless @waiting
  @waiting[:completion] = c
  @waiting, t = nil, @waiting
  t.run
end
receive_msg(m) click to toggle source
# File lib/pry-remote-em/client.rb, line 102
def receive_msg(m)
  Kernel.puts "\033[1m! msg: " + m + "\033[0m"
end
receive_msg_bcast(mb) click to toggle source
# File lib/pry-remote-em/client.rb, line 106
def receive_msg_bcast(mb)
  Kernel.puts "\033[1m!! msg: " + mb + "\033[0m"
end
receive_prompt(p) click to toggle source
# File lib/pry-remote-em/client.rb, line 167
def receive_prompt(p)
  readline(p)
end
receive_raw(r) click to toggle source
# File lib/pry-remote-em/client.rb, line 131
def receive_raw(r)
  pager.write(r)
rescue Pry::Pager::StopPaging
  warn '[pry-remote-em] stop paging is not implemented, use PRYEMNOPAGER environment variable to avoid paging at all'
end
receive_server_list(list) click to toggle source
# File lib/pry-remote-em/client.rb, line 78
def receive_server_list(list)
  if list.empty?
    log.info("\033[33m[pry-remote-em] no servers are registered with the broker\033[0m")
    Process.exit
  end
  url, proxy = choose_server(list)
  return unless url
  uri = URI.parse(url)
  if proxy
    @opts[:tls]  = uri.scheme == 'pryems'
    @negotiated  = false
    @tls_started = false
    return send_proxy_connection(url)
  end
  @reconnect_to = uri
  close_connection
end
receive_shell_cmd(c) click to toggle source
# File lib/pry-remote-em/client.rb, line 110
def receive_shell_cmd(c)
  Kernel.puts c
end
receive_shell_result(c) click to toggle source
# File lib/pry-remote-em/client.rb, line 114
def receive_shell_result(c)
  if @keyboard
    @keyboard.bufferio(true)
    @keyboard.close_connection
  end
  if c == 255 || c == 127
    Kernel.puts 'command not found'
  end
end
receive_unknown(j) click to toggle source
# File lib/pry-remote-em/client.rb, line 137
def receive_unknown(j)
  warn "[pry-remote-em] received unexpected data: #{j.inspect}"
end
ssl_handshake_completed() click to toggle source
# File lib/pry-remote-em/client.rb, line 51
def ssl_handshake_completed
  log.info('[pry-remote-em] TLS connection established')
  @opts[:tls] = true
end
unbind() click to toggle source
# File lib/pry-remote-em/client.rb, line 56
def unbind
  if (uri = @reconnect_to)
    @reconnect_to = nil
    tls       = uri.scheme == 'pryems'
    log.info("\033[35m[pry-remote-em] connection will not be encrypted\033[0m")  if @opts[:tls] && !tls
    @opts[:tls]   = tls
    @tls_started  = false
    reconnect(uri.host, uri.port)
  else
    @unbound = true
    log.info('[pry-remote-em] session terminated')
    error? ? fail : succeed
  end
end