|
| 1 | +struct JSONRPCError <: Exception |
| 2 | + code::Int |
| 3 | + msg::AbstractString |
| 4 | + data::Any |
| 5 | +end |
| 6 | + |
| 7 | +function Base.showerror(io::IO, ex::JSONRPCError) |
| 8 | + error_code_as_string = if ex.code==-32700 |
| 9 | + "ParseError" |
| 10 | + elseif ex.code==-32600 |
| 11 | + "InvalidRequest" |
| 12 | + elseif ex.code==-32601 |
| 13 | + "MethodNotFound" |
| 14 | + elseif ex.code==-32602 |
| 15 | + "InvalidParams" |
| 16 | + elseif ex.code==-32603 |
| 17 | + "InternalError" |
| 18 | + elseif ex.code==-32099 |
| 19 | + "serverErrorStart" |
| 20 | + elseif ex.code==-32000 |
| 21 | + "serverErrorEnd" |
| 22 | + elseif ex.code==-32002 |
| 23 | + "ServerNotInitialized" |
| 24 | + elseif ex.code==-32001 |
| 25 | + "UnknownErrorCode" |
| 26 | + elseif ex.code==-32800 |
| 27 | + "RequestCancelled" |
| 28 | + elseif ex.code==-32801 |
| 29 | + "ContentModified" |
| 30 | + else |
| 31 | + "Unkonwn" |
| 32 | + end |
| 33 | + |
| 34 | + print(io, error_code_as_string) |
| 35 | + print(io, ": ") |
| 36 | + print(io, ex.msg) |
| 37 | + if ex.data!==nothing |
| 38 | + print(io, " (") |
| 39 | + print(io, ex.data) |
| 40 | + print(io, ")") |
| 41 | + end |
| 42 | +end |
| 43 | + |
| 44 | +mutable struct JSONRPCEndpoint |
| 45 | + pipe_in |
| 46 | + pipe_out |
| 47 | + |
| 48 | + out_msg_queue::Channel{Any} |
| 49 | + in_msg_queue::Channel{Any} |
| 50 | + |
| 51 | + outstanding_requests::Dict{String, Channel{Any}} |
| 52 | + |
| 53 | + err_handler::Union{Nothing,Function} |
| 54 | + |
| 55 | + function JSONRPCEndpoint(pipe_in, pipe_out, err_handler=nothing) |
| 56 | + return new(pipe_in, pipe_out, Channel{Any}(Inf), Channel{Any}(Inf), Dict{String, Channel{Any}}(), err_handler) |
| 57 | + end |
| 58 | +end |
| 59 | + |
| 60 | +function write_transport_layer(stream, response) |
| 61 | + response_utf8 = transcode(UInt8, response) |
| 62 | + n = length(response_utf8) |
| 63 | + write(stream, "Content-Length: $n\r\n\r\n") |
| 64 | + write(stream, response_utf8) |
| 65 | +end |
| 66 | + |
| 67 | +function read_transport_layer(stream) |
| 68 | + header_dict = Dict{String,String}() |
| 69 | + line = chomp(readline(stream)) |
| 70 | + # Check whether the socket was closed |
| 71 | + if line == "" |
| 72 | + return nothing |
| 73 | + end |
| 74 | + while length(line) > 0 |
| 75 | + h_parts = split(line, ":") |
| 76 | + header_dict[chomp(h_parts[1])] = chomp(h_parts[2]) |
| 77 | + line = chomp(readline(stream)) |
| 78 | + end |
| 79 | + message_length = parse(Int, header_dict["Content-Length"]) |
| 80 | + message_str = String(read(stream, message_length)) |
| 81 | + return message_str |
| 82 | +end |
| 83 | + |
| 84 | +function Base.run(x::JSONRPCEndpoint) |
| 85 | + @async try |
| 86 | + for msg in x.out_msg_queue |
| 87 | + write_transport_layer(x.pipe_out, msg) |
| 88 | + end |
| 89 | + catch err |
| 90 | + bt = catch_backtrace() |
| 91 | + if x.err_handler!==nothing |
| 92 | + x.err_handler(err, bt) |
| 93 | + else |
| 94 | + Base.display_error(stderr, err, bt) |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + @async try |
| 99 | + while true |
| 100 | + message = read_transport_layer(x.pipe_in) |
| 101 | + |
| 102 | + if message===nothing |
| 103 | + break |
| 104 | + end |
| 105 | + |
| 106 | + message_dict = JSON.parse(message) |
| 107 | + |
| 108 | + if haskey(message_dict, "method") |
| 109 | + put!(x.in_msg_queue, message_dict) |
| 110 | + else |
| 111 | + # This must be a response |
| 112 | + id_of_request = message_dict["id"] |
| 113 | + |
| 114 | + channel_for_response = x.outstanding_requests[id_of_request] |
| 115 | + put!(channel_for_response, message_dict) |
| 116 | + end |
| 117 | + end |
| 118 | + catch err |
| 119 | + bt = catch_backtrace() |
| 120 | + if x.err_handler!==nothing |
| 121 | + x.err_handler(err, bt) |
| 122 | + else |
| 123 | + Base.display_error(stderr, err, bt) |
| 124 | + end |
| 125 | + end |
| 126 | +end |
| 127 | + |
| 128 | +function send_notification(x::JSONRPCEndpoint, method::AbstractString, params) |
| 129 | + message = Dict("jsonrpc"=>"2.0", "method"=>method, "params"=>params) |
| 130 | + |
| 131 | + message_json = JSON.json(message) |
| 132 | + |
| 133 | + put!(x.out_msg_queue, message_json) |
| 134 | +end |
| 135 | + |
| 136 | +function send_request(x::JSONRPCEndpoint, method::AbstractString, params) |
| 137 | + id = string(UUIDs.uuid4()) |
| 138 | + message = Dict("jsonrpc"=>"2.0", "method"=>method, "params"=>params, "id"=>id) |
| 139 | + |
| 140 | + response_channel = Channel{Any}(1) |
| 141 | + x.outstanding_requests[id] = response_channel |
| 142 | + |
| 143 | + message_json = JSON.json(message) |
| 144 | + |
| 145 | + put!(x.out_msg_queue, message_json) |
| 146 | + |
| 147 | + response = take!(response_channel) |
| 148 | + |
| 149 | + if haskey(response, "result") |
| 150 | + return response["result"] |
| 151 | + elseif haskey(response, "error") |
| 152 | + error_code = response["error"]["code"] |
| 153 | + error_msg = response["error"]["message"] |
| 154 | + error_data = get(response["error"], "data", nothing) |
| 155 | + throw(JSONRPCError(error_code, error_msg, error_data)) |
| 156 | + else |
| 157 | + throw(JSONRPCError(0, "ERROR AT THE TRANSPORT LEVEL", nothing)) |
| 158 | + end |
| 159 | +end |
| 160 | + |
| 161 | +function get_next_message(endpoint::JSONRPCEndpoint) |
| 162 | + msg = take!(endpoint.in_msg_queue) |
| 163 | + |
| 164 | + return msg |
| 165 | +end |
| 166 | + |
| 167 | +function send_success_response(endpoint, original_request, result) |
| 168 | + response = Dict("jsonrpc"=>"2.0", "id"=>original_request["id"], "result"=>result) |
| 169 | + |
| 170 | + response_json = JSON.json(response) |
| 171 | + |
| 172 | + put!(endpoint.out_msg_queue, response_json) |
| 173 | +end |
| 174 | + |
| 175 | +function send_error_response(endpoint, original_request, code, message, data) |
| 176 | + response = Dict("jsonrpc"=>"2.0", "id"=>original_request["id"], "error"=>Dict("code"=>code, "message"=>message, "data"=>data)) |
| 177 | + |
| 178 | + response_json = JSON.json(response) |
| 179 | + |
| 180 | + put!(endpoint.out_msg_queue, response_json) |
| 181 | +end |
0 commit comments