Module: PryRemoteEm::Client

Includes:
EM::Deferrable, Pry::Helpers::BaseHelpers, Generic, InteractiveMenu
Defined in:
lib/pry-remote-em/client.rb,
lib/pry-remote-em/client/proxy.rb,
lib/pry-remote-em/client/broker.rb,
lib/pry-remote-em/client/generic.rb,
lib/pry-remote-em/client/keyboard.rb,
lib/pry-remote-em/client/interactive_menu.rb

Defined Under Namespace

Modules: Broker, Generic, InteractiveMenu, Keyboard, Proxy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InteractiveMenu

#choose_server, #choose_url, #filter_server_list, #filtered_urls_list_for_server, #prepare_data_for_table, #second_column_for_server, #sort_by_uri, #sort_server_list, #third_column_for_server

Methods included from Generic

#connection_completed, #log, #start_tls

Methods included from Proto

#receive_clear_buffer, #receive_data, #receive_object, #receive_proxy_connection, #receive_register_server, #receive_server_reload_list, #receive_shell_data, #receive_shell_sig, #receive_start_tls, #receive_unregister_server, #send_auth, #send_banner, #send_clear_buffer, #send_completion, #send_heatbeat, #send_msg, #send_msg_bcast, #send_object, #send_prompt, #send_proxy_connection, #send_raw, #send_register_server, #send_server_list, #send_server_reload_list, #send_shell_cmd, #send_shell_data, #send_shell_result, #send_shell_sig, #send_start_tls, #send_unregister_server

Instance Attribute Details

#optsObject (readonly)

class << self



28
29
30
# File 'lib/pry-remote-em/client.rb', line 28

def opts
  @opts
end

Class Method Details

.start(host = nil, port = nil, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# 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

Instance Method Details

#add_to_history(line) ⇒ Object

readline(prompt = @last_prompt)



216
217
218
219
220
221
# 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

#authenticateObject



141
142
143
144
145
146
147
# 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) ⇒ Object

authenticate



149
150
151
152
153
154
155
156
157
158
# 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

#initialize(opts = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# 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

#pagerObject

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.



126
127
128
129
# 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_initObject



41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# 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) ⇒ Object



96
97
98
99
100
# 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) ⇒ Object



71
72
73
74
75
76
# 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) ⇒ Object



160
161
162
163
164
165
# 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) ⇒ Object



102
103
104
# 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) ⇒ Object



106
107
108
# 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) ⇒ Object



167
168
169
# File 'lib/pry-remote-em/client.rb', line 167

def receive_prompt(p)
  readline(p)
end

#receive_raw(r) ⇒ Object



131
132
133
134
135
# 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) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object



110
111
112
# File 'lib/pry-remote-em/client.rb', line 110

def receive_shell_cmd(c)
  Kernel.puts c
end

#receive_shell_result(c) ⇒ Object



114
115
116
117
118
119
120
121
122
# 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) ⇒ Object



137
138
139
# 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_completedObject



51
52
53
54
# 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

#unbindObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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