diff --git a/htdocs/openwebrx.js b/htdocs/openwebrx.js index 210045e..113fc64 100644 --- a/htdocs/openwebrx.js +++ b/htdocs/openwebrx.js @@ -1208,6 +1208,10 @@ function on_ws_recv(evt) var server_cpu_usage = json.value; progressbar_set(e("openwebrx-bar-server-cpu"),server_cpu_usage/100,"Server CPU [" + server_cpu_usage + "%]",server_cpu_usage>85); break; + case "clients": + var clients = json.value; + progressbar_set(e("openwebrx-bar-clients"), clients / max_clients_num, "Clients [" + clients + "]", clients > max_clients_num*0.85); + break; case "profiles": var listbox = e("openwebrx-sdr-profiles-listbox"); listbox.innerHTML = json.value.map(function(profile){ diff --git a/owrx/controllers.py b/owrx/controllers.py index a45b4f4..3f974dc 100644 --- a/owrx/controllers.py +++ b/owrx/controllers.py @@ -1,7 +1,7 @@ import mimetypes from owrx.websocket import WebSocketConnection from owrx.config import PropertyManager -from owrx.source import DspManager, CpuUsageThread, SdrService +from owrx.source import DspManager, CpuUsageThread, SdrService, ClientReporterThread import json import os from datetime import datetime @@ -72,6 +72,8 @@ class OpenWebRxClient(object): def __init__(self, conn): self.conn = conn + ClientReporterThread.getSharedInstance().addClient(self) + self.dsp = None self.sdr = None self.configProps = None @@ -120,6 +122,10 @@ class OpenWebRxClient(object): def close(self): self.stopDsp() CpuUsageThread.getSharedInstance().remove_client(self) + try: + ClientReporterThread.getSharedInstance().removeClient(self) + except ValueError: + pass logger.debug("connection closed") def stopDsp(self): @@ -157,6 +163,8 @@ class OpenWebRxClient(object): self.protected_send({"type":"smeter","value":level}) def write_cpu_usage(self, usage): self.protected_send({"type":"cpuusage","value":usage}) + def write_clients(self, clients): + self.protected_send({"type":"clients","value":clients}) def write_secondary_fft(self, data): self.protected_send(bytes([0x03]) + data) def write_secondary_demod(self, data): diff --git a/owrx/source.py b/owrx/source.py index c927d1a..5ed0918 100644 --- a/owrx/source.py +++ b/owrx/source.py @@ -456,13 +456,13 @@ class CpuUsageThread(threading.Thread): def run(self): while self.doRun: - time.sleep(3) try: cpu_usage = self.get_cpu_usage() except: cpu_usage = 0 for c in self.clients: c.write_cpu_usage(cpu_usage) + time.sleep(3) logger.debug("cpu usage thread shut down") def get_cpu_usage(self): @@ -500,3 +500,43 @@ class CpuUsageThread(threading.Thread): if CpuUsageThread.sharedInstance == self: CpuUsageThread.sharedInstance = None self.doRun = False + +class TooManyClientsException(Exception): + pass + +class ClientReporterThread(threading.Thread): + sharedInstance = None + @staticmethod + def getSharedInstance(): + if ClientReporterThread.sharedInstance is None: + ClientReporterThread.sharedInstance = ClientReporterThread() + ClientReporterThread.sharedInstance.start() + ClientReporterThread.sharedInstance.doRun = True + return ClientReporterThread.sharedInstance + + def __init__(self): + self.doRun = True + self.clients = [] + super().__init__() + + def run(self): + while (self.doRun): + n = len(self.clients) + for c in self.clients: + c.write_clients(n) + time.sleep(3) + ClientReporterThread.sharedInstance = None + + def addClient(self, client): + pm = PropertyManager.getSharedInstance() + if len(self.clients) >= pm["max_clients"]: + raise TooManyClientsException() + self.clients.append(client) + + def removeClient(self, client): + try: + self.clients.remove(client) + except ValueError: + pass + if not self.clients: + self.doRun = False \ No newline at end of file