Skip to content

Commit a3d49e6

Browse files
authored
Merge pull request #9 from glennsarti/refactor-transport-layer
(GH-11) Refactor transport layer and fix STDIO server
2 parents b404441 + a601208 commit a3d49e6

File tree

19 files changed

+356
-186
lines changed

19 files changed

+356
-186
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
66

77
## Unreleased
88

9+
- ([GH-11](https://github.com/lingua-pupuli/puppet-editor-services/issues/11)) Refactor the transport layers to loosen object coupling
10+
- ([GH-11](https://github.com/lingua-pupuli/puppet-editor-services/issues/11)) Fix STDIO server
11+
- Stop bad logfile destinations from crashing the language and debug servers
12+
913
## 0.10.0 - 2018-03-29
1014

1115
- ([GH-218](https://github.com/jpogran/puppet-vscode/issues/218)) Validate EPP files

lib/puppet-debugserver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def self.rpc_server(options)
8383

8484
server.add_service(options[:ipaddress], options[:port])
8585
trap('INT') { server.stop_services(true) }
86-
server.start(PuppetDebugServer::MessageRouter, options, 2)
86+
server.start(PuppetDebugServer::JSONHandler, options, 2)
8787

8888
log_message(:info, 'Debug Server exited.')
8989
end

lib/puppet-debugserver/json_handler.rb

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,31 @@
44
# https://github.com/Microsoft/vscode-debugadapter-node/blob/master/protocol/src/debugProtocol.ts
55

66
module PuppetDebugServer
7-
class JSONHandler < PuppetEditorServices::SimpleTCPServerConnection
8-
def initialize(*_options)
7+
class JSONHandler < PuppetEditorServices::SimpleServerConnectionHandler
8+
attr_accessor :message_router
9+
10+
def initialize(options = {})
11+
options = {} if options.nil?
12+
913
@state = :data
1014
@buffer = []
1115
@response_sequence = 1
16+
17+
@client_connection = options[:connection]
18+
if options[:message_router].nil?
19+
@message_router = PuppetDebugServer::MessageRouter.new(options)
20+
else
21+
@message_router = options[:message_router]
22+
end
23+
@message_router.json_handler = self
1224
end
1325

26+
# From PuppetEditorServices::SimpleServerConnectionHandler
1427
def post_init
1528
PuppetDebugServer.log_message(:info, 'Client has connected to the debug server')
1629
end
1730

31+
# From PuppetEditorServices::SimpleServerConnectionHandler
1832
def unbind
1933
PuppetDebugServer.log_message(:info, 'Client has disconnected from the debug server')
2034
end
@@ -35,6 +49,7 @@ def extract_headers(raw_header)
3549
header
3650
end
3751

52+
# From PuppetEditorServices::SimpleServerConnectionHandler
3853
def receive_data(data)
3954
# Inspired by https://github.com/PowerShell/PowerShellEditorServices/blob/dba65155c38d3d9eeffae5f0358b5a3ad0215fac/src/PowerShellEditorServices.Protocol/MessageProtocol/MessageReader.cs
4055
return if data.empty?
@@ -82,7 +97,7 @@ def send_response(response)
8297
PuppetDebugServer.log_message(:debug, "--- OUTBOUND\n#{response_json}\n---")
8398

8499
size = response_json.bytesize
85-
send_data "Content-Length: #{size}\r\n\r\n" + response_json
100+
@client_connection.send_data "Content-Length: #{size}\r\n\r\n" + response_json
86101
end
87102

88103
def send_event(response)
@@ -95,7 +110,7 @@ def send_event(response)
95110
PuppetDebugServer.log_message(:debug, "--- OUTBOUND\n#{response_json}\n---")
96111

97112
size = response_json.bytesize
98-
send_data "Content-Length: #{size}\r\n\r\n" + response_json
113+
@client_connection.send_data "Content-Length: #{size}\r\n\r\n" + response_json
99114
end
100115

101116
def parse_data(data)
@@ -115,7 +130,7 @@ def received_parsed_object(obj)
115130
# NOTE: Not implemented as it doesn't make sense using JSON RPC over pure TCP / UnixSocket.
116131
else
117132
PuppetDebugServer.log_message(:error, 'Closing connection as request is not a Hash')
118-
close_connection_after_writing
133+
@client_connection.close_connection_after_writing
119134
@state = :ignore
120135
end
121136
end
@@ -124,17 +139,12 @@ def process(obj)
124139
message = PuppetDebugServer::Protocol::ProtocolMessage.create(obj)
125140
case message['type']
126141
when 'request'
127-
receive_request(PuppetDebugServer::Protocol::Request.create(obj), obj)
142+
message_router.receive_request(PuppetDebugServer::Protocol::Request.create(obj), obj)
128143
else
129144
PuppetDebugServer.log_message(:error, "Unknown protocol message type #{message['type']}")
130145
end
131146
end
132147

133-
# This method must be overriden in the user's inherited class.
134-
def receive_request(request, _request_json)
135-
PuppetDebugServer.log_message(:debug, "request received:\n#{request.inspect}")
136-
end
137-
138148
def encode_json(data)
139149
JSON.generate(data)
140150
end
@@ -147,6 +157,10 @@ def reply_error(request, message, body)
147157
send_response response
148158
end
149159

160+
def close_connection
161+
@client_connection.close_connection unless @client_connection.nil?
162+
end
163+
150164
# This method could be overriden in the user's inherited class.
151165
def parsing_error(_data, exception)
152166
PuppetDebugServer.log_message(:error, "parsing error:\n#{exception.message}")

lib/puppet-debugserver/message_router.rb

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
module 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

Comments
 (0)