Class: PrayRemote::Server

Inherits:
Object show all
Defined in:
lib/pray-remote/pray-remote.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, *args) ⇒ Server

Returns a new instance of Server.



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/pray-remote/pray-remote.rb', line 260

def initialize(object, *args)
  options = PrayRemote.kwargs(args)

  @host    = options[:host] || DefaultHost
  @port    = options[:port] || DefaultPort

  @unix = options[:unix] || false
  @secret = ""
  if options[:secret] != nil
    @secret = "?secret=" + options[:secret]
  end

  if @unix == false
    @uri = "druby://#{@host}:#{@port}#{@secret}"
    @unix = ""
  else
    @uri = "drabunix:#{@unix}"
  end

  @object  = object
  @options = options

  @client = PrayRemote::Client.new
  DRab.start_service @uri, @client
end

Instance Attribute Details

#clientPryServer::Client (readonly)

Returns Client connecting to the pry-remote server.

Returns:

  • (PryServer::Client)

    Client connecting to the pry-remote server



372
373
374
# File 'lib/pray-remote/pray-remote.rb', line 372

def client
  @client
end

#hostString (readonly)

Returns Host of the server.

Returns:

  • (String)

    Host of the server



375
376
377
# File 'lib/pray-remote/pray-remote.rb', line 375

def host
  @host
end

#objectObject (readonly)

Returns Object to enter into.

Returns:

  • Object to enter into



369
370
371
# File 'lib/pray-remote/pray-remote.rb', line 369

def object
  @object
end

#portInteger (readonly)

Returns Port of the server.

Returns:

  • (Integer)

    Port of the server



378
379
380
# File 'lib/pray-remote/pray-remote.rb', line 378

def port
  @port
end

#unixString (readonly)

Returns Unix domain socket path of the server.

Returns:

  • (String)

    Unix domain socket path of the server



381
382
383
# File 'lib/pray-remote/pray-remote.rb', line 381

def unix
  @unix
end

#uriString (readonly)

Returns URI for DRab.

Returns:

  • (String)

    URI for DRab



384
385
386
# File 'lib/pray-remote/pray-remote.rb', line 384

def uri
  @uri
end

Class Method Details

.run(object, *args) ⇒ Object



255
256
257
258
# File 'lib/pray-remote/pray-remote.rb', line 255

def self.run(object, *args)
  options = PrayRemote.kwargs(args)
  new(object, options).run
end

Instance Method Details

#capture_outputObject

Captures $stdout and $stderr if so requested by the client.



342
343
344
345
# File 'lib/pray-remote/pray-remote.rb', line 342

def capture_output
  $stdout = @client.stdout
  $stderr = @client.stderr
end

#runObject

Actually runs pry-remote



354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/pray-remote/pray-remote.rb', line 354

def run
  STDOUT.puts "[pry-remote] Waiting for client on #{uri}"
  @client.wait

  STDOUT.puts "[pry-remote] Client received, starting remote session"
  setup

  Pry.start(@object, @options.merge(:input => client.input_proxy,
                                    :output => client.output,
                                    :hooks => @hooks))
ensure
  teardown
end

#setupObject

Code that has to be called for Pry-remote to work properly



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/pray-remote/pray-remote.rb', line 287

def setup
  if @client.stdout.nil?
    @client.stdout = $stdout
  end
  if @client.stderr.nil?
    @client.stderr = $stderr
  end
  @hooks = Pry::Hooks.new

  @hooks.add_hook :before_eval, :pry_remote_capture do
    capture_output
  end

  @hooks.add_hook :after_eval, :pry_remote_uncapture do
    uncapture_output
  end

  @hooks.add_hook :before_session, :pry_instance_get do |output, bind, mypry|
    #puts mypry.class
    #puts mypry.methods
    @client.mypry = mypry
  end

  # Before Pry starts, save the pager config.
  # We want to disable this because the pager won't do anything useful in
  # this case (it will run on the server).
  Pry.config.pager, @old_pager = false, Pry.config.pager

  # As above, but for system config
  Pry.config.system, @old_system = PrayRemote::System, Pry.config.system

  Pry.config.editor, @old_editor = nil, Pry.config.editor
  #binding.pry # for testing attacks on clients
end

#teardownObject

Code that has to be called after setup to return to the initial state



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/pray-remote/pray-remote.rb', line 323

def teardown
  # Reset config
  Pry.config.editor = @old_editor
  Pry.config.pager  = @old_pager
  Pry.config.system = @old_system

  STDOUT.puts "[pry-remote] Remote session terminated"

  begin
    @client.kill
  rescue DRab::DRabConnError
    STDOUT.puts "[pry-remote] Continuing to stop service"
  ensure
    STDOUT.puts "[pry-remote] Ensure stop service"
    DRab.stop_service
  end
end

#uncapture_outputObject

Resets $stdout and $stderr to their previous values.



348
349
350
351
# File 'lib/pray-remote/pray-remote.rb', line 348

def uncapture_output
  $stdout = STDOUT
  $stderr = STDOUT
end