Skip to content

Instantly share code, notes, and snippets.

@deivid-rodriguez
Created February 11, 2015 16:48
Show Gist options
  • Save deivid-rodriguez/94edb9d8934e98a681b9 to your computer and use it in GitHub Desktop.
Save deivid-rodriguez/94edb9d8934e98a681b9 to your computer and use it in GitHub Desktop.
diff --git a/lib/byebug/remote.rb b/lib/byebug/remote.rb
index a782cfc..33d629c 100644
--- a/lib/byebug/remote.rb
+++ b/lib/byebug/remote.rb
@@ -1,96 +1,105 @@
require 'socket'
module Byebug
- # Port number used for remote debugging
- PORT = 8989 unless defined?(PORT)
-
- class << self
- # If in remote mode, wait for the remote connection
- attr_accessor :wait_connection
-
- # The actual port that the server is started at
- attr_accessor :actual_port
- attr_reader :actual_control_port
-
- #
- # Interrupts the current thread
- #
- def interrupt
- current_context.interrupt
- end
+ module Remote
+ # Port number used for remote debugging
+ PORT = 8989 unless defined?(PORT)
- #
- # Starts a remote byebug
- #
- def start_server(host = nil, port = PORT)
- return if @thread
+ class Server
+ # If in remote mode, wait for the remote connection
+ attr_accessor :wait_connection
- handler.interface = nil
- start
+ # The actual port that the server is started at
+ attr_accessor :actual_port
+ attr_reader :actual_control_port
- start_control(host, port == 0 ? 0 : port + 1)
+ def initialize(wait = true)
+ @wait_connection = wait
+ end
- yield if block_given?
+ #
+ # Interrupts the current thread
+ #
+ def interrupt
+ current_context.interrupt
+ end
- mutex = Mutex.new
- proceed = ConditionVariable.new
+ #
+ # Starts a remote byebug
+ #
+ def start(host = nil, port = PORT)
+ return if @thread
- server = TCPServer.new(host, port)
- self.actual_port = server.addr[1]
- @thread = DebugThread.new do
- while (session = server.accept)
- handler.interface = RemoteInterface.new(session)
- mutex.synchronize { proceed.signal } if wait_connection
- end
- end
+ handler.interface = nil
+ start
- mutex.synchronize { proceed.wait(mutex) } if wait_connection
- end
+ start_control(host, port == 0 ? 0 : port + 1)
+
+ yield if block_given?
- def start_control(host = nil, ctrl_port = PORT + 1)
- return @actual_control_port if @control_thread
- server = TCPServer.new(host, ctrl_port)
- @actual_control_port = server.addr[1]
- @control_thread = DebugThread.new do
- while (session = server.accept)
- handler.interface = RemoteInterface.new(session)
- ControlCommandProcessor.new(handler.interface).process_commands
+ mutex = Mutex.new
+ proceed = ConditionVariable.new
+
+ server = TCPServer.new(host, port)
+ self.actual_port = server.addr[1]
+ @thread = DebugThread.new do
+ while (session = server.accept)
+ handler.interface = RemoteInterface.new(session)
+ mutex.synchronize { proceed.signal } if wait_connection
+ end
end
+
+ mutex.synchronize { proceed.wait(mutex) } if wait_connection
end
- @actual_control_port
- end
- #
- # Connects to the remote byebug
- #
- def start_client(host = 'localhost', port = PORT)
- handler.interface = LocalInterface.new
- puts 'Connecting to byebug server...'
- socket = TCPSocket.new(host, port)
- puts 'Connected.'
-
- catch(:exit) do
- while (line = socket.gets)
- case line
- when /^PROMPT (.*)$/
- input = handler.interface.read_command(Regexp.last_match[1])
- throw :exit unless input
- socket.puts input
- when /^CONFIRM (.*)$/
- input = handler.interface.confirm(Regexp.last_match[1])
- throw :exit unless input
- socket.puts input
- else
- puts line
+ def start_control(host = nil, ctrl_port = PORT + 1)
+ return @actual_control_port if @control_thread
+ server = TCPServer.new(host, ctrl_port)
+ @actual_control_port = server.addr[1]
+ @control_thread = DebugThread.new do
+ while (session = server.accept)
+ handler.interface = RemoteInterface.new(session)
+ ControlCommandProcessor.new(handler.interface).process_commands
end
end
+ @actual_control_port
end
- socket.close
end
- def parse_host_and_port(host_port_spec)
- location = host_port_spec.split(':')
- location[1] ? [location[0], location[1].to_i] : ['localhost', location[0]]
+ class Client
+ attr_reader :host, :port
+
+ def initialize(host = 'localhost', port = PORT)
+ @host, @port = host, port
+ end
+
+ #
+ # Connects to the remote byebug
+ #
+ def start
+ handler.interface = LocalInterface.new
+ puts 'Connecting to byebug server...'
+ socket = TCPSocket.new(host, port)
+ puts 'Connected.'
+
+ catch(:exit) do
+ while (line = socket.gets)
+ case line
+ when /^PROMPT (.*)$/
+ input = handler.interface.read_command(Regexp.last_match[1])
+ throw :exit unless input
+ socket.puts input
+ when /^CONFIRM (.*)$/
+ input = handler.interface.confirm(Regexp.last_match[1])
+ throw :exit unless input
+ socket.puts input
+ else
+ puts line
+ end
+ end
+ end
+ socket.close
+ end
end
end
end
diff --git a/lib/byebug/runner.rb b/lib/byebug/runner.rb
index e11069a..7129948 100644
--- a/lib/byebug/runner.rb
+++ b/lib/byebug/runner.rb
@@ -123,7 +123,7 @@ module Byebug
end
opts.on '-R', '--remote [host:]port', 'Remote debug [host:]port' do |p|
- self.remote = Byebug.parse_host_and_port(p)
+ self.remote = parse_host_and_port(p)
end
opts.on '-t', '--[no-]trace', 'Turn on line tracing' do |v|
@@ -139,5 +139,12 @@ module Byebug
end
end
end
+
+ private
+
+ def parse_host_and_port(host_port_spec)
+ location = host_port_spec.split(':')
+ location[1] ? [location[0], location[1].to_i] : ['localhost', location[0]]
+ end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment