grpc_client/clients/base_client.py

48 lines
1.7 KiB
Python
Raw Normal View History

2025-11-13 03:01:00 +08:00
import time
2026-02-02 09:37:47 +08:00
2026-02-03 13:36:52 +08:00
import grpc
from clients._path_setup import ensure_paths
ensure_paths()
2025-11-13 03:01:00 +08:00
from cmvr.api import dexhand_service_pb2_grpc
2026-02-03 13:36:52 +08:00
from cmvr.api import humanoid_robot_service_pb2_grpc
2025-11-13 03:01:00 +08:00
class RobotClientBase:
def __init__(self, address="192.168.0.222:50052", timeout=2, retries=1):
self.address = address
self.timeout = timeout
self.retries = retries
self.channel = None
self.stub = None
print(f"Attempting to connect to gRPC server: {self.address}")
self._connect_with_retry()
def _connect_with_retry(self):
attempt = 0
while attempt <= self.retries:
print(f"Connecting... (Attempt {attempt + 1})")
try:
self.channel = grpc.insecure_channel(self.address)
grpc.channel_ready_future(self.channel).result(timeout=self.timeout)
self.stub = humanoid_robot_service_pb2_grpc.HumanoidRobotServiceStub(self.channel)
self.stub_hand = dexhand_service_pb2_grpc.DexHandServiceStub(self.channel)
print(f"Successfully connected to gRPC server: {self.address}")
return
except grpc.FutureTimeoutError:
attempt += 1
print(f"Connection timed out, attempt {attempt} failed")
if attempt > self.retries:
raise ConnectionError(
f"Failed to connect to gRPC server {self.address} after {self.retries} retries."
)
print(f"Waiting {self.timeout}s before retrying...")
time.sleep(self.timeout)
def close(self):
if self.channel:
self.channel.close()
print(f"Closed connection to gRPC server {self.address}")