Skip to content

Commit

Permalink
Binding support. It was so easy.
Browse files Browse the repository at this point in the history
  • Loading branch information
tailor committed Oct 16, 2008
1 parent 8b85c76 commit 5dc55f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doc/lc_example.rb
Expand Up @@ -21,7 +21,7 @@
port = port.zero? ? 3333 : port port = port.zero? ? 3333 : port
$x = ARGV[1] $x = ARGV[1]


lc = LiveConsole.new :socket, :port => port lc = LiveConsole.new :socket, :port => port, :bind => binding
lc.start lc.start


puts "My PID is #{Process.pid}, " \ puts "My PID is #{Process.pid}, " \
Expand Down
18 changes: 13 additions & 5 deletions lib/live_console.rb
Expand Up @@ -17,7 +17,7 @@ class LiveConsole
include Socket::Constants include Socket::Constants
autoload :IOMethods, 'live_console/io_methods' autoload :IOMethods, 'live_console/io_methods'


attr_accessor :io_method, :io, :thread attr_accessor :io_method, :io, :thread, :bind
private :io_method=, :io=, :thread= private :io_method=, :io=, :thread=


# call-seq: # call-seq:
Expand All @@ -28,12 +28,18 @@ class LiveConsole
# LiveConsole.new(:socket, :port => 3030, :host => '0.0.0.0') # LiveConsole.new(:socket, :port => 3030, :host => '0.0.0.0')
# # Use a Unix Domain Socket (which is more secure) instead: # # Use a Unix Domain Socket (which is more secure) instead:
# LiveConsole.new(:unix_socket, :path => '/tmp/my_liveconsole.sock', # LiveConsole.new(:unix_socket, :path => '/tmp/my_liveconsole.sock',
# :mode => 0600) # :mode => 0600, :uid => Process.uid, :gid => Process.gid)
# # By default, the mode is 0600, and the uid and gid are those of the
# # current process. These three options are for the file's permissions.
# # You can also supply a binding for IRB's toplevel:
# LiveConsole.new(:unix_socket,
# :path => "/tmp/live_console_#{Process.pid}.sock", :bind => binding)
# #
# Creates a new LiveConsole. You must next call LiveConsole#start when you # Creates a new LiveConsole. You must next call LiveConsole#start when you
# want to spawn the thread to accept connections and start the console. # want to spawn the thread to accept connections and start the console.
def initialize(io_method, opts = {}) def initialize(io_method, opts = {})
self.io_method = io_method.to_sym self.io_method = io_method.to_sym
self.bind = opts.delete :bind
unless IOMethods::List.include?(self.io_method) unless IOMethods::List.include?(self.io_method)
raise ArgumentError, "Unknown IO method: #{io_method}" raise ArgumentError, "Unknown IO method: #{io_method}"
end end
Expand All @@ -52,7 +58,7 @@ def start
if io.start if io.start
irb_io = GenericIOMethod.new io.raw_input, io.raw_output irb_io = GenericIOMethod.new io.raw_input, io.raw_output
begin begin
IRB.start_with_io(irb_io) IRB.start_with_io(irb_io, bind)
rescue Errno::EPIPE => e rescue Errno::EPIPE => e
io.stop io.stop
end end
Expand Down Expand Up @@ -93,15 +99,17 @@ module IRB
@inited = false @inited = false


# Overridden a la FXIrb to accomodate our needs. # Overridden a la FXIrb to accomodate our needs.
def IRB.start_with_io(io, &block) def IRB.start_with_io(io, bind, &block)
unless @inited unless @inited
setup '/dev/null' setup '/dev/null'
IRB.parse_opts IRB.parse_opts
IRB.load_modules IRB.load_modules
@inited = true @inited = true
end end


irb = Irb.new(nil, io, io) bind ||= IRB::Frame.top(1)
ws = IRB::WorkSpace.new(bind)
irb = Irb.new(ws, io, io)


@CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC] @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
@CONF[:MAIN_CONTEXT] = irb.context @CONF[:MAIN_CONTEXT] = irb.context
Expand Down

0 comments on commit 5dc55f4

Please sign in to comment.