mirror of
https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git
synced 2025-01-27 11:28:11 +00:00
signalling: Use logging to log info instead of prints
This commit is contained in:
parent
57f459f43b
commit
80e3acda93
1 changed files with 21 additions and 17 deletions
|
@ -22,8 +22,6 @@ from collections import defaultdict
|
|||
|
||||
from concurrent.futures._base import TimeoutError
|
||||
|
||||
import logging
|
||||
|
||||
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||
parser.add_argument('--addr', default='0.0.0.0', help='Address to listen on')
|
||||
parser.add_argument('--port', default=8443, type=int, help='Port to listen on')
|
||||
|
@ -35,6 +33,13 @@ options = parser.parse_args(sys.argv[1:])
|
|||
ADDR_PORT = (options.addr, options.port)
|
||||
KEEPALIVE_TIMEOUT = options.keepalive_timeout
|
||||
|
||||
logger = logging.getLogger('webrtc.signalling')
|
||||
handler = logging.StreamHandler(sys.stderr)
|
||||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
handler.setFormatter(formatter)
|
||||
logger.setLevel(logging.INFO)
|
||||
logger.addHandler(handler)
|
||||
|
||||
############### Global data ###############
|
||||
|
||||
# Format: {uid: (Peer WebSocketServerProtocol,
|
||||
|
@ -62,7 +67,7 @@ async def recv_msg_ping(ws, raddr):
|
|||
try:
|
||||
msg = await asyncio.wait_for(ws.recv(), KEEPALIVE_TIMEOUT)
|
||||
except (asyncio.TimeoutError, concurrent.futures._base.TimeoutError):
|
||||
print('Sending keepalive ping to {!r} in recv'.format(raddr))
|
||||
logger.debug('Sending keepalive ping to {!r} in recv'.format(raddr))
|
||||
await ws.ping()
|
||||
return msg
|
||||
|
||||
|
@ -70,7 +75,7 @@ async def cleanup_session(uid):
|
|||
for other_id in sessions[uid]:
|
||||
await peers[other_id][0].send('END_SESSION {}'.format(uid))
|
||||
sessions[other_id].remove(uid)
|
||||
print("Cleaned up {} -> {} session".format(uid, other_id))
|
||||
logger.info("Cleaned up {} -> {} session".format(uid, other_id))
|
||||
del sessions[uid]
|
||||
|
||||
async def end_session(uid, other_id):
|
||||
|
@ -93,7 +98,7 @@ async def remove_peer(uid):
|
|||
ws, raddr, status = peers[uid]
|
||||
del peers[uid]
|
||||
await ws.close()
|
||||
print("Disconnected from peer {!r} at {!r}".format(uid, raddr))
|
||||
logger.info("Disconnected from peer {!r} at {!r}".format(uid, raddr))
|
||||
|
||||
if uid in producers:
|
||||
for peer_id in listeners:
|
||||
|
@ -113,7 +118,7 @@ async def connection_handler(ws, uid):
|
|||
raddr = ws.remote_address
|
||||
peers_status = []
|
||||
peers[uid] = [ws, raddr, peers_status]
|
||||
print("Registered peer {!r} at {!r}".format(uid, raddr))
|
||||
logger.info("Registered peer {!r} at {!r}".format(uid, raddr))
|
||||
while True:
|
||||
# Receive command, wait forever if necessary
|
||||
msg = await recv_msg_ping(ws, raddr)
|
||||
|
@ -121,14 +126,14 @@ async def connection_handler(ws, uid):
|
|||
peers_status = peers[uid][2]
|
||||
# Requested a session with a specific peer
|
||||
if msg.startswith('START_SESSION '):
|
||||
print("{!r} command {!r}".format(uid, msg))
|
||||
logger.info("{!r} command {!r}".format(uid, msg))
|
||||
_, callee_id = msg.split(maxsplit=1)
|
||||
if callee_id not in peers:
|
||||
await ws.send('ERROR peer {!r} not found'.format(callee_id))
|
||||
continue
|
||||
wsc = peers[callee_id][0]
|
||||
await wsc.send('START_SESSION {}'.format(uid))
|
||||
print('Session from {!r} ({!r}) to {!r} ({!r})'
|
||||
logger.info('Session from {!r} ({!r}) to {!r} ({!r})'
|
||||
''.format(uid, raddr, callee_id, wsc.remote_address))
|
||||
# Register session
|
||||
peers[uid][2] = peer_status = 'session'
|
||||
|
@ -153,7 +158,7 @@ async def connection_handler(ws, uid):
|
|||
|
||||
msg['peer-id'] = uid
|
||||
msg = json.dumps(msg)
|
||||
print("{} -> {}: {}".format(uid, other_id, msg))
|
||||
logger.debug("Got peer: {} -> {}: {}".format(uid, other_id, msg))
|
||||
await wso.send(msg)
|
||||
|
||||
async def register_peer(ws):
|
||||
|
@ -184,7 +189,7 @@ async def handler(ws, path):
|
|||
All incoming messages are handled here. @path is unused.
|
||||
'''
|
||||
raddr = ws.remote_address
|
||||
print("Connected to {!r}".format(raddr))
|
||||
logger.info("Connected to {!r}".format(raddr))
|
||||
try:
|
||||
typ, peer_id = await register_peer(ws)
|
||||
except:
|
||||
|
@ -201,7 +206,7 @@ async def handler(ws, path):
|
|||
try:
|
||||
await connection_handler(ws, peer_id)
|
||||
except websockets.ConnectionClosed:
|
||||
print("Connection to peer {!r} closed, exiting handler".format(raddr))
|
||||
logger.info("Connection to peer {!r} closed, exiting handler".format(raddr))
|
||||
finally:
|
||||
await remove_peer(peer_id)
|
||||
|
||||
|
@ -217,13 +222,13 @@ else:
|
|||
try:
|
||||
sslctx.load_cert_chain(chain_pem, keyfile=key_pem)
|
||||
except FileNotFoundError:
|
||||
print("Certificates not found, did you run generate_cert.sh?")
|
||||
logger.error("Certificates not found, did you run generate_cert.sh?")
|
||||
sys.exit(1)
|
||||
# FIXME
|
||||
sslctx.check_hostname = False
|
||||
sslctx.verify_mode = ssl.CERT_NONE
|
||||
|
||||
print("Listening on wss://{}:{}".format(*ADDR_PORT))
|
||||
logger.info("Listening on wss://{}:{}".format(*ADDR_PORT))
|
||||
# Websocket server
|
||||
wsd = websockets.serve(handler, *ADDR_PORT, ssl=sslctx,
|
||||
# Maximum number of messages that websockets will pop
|
||||
|
@ -231,10 +236,9 @@ wsd = websockets.serve(handler, *ADDR_PORT, ssl=sslctx,
|
|||
# https://websockets.readthedocs.io/en/stable/api.html#websockets.protocol.WebSocketCommonProtocol
|
||||
max_queue=16)
|
||||
|
||||
logger = logging.getLogger('websockets.server')
|
||||
|
||||
logger.setLevel(logging.ERROR)
|
||||
logger.addHandler(logging.StreamHandler())
|
||||
ws_logger = logging.getLogger('websockets.server')
|
||||
ws_logger.setLevel(logging.ERROR)
|
||||
ws_logger.addHandler(logging.StreamHandler())
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(wsd)
|
||||
asyncio.get_event_loop().run_forever()
|
||||
|
|
Loading…
Reference in a new issue