Skip to content
This repository has been archived by the owner on Mar 18, 2023. It is now read-only.

Commit

Permalink
docs(connection_manager): #13 Add Description and TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
miragecentury committed Feb 9, 2022
1 parent 22ce1dc commit 8eb6171
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/application/service/device/connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@


class ConnectionManager:
"""
Connection Manager is responsible to manage and store reference of device's connection
"""

PREFIX_KEY_DEVICE = "device_connected:"

Expand All @@ -21,15 +24,30 @@ def __init__(self, cache: Cache, application_instance_name: str) -> None:
self.local_device_store = dict()

async def register_application(self) -> None:
"""
Register the Application
"""
# TODO : Put the Instance Name to Redis
pass

async def unregister_application(self) -> None:
"""
Unregister the Application
"""
# TODO : Remove the Instance Name from Redis
# TODO : How manage the crash of application ? (liveness probing)
pass

def _get_device_key(self, device_id: str):
"""
Format Device Key for Redis
"""
return f"{self.PREFIX_KEY_DEVICE}{device_id}"

async def register_connection(self, websocket: WebSocket, device_id: str):
"""
Register Connection for Device Connected to this specific instance
"""
async with self.cache.acquire() as _cache_connection:
await _cache_connection.set(
key=self._get_device_key(device_id=device_id),
Expand All @@ -38,6 +56,9 @@ async def register_connection(self, websocket: WebSocket, device_id: str):
self.local_device_store[device_id] = websocket

async def unregister_connection(self, websocket: WebSocket, device_id: str):
"""
Unregister Connection for Device Connected to this specific instance
"""
if self.local_device_store.get(device_id, None) is not None:
del self.local_device_store[device_id]
async with self.cache.acquire() as _cache_connection:
Expand Down

0 comments on commit 8eb6171

Please sign in to comment.