Class: PuppetEditorServices::Protocol::DebugAdapter
- Inherits:
-
Base
- Object
- Base
- PuppetEditorServices::Protocol::DebugAdapter
show all
- Defined in:
- lib/puppet_editor_services/protocol/debug_adapter.rb
Constant Summary
collapse
- KEY_TYPE =
'type'
Instance Attribute Summary
Attributes inherited from Base
#connection, #handler
Instance Method Summary
collapse
Methods inherited from Base
#close_connection, #connection_error?
Constructor Details
#initialize(connection) ⇒ DebugAdapter
Returns a new instance of DebugAdapter.
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/puppet_editor_services/protocol/debug_adapter.rb', line 13
def initialize(connection)
super
@state = :data
@buffer = []
@request_sequence_id = 0
@request_seq_mutex = Mutex.new
end
|
Instance Method Details
#encode_and_send(object) ⇒ Object
86
87
88
89
90
91
92
|
# File 'lib/puppet_editor_services/protocol/debug_adapter.rb', line 86
def encode_and_send(object)
raise "#{object.class} is not a PuppetEditorServices::Protocol::DebugAdapterMessages::ProtocolMessage" unless object.is_a?(PuppetEditorServices::Protocol::DebugAdapterMessages::ProtocolMessage)
object.seq = next_sequence_id!
send_json_string(::JSON.generate(object))
end
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/puppet_editor_services/protocol/debug_adapter.rb', line 24
def ()
= {}
.split("\r\n").each do |item|
name, value = item.split(':', 2)
if name.casecmp('Content-Length').zero?
['Content-Length'] = value.strip.to_i
elsif name.casecmp('Content-Type').zero?
['Content-Length'] = value.strip
else
raise("Unknown header #{name} in JSON message")
end
end
end
|
#receive_data(data) ⇒ Object
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
|
# File 'lib/puppet_editor_services/protocol/debug_adapter.rb', line 40
def receive_data(data)
return if data.empty?
return if @state == :ignore
@buffer += data.bytes.to_a
while @buffer.length > 4
offset = 0
while offset < @buffer.length - 4
break if @buffer[offset] == 13 && @buffer[offset + 1] == 10 && @buffer[offset + 2] == 13 && @buffer[offset + 3] == 10
offset += 1
end
return unless offset < @buffer.length - 4
= @buffer.slice(0, offset).pack('C*').force_encoding('ASCII')
= ()
raise('Missing Content-Length header') if ['Content-Length'].nil?
minimum_buf_length = offset + 3 + ['Content-Length'] + 1
return if @buffer.length < minimum_buf_length
content = @buffer.slice(offset + 3 + 1, ['Content-Length']).pack('C*').force_encoding('utf-8')
@buffer = @buffer.slice(minimum_buf_length, @buffer.length - minimum_buf_length)
@buffer = [] if @buffer.nil?
PuppetEditorServices.log_message(:debug, "--- INBOUND\n#{content}\n---")
receive_json_message_as_string(content)
end
end
|
#receive_json_message_as_hash(json_obj) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/puppet_editor_services/protocol/debug_adapter.rb', line 110
def receive_json_message_as_hash(json_obj)
unless json_obj[KEY_TYPE] == 'request'
PuppetEditorServices.log_message(:error, "Unknown protocol message type #{json_obj[KEY_TYPE]}")
return false
end
handler.handle(PuppetEditorServices::Protocol::DebugAdapterMessages::Request.new(json_obj))
true
end
|
#receive_json_message_as_string(content) ⇒ Object
Seperate method so async JSON processing can be supported.
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/puppet_editor_services/protocol/debug_adapter.rb', line 95
def receive_json_message_as_string(content)
json_obj = ::JSON.parse(content)
return receive_json_message_as_hash(json_obj) if json_obj.is_a?(Hash)
return unless json_obj.is_a?(Array)
PuppetEditorServices.log_message(:error, 'Batch request received but not implemented')
send_json_string BATCH_NOT_SUPPORTED_RESPONSE
connection.close_after_writing
@state = :ignore
end
|
#send_json_string(string) ⇒ Object
79
80
81
82
83
84
|
# File 'lib/puppet_editor_services/protocol/debug_adapter.rb', line 79
def send_json_string(string)
PuppetEditorServices.log_message(:debug, "--- OUTBOUND\n#{string}\n---")
size = string.bytesize if string.respond_to?(:bytesize)
connection.send_data "Content-Length: #{size}\r\n\r\n" + string
end
|