move connection tracking to all websockets

This commit is contained in:
Jakob Ketterl 2019-09-22 12:57:13 +02:00
parent b4ffc6e2f0
commit 57975b6f96
2 changed files with 19 additions and 3 deletions

View File

@ -4,10 +4,11 @@ from http.server import HTTPServer
from owrx.http import RequestHandler from owrx.http import RequestHandler
from owrx.config import PropertyManager from owrx.config import PropertyManager
from owrx.feature import FeatureDetector from owrx.feature import FeatureDetector
from owrx.source import SdrService, ClientRegistry from owrx.source import SdrService
from socketserver import ThreadingMixIn from socketserver import ThreadingMixIn
from owrx.sdrhu import SdrHuUpdater from owrx.sdrhu import SdrHuUpdater
from owrx.service import Services from owrx.service import Services
from owrx.websocket import WebSocketConnection
import logging import logging
@ -58,5 +59,4 @@ if __name__ == "__main__":
try: try:
main() main()
except KeyboardInterrupt: except KeyboardInterrupt:
for c in ClientRegistry.getSharedInstance().clients: WebSocketConnection.closeAll()
c.close()

View File

@ -15,6 +15,16 @@ class IncompleteRead(Exception):
class WebSocketConnection(object): class WebSocketConnection(object):
connections = []
@staticmethod
def closeAll():
for c in WebSocketConnection.connections:
try:
c.close()
except:
logger.exception("exception while shutting down websocket connections")
def __init__(self, handler, messageHandler): def __init__(self, handler, messageHandler):
self.handler = handler self.handler = handler
self.handler.connection.setblocking(0) self.handler.connection.setblocking(0)
@ -121,6 +131,7 @@ class WebSocketConnection(object):
self.interruptPipeSend.send(bytes(0x00)) self.interruptPipeSend.send(bytes(0x00))
def read_loop(self): def read_loop(self):
WebSocketConnection.connections.append(self)
self.open = True self.open = True
while self.open: while self.open:
(read, _, _) = select.select([self.interruptPipeRecv, self.handler.rfile], [], []) (read, _, _) = select.select([self.interruptPipeRecv, self.handler.rfile], [], [])
@ -168,6 +179,11 @@ class WebSocketConnection(object):
except OSError: except OSError:
logger.exception("OSError while writing close frame:") logger.exception("OSError while writing close frame:")
try:
WebSocketConnection.connections.remove(self)
except ValueError:
pass
def close(self): def close(self):
self.open = False self.open = False
self.interrupt() self.interrupt()