1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
require 'breakpoint'
require 'optparse'
require 'timeout'
Options = {
:ClientURI => nil,
:ServerURI => "druby://localhost:42531",
:RetryDelay => 3,
:Permanent => true,
:Verbose => false
}
ARGV.options do |opts|
script_name = File.basename($0)
opts.banner = [
"Usage: ruby #{script_name} [Options] [server uri]",
"",
"This tool lets you connect to a breakpoint service ",
"which was started via Breakpoint.activate_drb.",
"",
"The server uri defaults to druby://localhost:42531"
].join("\n")
opts.separator ""
opts.on("-c", "--client-uri=uri",
"Run the client on the specified uri.",
"This can be used to specify the port",
"that the client uses to allow for back",
"connections from the server.",
"Default: Find a good URI automatically.",
"Example: -c druby://localhost:12345"
) { |Options[:ClientURI]| }
opts.on("-s", "--server-uri=uri",
"Connect to the server specified at the",
"specified uri.",
"Default: druby://localhost:42531"
) { |Options[:ServerURI]| }
opts.on("-R", "--retry-delay=delay", Integer,
"Automatically try to reconnect to the",
"server after delay seconds when the",
"connection failed or timed out.",
"A value of 0 disables automatical",
"reconnecting completely.",
"Default: 10"
) { |Options[:RetryDelay]| }
opts.on("-P", "--[no-]permanent",
"Run the breakpoint client in permanent mode.",
"This means that the client will keep continue",
"running even after the server has closed the",
"connection. Useful for example in Rails."
) { |Options[:Permanent]| }
opts.on("-V", "--[no-]verbose",
"Run the breakpoint client in verbose mode.",
"Will produce more messages, for example between",
"individual breakpoints. This might help in seeing",
"that the breakpoint client is still alive, but adds",
"quite a bit of clutter."
) { |Options[:Verbose]| }
opts.separator ""
opts.on("-h", "--help",
"Show this help message."
) { puts opts; exit }
opts.on("-v", "--version",
"Display the version information."
) do
id = %q$Id: breakpoint_client.rb 40 2005-01-22 20:05:00Z flgr $
puts id.sub("Id: ", "")
puts "(Breakpoint::Version = #{Breakpoint::Version})"
exit
end
opts.parse!
end
Options[:ServerURI] = ARGV[0] if ARGV[0]
module Handlers
extend self
def breakpoint_handler(workspace, message)
puts message
IRB.start(nil, nil, workspace)
puts ""
if Options[:Verbose] then
puts "Resumed execution. Waiting for next breakpoint...", ""
end
end
def eval_handler(code)
result = eval(code, TOPLEVEL_BINDING)
if result then
DRbObject.new(result)
else
result
end
end
def collision_handler()
msg = [
" *** Breakpoint service collision ***",
" Another Breakpoint service tried to use the",
" port already occupied by this one. It will",
" keep waiting until this Breakpoint service",
" is shut down.",
" ",
" If you are using the Breakpoint library for",
" debugging a Rails or other CGI application",
" this likely means that this Breakpoint",
" session belongs to an earlier, outdated",
" request and should be shut down via 'exit'."
].join("\n")
if RUBY_PLATFORM["win"] then
# This sucks. Sorry, I'm not doing this because
# I like funky message boxes -- I need to do this
# because on Windows I have no way of displaying
# my notification via puts() when gets() is still
# being performed on STDIN. I have not found a
# better solution.
begin
require 'tk'
root = TkRoot.new { withdraw }
Tk.messageBox('message' => msg, 'type' => 'ok')
root.destroy
rescue Exception
puts "", msg, ""
end
else
puts "", msg, ""
end
end
end
# Used for checking whether we are currently in the reconnecting loop.
reconnecting = false
loop do
DRb.start_service(Options[:ClientURI])
begin
service = DRbObject.new(nil, Options[:ServerURI])
begin
service.eval_handler = Handlers.method(:eval_handler)
service.collision_handler = Handlers.method(:collision_handler)
service.handler = Handlers.method(:breakpoint_handler)
reconnecting = false
if Options[:Verbose] then
puts "Connection established. Waiting for breakpoint...", ""
end
loop do
begin
service.ping
rescue DRb::DRbConnError => error
puts "Server exited. Closing connection...", ""
exit! unless Options[:Permanent]
break
end
sleep(0.5)
end
ensure
service.eval_handler = nil
service.collision_handler = nil
service.handler = nil
end
rescue Exception => error
if Options[:RetryDelay] > 0 then
if not reconnecting then
reconnecting = true
puts "No connection to breakpoint service at #{Options[:ServerURI]} " +
"(#{error.class})"
puts error.backtrace if $DEBUG
puts "Tries to connect will be made every #{Options[:RetryDelay]} seconds..."
end
sleep Options[:RetryDelay]
retry
else
raise
end
end
end
|