{"id":26864127,"url":"https://github.com/olooce/demorpc","last_synced_at":"2025-03-31T03:38:00.307Z","repository":{"id":254306818,"uuid":"833684472","full_name":"Olooce/demoRPC","owner":"Olooce","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-16T06:38:17.000Z","size":987,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-17T23:34:58.750Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Olooce.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-25T14:35:12.000Z","updated_at":"2024-10-16T06:38:21.000Z","dependencies_parsed_at":"2024-08-22T18:03:00.270Z","dependency_job_id":"6491c67e-423b-48ca-bdc1-239d7024d910","html_url":"https://github.com/Olooce/demoRPC","commit_stats":null,"previous_names":["olooce/demorpc"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Olooce%2FdemoRPC","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Olooce%2FdemoRPC/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Olooce%2FdemoRPC/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Olooce%2FdemoRPC/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Olooce","download_url":"https://codeload.github.com/Olooce/demoRPC/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246413350,"owners_count":20773052,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-31T03:37:59.876Z","updated_at":"2025-03-31T03:38:00.300Z","avatar_url":"https://github.com/Olooce.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DemoRPC:\n\nThis README provides instructions for setting up a gRPC project with Python and C using Protocol Buffers. The project demonstrates RPC communication between a server and clients written in both languages.\n\n## Prerequisites\n\nEnsure you have the following installed on your system:\n- Python (version 3.6 or higher)\n- C Compiler (e.g., GCC)\n- Protocol Buffers Compiler (`protoc`)\n- gRPC Python library\n- gRPC C library\n\n## Project Structure\n\n```plaintext\n.\n├── README.md\n├── service.proto\n├── python_client.py\n├── python_server.py\n├── c_client.c\n├── c_server.c\n└── generated\n    ├── python\n    └── c\n```\n\n## 1. Define the Service\n\nCreate a `service.proto` file with the following content:\n\n```proto\nsyntax = \"proto3\";\n\nservice Service {\n  rpc SayHello (HelloRequest) returns (HelloResponse);\n}\n\nmessage HelloRequest {\n  string name = 1;\n}\n\nmessage HelloResponse {\n  string message = 1;\n}\n```\n\n## 2. Generate Protobuf Files\n\n### Python\n\n1. **Install Protobuf Compiler**\n\n   Ensure `protoc` is installed and available in your PATH.\n\n2. **Install gRPC Python Tools**\n\n   Install the required packages using pip:\n\n   ```sh\n   pip install grpcio grpcio-tools\n   ```\n\n3. **Generate Python Files**\n\n   Run the following command to generate Python files:\n\n   ```sh\n   python -m grpc_tools.protoc --python_out=generated/python --grpc_python_out=generated/python service.proto\n   ```\n\n   This will generate `service_pb2.py` and `service_pb2_grpc.py` in the `generated/python` directory.\n\n### C\n\n1. **Install Protobuf Compiler**\n\n   Ensure `protoc` is installed and available in your PATH.\n\n2. **Install gRPC C Libraries**\n\n   Follow the [gRPC C Quickstart Guide](https://grpc.io/docs/languages/c/quickstart/) to install the gRPC C libraries.\n\n3. **Generate C Files**\n\n   Run the following command to generate C files:\n\n   ```sh\n   protoc --c_out=generated/c --grpc_out=generated/c --plugin=protoc-gen-grpc=`which grpc_c_plugin` service.proto\n   ```\n\n   This will generate `service.pb.c`, `service.pb.h`, `service.grpc.pb.c`, and `service.grpc.pb.h` in the `generated/c` directory.\n\n## 3. Implement Server and Client\n\n### Python\n\n1. **Server Implementation (`python_server.py`)**\n\n   ```python\n   import grpc\n   from concurrent import futures\n   import service_pb2\n   import service_pb2_grpc\n\n   class ServiceServicer(service_pb2_grpc.ServiceServicer):\n       def SayHello(self, request, context):\n           response = service_pb2.HelloResponse(message=f\"Hello, {request.name}\")\n           return response\n\n   def serve():\n       server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))\n       service_pb2_grpc.add_ServiceServicer_to_server(ServiceServicer(), server)\n       server.add_insecure_port('[::]:50051')\n       server.start()\n       server.wait_for_termination()\n\n   if __name__ == '__main__':\n       serve()\n   ```\n\n2. **Client Implementation (`python_client.py`)**\n\n   ```python\n   import grpc\n   import service_pb2\n   import service_pb2_grpc\n\n   def run():\n       with grpc.insecure_channel('localhost:50051') as channel:\n           stub = service_pb2_grpc.ServiceStub(channel)\n           response = stub.SayHello(service_pb2.HelloRequest(name='World'))\n           print(\"Service received: \" + response.message)\n\n   if __name__ == '__main__':\n       run()\n   ```\n\n### C\n\n1. **Server Implementation (`c_server.c`)**\n\n   ```c\n   #include \u003cgrpc/grpc.h\u003e\n   #include \u003cgrpc/impl/codegen/grpc_types.h\u003e\n   #include \"service.grpc.pb.h\"\n\n   typedef struct {\n       grpc_server *server;\n   } Service;\n\n   void SayHello(grpc_call *call, grpc_completion_queue *cq, void *user_data) {\n       // Handle the RPC\n   }\n\n   int main() {\n       grpc_init();\n       Service service = {};\n       grpc_server *server = grpc_server_create(NULL, NULL);\n       grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL);\n       grpc_server_register_completion_queue(server, cq, NULL);\n       grpc_server_start(server);\n       grpc_server_shutdown_and_notify(server, cq, NULL);\n       grpc_completion_queue_shutdown(cq);\n       grpc_completion_queue_destroy(cq);\n       grpc_server_destroy(server);\n       grpc_shutdown();\n       return 0;\n   }\n   ```\n\n2. **Client Implementation (`c_client.c`)**\n\n   ```c\n   #include \u003cgrpc/grpc.h\u003e\n   #include \u003cgrpc/impl/codegen/grpc_types.h\u003e\n   #include \"service.grpc.pb.h\"\n\n   int main() {\n       grpc_init();\n       grpc_channel *channel = grpc_insecure_channel_create(\"localhost:50051\", NULL, NULL);\n       grpc_stub *stub = service_stub_new(channel);\n       grpc_call *call = grpc_channel_create_call(stub, NULL, 0);\n       grpc_call_start_batch(call, NULL);\n       grpc_shutdown();\n       return 0;\n   }\n   ```\n\n## 4. Run the Applications\n\n### Python\n\n1. **Start the Python Server**\n\n   ```sh\n   python python_server.py\n   ```\n\n2. **Run the Python Client**\n\n   ```sh\n   python python_client.py\n   ```\n\n### C\n\n1. **Compile the C Server**\n\n   ```sh\n   gcc -o c_server c_server.c -lgrpc -lprotobuf\n   ```\n\n2. **Compile the C Client**\n\n   ```sh\n   gcc -o c_client c_client.c -lgrpc -lprotobuf\n   ```\n\n3. **Start the C Server**\n\n   ```sh\n   ./c_server\n   ```\n\n4. **Run the C Client**\n\n   ```sh\n   ./c_client\n   ```\n\n## Troubleshooting\n\n- Ensure that `protoc` and gRPC plugins are correctly installed and available in your PATH.\n- Verify that all dependencies for C and Python are properly installed.\n- Check for any errors or missing files and follow the error messages for resolution.\n\nFeel free to update any specifics based on your setup!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folooce%2Fdemorpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Folooce%2Fdemorpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folooce%2Fdemorpc/lists"}