def run(self): while not self.shutdown_flag: try: jsonrpc_message = self.json_rpc_endpoint.recv_response() if jsonrpc_message is None: print("server quit") break method = jsonrpc_message.get("method") result = jsonrpc_message.get("result") error = jsonrpc_message.get("error") rpc_id = jsonrpc_message.get("id") params = jsonrpc_message.get("params") if method: if rpc_id: # a call for method if method not in self.method_callbacks: raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.MethodNotFound, "Method not found: {method}".format(method=method)) result = self.method_callbacks[method](params) self.send_response(rpc_id, result, None) else: # a call for notify if method not in self.notify_callbacks: # Have nothing to do with this. print("Notify method not found: {method}.".format(method=method)) else: self.notify_callbacks[method](params) else: self.handle_result(rpc_id, result, error) except lsp_structs.ResponseError as e: self.send_response(rpc_id, None, e)
def recv_response(self): ''' Recives a message. :return: a message ''' with self.read_lock: message_size = None while True: #read header line = self.stdout.readline() if not line: # server quit return None line = line.decode("utf-8") if not line.endswith("\r\n"): raise lsp_structs.ResponseError( lsp_structs.ErrorCodes.ParseError, "Bad header: missing newline") #remove the "\r\n" line = line[:-2] if line == "": # done with the headers break elif line.startswith(LEN_HEADER): line = line[len(LEN_HEADER):] if not line.isdigit(): raise lsp_structs.ResponseError( lsp_structs.ErrorCodes.ParseError, "Bad header: size is not int") message_size = int(line) elif line.startswith(TYPE_HEADER): # nothing todo with type for now. pass else: raise lsp_structs.ResponseError( lsp_structs.ErrorCodes.ParseError, "Bad header: unkown header") if not message_size: raise lsp_structs.ResponseError( lsp_structs.ErrorCodes.ParseError, "Bad header: missing size") jsonrpc_res = self.stdout.read(message_size).decode("utf-8") return json.loads(jsonrpc_res)
def call_method(self, method_name, **kwargs): current_id = self.next_id self.next_id += 1 cond = threading.Condition() self.event_dict[current_id] = cond cond.acquire() self.send_message(method_name, kwargs, current_id) cond.wait() cond.release() result, error = self.response_dict[current_id] if error: raise lsp_structs.ResponseError(error.get("code"), error.get("message"), error.get("data")) return result
def run(self): while not self.shutdown_flag: try: jsonrpc_message = self.json_rpc_endpoint.recv_response() #print(type(jsonrpc_message)) # it's a dict if jsonrpc_message is None: print("server quit") break print("received message:\n", jsonrpc_message) method = jsonrpc_message.get("method") result = jsonrpc_message.get("result") error = jsonrpc_message.get("error") rpc_id = jsonrpc_message.get("id") params = jsonrpc_message.get("params") if method: if rpc_id: # a call for method if method not in self.method_callbacks: raise lsp_structs.ResponseError(lsp_structs.ErrorCodes.MethodNotFound, "Client->Method not found: {method}".format(method=method)) result = self.method_callbacks[method](params) self.send_response(rpc_id, result, None) else: # a call for notify if method == "textDocument/publishDiagnostics": # The if/else below is just debugging and could be removed if params.get('diagnostics'): print(f"Diagnostic message: {params.get('diagnostics')[0].get('message')}\n") start = params.get("diagnostics")[0].get("range").get("start") print(f"Diagnostic range start: line: {start.get('line')}; char: {start.get('character')}\n") #self.socket.send_string(f"Diagnostic range start: line: {start.get('line')}; char: {start.get('character')}") #this works #self.socket.send_json(params.get("diagnostics")) else: print("There were no errors\n") # could just send params.get("diagnostics") since that is [] #self.socket.send_json([]) self.socket.send_json(params.get("diagnostics")) #the main event if method not in self.notify_callbacks: # Have nothing to do with this. print("Client->Notify method not found: {method}.".format(method=method)) else: self.notify_callbacks[method](params) else: self.handle_result(rpc_id, result, error) except lsp_structs.ResponseError as e: self.send_response(rpc_id, None, e)
def call_method(self, method_name, **kwargs): current_id = self.next_id self.next_id += 1 cond = threading.Condition() self.event_dict[current_id] = cond cond.acquire() self.send_message(method_name, kwargs, current_id) if self.shutdown_flag: return None if not cond.wait(timeout=self._timeout): raise TimeoutError() cond.release() self.event_dict.pop(current_id) result, error = self.response_dict.pop(current_id) if error: raise lsp_structs.ResponseError(error.get("code"), error.get("message"), error.get("data")) return result