Skip to content

Commit 2849d3f

Browse files
viuginick1valich
authored andcommitted
new strategy of notifying the dispatcher
(cherry picked from commit f1c9639)
1 parent e912ed2 commit 2849d3f

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

lib/ruby-debug-ide.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
module Debugger
1919

2020
class << self
21+
def find_free_port(host)
22+
server = TCPServer.open(host, 0)
23+
port = server.addr[1]
24+
server.close
25+
port
26+
end
27+
2128
# Prints to the stderr using printf(*args) if debug logging flag (-d) is on.
2229
def print_debug(*args)
2330
if Debugger.cli_debug
@@ -111,9 +118,15 @@ def start_control(host, port, notify_dispatcher)
111118
# 127.0.0.1 seemingly works with all systems and with IPv6 as well.
112119
# "localhost" and nil have problems on some systems.
113120
host ||= '127.0.0.1'
114-
server = TCPServer.new(host, port)
115-
print_greeting_msg($stderr, host, port) if defined? IDE_VERSION
116-
notify_dispatcher(port) if notify_dispatcher
121+
122+
server = notify_dispatcher_if_needed(host, port, notify_dispatcher) do |real_port, port_changed = false|
123+
s = TCPServer.new(host, real_port)
124+
print_greeting_msg $stderr, host, real_port, port_changed ? "Subprocess" : "Fast" if defined? IDE_VERSION
125+
s
126+
end
127+
128+
return unless server
129+
117130
while (session = server.accept)
118131
$stderr.puts "Connected from #{session.peeraddr[2]}" if Debugger.cli_debug
119132
dispatcher = ENV['IDE_PROCESS_DISPATCHER']
@@ -141,8 +154,9 @@ def start_control(host, port, notify_dispatcher)
141154

142155
private
143156

157+
def notify_dispatcher_if_needed(host, port, need_notify)
158+
return yield port unless need_notify
144159

145-
def notify_dispatcher(port)
146160
return unless ENV['IDE_PROCESS_DISPATCHER']
147161
acceptor_host, acceptor_port = ENV['IDE_PROCESS_DISPATCHER'].split(":")
148162
acceptor_host, acceptor_port = '127.0.0.1', acceptor_host unless acceptor_port
@@ -151,11 +165,19 @@ def notify_dispatcher(port)
151165
3.times do |i|
152166
begin
153167
s = TCPSocket.open(acceptor_host, acceptor_port)
168+
dispatcher_answer = s.gets.chomp
169+
170+
if dispatcher_answer == "true"
171+
port = Debugger.find_free_port(host)
172+
end
173+
174+
server = yield port, dispatcher_answer == "true"
175+
154176
s.print(port)
155177
s.close
156178
connected = true
157179
print_debug "Ide process dispatcher notified about sub-debugger which listens on #{port}\n"
158-
return
180+
return server
159181
rescue => bt
160182
$stderr.puts "#{Process.pid}: connection failed(#{i+1})"
161183
$stderr.puts "Exception: #{bt}"
@@ -164,7 +186,6 @@ def notify_dispatcher(port)
164186
end unless connected
165187
end
166188
end
167-
168189
end
169190

170191
class Exception # :nodoc:

lib/ruby-debug-ide/greeter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
module Debugger
1111

1212
class << self
13-
def print_greeting_msg(stream, host, port)
13+
def print_greeting_msg(stream, host, port, debugger_name = "Fast")
1414
base_gem_name = if defined?(JRUBY_VERSION) || RUBY_VERSION < '1.9.0'
1515
'ruby-debug-base'
1616
elsif RUBY_VERSION < '2.0.0'
@@ -31,7 +31,7 @@ def print_greeting_msg(stream, host, port)
3131
listens_on = "\n"
3232
end
3333

34-
msg = "Fast Debugger (ruby-debug-ide #{IDE_VERSION}, #{base_gem_name} #{VERSION}, file filtering is #{file_filtering_support})" + listens_on
34+
msg = "#{debugger_name} Debugger (ruby-debug-ide #{IDE_VERSION}, #{base_gem_name} #{VERSION}, file filtering is #{file_filtering_support})" + listens_on
3535

3636
stream.printf msg
3737
end

lib/ruby-debug-ide/multiprocess/pre_child.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def pre_child(options = nil)
1111
'frame_bind' => false,
1212
'host' => host,
1313
'load_mode' => false,
14-
'port' => find_free_port(host),
14+
'port' => Debugger.find_free_port(host),
1515
'stop' => false,
1616
'tracing' => false,
1717
'int_handler' => true,
@@ -24,7 +24,7 @@ def pre_child(options = nil)
2424
)
2525

2626
if(options.ignore_port)
27-
options.port = find_free_port(options.host)
27+
options.port = Debugger.find_free_port(options.host)
2828
options.notify_dispatcher = true
2929
end
3030

@@ -54,14 +54,6 @@ def start_debugger(options)
5454
Debugger.cli_debug = options.cli_debug
5555
Debugger.prepare_debugger(options)
5656
end
57-
58-
59-
def find_free_port(host)
60-
server = TCPServer.open(host, 0)
61-
port = server.addr[1]
62-
server.close
63-
port
64-
end
6557
end
6658
end
6759
end

lib/ruby-debug-ide/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Debugger
2-
IDE_VERSION='0.7.0.beta1'
2+
IDE_VERSION='0.7.0.beta2'
33
end

0 commit comments

Comments
 (0)