Permalink
Browse files

Stab #1

  • Loading branch information...
1 parent 0cf851a commit 43a70ff35e7b9ce3e1c5c04e88e5713858125471 tailor committed Feb 11, 2008
Showing with 34 additions and 24 deletions.
  1. +34 −24 lib/live_console.rb
View
@@ -14,20 +14,27 @@
# fly. There is currently no readline support.
class LiveConsole
include Socket::Constants
+ autoload :IOMethods, 'live_console/io_methods'
- attr_accessor :tcp_server, :lc_thread
- private :tcp_server=, :lc_thread=
+ NoIOMethodError = Class.new StandardError
+
+ attr_accessor :io_method, :io, :lc_thread
+ private :io_method=, :io=, :lc_thread=
# call-seq:
# # Bind a LiveConsole to localhost:3030:
- # LiveConsole.new 3030
+ # LiveConsole.new :socket, :port => 3030
# # Accept connections from anywhere on port 3030. Ridiculously insecure:
- # LiveConsole.new(3030, 'Your.IP.address')
+ # LiveConsole.new(:socket, :port => 3030, :host => 'Your.IP.address')
#
# Creates a new LiveConsole. You must next call LiveConsole#run when you
# want to spawn the thread to accept connections and run the console.
- def initialize(listen_port, listen_addr = '127.0.0.1')
- self.tcp_server = TCPServer.new listen_addr, listen_port
+ def initialize(io_method = :socket, opts = {})
+ unless IOMethods::List.include?(io_method.to_sym)
+ raise ArgumentError, "Unknown IO method: #{io_method}"
+ end
+
+ init_io opts
end
# LiveConsole#run spawns a thread to listen for, accept, and provide an IRB
@@ -40,14 +47,11 @@ def run
socket = nil
begin
Thread.pass
- socket = tcp_server.accept_nonblock
- io = SocketIOMethod.new(socket)
- IRB.start_with_io(io)
+ io.start
+ IRB.start_with_io(io.raw)
rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO,
Errno::EINTR
- socket.close rescue nil
- IO.select([tcp_server], [], [], 1)
-
+ io.stop
retry
end
}
@@ -67,11 +71,17 @@ def stop
end
end
+ private
+
def init_irb
return if @@irb_inited_already
IRB.setup nil
@@irb_inited_already = true
end
+
+ def init_io opts
+ self.io = IOMethods.send(io_method).new opts
+ end
end
# We need to make a couple of changes to the IRB module to account for using a
@@ -102,7 +112,7 @@ def IRB.start_with_io(io, &block)
retry
end
}
- print "\n"
+ irb.print "\n"
end
class Context
@@ -126,31 +136,31 @@ def print(*args)
end
# The SocketIOMethod is a class that wraps I/O over a socket for IRB.
-class SocketIOMethod < IRB::StdioInputMethod
- def initialize(socket)
- @socket = socket
+class GenericIOMethod < IRB::StdioInputMethod
+ def initialize(io)
+ @io = io
@line = []
@line_no = 0
end
def gets
- @socket.print @prompt
- @socket.flush
- @line[@line_no += 1] = @socket.gets
- @socket.flush
+ @io.print @prompt
+ @io.flush
+ @line[@line_no += 1] = @io.gets
+ @io.flush
@line[@line_no]
end
- # These just pass through to the socket.
+ # These just pass through to the io.
%w(eof? close).each { |mname|
- define_method(mname) { || @socket.send mname }
+ define_method(mname) { || @io.send mname }
}
def print(*a)
- @socket.print *a
+ @io.print *a
end
def file_name
- @socket.inspect
+ @io.inspect
end
end

0 comments on commit 43a70ff

Please sign in to comment.