|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | +from collections.abc import AsyncIterable |
| 4 | + |
| 5 | +import slim_bindings |
| 6 | +from examples.slimrpc.simple.types.example_pb2 import ExampleRequest, ExampleResponse |
| 7 | +from examples.slimrpc.simple.types.example_pb2_slimrpc import ( |
| 8 | + TestServicer, |
| 9 | + add_TestServicer_to_server, |
| 10 | +) |
| 11 | +from slim_bindings._slim_bindings.slim_bindings import uniffi_set_event_loop |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class TestService(TestServicer): |
| 17 | + async def ExampleUnaryUnary( |
| 18 | + self, |
| 19 | + request: ExampleRequest, |
| 20 | + msg_context: slim_bindings.MessageContext, |
| 21 | + session_context: slim_bindings.Context, |
| 22 | + ) -> ExampleResponse: |
| 23 | + logger.info(f"Received unary-unary request: {request}") |
| 24 | + |
| 25 | + return ExampleResponse(example_integer=1, example_string="Hello, World!") |
| 26 | + |
| 27 | + async def ExampleUnaryStream( |
| 28 | + self, |
| 29 | + request: ExampleRequest, |
| 30 | + msg_context: slim_bindings.MessageContext, |
| 31 | + session_context: slim_bindings.Context, |
| 32 | + ) -> AsyncIterable[ExampleResponse]: |
| 33 | + logger.info(f"Received unary-stream request: {request}") |
| 34 | + |
| 35 | + # generate async responses stream |
| 36 | + for i in range(5): |
| 37 | + logger.info(f"Sending response {i}") |
| 38 | + yield ExampleResponse(example_integer=i, example_string=f"Response {i}") |
| 39 | + |
| 40 | + async def ExampleStreamUnary( |
| 41 | + self, |
| 42 | + request_iterator: AsyncIterable[ExampleRequest], |
| 43 | + session_context: slim_bindings.Context, |
| 44 | + ) -> ExampleResponse: |
| 45 | + logger.info("Received stream-unary request") |
| 46 | + |
| 47 | + received_strs = [] |
| 48 | + async for request in request_iterator: |
| 49 | + logger.info(f"Received request: {request}") |
| 50 | + received_strs.append(request.example_string) |
| 51 | + |
| 52 | + response = ExampleResponse( |
| 53 | + example_integer=len(received_strs), |
| 54 | + example_string="Saw: " + ", ".join(received_strs), |
| 55 | + ) |
| 56 | + return response |
| 57 | + |
| 58 | + async def ExampleStreamStream( |
| 59 | + self, |
| 60 | + request_iterator: AsyncIterable[ExampleRequest], |
| 61 | + session_context: slim_bindings.Context, |
| 62 | + ) -> AsyncIterable[ExampleResponse]: |
| 63 | + logger.info("Received stream-stream request") |
| 64 | + async for request in request_iterator: |
| 65 | + logger.info(f"Echoing back request: {request}") |
| 66 | + yield ExampleResponse( |
| 67 | + example_integer=request.example_integer * 100, |
| 68 | + example_string=f"Echo: {request.example_string}", |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +async def amain() -> None: |
| 73 | + uniffi_set_event_loop(asyncio.get_running_loop()) # type: ignore[arg-type] |
| 74 | + |
| 75 | + # Initialize service |
| 76 | + tracing_config = slim_bindings.new_tracing_config() |
| 77 | + runtime_config = slim_bindings.new_runtime_config() |
| 78 | + service_config = slim_bindings.new_service_config() |
| 79 | + |
| 80 | + tracing_config.log_level = "info" |
| 81 | + |
| 82 | + slim_bindings.initialize_with_configs( |
| 83 | + tracing_config=tracing_config, |
| 84 | + runtime_config=runtime_config, |
| 85 | + service_config=[service_config], |
| 86 | + ) |
| 87 | + |
| 88 | + service = slim_bindings.get_global_service() |
| 89 | + |
| 90 | + # Create local name |
| 91 | + local_name = slim_bindings.Name("agntcy", "grpc", "server") |
| 92 | + |
| 93 | + # Connect to SLIM |
| 94 | + client_config = slim_bindings.new_insecure_client_config("http://localhost:46357") |
| 95 | + conn_id = await service.connect_async(client_config) |
| 96 | + |
| 97 | + # Create app with shared secret |
| 98 | + local_app = service.create_app_with_secret( |
| 99 | + local_name, "my_shared_secret_for_testing_purposes_only" |
| 100 | + ) |
| 101 | + |
| 102 | + # Subscribe to local name |
| 103 | + await local_app.subscribe_async(local_name, conn_id) |
| 104 | + |
| 105 | + # Create server |
| 106 | + server = slim_bindings.Server.new_with_connection(local_app, local_name, conn_id) |
| 107 | + |
| 108 | + # Add servicer |
| 109 | + add_TestServicer_to_server(TestService(), server) |
| 110 | + |
| 111 | + # Run server |
| 112 | + logger.info("Server starting...") |
| 113 | + await server.serve_async() |
| 114 | + |
| 115 | + |
| 116 | +def main() -> None: |
| 117 | + """ |
| 118 | + Main entry point for the server. |
| 119 | + """ |
| 120 | + logging.basicConfig(level=logging.DEBUG) |
| 121 | + try: |
| 122 | + asyncio.run(amain()) |
| 123 | + except KeyboardInterrupt: |
| 124 | + print("Server interrupted by user.") |
0 commit comments