Skip to content

Commit

Permalink
Added commands to run something when client connect/disconnect
Browse files Browse the repository at this point in the history
- Added support for SELKIES_START_AFTER_CONNECT and SELKIES_START_AFTER_CONNECT variables
- Added support for --start-after-connect and --start-after-disconnect start arguments
- Removed duplicated base64 import in signalling_web.py
  • Loading branch information
skipperro committed Nov 24, 2024
1 parent d45f2da commit 8510942
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/selkies_gstreamer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,12 @@ def main():
parser.add_argument('--metrics_http_port',
default=os.environ.get('SELKIES_METRICS_HTTP_PORT', '8000'),
help='Port to start the Prometheus metrics server on')
parser.add_argument('--start-after-connect',
default=os.environ.get('SELKIES_START_AFTER_CONNECT', ''),
help='Program or command to run on server after the first client has connected')
parser.add_argument('--start-after-disconnect',
default=os.environ.get('SELKIES_START_AFTER_DISCONNECT', ''),
help='Program or command to run on server after the last client has disconnected')
parser.add_argument('--debug', action='store_true',
help='Enable debug logging')
args = parser.parse_args()
Expand Down Expand Up @@ -516,6 +522,12 @@ def main():
using_webrtc_csv = args.enable_webrtc_statistics.lower() == 'true'
metrics = Metrics(int(args.metrics_http_port), using_webrtc_csv)

# Setup commands to run on connect and disconnect
if args.start_after_connect != '':
os.environ['SELKIES_START_AFTER_CONNECT'] = args.start_after_connect
if args.start_after_disconnect != '':
os.environ['SELKIES_START_AFTER_DISCONNECT'] = args.start_after_disconnect

# Initialize the signalling client
using_https = args.enable_https.lower() == 'true'
using_basic_auth = args.enable_basic_auth.lower() == 'true'
Expand Down
20 changes: 19 additions & 1 deletion src/selkies_gstreamer/signalling_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# Author: Nirbheek Chauhan <[email protected]>

import os
import base64
import sys
import ssl
import logging
Expand All @@ -32,6 +31,7 @@
import hashlib
import hmac
import base64
import subprocess

from pathlib import Path
from http import HTTPStatus
Expand Down Expand Up @@ -277,6 +277,15 @@ async def cleanup_session(self, uid):
wso, oaddr, _, _ = self.peers[other_id]
del self.peers[other_id]
await wso.close()
# Run application after last session disconnected
try:
if 'SELKIES_START_AFTER_DISCONNECT' in os.environ and len(self.sessions) == 0:
if os.environ['SELKIES_START_AFTER_DISCONNECT'] != '':
command = os.environ['SELKIES_START_AFTER_DISCONNECT'].split(' ')
subprocess.Popen(command, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
except Exception as e:
logger.error('Failed to run SELKIES_START_AFTER_DISCONNECT: {}'.format(e))

async def cleanup_room(self, uid, room_id):
room_peers = self.rooms[room_id]
Expand Down Expand Up @@ -367,6 +376,15 @@ async def connection_handler(self, ws, uid, meta=None):
wsc = self.peers[callee_id][0]
logger.info('Session from {!r} ({!r}) to {!r} ({!r})'
''.format(uid, raddr, callee_id, wsc.remote_address))
# Run application on first session connection
try:
if 'SELKIES_START_AFTER_CONNECT' in os.environ and len(self.sessions) == 0:
if os.environ['SELKIES_START_AFTER_CONNECT'] != '':
command = os.environ['SELKIES_START_AFTER_CONNECT'].split(' ')
subprocess.Popen(command, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
except Exception as e:
logger.error('Failed to run SELKIES_START_AFTER_CONNECT: {}'.format(e))
# Register session
self.peers[uid][2] = peer_status = 'session'
self.sessions[uid] = callee_id
Expand Down

0 comments on commit 8510942

Please sign in to comment.