|
| 1 | +from __future__ import print_function |
| 2 | +import json |
| 3 | +from pylspclient import lsp_structs |
| 4 | +import threading |
| 5 | + |
| 6 | +JSON_RPC_REQ_FORMAT = "Content-Length: {json_string_len}\r\n\r\n{json_string}" |
| 7 | +LEN_HEADER = "Content-Length: " |
| 8 | +TYPE_HEADER = "Content-Type: " |
| 9 | + |
| 10 | + |
| 11 | +# TODO: add content-type |
| 12 | + |
| 13 | + |
| 14 | +class MyEncoder(json.JSONEncoder): |
| 15 | + """ |
| 16 | + Encodes an object in JSON |
| 17 | + """ |
| 18 | + def default(self, o): # pylint: disable=E0202 |
| 19 | + return o.__dict__ |
| 20 | + |
| 21 | + |
| 22 | +class JsonRpcEndpoint(object): |
| 23 | + ''' |
| 24 | + Thread safe JSON RPC endpoint implementation. Responsible to recieve and send JSON RPC messages, as described in the |
| 25 | + protocol. More information can be found: https://www.jsonrpc.org/ |
| 26 | + ''' |
| 27 | + def __init__(self, stdin, stdout): |
| 28 | + self.stdin = stdin |
| 29 | + self.stdout = stdout |
| 30 | + self.read_lock = threading.Lock() |
| 31 | + self.write_lock = threading.Lock() |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def __add_header(json_string): |
| 35 | + ''' |
| 36 | + Adds a header for the given json string |
| 37 | + |
| 38 | + :param str json_string: The string |
| 39 | + :return: the string with the header |
| 40 | + ''' |
| 41 | + return JSON_RPC_REQ_FORMAT.format(json_string_len=len(json_string), json_string=json_string) |
| 42 | + |
| 43 | + |
| 44 | + def send_request(self, message): |
| 45 | + ''' |
| 46 | + Sends the given message. |
| 47 | +
|
| 48 | + :param dict message: The message to send. |
| 49 | + ''' |
| 50 | + json_string = json.dumps(message, cls=MyEncoder) |
| 51 | + jsonrpc_req = self.__add_header(json_string) |
| 52 | + with self.write_lock: |
| 53 | + self.stdin.write(jsonrpc_req.encode()) |
| 54 | + self.stdin.flush() |
| 55 | + |
| 56 | + |
| 57 | + def recv_response(self): |
| 58 | + ''' |
| 59 | + Recives a message. |
| 60 | +
|
| 61 | + :return: a message |
| 62 | + ''' |
| 63 | + with self.read_lock: |
| 64 | + message_size = None |
| 65 | + while True: |
| 66 | + #read header |
| 67 | + line = self.stdout.readline() |
| 68 | + if not line: |
| 69 | + # server quit |
| 70 | + return None |
| 71 | + line = line.decode("utf-8") |
| 72 | + if not line.endswith("\r\n"): |
| 73 | + raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.ParseError, "Bad header: missing newline") |
| 74 | + #remove the "\r\n" |
| 75 | + line = line[:-2] |
| 76 | + if line == "": |
| 77 | + # done with the headers |
| 78 | + break |
| 79 | + elif line.startswith(LEN_HEADER): |
| 80 | + line = line[len(LEN_HEADER):] |
| 81 | + if not line.isdigit(): |
| 82 | + raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.ParseError, "Bad header: size is not int") |
| 83 | + message_size = int(line) |
| 84 | + elif line.startswith(TYPE_HEADER): |
| 85 | + # nothing todo with type for now. |
| 86 | + pass |
| 87 | + else: |
| 88 | + raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.ParseError, "Bad header: unkown header") |
| 89 | + if not message_size: |
| 90 | + raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.ParseError, "Bad header: missing size") |
| 91 | + |
| 92 | + jsonrpc_res = self.stdout.read(message_size).decode("utf-8") |
| 93 | + return json.loads(jsonrpc_res) |
0 commit comments