Skip to content

Commit

Permalink
fix emit_message mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
marios8543 committed Oct 31, 2023
1 parent e4b1efc commit 7565a66
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions backend/src/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
Plugins = dict[str, PluginWrapper]
ReloadQueue = Queue[Tuple[str, str, bool | None] | Tuple[str, str]]

#TODO: Remove placeholder method
async def log_plugin_emitted_message(message: Any):
getLogger().debug(f"EMITTED MESSAGE: " + str(message))

class FileChangeHandler(RegexMatchingEventHandler):
def __init__(self, queue: ReloadQueue, plugin_path: str) -> None:
super().__init__(regexes=[r'^.*?dist\/index\.js$', r'^.*?main\.py$']) # type: ignore
Expand Down Expand Up @@ -143,6 +147,7 @@ def import_plugin(self, file: str, plugin_directory: str, refresh: bool | None =
if plugin.passive:
self.logger.info(f"Plugin {plugin.name} is passive")
self.plugins[plugin.name] = plugin.start()
self.plugins[plugin.name].set_emitted_message_callback(log_plugin_emitted_message)
self.logger.info(f"Loaded {plugin.name}")
if not batch:
self.loop.create_task(self.dispatch_plugin(plugin.name, plugin.version))
Expand Down
7 changes: 7 additions & 0 deletions backend/src/localplatform/localsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, on_new_message: Callable[[str], Coroutine[Any, Any, Any]]):
self.socket = None
self.reader = None
self.writer = None
self.server_writer = None

async def setup_server(self):
self.socket = await asyncio.start_unix_server(self._listen_for_method_call, path=self.socket_addr, limit=BUFFER_LIMIT)
Expand Down Expand Up @@ -90,8 +91,14 @@ async def _write_single_line(self, writer: asyncio.StreamWriter, message : str):

writer.write(message.encode("utf-8"))
await writer.drain()

async def write_single_line_server(self, message: str):
if self.server_writer is None:
return
await self._write_single_line(self.server_writer, message)

async def _listen_for_method_call(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
self.server_writer = writer
while True:

def _(task: asyncio.Task[str|None]):
Expand Down
4 changes: 2 additions & 2 deletions backend/src/plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ async def _response_listener(self):
res = loads(line)
if res["id"] == "0":
create_task(self.emitted_message_callback(res["payload"]))
return
self._method_call_requests.pop(res["id"]).set_result(res)
else:
self._method_call_requests.pop(res["id"]).set_result(res)
except:
pass

Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugin/sandboxed_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async def on_new_message(self, message : str) -> str|None:
return dumps(d, ensure_ascii=False)

async def emit_message(self, message: Dict[Any, Any]):
await self._socket.write_single_line(dumps({
await self._socket.write_single_line_server(dumps({
"id": "0",
"payload": message
}))

0 comments on commit 7565a66

Please sign in to comment.