re-establish client reporting

This commit is contained in:
Jakob Ketterl 2019-05-10 22:47:40 +02:00
parent 0a22978660
commit dac35ae526
3 changed files with 54 additions and 2 deletions

View File

@ -1208,6 +1208,10 @@ function on_ws_recv(evt)
var server_cpu_usage = json.value; 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); progressbar_set(e("openwebrx-bar-server-cpu"),server_cpu_usage/100,"Server CPU [" + server_cpu_usage + "%]",server_cpu_usage>85);
break; 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": case "profiles":
var listbox = e("openwebrx-sdr-profiles-listbox"); var listbox = e("openwebrx-sdr-profiles-listbox");
listbox.innerHTML = json.value.map(function(profile){ listbox.innerHTML = json.value.map(function(profile){

View File

@ -1,7 +1,7 @@
import mimetypes import mimetypes
from owrx.websocket import WebSocketConnection from owrx.websocket import WebSocketConnection
from owrx.config import PropertyManager from owrx.config import PropertyManager
from owrx.source import DspManager, CpuUsageThread, SdrService from owrx.source import DspManager, CpuUsageThread, SdrService, ClientReporterThread
import json import json
import os import os
from datetime import datetime from datetime import datetime
@ -72,6 +72,8 @@ class OpenWebRxClient(object):
def __init__(self, conn): def __init__(self, conn):
self.conn = conn self.conn = conn
ClientReporterThread.getSharedInstance().addClient(self)
self.dsp = None self.dsp = None
self.sdr = None self.sdr = None
self.configProps = None self.configProps = None
@ -120,6 +122,10 @@ class OpenWebRxClient(object):
def close(self): def close(self):
self.stopDsp() self.stopDsp()
CpuUsageThread.getSharedInstance().remove_client(self) CpuUsageThread.getSharedInstance().remove_client(self)
try:
ClientReporterThread.getSharedInstance().removeClient(self)
except ValueError:
pass
logger.debug("connection closed") logger.debug("connection closed")
def stopDsp(self): def stopDsp(self):
@ -157,6 +163,8 @@ class OpenWebRxClient(object):
self.protected_send({"type":"smeter","value":level}) self.protected_send({"type":"smeter","value":level})
def write_cpu_usage(self, usage): def write_cpu_usage(self, usage):
self.protected_send({"type":"cpuusage","value":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): def write_secondary_fft(self, data):
self.protected_send(bytes([0x03]) + data) self.protected_send(bytes([0x03]) + data)
def write_secondary_demod(self, data): def write_secondary_demod(self, data):

View File

@ -456,13 +456,13 @@ class CpuUsageThread(threading.Thread):
def run(self): def run(self):
while self.doRun: while self.doRun:
time.sleep(3)
try: try:
cpu_usage = self.get_cpu_usage() cpu_usage = self.get_cpu_usage()
except: except:
cpu_usage = 0 cpu_usage = 0
for c in self.clients: for c in self.clients:
c.write_cpu_usage(cpu_usage) c.write_cpu_usage(cpu_usage)
time.sleep(3)
logger.debug("cpu usage thread shut down") logger.debug("cpu usage thread shut down")
def get_cpu_usage(self): def get_cpu_usage(self):
@ -500,3 +500,43 @@ class CpuUsageThread(threading.Thread):
if CpuUsageThread.sharedInstance == self: if CpuUsageThread.sharedInstance == self:
CpuUsageThread.sharedInstance = None CpuUsageThread.sharedInstance = None
self.doRun = False 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