11module PuppetDebugServer
2- class MessageRouter < JSONHandler
3- def initialize ( *options )
4- super ( *options )
2+ class MessageRouter
3+ attr_accessor :json_handler
4+
5+ def initialize ( *_options )
56 end
67
78 def send_termination_event
89 obj = PuppetDebugServer ::Protocol ::TerminatedEvent . create ( { } )
9- send_event obj
10+ @json_handler . send_event obj
1011 end
1112
1213 def send_exited_event ( exitcode )
1314 obj = PuppetDebugServer ::Protocol ::ExitedEvent . create ( 'exitCode' => exitcode )
14- send_event obj
15+ @json_handler . send_event obj
1516 end
1617
1718 def send_output_event ( options )
1819 obj = PuppetDebugServer ::Protocol ::OutputEvent . create ( options )
19- send_event obj
20+ @json_handler . send_event obj
2021 end
2122
2223 def send_stopped_event ( reason , options = { } )
2324 options [ 'reason' ] = reason
2425 obj = PuppetDebugServer ::Protocol ::StoppedEvent . create ( options )
25- send_event obj
26+ @json_handler . send_event obj
2627 end
2728
2829 def send_thread_event ( reason , thread_id )
2930 obj = PuppetDebugServer ::Protocol ::ThreadEvent . create ( 'reason' => reason , 'threadId' => thread_id )
30- send_event obj
31+ @json_handler . send_event obj
3132 end
3233
3334 def receive_request ( request , original_json )
@@ -59,12 +60,12 @@ def receive_request(request, original_json)
5960 'success' => true
6061 } , request
6162 )
62- send_response response
63+ @json_handler . send_response response
6364
6465 # Send a message that we are initialized
6566 # This must happen _after_ the capabilites are sent
6667 sleep ( 0.5 ) # Sleep for a small amount of time to give the client time to process the capabilites response
67- send_event PuppetDebugServer ::Protocol ::InitializedEvent . create
68+ @json_handler . send_event PuppetDebugServer ::Protocol ::InitializedEvent . create
6869
6970 when 'configurationDone'
7071 PuppetDebugServer . log_message ( :debug , 'Received configurationDone request.' )
@@ -75,7 +76,7 @@ def receive_request(request, original_json)
7576 'success' => true
7677 } , request
7778 )
78- send_response response
79+ @json_handler . send_response response
7980
8081 # Start the debug session if the session is not already running and, setup and configuration have completed
8182 PuppetDebugServer ::PuppetDebugSession . start if !PuppetDebugServer ::PuppetDebugSession . session_active? && PuppetDebugServer ::PuppetDebugSession . setup?
@@ -92,7 +93,7 @@ def receive_request(request, original_json)
9293 'success' => 'true'
9394 } , request
9495 )
95- send_response response
96+ @json_handler . send_response response
9697
9798 when 'setFunctionBreakpoints'
9899 PuppetDebugServer . log_message ( :debug , 'Received setFunctionBreakpoints request.' )
@@ -112,7 +113,7 @@ def receive_request(request, original_json)
112113 'success' => 'true'
113114 } , request
114115 )
115- send_response response
116+ @json_handler . send_response response
116117
117118 when 'launch'
118119 PuppetDebugServer . log_message ( :debug , 'Received launch request.' )
@@ -124,7 +125,7 @@ def receive_request(request, original_json)
124125 'success' => true
125126 } , request
126127 )
127- send_response response
128+ @json_handler . send_response response
128129
129130 # Start the debug session
130131 PuppetDebugServer ::PuppetDebugSession . setup ( self , original_json [ 'arguments' ] )
@@ -148,7 +149,7 @@ def receive_request(request, original_json)
148149 } , request
149150 )
150151 end
151- send_response response
152+ @json_handler . send_response response
152153
153154 when 'stackTrace'
154155 PuppetDebugServer . log_message ( :debug , 'Received stackTrace request.' )
@@ -160,7 +161,7 @@ def receive_request(request, original_json)
160161 'success' => false
161162 } , request
162163 )
163- send_response response
164+ @json_handler . send_response response
164165 return
165166 end
166167
@@ -171,7 +172,7 @@ def receive_request(request, original_json)
171172 'stackFrames' => frames
172173 } , request
173174 )
174- send_response response
175+ @json_handler . send_response response
175176
176177 when 'scopes'
177178 PuppetDebugServer . log_message ( :debug , 'Received scopes request.' )
@@ -183,7 +184,7 @@ def receive_request(request, original_json)
183184 'success' => false
184185 } , request
185186 )
186- send_response response
187+ @json_handler . send_response response
187188 return
188189 end
189190
@@ -196,7 +197,7 @@ def receive_request(request, original_json)
196197 'scopes' => [ ]
197198 } , request
198199 )
199- send_response response
200+ @json_handler . send_response response
200201 return
201202 end
202203
@@ -207,7 +208,7 @@ def receive_request(request, original_json)
207208 'scopes' => scopes
208209 } , request
209210 )
210- send_response response
211+ @json_handler . send_response response
211212
212213 when 'variables'
213214 PuppetDebugServer . log_message ( :debug , 'Received variables request.' )
@@ -219,7 +220,7 @@ def receive_request(request, original_json)
219220 'success' => false
220221 } , request
221222 )
222- send_response response
223+ @json_handler . send_response response
223224 return
224225 end
225226
@@ -230,7 +231,7 @@ def receive_request(request, original_json)
230231 'variables' => variables
231232 } , request
232233 )
233- send_response response
234+ @json_handler . send_response response
234235
235236 when 'evaluate'
236237 PuppetDebugServer . log_message ( :debug , 'Received evaluate request.' )
@@ -242,7 +243,7 @@ def receive_request(request, original_json)
242243 'success' => false
243244 } , request
244245 )
245- send_response response
246+ @json_handler . send_response response
246247 return
247248 end
248249
@@ -254,7 +255,7 @@ def receive_request(request, original_json)
254255 'success' => true
255256 } , request
256257 )
257- send_response response
258+ @json_handler . send_response response
258259 return
259260 end
260261
@@ -270,15 +271,15 @@ def receive_request(request, original_json)
270271 'variablesReference' => 0
271272 } , request
272273 )
273- send_response response
274+ @json_handler . send_response response
274275 rescue => exception # rubocop:disable Style/RescueStandardError
275276 response = PuppetDebugServer ::Protocol ::Response . create_from_request (
276277 {
277278 'success' => false ,
278279 'message' => exception . to_s
279280 } , request
280281 )
281- send_response response
282+ @json_handler . send_response response
282283 end
283284
284285 when 'continue'
@@ -293,7 +294,7 @@ def receive_request(request, original_json)
293294 'allThreadsContinued' => true
294295 } , request
295296 )
296- send_response response
297+ @json_handler . send_response response
297298
298299 when 'stepIn'
299300 PuppetDebugServer . log_message ( :debug , 'Received stepIn request.' )
@@ -305,14 +306,14 @@ def receive_request(request, original_json)
305306 'success' => false
306307 } , request
307308 )
308- send_response response
309+ @json_handler . send_response response
309310 return
310311 end
311312
312313 # Stepin the debug session
313314 PuppetDebugServer ::PuppetDebugSession . continue_stepin_session
314315
315- send_response PuppetDebugServer ::Protocol ::StepInResponse . create_from_request ( { 'success' => true } , request )
316+ @json_handler . send_response PuppetDebugServer ::Protocol ::StepInResponse . create_from_request ( { 'success' => true } , request )
316317
317318 when 'stepOut'
318319 PuppetDebugServer . log_message ( :debug , 'Received stepOut request.' )
@@ -324,14 +325,14 @@ def receive_request(request, original_json)
324325 'success' => false
325326 } , request
326327 )
327- send_response response
328+ @json_handler . send_response response
328329 return
329330 end
330331
331332 # Next the debug session
332333 PuppetDebugServer ::PuppetDebugSession . continue_stepout_session
333334
334- send_response PuppetDebugServer ::Protocol ::StepOutResponse . create_from_request ( { 'success' => true } , request )
335+ @json_handler . send_response PuppetDebugServer ::Protocol ::StepOutResponse . create_from_request ( { 'success' => true } , request )
335336
336337 when 'next'
337338 PuppetDebugServer . log_message ( :debug , 'Received next request.' )
@@ -343,20 +344,20 @@ def receive_request(request, original_json)
343344 'success' => false
344345 } , request
345346 )
346- send_response response
347+ @json_handler . send_response response
347348 return
348349 end
349350
350351 # Next the debug session
351352 PuppetDebugServer ::PuppetDebugSession . continue_next_session
352353
353- send_response PuppetDebugServer ::Protocol ::NextResponse . create_from_request ( { 'success' => true } , request )
354+ @json_handler . send_response PuppetDebugServer ::Protocol ::NextResponse . create_from_request ( { 'success' => true } , request )
354355
355356 when 'disconnect'
356357 # Don't really care about the arguments - Kill everything
357358 PuppetDebugServer . log_message ( :info , 'Received disconnect request. Closing connection to client...' )
358- close_connection
359-
359+ @json_handler . close_connection
360+ # TODO: client isn't disconnecting properly....
360361 else
361362 PuppetDebugServer . log_message ( :error , "Unknown request command #{ request [ 'command' ] } " )
362363
@@ -366,7 +367,7 @@ def receive_request(request, original_json)
366367 'message' => "This feature is not supported - Request #{ request [ 'command' ] } "
367368 } , request
368369 )
369- send_response response
370+ @json_handler . send_response response
370371 end
371372 end
372373 end
0 commit comments