From 2323f934a49cce95853f5d73b293ffe5fa2d5dd0 Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 25 Jun 2022 18:51:11 +0200 Subject: [PATCH] Implement shutdown procedure --- fieldpoc/controller.py | 15 +++++---------- fieldpoc/dect.py | 23 +++++++++++++---------- fieldpoc/fieldpoc.py | 14 +++++++++++++- fieldpoc/run.py | 23 +++++++++++++++++++++++ 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/fieldpoc/controller.py b/fieldpoc/controller.py index a1ed9fc..50b61d1 100644 --- a/fieldpoc/controller.py +++ b/fieldpoc/controller.py @@ -21,14 +21,10 @@ class Controller: if data == "help": self.request.sendall("""Availiable commands: help Show this info -stop Shutdown FieldPOC exit Disconnect """.encode("utf-8")) elif data == "quit" or data == "exit": break - elif data == "stop": - self.fp.stop.set() - break else: self.request.sendall("Unknown command, type 'help'\n".encode("utf-8")) @@ -42,10 +38,9 @@ exit Disconnect threading.Thread(target=server.serve_forever).start() - self.fp.stop.wait() + while True: + msg = self.fp.queues["controller"].get() - print("stopping interactive controller") - - server.shutdown() - - print("stopped interactive controller") + if msg.get("type") == "stop": + server.shutdown() + break diff --git a/fieldpoc/dect.py b/fieldpoc/dect.py index fbdb73b..b9aa2eb 100644 --- a/fieldpoc/dect.py +++ b/fieldpoc/dect.py @@ -33,15 +33,18 @@ class Dect: self._init_client() while True: - unbound_devices = self.c.find_devices(lambda d: d.relType == mitel_ommclient2.types.PPRelTypeType("Unbound")) + msg = self.fp.queues["dect"].get() - for d in unbound_devices: - print(d) - temp_num = self.get_temp_number() - u = self.c.create_user(temp_num) - print(u) - self.c.attach_user_device(u.uid, d.ppn) - self.c.set_user_relation_fixed(u.uid) - self.c.set_user_sipauth(u.uid, temp_num, self.get_sip_password_for_number(temp_num)) + if msg.get("type") == "stop": + break + elif msg.get("type") == "sync": + unbound_devices = self.c.find_devices(lambda d: d.relType == mitel_ommclient2.types.PPRelTypeType("Unbound")) - time.sleep(2) + for d in unbound_devices: + print(d) + temp_num = self.get_temp_number() + u = self.c.create_user(temp_num) + print(u) + self.c.attach_user_device(u.uid, d.ppn) + self.c.set_user_relation_fixed(u.uid) + self.c.set_user_sipauth(u.uid, temp_num, self.get_sip_password_for_number(temp_num)) diff --git a/fieldpoc/fieldpoc.py b/fieldpoc/fieldpoc.py index 11a7307..dc10ca3 100644 --- a/fieldpoc/fieldpoc.py +++ b/fieldpoc/fieldpoc.py @@ -2,6 +2,7 @@ import json import pathlib +import queue import threading from . import config @@ -18,11 +19,22 @@ class FieldPOC: self.extensions_file_path = pathlib.Path(extensions_file_path) self._load_extensions() - self.stop = threading.Event() + self.queues = { + "controller": queue.Queue(), + "dect": queue.Queue(), + } self._controller = controller.Controller(self) self._dect = dect.Dect(self) + def queue_all(self, msg): + """ + Send message to every queue + """ + + for queue in self.queues.values(): + queue.put(msg) + def run(self): self._controller_thread = threading.Thread(target=self._controller.run) self._controller_thread.start() diff --git a/fieldpoc/run.py b/fieldpoc/run.py index f021ac1..b8d1811 100644 --- a/fieldpoc/run.py +++ b/fieldpoc/run.py @@ -1,6 +1,10 @@ #!/usr/bin/env python3 import argparse +import selectors +import signal +import socket + from . import fieldpoc ap = argparse.ArgumentParser(prog="fieldpoc") @@ -8,9 +12,28 @@ ap.add_argument("-c", "--config", dest="config_file_path", default="fieldpoc_con ap.add_argument("-e", "--extensions", dest="extensions_file_path", default="fieldpoc_extensions.json", help="Path to the fieldpoc extensions file") def run(): + sel = selectors.DefaultSelector() + + interrupt_stop_read, interrupt_stop_write = socket.socketpair() + signal.signal(signal.SIGINT, lambda signum, frame: interrupt_stop_write.send(b'\0')) # Keyboard Interrupt + sel.register(interrupt_stop_read, selectors.EVENT_READ) + + interrupt_reload_read, interrupt_reload_write = socket.socketpair() + signal.signal(signal.SIGHUP, lambda signum, frame: interrupt_reload_write.send(b'\0')) + sel.register(interrupt_reload_read, selectors.EVENT_READ) + args = ap.parse_args() fp = fieldpoc.FieldPOC(**args.__dict__) fp.run() + while True: + for key, mask in sel.select(): + key.fileobj.recv(1) + if key.fileobj == interrupt_stop_read: + fp.queue_all({"type": "stop"}) + exit() + elif key.fileobj == interrupt_reload_read: + fp.queue_all({"type": "reload"}) + if __name__ == "__main__": run()