Skip to content

Commit fc3c6ce

Browse files
committed
Add termination handler support
Add support for yate clients to handle the situation that yate has closed the connection to us. Make callgen script make use of this and terminate upon yate connection closing.
1 parent 72d605d commit fc3c6ce

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

yate/asyncio.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def __init__(self, host=None, port=None, sockpath=None):
2020
self.writer = None
2121
self.main_task = None
2222
self._automatic_bufsize = False
23+
self._termination_handler = None
2324

2425
if host is not None:
2526
self.mode = self.MODE_TCP
@@ -34,6 +35,9 @@ def __init__(self, host=None, port=None, sockpath=None):
3435
def run(self, application_main):
3536
asyncio.run(self._amain(application_main))
3637

38+
def set_termination_handler(self, termination_handler):
39+
self._termination_handler = termination_handler
40+
3741
async def _amain(self, application_main):
3842
if self.mode == self.MODE_STDIO:
3943
await self.setup_for_stdio()
@@ -108,7 +112,8 @@ def deferred_msg_write(_param, _value, _success):
108112
logger.debug("> %s", repr(msg))
109113

110114
async def _yate_stream_closed(self):
111-
pass
115+
if self._termination_handler is not None:
116+
self._termination_handler()
112117

113118
async def drain(self):
114119
await self.writer.drain()

yate/callgen.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def __init__(self, port, sounds_directory, bind_global=False):
2929

3030
self.active_calls = {}
3131
self.yate = YateAsync("127.0.0.1", port)
32+
self.yate.set_termination_handler(self.termination_handler)
3233
self.sounds_directory = sounds_directory
3334

3435
self.web_app = web.Application()
@@ -71,6 +72,12 @@ async def application_main(self, yate):
7172
def shutdown(self):
7273
self.shutdown_future.set_result(True)
7374

75+
@staticmethod
76+
def termination_handler():
77+
logging.info("Yate has closed the connection. Terminating application.")
78+
os._exit(1)
79+
80+
7481
async def web_call_handler(self, request):
7582
params = await request.post()
7683

0 commit comments

Comments
 (0)