How to create a C++ gRPC application

From epsciwiki
Revision as of 22:43, 16 December 2022 by Timmer (talk | contribs)
Jump to navigation Jump to search

Setup some environmental variables (assuming bash)
export GRPC_INSTALL_DIR=/daqfs/gRPC/installation
export PATH="$GRPC_INSTALL_DIR/bin:$PATH"
export LD_LIBRARY_PATH="$GRPC_INSTALL_DIR/lib:$LD_LIBRARY_PATH"
Start by copying the hello world example and compiling it (official instructions here). The compilation steps differ slightly between examples.
cd <my_gRPC_dir>
mkdir ejfat
cd ejfat
mkdir cpp protos

cp /daqfs/gRPC/grpc/examples/protos/helloworld.proto protos/.
cp -r /daqfs/gRPC/grpc/examples/cpp/cmake cpp/.
cp -r /daqfs/gRPC/grpc/examples/cpp/helloworld cpp/.

cd cpp/helloworld
mkdir -p cmake/build
cd cmake/build
cmake -DCMAKE_PREFIX_PATH=$GRPC_INSTALL_DIR -DBUILD_SHARED_LIBS=ON ../..
make -j 4
Rename a few files and directories, from helloworld to loadBalancerControl (or whatever you want)
cd <my_gRPC_dir>/ejfat

mv protos/helloworld.proto protos/loadBalancerControl.proto
mv cpp/helloworld cpp/loadBalancerControl

As an example, implement ERSAP backend reassembler communication of fifo fill percentage to load-balancer control plane

Start by modifying loadBalancerControl.proto to define the message and the communication API. Make it look like the following and don't worry about option and package statements.
// The ERSAP backend state reporting service definition.
service BackendState {
  // Sends a request to get the backend's state
  rpc GetState (StateRequest) returns (StateReply) {}
}

// The get-state request message containing the LB control plane's name.
message StateRequest {
  string name = 1;
}

// The response message containing the backend's current state
message StateReply {
  int32  bufferCount = 1;     // number of backend's buffers or fifo entries
  int32  bufferSize  = 2;     // size in bytes of each buffer or fifo entry
  int32  fillPercent = 3;     // % of fifo entries that are filled with unprocessed data
  int32  pidError    = 4;     // PID loop error term in percentage of fifo entries
}


Next modify 5 lines in the loadBalancerControl/CMakefile.txt in order to reflect file/directory name changes. Get the proto file path correct.
# Proto file
get_filename_component(hw_proto "../../protos/loadBalancerControl.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)

# Generated sources
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/loadBalancerControl.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/loadBalancerControl.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/loadBalancerControl.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/loadBalancerControl.grpc.pb.h")
Now recompile
cd cpp/loadBalancerControl
rm -fr cmake
mkdir -p cmake/build
cd cmake/build
cmake -DCMAKE_PREFIX_PATH=$GRPC_INSTALL_DIR -DBUILD_SHARED_LIBS=ON ../..
make -j 4

The application in this case is the reporting to the control plane of the fill level of an ERSAP backend reassembler's fifo.