2022-08-29 19:41:15 +00:00
|
|
|
import logging
|
|
|
|
from threading import RLock
|
2022-09-02 21:05:16 +00:00
|
|
|
from typing import List, Optional, Type
|
|
|
|
from uuid import uuid4
|
2022-08-29 19:41:15 +00:00
|
|
|
|
2022-09-06 18:40:58 +00:00
|
|
|
from fastapi import WebSocket as FastAPIWebSocket
|
|
|
|
|
2022-08-29 19:41:15 +00:00
|
|
|
from freqtrade.rpc.api_server.ws.proxy import WebSocketProxy
|
2022-09-02 06:05:36 +00:00
|
|
|
from freqtrade.rpc.api_server.ws.serializer import (HybridJSONWebSocketSerializer,
|
|
|
|
WebSocketSerializer)
|
2022-08-29 19:41:15 +00:00
|
|
|
from freqtrade.rpc.api_server.ws.types import WebSocketType
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class WebSocketChannel:
|
|
|
|
"""
|
|
|
|
Object to help facilitate managing a websocket connection
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
websocket: WebSocketType,
|
2022-09-02 21:05:16 +00:00
|
|
|
channel_id: Optional[str] = None,
|
2022-09-02 06:05:36 +00:00
|
|
|
serializer_cls: Type[WebSocketSerializer] = HybridJSONWebSocketSerializer
|
2022-08-29 19:41:15 +00:00
|
|
|
):
|
2022-09-02 21:05:16 +00:00
|
|
|
|
|
|
|
self.channel_id = channel_id if channel_id else uuid4().hex[:8]
|
|
|
|
|
2022-08-29 19:41:15 +00:00
|
|
|
# The WebSocket object
|
|
|
|
self._websocket = WebSocketProxy(websocket)
|
|
|
|
# The Serializing class for the WebSocket object
|
|
|
|
self._serializer_cls = serializer_cls
|
|
|
|
|
2022-08-29 21:48:29 +00:00
|
|
|
self._subscriptions: List[str] = []
|
|
|
|
|
2022-08-29 19:41:15 +00:00
|
|
|
# Internal event to signify a closed websocket
|
|
|
|
self._closed = False
|
|
|
|
|
|
|
|
# Wrap the WebSocket in the Serializing class
|
|
|
|
self._wrapped_ws = self._serializer_cls(self._websocket)
|
|
|
|
|
2022-09-02 21:05:16 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return f"WebSocketChannel({self.channel_id}, {self.remote_addr})"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def remote_addr(self):
|
|
|
|
return self._websocket.remote_addr
|
|
|
|
|
2022-08-29 19:41:15 +00:00
|
|
|
async def send(self, data):
|
|
|
|
"""
|
|
|
|
Send data on the wrapped websocket
|
|
|
|
"""
|
|
|
|
await self._wrapped_ws.send(data)
|
|
|
|
|
|
|
|
async def recv(self):
|
|
|
|
"""
|
|
|
|
Receive data on the wrapped websocket
|
|
|
|
"""
|
|
|
|
return await self._wrapped_ws.recv()
|
|
|
|
|
|
|
|
async def ping(self):
|
|
|
|
"""
|
|
|
|
Ping the websocket
|
|
|
|
"""
|
|
|
|
return await self._websocket.ping()
|
|
|
|
|
|
|
|
async def close(self):
|
|
|
|
"""
|
|
|
|
Close the WebSocketChannel
|
|
|
|
"""
|
|
|
|
|
2022-09-10 21:12:18 +00:00
|
|
|
await self._websocket.close()
|
2022-08-29 19:41:15 +00:00
|
|
|
self._closed = True
|
|
|
|
|
2022-08-29 21:48:29 +00:00
|
|
|
def is_closed(self) -> bool:
|
|
|
|
"""
|
|
|
|
Closed flag
|
|
|
|
"""
|
2022-08-29 19:41:15 +00:00
|
|
|
return self._closed
|
|
|
|
|
2022-08-29 21:48:29 +00:00
|
|
|
def set_subscriptions(self, subscriptions: List[str] = []) -> None:
|
|
|
|
"""
|
|
|
|
Set which subscriptions this channel is subscribed to
|
|
|
|
|
|
|
|
:param subscriptions: List of subscriptions, List[str]
|
|
|
|
"""
|
|
|
|
self._subscriptions = subscriptions
|
|
|
|
|
|
|
|
def subscribed_to(self, message_type: str) -> bool:
|
|
|
|
"""
|
|
|
|
Check if this channel is subscribed to the message_type
|
|
|
|
|
|
|
|
:param message_type: The message type to check
|
|
|
|
"""
|
|
|
|
return message_type in self._subscriptions
|
|
|
|
|
2022-08-29 19:41:15 +00:00
|
|
|
|
|
|
|
class ChannelManager:
|
|
|
|
def __init__(self):
|
|
|
|
self.channels = dict()
|
|
|
|
self._lock = RLock() # Re-entrant Lock
|
|
|
|
|
|
|
|
async def on_connect(self, websocket: WebSocketType):
|
|
|
|
"""
|
|
|
|
Wrap websocket connection into Channel and add to list
|
|
|
|
|
|
|
|
:param websocket: The WebSocket object to attach to the Channel
|
|
|
|
"""
|
2022-09-06 18:40:58 +00:00
|
|
|
if isinstance(websocket, FastAPIWebSocket):
|
2022-08-29 19:41:15 +00:00
|
|
|
try:
|
|
|
|
await websocket.accept()
|
|
|
|
except RuntimeError:
|
|
|
|
# The connection was closed before we could accept it
|
|
|
|
return
|
|
|
|
|
|
|
|
ws_channel = WebSocketChannel(websocket)
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
self.channels[websocket] = ws_channel
|
|
|
|
|
|
|
|
return ws_channel
|
|
|
|
|
|
|
|
async def on_disconnect(self, websocket: WebSocketType):
|
|
|
|
"""
|
|
|
|
Call close on the channel if it's not, and remove from channel list
|
|
|
|
|
|
|
|
:param websocket: The WebSocket objet attached to the Channel
|
|
|
|
"""
|
|
|
|
with self._lock:
|
|
|
|
channel = self.channels.get(websocket)
|
|
|
|
if channel:
|
|
|
|
if not channel.is_closed():
|
|
|
|
await channel.close()
|
|
|
|
|
|
|
|
del self.channels[websocket]
|
|
|
|
|
|
|
|
async def disconnect_all(self):
|
|
|
|
"""
|
|
|
|
Disconnect all Channels
|
|
|
|
"""
|
|
|
|
with self._lock:
|
|
|
|
for websocket, channel in self.channels.items():
|
|
|
|
if not channel.is_closed():
|
|
|
|
await channel.close()
|
|
|
|
|
|
|
|
self.channels = dict()
|
|
|
|
|
|
|
|
async def broadcast(self, data):
|
|
|
|
"""
|
|
|
|
Broadcast data on all Channels
|
|
|
|
|
|
|
|
:param data: The data to send
|
|
|
|
"""
|
|
|
|
with self._lock:
|
2022-08-29 21:48:29 +00:00
|
|
|
message_type = data.get('type')
|
2022-08-29 19:41:15 +00:00
|
|
|
for websocket, channel in self.channels.items():
|
|
|
|
try:
|
2022-08-29 21:48:29 +00:00
|
|
|
if channel.subscribed_to(message_type):
|
|
|
|
await channel.send(data)
|
2022-08-29 19:41:15 +00:00
|
|
|
except RuntimeError:
|
|
|
|
# Handle cannot send after close cases
|
|
|
|
await self.on_disconnect(websocket)
|
|
|
|
|
|
|
|
async def send_direct(self, channel, data):
|
|
|
|
"""
|
|
|
|
Send data directly through direct_channel only
|
|
|
|
|
|
|
|
:param direct_channel: The WebSocketChannel object to send data through
|
|
|
|
:param data: The data to send
|
|
|
|
"""
|
|
|
|
await channel.send(data)
|
|
|
|
|
|
|
|
def has_channels(self):
|
|
|
|
"""
|
|
|
|
Flag for more than 0 channels
|
|
|
|
"""
|
|
|
|
return len(self.channels) > 0
|