Implement shutdown procedure
This commit is contained in:
parent
ae9d73659a
commit
2323f934a4
@ -21,14 +21,10 @@ class Controller:
|
|||||||
if data == "help":
|
if data == "help":
|
||||||
self.request.sendall("""Availiable commands:
|
self.request.sendall("""Availiable commands:
|
||||||
help Show this info
|
help Show this info
|
||||||
stop Shutdown FieldPOC
|
|
||||||
exit Disconnect
|
exit Disconnect
|
||||||
""".encode("utf-8"))
|
""".encode("utf-8"))
|
||||||
elif data == "quit" or data == "exit":
|
elif data == "quit" or data == "exit":
|
||||||
break
|
break
|
||||||
elif data == "stop":
|
|
||||||
self.fp.stop.set()
|
|
||||||
break
|
|
||||||
else:
|
else:
|
||||||
self.request.sendall("Unknown command, type 'help'\n".encode("utf-8"))
|
self.request.sendall("Unknown command, type 'help'\n".encode("utf-8"))
|
||||||
|
|
||||||
@ -42,10 +38,9 @@ exit Disconnect
|
|||||||
|
|
||||||
threading.Thread(target=server.serve_forever).start()
|
threading.Thread(target=server.serve_forever).start()
|
||||||
|
|
||||||
self.fp.stop.wait()
|
while True:
|
||||||
|
msg = self.fp.queues["controller"].get()
|
||||||
|
|
||||||
print("stopping interactive controller")
|
if msg.get("type") == "stop":
|
||||||
|
server.shutdown()
|
||||||
server.shutdown()
|
break
|
||||||
|
|
||||||
print("stopped interactive controller")
|
|
||||||
|
@ -33,15 +33,18 @@ class Dect:
|
|||||||
self._init_client()
|
self._init_client()
|
||||||
|
|
||||||
while True:
|
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:
|
if msg.get("type") == "stop":
|
||||||
print(d)
|
break
|
||||||
temp_num = self.get_temp_number()
|
elif msg.get("type") == "sync":
|
||||||
u = self.c.create_user(temp_num)
|
unbound_devices = self.c.find_devices(lambda d: d.relType == mitel_ommclient2.types.PPRelTypeType("Unbound"))
|
||||||
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))
|
|
||||||
|
|
||||||
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))
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
import pathlib
|
import pathlib
|
||||||
|
import queue
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from . import config
|
from . import config
|
||||||
@ -18,11 +19,22 @@ class FieldPOC:
|
|||||||
self.extensions_file_path = pathlib.Path(extensions_file_path)
|
self.extensions_file_path = pathlib.Path(extensions_file_path)
|
||||||
self._load_extensions()
|
self._load_extensions()
|
||||||
|
|
||||||
self.stop = threading.Event()
|
self.queues = {
|
||||||
|
"controller": queue.Queue(),
|
||||||
|
"dect": queue.Queue(),
|
||||||
|
}
|
||||||
|
|
||||||
self._controller = controller.Controller(self)
|
self._controller = controller.Controller(self)
|
||||||
self._dect = dect.Dect(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):
|
def run(self):
|
||||||
self._controller_thread = threading.Thread(target=self._controller.run)
|
self._controller_thread = threading.Thread(target=self._controller.run)
|
||||||
self._controller_thread.start()
|
self._controller_thread.start()
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import selectors
|
||||||
|
import signal
|
||||||
|
import socket
|
||||||
|
|
||||||
from . import fieldpoc
|
from . import fieldpoc
|
||||||
|
|
||||||
ap = argparse.ArgumentParser(prog="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")
|
ap.add_argument("-e", "--extensions", dest="extensions_file_path", default="fieldpoc_extensions.json", help="Path to the fieldpoc extensions file")
|
||||||
|
|
||||||
def run():
|
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()
|
args = ap.parse_args()
|
||||||
fp = fieldpoc.FieldPOC(**args.__dict__)
|
fp = fieldpoc.FieldPOC(**args.__dict__)
|
||||||
fp.run()
|
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__":
|
if __name__ == "__main__":
|
||||||
run()
|
run()
|
||||||
|
Loading…
Reference in New Issue
Block a user