-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56dd81d
commit b50965a
Showing
7 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import sys | ||
sys.path.append('protos') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |