Skip to content

Commit

Permalink
feat: add client and server code
Browse files Browse the repository at this point in the history
  • Loading branch information
keivanipchihagh committed Mar 19, 2024
1 parent 56dd81d commit b50965a
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 0 deletions.
19 changes: 19 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (C) 2023-PRESENT: Keivan Ipchi Hagh
#
# Email: [email protected]
# GitHub: https://github.com/keivanipchihagh
#
##########################################################

# Third-party imports
from src.athena.server import AthenaServer


if __name__ == '__main__':

server = AthenaServer(
host = 'localhost',
port = 50051,
n_workers = 1
)
server.start()
Empty file added src/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions src/athena/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import sys
sys.path.append('protos')
34 changes: 34 additions & 0 deletions src/athena/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
##########################################################
#
# Copyright (C) 2023-PRESENT: Keivan Ipchi Hagh
#
# Email: [email protected]
# GitHub: https://github.com/keivanipchihagh
#
##########################################################

import grpc

# Third-party imports
from base_clienty import BaseClient
from protos.athena import athena_pb2_grpc


class AthenaClient(BaseClient):

def __init__(
self,
host: str = 'localhost',
port: int = 50051
) -> 'AthenaClient':
"""
AthenaClient Constructor.
Parameters:
- host (str): Server hostname. Defaults to `localhost`.
- port (int): Server port number. Defaults to `50051`.
"""
super().__init__(host, port)

self.channel = grpc.insecure_channel(self.target)
self.stub = athena_pb2_grpc.AthenaStub(self.channel)
58 changes: 58 additions & 0 deletions src/athena/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
##########################################################
#
# Copyright (C) 2023-PRESENT: Keivan Ipchi Hagh
#
# Email: [email protected]
# GitHub: https://github.com/keivanipchihagh
#
##########################################################

import grpc

# Third-party imports
from src.base_server import BaseServer
from protos.athena import athena_pb2
from protos.athena import athena_pb2_grpc


class BacktestService(athena_pb2_grpc.AthenaServicer):

def Backtest(
self,
request: athena_pb2.RequestBacktest,
context: grpc.ServicerContext,
) -> athena_pb2.ResponseBacktest:
"""
Executes a backtest based on the provided request.
Parameters:
- request (athena_pb2.RequestBacktest): The backtest request.
- context (grpc.ServicerContext): The gRPC context.
Returns:
- (athena_pb2.ResponseBacktest): The backtest response.
"""
response = athena_pb2.ResponseBacktest()
return response


class AthenaServer(BaseServer):

def __init__(
self,
host: str = 'localhost',
port: int = 50051,
n_workers: int = 1,
) -> 'AthenaServer':
"""
AthenaServer Constructor.
Parameters:
- host (str): Server hostname. Defaults to `localhost`.
- port (int): Server port number. Defaults to `50051`.
- n_workers (int): Number of threads. Defaults to `1`.
"""
super().__init__(host, port, n_workers)

# Register servicers
athena_pb2_grpc.add_AthenaServicer_to_server(BacktestService(), self.server)
50 changes: 50 additions & 0 deletions src/base_clienty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
##########################################################
#
# Copyright (C) 2023-PRESENT: Keivan Ipchi Hagh
#
# Email: [email protected]
# GitHub: https://github.com/keivanipchihagh
#
##########################################################

import grpc


class BaseClient(object):

def __init__(
self,
host: str = 'localhost',
port: int = 50051,
) -> 'BaseClient':
"""
BaseClient Constructor.
Parameters:
- host (str): Server hostname. Defaults to `localhost`.
- port (int): Server port number. Defaults to `50051`.
"""
self.host = host
self.port = port
self.target = f'{host}:{port}'

self.channel = None


def is_server_ready(self, timeout: int = 1) -> bool:
"""
Returns whether server is connected and ready.
Parameters:
- timeout (int): Timeout for future request. Defaults to `1`.
Returns:
- (bool): Server readiness status.
"""
try:
grpc.channel_ready_future(self.channel).result(timeout)
return True
except grpc.FutureTimeoutError:
return False
except Exception as ex:
raise ex
45 changes: 45 additions & 0 deletions src/base_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
##########################################################
#
# Copyright (C) 2023-PRESENT: Keivan Ipchi Hagh
#
# Email: [email protected]
# GitHub: https://github.com/keivanipchihagh
#
##########################################################

import grpc
from concurrent import futures


class BaseServer(object):

def __init__(
self,
host: str = 'localhost',
port: int = 50051,
n_workers: int = 1,
) -> 'BaseServer':
"""
BaseServer Constructor.
Parameters:
- host (str): Server hostname. Defaults to `localhost`.
- port (int): Server port number. Defaults to `50051`.
- n_workers (int): Number of threads. Defaults to `1`.
"""
self.host = host
self.port = port
self.n_workers = n_workers
self.target = f'{host}:{port}'

self.thread_pool = futures.ThreadPoolExecutor(n_workers)
self.server = grpc.server(self.thread_pool)
self.server.add_insecure_port(self.target)


def start(self) -> None:
"""
Starts the server in blocking mode.
"""
self.server.start()
self.server.wait_for_termination()

0 comments on commit b50965a

Please sign in to comment.