Skip to content

Commit

Permalink
refs #2 WIP: writes to STD{OUT,ERR} and reads from STDIN will go to
Browse files Browse the repository at this point in the history
client most likely to have initiated the write/read
  • Loading branch information
gruis committed Jul 29, 2012
1 parent 652fa79 commit 9edae63
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 7 deletions.
8 changes: 8 additions & 0 deletions lib/pry-remote-em/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,5 +310,13 @@ def readline(prompt)
end
end # readline(prompt = @last_prompt)

def receive_gets(*args)
send_gets(gets(*args))
end

def receive_getc
send_getc(getc)
end

end # module::Client
end # module::PryRemoteEm
28 changes: 21 additions & 7 deletions lib/pry-remote-em/proto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ def receive_json(j)
receive_start_tls
elsif j['pc']
receive_proxy_connection(j['pc'])
elsif j['e']
receive_edit(*j['e'])
elsif j['ec']
receive_edit_changed(*j['ec'])
elsif j['ef']
receive_edit_failed(*j['ef'])
elsif j['e']
receive_edit(*j['e'])
elsif j['ec']
receive_edit_changed(*j['ec'])
elsif j['ef']
receive_edit_failed(*j['ef'])
elsif j['gts']
receive_gets(*j['gts'])
elsif j.include?('gtc')
j['gtc'].nil? ? receive_getc : receive_getc(j['gtc'])
else
receive_unknown(j)
receive_unknown(j)
end
j
end
Expand Down Expand Up @@ -125,6 +129,9 @@ def receive_edit(file, line, contents); end
def receive_edit_changed(file, line_yes_no, diff = ""); end
def receive_edit_failed(file, line, error); end

def receive_gets(sep_got, limit = nil); end
def receive_getc(got = nil); end


def send_banner(g)
send_json({:g => g})
Expand Down Expand Up @@ -154,6 +161,13 @@ def send_raw(l)
send_json(l)
end

def send_gets(*args)
send_json({:gts => args})
end
def send_getc(got = nil)
send_json({:gtc => got})
end

def send_start_tls
send_json({:tls => true})
end
Expand Down
55 changes: 55 additions & 0 deletions lib/pry-remote-em/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'pry-remote-em'
require 'pry-remote-em/broker'
require 'pry-remote-em/server/shell_cmd'
require 'pry-remote-em/server/io'
require 'pry-remote-em/ext/server'

# How it works with Pry
Expand Down Expand Up @@ -141,11 +142,29 @@ def peers(obj = nil)
# Record the association between a given object and a given pry-remote-em connection.
def register(obj, peer)
peers(obj).tap { |plist| plist.include?(peer) || plist.push(peer) }
stdin.register(peer)
stdout.register(peer)
stderr.register(peer)
end

# Remove the association between a given object and a given pry-remote-em connection.
def unregister(obj, peer)
peers(obj).tap {|plist| true while plist.delete(peer) }
stdin.unregister(peer)
stdout.unregister(peer)
stderr.unregister(peer)
end

def stdout
@stdout ||= PryRemoteEm::IO.new($stdout).tap{|io| $stdout = io }
end

def stderr
@stderr ||= PryRemoteEm::IO.new($stderr).tap{|io| $stderr = io }
end

def stdin
@stdin ||= PryRemoteEm::IO.new($stdin).tap{|io| $stdin = io }
end
end # class << self

Expand Down Expand Up @@ -395,6 +414,16 @@ def send_error(msg)
puts "\033[31m#{msg}\033[0m"
end

# Has data been requested from the client, but not provided yet?
def waiting?
@auth_required ||
@waiting_gets ||
@waiting_getc ||
@waiting ||
!@asking_confirm.empty? ||
!@editing.empty?
end

# Methods that make Server compatible with Pry

def readline(prompt)
Expand All @@ -415,6 +444,32 @@ def puts(data = "")
print(s[0] == "\n" ? s : s + "\n")
end

def gets(*args)
@waiting_gets = Fiber.current
send_gets(*args)
Fiber.yield
end

def receive_gets(got)
if @waiting_gets
f, @waiting_gets = @waiting_gets, nil
f.resume(got)
end
end

def getc(*args)
@waiting_getc = Fiber.current
send_getc(*args)
Fiber.yield
end

def receive_getc(got)
if @waiting_getc
f, @waiting_getc = @waiting_getc, nil
f.resume(got)
end
end

def completion_proc=(compl)
@compl_proc = compl
end
Expand Down
79 changes: 79 additions & 0 deletions lib/pry-remote-em/server/io.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module PryRemoteEm
class IO
attr_reader :fileno

def initialize(io)
@io = io.to_io
@fileno = @io.fileno
@clients = []
end

def register(client)
return if @clients.include?(client)
@clients << client
end

def unregister(client)
@clients.delete(client)
end

def puts(*args)
# clients waiting for responses are not the client responsible
# for this #puts call; not true in a Threaded environment
@clients && @clients.each { |c|
!c.waiting? && c.puts(*args)
}
@io.puts(*args)
end

def print(*args)
# clients waiting for responses are not the client responsible
# for this #puts call; not true in a Threaded environment
@clients && @clients.each { |c|
!c.waiting? && c.print(*args)
}
@io.print(*args)
end
alias :write :print

def gets(*args)
f = Fiber.current
given = false
@clients.each do |c|
# clients waiting for responses are not the client responsible
# for this #puts call; not true in a Threaded environment
next if c.waiting?
Fiber.new {
got = c.gets(*args)
if !given && f.alive?
given = true
f.resume(got)
end
}.resume
end
return Fiber.yield
end

def tty?
true
end

def flush
true
end

def eof?
false
end

def to_io
self
end

def method_missing(meth, *args, &blk)
STDERR.puts "call to io (#{fileno}) #{meth.inspect} not overriden by PryRemoteEm::IO"
block_given? ? send(meth, *args, &blk) : send(meth, *args)
end

end
end
19 changes: 19 additions & 0 deletions test/std.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env ruby
require 'pry-remote-em/server'
require 'highline'

class Std
def ping
"pong"
end

def say(msg = "pry rocks!")
puts msg
end

def ask
"42" == HighLine.new.ask("What is the Ultimate Answer to the Ultimate Question of Life, The Universe, and Everything? ")
end
end

EM.run { Std.new.remote_pry_em('0.0.0.0', :auto) }

0 comments on commit 9edae63

Please sign in to comment.