Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
async def main() -> None:
logging.basicConfig(level=logging.INFO)
# SSL is disabled at this stage of the project
server = BridgeServer("localhost", 50050, "", "", "ws://localhost:8765")
server = BridgeServer("localhost", 50050, "", "")
await server.start()


Expand Down
37 changes: 5 additions & 32 deletions src/server/BackendHandler.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,21 @@
import asyncio
import json
from json import JSONDecodeError
from typing import Any, Dict
from typing import Any, Dict, Set

import websockets
from websockets import ConnectionClosed, ConnectionClosedError


class BackendHandler:
def __init__(self, backend_uri: str) -> None:
self._uri = backend_uri
def __init__(self) -> None:
self._connection = None

async def connect(self) -> None:
self._connection = await websockets.connect(self._uri)
self._connection = True

async def get_preprocessing_params(self) -> Dict[str, Any]:
if self._connection is None:
await self.connect()
try:
await self._connection.send(json.dumps({"request": "get_preprocessing_params"}))

response = await self._connection.recv()
return json.loads(response)
except (ConnectionClosed, ConnectionClosedError, ConnectionRefusedError):
raise ConnectionError("Connection to backend was lost or refused")
except JSONDecodeError as e:
raise JSONDecodeError("Invalid response format from backend", e.doc, e.pos)
except asyncio.TimeoutError:
raise TimeoutError("Timeout while waiting for response from backend")
except Exception as e:
raise Exception(f"Unexpected Error: {e}")
return {"arg1": "dummy", "arg2": "dummy"}

async def get_public_key(self) -> Dict[str, str]:
try:
await self._connection.send(json.dumps({"request": "get_public_key"}))

response = await self._connection.recv()
return json.loads(response)
except (ConnectionClosed, ConnectionClosedError, ConnectionRefusedError):
raise ConnectionError("Connection to backend was lost or refused")
except JSONDecodeError as e:
raise JSONDecodeError("Invalid response format from backend", e.doc, e.pos)
except asyncio.TimeoutError:
raise TimeoutError("Timeout while waiting for response from backend")
except Exception as e:
raise Exception(str(e))
return {"result": "8V50JdK/pEAU6BWsVl7tUM2QonDpugCogHkdcwq7WEc="}
4 changes: 2 additions & 2 deletions src/server/BridgeServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@


class BridgeServer:
def __init__(self, ip: str, port: int, ssl_cert: str, ssl_key: str, backend_uri: str) -> None:
def __init__(self, ip: str, port: int, ssl_cert: str, ssl_key: str) -> None:
self._ip: str = ip
self._port: int = port
# self._ssl_ctx = self._create_ssl_context(ssl_cert, ssl_key)
self._backend_handler: BackendHandler = BackendHandler(backend_uri)
self._backend_handler: BackendHandler = BackendHandler()
self._frontend_handler: FrontendHandler = FrontendHandler(self._backend_handler)

def _create_ssl_context(self, ssl_cert: str, ssl_key: str) -> ssl.SSLContext:
Expand Down