Initial wiring of client calls and responses

This commit is contained in:
2024-09-14 01:16:47 -05:00
parent 13b126ef6e
commit e0664123da
9 changed files with 294 additions and 51 deletions

View File

@@ -27,7 +27,7 @@ class ClientRequest(object):
:param int id: Message id to track instance.
:param str method: The type of lsp request being made.
:param dict params: The arguments of the given method.
:param dict params: The arguments of the given method.
"""
self.jsonrpc = "2.0"
self.id = id
@@ -41,24 +41,42 @@ class ClientNotification(object):
Constructs a new Client Notification instance.
:param str method: The type of lsp notification being made.
:param dict params: The arguments of the given method.
:param dict params: The arguments of the given method.
"""
self.jsonrpc = "2.0"
self.method = method
self.params = params
@dataclass
class LSPResponse(object):
class LSPResponseRequest(object):
"""
Constructs a new LSP Response instance.
Constructs a new LSP Response Request instance.
:param str method: The type of lsp notification being made.
:param id result: The id of the given message.
:param dict result: The arguments of the given method.
"""
jsonrpc: str
id: int or None
result: {}
id: int
result: dict
@dataclass
class LSPResponseNotification(object):
"""
Constructs a new LSP Response Notification instance.
:param str method: The type of lsp notification being made.
:params dict result: The arguments of the given method.
"""
jsonrpc: str
method: str
params: dict
class MessageTypes(ClientRequest, ClientNotification, LSPResponse):
...
class MessageTypes(ClientRequest, ClientNotification, LSPResponseRequest, LSPResponseNotification):
...
class ClientMessageTypes(ClientRequest, ClientNotification):
...
class LSPResponseTypes(LSPResponseRequest, LSPResponseNotification):
...

View File

@@ -40,6 +40,19 @@ content_part = {
}
}
didopen_notification = {
"method": "textDocument/didOpen",
"params": {
"textDocument": {
"uri": "file://<path>",
"languageId": "python3",
"version": 1,
"text": ""
}
}
}
definition_query = {
"method": "textDocument/definition",

View File

@@ -76,6 +76,9 @@ class IPCServer(Singleton):
if file:
event_system.emit("handle-file-from-ipc", file)
conn.close()
break
if "DIR|" in msg:
file = msg.split("DIR|")[1].strip()
if file:
@@ -129,4 +132,4 @@ class IPCServer(Singleton):
logger.error("IPC Socket no longer valid.... Removing.")
os.unlink(self._ipc_address)
except Exception as e:
logger.error( repr(e) )
logger.error( repr(e) )

View File

@@ -0,0 +1,148 @@
# Python imports
import os
import threading
import time
import json
import base64
from multiprocessing.connection import Client
from multiprocessing.connection import Listener
# Lib imports
# Application imports
from .singleton import Singleton
class LSPEndpointServer(Singleton):
""" Create a listener so that LSP Clients can communicate to this instances and get responses back. """
def __init__(self, ipc_address: str = '127.0.0.1', conn_type: str = "socket"):
self.is_ipc_alive = False
self._ipc_port = 4848
self._ipc_address = ipc_address
self._conn_type = conn_type
self._ipc_authkey = b'' + bytes(f'lsp-manager-endpoint-ipc', 'utf-8')
self._client_ipc_authkey = b'' + bytes(f'lsp-client-endpoint-ipc', 'utf-8')
self._ipc_timeout = 15.0
if conn_type == "socket":
self._ipc_address = f'/tmp/lsp-manager-endpoint-ipc.sock'
self._client_ipc_address = f'/tmp/lsp-client-endpoint-ipc.sock'
elif conn_type == "full_network":
self._ipc_address = '0.0.0.0'
elif conn_type == "full_network_unsecured":
self._ipc_authkey = None
self._ipc_address = '0.0.0.0'
elif conn_type == "local_network_unsecured":
self._ipc_authkey = None
self._subscribe_to_events()
def _subscribe_to_events(self):
event_system.subscribe("respond-to-client", self.send_client_ipc_message)
def create_ipc_listener(self) -> None:
if self._conn_type == "socket":
if os.path.exists(self._ipc_address) and settings_manager.is_dirty_start():
os.unlink(self._ipc_address)
listener = Listener(address=self._ipc_address, family="AF_UNIX", authkey=self._ipc_authkey)
elif "unsecured" not in self._conn_type:
listener = Listener((self._ipc_address, self._ipc_port), authkey=self._ipc_authkey)
else:
listener = Listener((self._ipc_address, self._ipc_port))
self.is_ipc_alive = True
self._run_ipc_loop(listener)
@daemon_threaded
def _run_ipc_loop(self, listener) -> None:
# NOTE: Not thread safe if using with Gtk. Need to import GLib and use idle_add
while True:
try:
conn = listener.accept()
start_time = time.perf_counter()
self._handle_ipc_message(conn, start_time)
except Exception as e:
logger.debug( repr(e) )
listener.close()
def _handle_ipc_message(self, conn, start_time) -> None:
while True:
msg = conn.recv()
logger.debug(msg)
if "CLIENT|" in msg:
data = msg.split("CLIENT|")[1].strip()
if data:
data_str = base64.b64decode(data.encode("utf-8")).decode("utf-8")
json_blob = json.loads(data_str)
event_system.emit(json_blob["method"], (json_blob,))
conn.close()
break
if msg in ['close connection', 'close server']:
conn.close()
break
# NOTE: Not perfect but insures we don't lock up the connection for too long.
end_time = time.perf_counter()
if (end_time - start_time) > self._ipc_timeout:
conn.close()
break
def send_client_ipc_message(self, message: str = "Empty Data...") -> None:
try:
if self._conn_type == "socket":
conn = Client(address=self._client_ipc_address, family="AF_UNIX", authkey=self._client_ipc_authkey)
elif "unsecured" not in self._conn_type:
conn = Client((self._ipc_address, self._ipc_port), authkey=self._ipc_authkey)
else:
conn = Client((self._ipc_address, self._ipc_port))
conn.send( f"MANAGER|{ base64.b64encode(message.encode("utf-8")).decode("utf-8") }" )
conn.close()
except ConnectionRefusedError as e:
logger.error("Connection refused...")
except Exception as e:
logger.error( repr(e) )
def send_ipc_message(self, message: str = "Empty Data...") -> None:
try:
if self._conn_type == "socket":
conn = Client(address=self._ipc_address, family="AF_UNIX", authkey=self._ipc_authkey)
elif "unsecured" not in self._conn_type:
conn = Client((self._ipc_address, self._ipc_port), authkey=self._ipc_authkey)
else:
conn = Client((self._ipc_address, self._ipc_port))
conn.send(message)
conn.close()
except ConnectionRefusedError as e:
logger.error("Connection refused...")
except Exception as e:
logger.error( repr(e) )
def send_test_ipc_message(self, message: str = "Empty Data...") -> None:
try:
if self._conn_type == "socket":
conn = Client(address=self._ipc_address, family="AF_UNIX", authkey=self._ipc_authkey)
elif "unsecured" not in self._conn_type:
conn = Client((self._ipc_address, self._ipc_port), authkey=self._ipc_authkey)
else:
conn = Client((self._ipc_address, self._ipc_port))
conn.send(message)
conn.close()
except ConnectionRefusedError as e:
if self._conn_type == "socket":
logger.error("LSP Socket no longer valid.... Removing.")
os.unlink(self._ipc_address)
except Exception as e:
logger.error( repr(e) )