[Update] WebSocket: Implement availability check for WebSocket server and refactor related logic
This commit is contained in:
@@ -6,7 +6,7 @@ import re
|
|||||||
from device_manager import device_manager
|
from device_manager import device_manager
|
||||||
from config import config
|
from config import config
|
||||||
from model import model
|
from model import model
|
||||||
from utils import removeLog, printLog, errorLogging, isConnectedNetwork, isValidIpAddress
|
from utils import removeLog, printLog, errorLogging, isConnectedNetwork, isValidIpAddress, isAvailableWebSocketServer
|
||||||
|
|
||||||
class Controller:
|
class Controller:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
@@ -1835,8 +1835,10 @@ class Controller:
|
|||||||
config.WEBSOCKET_HOST = data
|
config.WEBSOCKET_HOST = data
|
||||||
response = {"status":200, "result":config.WEBSOCKET_HOST}
|
response = {"status":200, "result":config.WEBSOCKET_HOST}
|
||||||
else:
|
else:
|
||||||
model.stopWebSocketServer()
|
if data == config.WEBSOCKET_HOST:
|
||||||
if model.checkWebSocketServerAvailable() is True:
|
response = {"status":200, "result":config.WEBSOCKET_HOST}
|
||||||
|
elif isAvailableWebSocketServer(data, config.WEBSOCKET_PORT):
|
||||||
|
model.stopWebSocketServer()
|
||||||
model.startWebSocketServer(data, config.WEBSOCKET_PORT)
|
model.startWebSocketServer(data, config.WEBSOCKET_PORT)
|
||||||
config.WEBSOCKET_HOST = data
|
config.WEBSOCKET_HOST = data
|
||||||
response = {"status":200, "result":config.WEBSOCKET_HOST}
|
response = {"status":200, "result":config.WEBSOCKET_HOST}
|
||||||
@@ -1861,8 +1863,10 @@ class Controller:
|
|||||||
config.WEBSOCKET_PORT = int(data)
|
config.WEBSOCKET_PORT = int(data)
|
||||||
response = {"status":200, "result":config.WEBSOCKET_PORT}
|
response = {"status":200, "result":config.WEBSOCKET_PORT}
|
||||||
else:
|
else:
|
||||||
model.stopWebSocketServer()
|
if int(data) == config.WEBSOCKET_PORT:
|
||||||
if model.checkWebSocketServerAvailable() is True:
|
return {"status":200, "result":config.WEBSOCKET_PORT}
|
||||||
|
elif isAvailableWebSocketServer(config.WEBSOCKET_HOST, int(data)) is True:
|
||||||
|
model.stopWebSocketServer()
|
||||||
model.startWebSocketServer(config.WEBSOCKET_HOST, int(data))
|
model.startWebSocketServer(config.WEBSOCKET_HOST, int(data))
|
||||||
config.WEBSOCKET_PORT = int(data)
|
config.WEBSOCKET_PORT = int(data)
|
||||||
response = {"status":200, "result":config.WEBSOCKET_PORT}
|
response = {"status":200, "result":config.WEBSOCKET_PORT}
|
||||||
@@ -1882,7 +1886,7 @@ class Controller:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def setEnableWebSocketServer(*args, **kwargs) -> dict:
|
def setEnableWebSocketServer(*args, **kwargs) -> dict:
|
||||||
if model.checkWebSocketServerAvailable() is True:
|
if isAvailableWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT) is True:
|
||||||
model.startWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT)
|
model.startWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT)
|
||||||
config.WEBSOCKET_SERVER = True
|
config.WEBSOCKET_SERVER = True
|
||||||
response = {"status":200, "result":config.WEBSOCKET_SERVER}
|
response = {"status":200, "result":config.WEBSOCKET_SERVER}
|
||||||
@@ -2003,7 +2007,7 @@ class Controller:
|
|||||||
|
|
||||||
printLog("Init WebSocket Server")
|
printLog("Init WebSocket Server")
|
||||||
if config.WEBSOCKET_SERVER is True:
|
if config.WEBSOCKET_SERVER is True:
|
||||||
if model.checkWebSocketServerAvailable() is True:
|
if isAvailableWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT) is True:
|
||||||
model.startWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT)
|
model.startWebSocketServer(config.WEBSOCKET_HOST, config.WEBSOCKET_PORT)
|
||||||
else:
|
else:
|
||||||
config.WEBSOCKET_SERVER = False
|
config.WEBSOCKET_SERVER = False
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import copy
|
import copy
|
||||||
import gc
|
import gc
|
||||||
import asyncio
|
import asyncio
|
||||||
import socket
|
|
||||||
import errno
|
|
||||||
import json
|
import json
|
||||||
from subprocess import Popen
|
from subprocess import Popen
|
||||||
from os import makedirs as os_makedirs
|
from os import makedirs as os_makedirs
|
||||||
@@ -840,29 +838,6 @@ class Model:
|
|||||||
"""WebSocketメッセージ受信時の処理"""
|
"""WebSocketメッセージ受信時の処理"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def checkWebSocketServerAvailable(self):
|
|
||||||
"""WebSocketサーバーのポートが使用中かどうかを確認する"""
|
|
||||||
response = True
|
|
||||||
try:
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as chk:
|
|
||||||
# SO_REUSEADDRを設定してソケットの再利用を許可
|
|
||||||
chk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
||||||
try:
|
|
||||||
chk.bind((config.WEBSOCKET_HOST, config.WEBSOCKET_PORT))
|
|
||||||
# シャットダウン前にリッスン状態にする必要はない
|
|
||||||
chk.close()
|
|
||||||
except OSError as e:
|
|
||||||
if e.errno == errno.EADDRINUSE:
|
|
||||||
response = False
|
|
||||||
else:
|
|
||||||
errorLogging()
|
|
||||||
response = False
|
|
||||||
except Exception:
|
|
||||||
errorLogging()
|
|
||||||
response = False
|
|
||||||
|
|
||||||
return response
|
|
||||||
|
|
||||||
def startWebSocketServer(self, host, port):
|
def startWebSocketServer(self, host, port):
|
||||||
"""WebSocketサーバーを起動し、別スレッドで実行する"""
|
"""WebSocketサーバーを起動し、別スレッドで実行する"""
|
||||||
if self.websocket_server_alive is True:
|
if self.websocket_server_alive is True:
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from logging.handlers import RotatingFileHandler
|
|||||||
from ctranslate2 import get_supported_compute_types
|
from ctranslate2 import get_supported_compute_types
|
||||||
import requests
|
import requests
|
||||||
import ipaddress
|
import ipaddress
|
||||||
|
import socket
|
||||||
|
|
||||||
def isConnectedNetwork(url="http://www.google.com", timeout=3) -> bool:
|
def isConnectedNetwork(url="http://www.google.com", timeout=3) -> bool:
|
||||||
try:
|
try:
|
||||||
@@ -16,6 +17,25 @@ def isConnectedNetwork(url="http://www.google.com", timeout=3) -> bool:
|
|||||||
except requests.RequestException:
|
except requests.RequestException:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def isAvailableWebSocketServer(host:str, port:int) -> bool:
|
||||||
|
"""WebSocketサーバーのポートが使用中かどうかを確認する"""
|
||||||
|
response = True
|
||||||
|
try:
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as chk:
|
||||||
|
try:
|
||||||
|
# SO_REUSEADDRを設定してソケットの再利用を許可
|
||||||
|
chk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
chk.bind((host, port))
|
||||||
|
# シャットダウン前にリッスン状態にする必要はない
|
||||||
|
chk.close()
|
||||||
|
except Exception:
|
||||||
|
response = False
|
||||||
|
except Exception:
|
||||||
|
errorLogging()
|
||||||
|
response = False
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
def isValidIpAddress(ip_address: str) -> bool:
|
def isValidIpAddress(ip_address: str) -> bool:
|
||||||
try:
|
try:
|
||||||
ipaddress.ip_address(ip_address)
|
ipaddress.ip_address(ip_address)
|
||||||
|
|||||||
Reference in New Issue
Block a user