Commit 334a148f authored by Kirill Smelkov's avatar Kirill Smelkov

tmcast.py: Handle SIGINT/SIGTERM

See nxdtest@b0cf277d for details
and for why signal handling is implemented like this.
parent 2805de89
...@@ -12,8 +12,9 @@ from socket import socket, AF_INET, AF_INET6, SOCK_DGRAM, IPPROTO_UDP, ...@@ -12,8 +12,9 @@ from socket import socket, AF_INET, AF_INET6, SOCK_DGRAM, IPPROTO_UDP,
import socket as net import socket as net
from struct import pack from struct import pack
from golang import b, u from golang import b, u, go, func, defer, chan
from golang import sync, context from golang import os as gos, sync, context, syscall
from golang.os import signal
group4 = '224.1.1.1' group4 = '224.1.1.1'
...@@ -94,6 +95,7 @@ def rxloop(ctx, sk): ...@@ -94,6 +95,7 @@ def rxloop(ctx, sk):
print("rx: %s" % u(pkt)) print("rx: %s" % u(pkt))
@func
def main(): def main():
action = sys.argv[1] action = sys.argv[1]
...@@ -107,7 +109,24 @@ def main(): ...@@ -107,7 +109,24 @@ def main():
if G is None: if G is None:
raise RuntimeError("4/6 must be specified") raise RuntimeError("4/6 must be specified")
wg = sync.WorkGroup(context.background()) # prepare ctx that is canceled on SIGINT/SIGTERM
ctx, cancel = context.with_cancel(context.background())
sigq = chan(1, dtype=gos.Signal)
signal.Notify(sigq, syscall.SIGINT, syscall.SIGTERM)
def _():
signal.Stop(sigq)
sigq.close()
defer(_)
def _():
sig, ok = sigq.recv_()
if not ok:
return
print("# %s" % sig)
cancel()
go(_)
defer(cancel)
wg = sync.WorkGroup(ctx)
if "tx" in action: if "tx" in action:
sktx = mjoin_tx(*G) sktx = mjoin_tx(*G)
wg.go(txloop, sktx, b(sys.argv[2])) wg.go(txloop, sktx, b(sys.argv[2]))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment