module PryRemoteEm
require 'pry-coolline' rescue require 'readline'
How it works with Pry
When PryRemoteEm.run is called it registers with EventMachine
for a given ip and port. When a connection is received EM yields an instance of PryRemoteEm
, we start a Fiber (f1) then call Pry.start specifying the Server
instance as the input and output object for Pry
. The Pry
instance that is created goes into its REPL. When it gets to the read part it calls @input.readline (PryRemoteEm#readline) and passes a prompt, e.g., [1] pry(#<Foo>)>.
PryRemoteEm#readline calls send_data with the prompt then yields from the current Fiber (f1); the one we started when EventMachine
yielded to us. The root Fiber is now active again. At some point, the root Fiber receives data from the client. It calls receive_data in our Server
instance. That instance then resumes the Fiber (f1) that was waiting for readline to finish.
Inside the resumed Fiber (f1) PryRemoteEm#readline returns the recieved data to the instance of Pry
, which continues its REPL. Pry
calls puts, or print or write on its output object (PryRemoteEm
). We send that data out to the client and immediately return. Pry
then calls PryRemoteEm#readline again and again we send the prompt then immediately yield back to the root Fiber.
Pry
just interacts with PryRemoteEm
as if it were any other blocking Readline object. The important bit is making sure that it is started in a new Fiber that can be paused and resumed as needed. PryRemoteEm#readline pauses it and PryRemoteEm#receive_raw resumes it.
Reference: www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers/
Constants
- DEFAULT_BROKER_HOST
- DEFAULT_BROKER_PORT
- DEFAULT_SERVER_HOST
- DEFAULT_SERVER_PORT
- HEARTBEAT_CHECK_INTERVAL
- HEARTBEAT_SEND_INTERVAL
- MAXIMUM_ERRORS_IN_SANDBOX
- NEGOTIATION_TIMEOUT
- RECONNECT_TO_BROKER_TIMEOUT
- VERSION
Public Class Methods
Local PryRemoteEm
servers, including EM signatures, indexed by id. Each signature can be used with high level EM methods like EM.stop_server or EM.get_sockname. If a server has been stopped EM.get_sockname will return nil for that server's signature.
# File lib/pry-remote-em/server.rb, line 44 def servers @servers ||= {} end
Safely stop one or more PryRemoteEm
servers and remove them from the list of servers. @param [String, nil] argument id, url or name, use `nil` to stop them all @return [Hash] stopped servers if they were
# File lib/pry-remote-em/server.rb, line 52 def stop_server(argument = nil) servers_to_stop = if argument servers.select do |id, description| argument == id || description[:urls].include?(argument) || argument == description[:name] end else servers end servers_to_stop.each do |id, description| EM.stop_server(description[:server]) if EM.get_sockname(description[:server]) description[:heartbeat_timer].cancel Broker.unregister(id) servers.delete(id) end end