diff --git a/pipe_io_server.py b/pipe_io_server.py index 9316c50..accf535 100644 --- a/pipe_io_server.py +++ b/pipe_io_server.py @@ -5,6 +5,7 @@ from os.path import exists, join from signal import signal, SIGTERM, SIGINT from secrets import token_urlsafe from collections import namedtuple +from abc import ABC, abstractmethod class PipeWriterThread(Thread): @@ -66,7 +67,7 @@ class PipeHandler: remove(pipe_path) -class PipeIOServer: +class PipeIOServer(ABC): def __init__(self, in_pipe=None, out_pipe=None): self.in_pipe, self.out_pipe = in_pipe, out_pipe self._create_pipes() @@ -85,7 +86,11 @@ class PipeIOServer: self.out_pipe = join('/tmp', f'out_pipe_{pipe_id}') PipeHandler(self.in_pipe, self.out_pipe).recreate() + @abstractmethod def _handle_message(self, message): + raise NotImplementedError() + + def send(self, message): self._io_threads.writer.write(message) def run(self): @@ -98,8 +103,13 @@ class PipeIOServer: PipeHandler(self.in_pipe, self.out_pipe).remove() +class EchoPipeIOServer(PipeIOServer): + def _handle_message(self, message): + self.send(message) + + if __name__ == "__main__": - pipe_io = PipeIOServer() + pipe_io = EchoPipeIOServer() signal(SIGTERM, lambda a, b: pipe_io.stop()) signal(SIGINT, lambda a, b: pipe_io.stop()) pipe_io.run()