import asyncio
from ri import (
RiGrpcConfig,
RiGrpcStats,
RiGrpcServiceRegistry,
RiGrpcPythonService,
RiGrpcServiceRegistryPy,
RiGrpcServer,
RiGrpcClient,
)
async def main():
config = RiGrpcConfig()
config.host = "0.0.0.0"
config.port = 50051
config.max_concurrent_streams = 100
config.keepalive_time_seconds = 60
config.keepalive_timeout_seconds = 20
config.enable_reflection = True
print("Creating gRPC server...")
server = RiGrpcServer(config)
registry = RiGrpcServiceRegistryPy()
print("\nDefining gRPC services...")
user_service = RiGrpcPythonService()
user_service.service_name = "UserService"
user_service.methods = ["GetUser", "CreateUser", "UpdateUser", "DeleteUser"]
user_service.proto_file = "user.proto"
order_service = RiGrpcPythonService()
order_service.service_name = "OrderService"
order_service.methods = ["GetOrder", "CreateOrder", "ListOrders"]
order_service.proto_file = "order.proto"
registry.register_service(user_service)
registry.register_service(order_service)
print(f"Registered {len(registry.list_services())} services")
print("\nRegistered services:")
for service_name in registry.list_services():
print(f" - {service_name}")
service_info = registry.get_service("UserService")
if service_info:
print(f"\nUserService methods: {service_info.methods}")
print("\nCreating gRPC client...")
client_config = RiGrpcConfig()
client_config.host = "localhost"
client_config.port = 50051
client_config.timeout_seconds = 10
client = RiGrpcClient(client_config)
print("\ngRPC Statistics:")
stats = RiGrpcStats()
print(f"Total requests: {stats.total_requests}")
print(f"Active connections: {stats.active_connections}")
print(f"Average latency: {stats.average_latency_ms}ms")
print("\nSimulating service calls...")
print("Unary call: GetUser")
request_data = b'{"user_id": "123"}'
print(f"Request: {request_data}")
print("\nStreaming call: ListOrders")
stream_request = b'{"user_id": "123", "page": 1, "page_size": 10}'
print(f"Stream request: {stream_request}")
print("\nBidirectional streaming: Chat")
chat_messages = [
b'{"message": "Hello"}',
b'{"message": "How are you?"}',
b'{"message": "Goodbye"}',
]
print(f"Chat messages: {len(chat_messages)}")
print("\ngRPC operations completed successfully!")
if __name__ == "__main__":
asyncio.run(main())