Skip to content

Commit

Permalink
Create rpc.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 6, 2024
1 parent fe5531f commit 95ca3c0
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions blockchain/network/rpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import grpc
from concurrent import futures

from typing import Dict, List, Tuple

class RPC:
def __init__(self):
self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
self.services: Dict[str, grpc.Service] = {}

def add_service(self, service: grpc.Service):
self.services[service.name] = service
self.server.add_generic_rpc_handlers((service,))

def start(self):
self.server.add_insecure_port('[::]:50051')
self.server.start()

def stop(self):
self.server.stop(0)

class RPCService(grpc.Service):
def __init__(self, name: str):
self.name = name

def handle_request(self, request: grpc.Request) -> grpc.Response:
# Handle incoming requests
pass

class RPCClient:
def __init__(self, host: str, port: int):
self.channel = grpc.insecure_channel(f"{host}:{port}")
self.stub = grpc.GenericStub(self.channel)

def call(self, service_name: str, method_name: str, request: grpc.Request) -> grpc.Response:
return self.stub.call(service_name, method_name, request)

0 comments on commit 95ca3c0

Please sign in to comment.