Implement shutdown procedure

This commit is contained in:
clerie 2022-06-25 18:51:11 +02:00
parent ae9d73659a
commit 2323f934a4
4 changed files with 54 additions and 21 deletions

View File

@ -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

View File

@ -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))

View File

@ -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()

View File

@ -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()