An open API service indexing awesome lists of open source software.

https://github.com/olooce/demorpc


https://github.com/olooce/demorpc

Last synced: about 1 year ago
JSON representation

Awesome Lists containing this project

README

          

# DemoRPC:

This 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.

## Prerequisites

Ensure you have the following installed on your system:
- Python (version 3.6 or higher)
- C Compiler (e.g., GCC)
- Protocol Buffers Compiler (`protoc`)
- gRPC Python library
- gRPC C library

## Project Structure

```plaintext
.
├── README.md
├── service.proto
├── python_client.py
├── python_server.py
├── c_client.c
├── c_server.c
└── generated
├── python
└── c
```

## 1. Define the Service

Create a `service.proto` file with the following content:

```proto
syntax = "proto3";

service Service {
rpc SayHello (HelloRequest) returns (HelloResponse);
}

message HelloRequest {
string name = 1;
}

message HelloResponse {
string message = 1;
}
```

## 2. Generate Protobuf Files

### Python

1. **Install Protobuf Compiler**

Ensure `protoc` is installed and available in your PATH.

2. **Install gRPC Python Tools**

Install the required packages using pip:

```sh
pip install grpcio grpcio-tools
```

3. **Generate Python Files**

Run the following command to generate Python files:

```sh
python -m grpc_tools.protoc --python_out=generated/python --grpc_python_out=generated/python service.proto
```

This will generate `service_pb2.py` and `service_pb2_grpc.py` in the `generated/python` directory.

### C

1. **Install Protobuf Compiler**

Ensure `protoc` is installed and available in your PATH.

2. **Install gRPC C Libraries**

Follow the [gRPC C Quickstart Guide](https://grpc.io/docs/languages/c/quickstart/) to install the gRPC C libraries.

3. **Generate C Files**

Run the following command to generate C files:

```sh
protoc --c_out=generated/c --grpc_out=generated/c --plugin=protoc-gen-grpc=`which grpc_c_plugin` service.proto
```

This will generate `service.pb.c`, `service.pb.h`, `service.grpc.pb.c`, and `service.grpc.pb.h` in the `generated/c` directory.

## 3. Implement Server and Client

### Python

1. **Server Implementation (`python_server.py`)**

```python
import grpc
from concurrent import futures
import service_pb2
import service_pb2_grpc

class ServiceServicer(service_pb2_grpc.ServiceServicer):
def SayHello(self, request, context):
response = service_pb2.HelloResponse(message=f"Hello, {request.name}")
return response

def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
service_pb2_grpc.add_ServiceServicer_to_server(ServiceServicer(), server)
server.add_insecure_port('[::]:50051')
server.start()
server.wait_for_termination()

if __name__ == '__main__':
serve()
```

2. **Client Implementation (`python_client.py`)**

```python
import grpc
import service_pb2
import service_pb2_grpc

def run():
with grpc.insecure_channel('localhost:50051') as channel:
stub = service_pb2_grpc.ServiceStub(channel)
response = stub.SayHello(service_pb2.HelloRequest(name='World'))
print("Service received: " + response.message)

if __name__ == '__main__':
run()
```

### C

1. **Server Implementation (`c_server.c`)**

```c
#include
#include
#include "service.grpc.pb.h"

typedef struct {
grpc_server *server;
} Service;

void SayHello(grpc_call *call, grpc_completion_queue *cq, void *user_data) {
// Handle the RPC
}

int main() {
grpc_init();
Service service = {};
grpc_server *server = grpc_server_create(NULL, NULL);
grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL);
grpc_server_register_completion_queue(server, cq, NULL);
grpc_server_start(server);
grpc_server_shutdown_and_notify(server, cq, NULL);
grpc_completion_queue_shutdown(cq);
grpc_completion_queue_destroy(cq);
grpc_server_destroy(server);
grpc_shutdown();
return 0;
}
```

2. **Client Implementation (`c_client.c`)**

```c
#include
#include
#include "service.grpc.pb.h"

int main() {
grpc_init();
grpc_channel *channel = grpc_insecure_channel_create("localhost:50051", NULL, NULL);
grpc_stub *stub = service_stub_new(channel);
grpc_call *call = grpc_channel_create_call(stub, NULL, 0);
grpc_call_start_batch(call, NULL);
grpc_shutdown();
return 0;
}
```

## 4. Run the Applications

### Python

1. **Start the Python Server**

```sh
python python_server.py
```

2. **Run the Python Client**

```sh
python python_client.py
```

### C

1. **Compile the C Server**

```sh
gcc -o c_server c_server.c -lgrpc -lprotobuf
```

2. **Compile the C Client**

```sh
gcc -o c_client c_client.c -lgrpc -lprotobuf
```

3. **Start the C Server**

```sh
./c_server
```

4. **Run the C Client**

```sh
./c_client
```

## Troubleshooting

- Ensure that `protoc` and gRPC plugins are correctly installed and available in your PATH.
- Verify that all dependencies for C and Python are properly installed.
- Check for any errors or missing files and follow the error messages for resolution.

Feel free to update any specifics based on your setup!