diff --git a/sylk/applications/webrtcgateway/logger.py b/sylk/applications/webrtcgateway/logger.py index 817313f..2bdb87b 100644 --- a/sylk/applications/webrtcgateway/logger.py +++ b/sylk/applications/webrtcgateway/logger.py @@ -1,80 +1,80 @@ import logging from application.log import ContextualLogger from sylk.applications import ApplicationLogger from sylk.log import TraceLogger from .configuration import GeneralConfig, JanusConfig __all__ = 'log', 'ConnectionLogger', 'VideoroomLogger' log = ApplicationLogger(__package__) class ConnectionLogger(ContextualLogger): def __init__(self, connection): super(ConnectionLogger, self).__init__(logger=log) self.device_id = connection.device_id self.peer=connection.protocol.peer self.connection = connection def apply_context(self, message): try: account_id = self.connection.devices_map[self.device_id] except KeyError: - return '[device {0}/{2}] {1}'.format(self.peer, message, self.device_id) if message != '' else '' + return '[device {2}] {1}'.format(message, self.device_id) if message != '' else '' else: return '[account {0}/{2}] {1}'.format(account_id, message, self.device_id) if message != '' else '' class VideoroomLogger(ContextualLogger): def __init__(self, videoroom): super(VideoroomLogger, self).__init__(logger=log) self.room_uri = videoroom.uri def apply_context(self, message): return '[videoroom {0}] {1}'.format(self.room_uri, message) if message != '' else '' class WebRTCClientTraceFormatter(logging.Formatter): _format = '{time} Packet {packet} {data.direction}, client at {data.peer}\n{data.message}\n' _packet = 0 def format(self, record): self._packet += 1 notification = record.notification return self._format.format(time=notification.datetime, packet=self._packet, data=notification.data) class WebRTCJanusTraceFormatter(logging.Formatter): _format = '{time} Packet {packet} {data.direction}, janus at {data.peer}\n{data.message}\n' _packet = 0 def format(self, record): self._packet += 1 notification = record.notification return self._format.format(time=notification.datetime, packet=self._packet, data=notification.data) class WebRTCClientTraceLogger(TraceLogger): name = 'webrtc_client_trace' owner = 'webrtcgateway' enabled = GeneralConfig.trace_client formatter = WebRTCClientTraceFormatter() def _NH_WebRTCClientTrace(self, notification): self.logger.log_notification(notification) class WebRTCJanusTraceLogger(TraceLogger): name = 'webrtc_janus_trace' owner = 'webrtcgateway' enabled = JanusConfig.trace_janus formatter = WebRTCJanusTraceFormatter() def _NH_WebRTCJanusTrace(self, notification): self.logger.log_notification(notification) diff --git a/sylk/applications/webrtcgateway/protocol.py b/sylk/applications/webrtcgateway/protocol.py index 8c3a00b..7850c5b 100644 --- a/sylk/applications/webrtcgateway/protocol.py +++ b/sylk/applications/webrtcgateway/protocol.py @@ -1,64 +1,64 @@ import json from application.notification import NotificationCenter, NotificationData from autobahn.twisted.websocket import WebSocketServerProtocol from autobahn.websocket import ConnectionDeny from .handler import ConnectionHandler from .janus import JanusBackend from .logger import log SYLK_WS_PROTOCOL = 'sylkRTC-2' class SylkWebSocketServerProtocol(WebSocketServerProtocol): janus_backend = JanusBackend() connection_handler = None notification_center = NotificationCenter() def onConnect(self, request): if SYLK_WS_PROTOCOL not in request.protocols: log.debug('Connection from {} request: {}'.format(self.peer, request)) log.info('Rejecting connection from {}, client uses unsupported protocol: {}'.format(self.peer, ','.join(request.protocols))) raise ConnectionDeny(406, u'No compatible protocol specified') if not self.janus_backend.ready: log.warning('Rejecting connection from {}, Janus backend is not connected'.format(self.peer)) raise ConnectionDeny(503, u'Backend is not connected') return SYLK_WS_PROTOCOL def onOpen(self): self.factory.connections.add(self) self.connection_handler = ConnectionHandler(self) self.connection_handler.start() - self.connection_handler.log.info('{address} web socket connected'.format(address=self.peer)) + self.connection_handler.log.info('websocket connected from: {address}'.format(address=self.peer)) def onMessage(self, payload, is_binary): if is_binary: self.connection_handler.log.error('received invalid binary message') return self.notification_center.post_notification('WebRTCClientTrace', sender=self, data=NotificationData(direction='INCOMING', message=payload, peer=self.peer)) try: data = json.loads(payload) except Exception as e: self.connection_handler.log.error('could not parse WebSocket payload: {exception!s}'.format(exception=e)) else: self.connection_handler.handle_message(data) def onClose(self, clean, code, reason): if self.connection_handler is None: # Connection was closed very early before onOpen was even called return - self.connection_handler.log.info('{address} web socket disconnected'.format(address=self.peer)) + self.connection_handler.log.info('websocket disconnected from {address}'.format(address=self.peer)) self.factory.connections.discard(self) self.connection_handler.stop() self.connection_handler = None def sendMessage(self, payload, *args, **kw): self.notification_center.post_notification('WebRTCClientTrace', sender=self, data=NotificationData(direction='OUTGOING', message=payload, peer=self.peer)) #log.info('Sending %s to web socket %s' % (payload, self.peer)); super(SylkWebSocketServerProtocol, self).sendMessage(payload, *args, **kw) def disconnect(self, code=1000, reason=u''): self.sendClose(code, reason)