C++ gRPC client module#

namespace client#

Namespace including the API Eigen Example Client implemented in C++.

class GRPCClient#

Class containing the basic functionalities to interact with the API Eigen Example server.

Public Functions

GRPCClient(const std::string host = std::string{"0.0.0.0"}, const int port = 50000, const bool debug_log = false)#

Construct a new GRPC Client object.

Parameters:
  • host – the host (DNS/IP) where the server is located. Default: 0.0.0.0.

  • port – the port through which the server is exposed. Default: 50000.

  • debug_log – whether to show the enhanced debugging logs or not. Default: false.

~GRPCClient()#

Destroy the GRPC Client object.

void request_greeting(const std::string &name)#

Method to request a greeting from the endpoint server.

Parameters:

name – the name of the entity requesting the greeting (i.e. us).

std::vector<double> flip_vector(const std::vector<double> &vec)#

Method in charge of requesting a vector position flip to the endpoint server.

Parameters:

vec – the first vector involved in the operation.

Returns:

std::vector<double>

std::vector<double> add_vectors(const std::vector<double> &vec1, const std::vector<double> &vec2)#

Method in charge of requesting a vector addition to the endpoint server.

Parameters:
  • vec1 – the first vector involved in the operation.

  • vec2 – the second vector involved in the operation.

Returns:

std::vector<double>

double multiply_vectors(const std::vector<double> &vec1, const std::vector<double> &vec2)#

Method in charge of requesting a vector dot product to the endpoint server.

Parameters:
  • vec1 – the first vector involved in the operation.

  • vec2 – the second vector involved in the operation.

Returns:

double

std::vector<std::vector<double>> add_matrices(const std::vector<std::vector<double>> &mat1, const std::vector<std::vector<double>> &mat2)#

Method in charge of requesting a matrix addition to the endpoint server.

Parameters:
  • mat1 – the first matrix involved in the operation.

  • mat2 – the second matrix involved in the operation.

Returns:

std::vector<std::vector<double>>

std::vector<std::vector<double>> multiply_matrices(const std::vector<std::vector<double>> &mat1, const std::vector<std::vector<double>> &mat2)#

Method in charge of requesting a matrix multiplication to the endpoint server.

Parameters:
  • mat1 – the first matrix involved in the operation.

  • mat2 – the second matrix involved in the operation.

Returns:

std::vector<std::vector<double>>

Private Functions

std::vector<std::vector<int>> define_vecstream_metadata(::grpc::ClientContext *context, const std::vector<double> &vec1, const std::vector<double> &vec2 = {})#

Method in charge of defining the Client Metadata in the bidirectional stream transfer of Vector messages.

Parameters:
  • context – the gRPC context.

  • vec1 – the vector to be transmitted.

  • vec2 – (optional) the second vector to be transmitted.

Returns:

std::vector<std::vector<int>>

std::vector<int> set_vector_metadata(::grpc::ClientContext *context, const std::vector<double> &vec, const std::string &vec_name)#

Set the Vector-specific message metadata (i.e. how many partial Vector messages constitute an entire Vector).

Parameters:
  • context – the gRPC context.

  • vec – the vector to be transmitted.

  • vec_name – the identifier of the vector.

Returns:

std::vector<int>

std::vector<std::vector<int>> define_matstream_metadata(::grpc::ClientContext *context, const std::vector<std::vector<double>> &mat1, const std::vector<std::vector<double>> &mat2)#

Method in charge of defining the Client Metadata in the bidirectional stream transfer of Matrix messages.

Parameters:
  • context – the gRPC context.

  • mat1 – the first matrix to be transmitted.

  • mat2 – the second matrix to be transmitted.

Returns:

std::vector<std::vector<int>>

std::vector<int> set_matrix_metadata(::grpc::ClientContext *context, const std::vector<std::vector<double>> &mat, const std::string &mat_name)#

Set the Matrix-specific message metadata (i.e. how many partial Matrix messages constitute an entire Matrix).

Parameters:
  • context – the gRPC context.

  • mat – the matrix to be transmitted.

  • mat_name – the identifier of the matrix.

Returns:

std::vector<int>

std::vector<double> deserialize_vector(const std::string &bytes, const int length, grpcdemo::DataType type)#

Method used to deserialize a Vector message into an std::vector<double> object.

Parameters:
  • bytes – the chunk of bytes from where the vector is deserialized.

  • length – the length of the vector we are deserializing.

  • type – the type of data inside the vector (e.g. double, int…).

Returns:

std::vector<double>

std::string serialize_vector(const std::vector<double> &vector, const int start, const int end)#

Method used to serialize an std::vector<double> object into a Vector message.

Parameters:
  • vector – the std::vector<double> to be serialized.

  • start – the starting index to serialize.

  • end – the last index to serialize (not included).

Returns:

std::string

std::vector<std::vector<double>> deserialize_matrix(const std::string &bytes, const int rows, const int cols, grpcdemo::DataType type)#

Method used to deserialize a Matrix message into an std::vector<std::vector<double>> object.

Parameters:
  • bytes – the chunk of bytes from where the matrix is deserialized.

  • rows – the number of rows of the matrix we are deserializing.

  • cols – the number of columns of the matrix we are deserializing.

  • type – the type of data inside the matrix (e.g. double, int…).

Returns:

std::vector<std::vector<double>>

std::string serialize_matrix(const std::vector<std::vector<double>> &matrix, const int start, const int end)#

Method used to serialize an std::vector<std::vector<double>> object into a Matrix message.

Parameters:
  • matrix – the std::vector<std::vector<double>> to be serialized.

  • start – the starting row index to serialize.

  • end – the last row index to serialize (not included).

Returns:

std::string

void send_vector(std::unique_ptr<::grpc::ClientReaderWriter<grpcdemo::Vector, grpcdemo::Vector>> &reader_writer, const std::vector<double> &vector, const std::vector<int> &chunks)#

Method in charge of sending a message for stream-based inputs in RPC method. Targeted to Vector messages.

Parameters:
  • reader_writer – the writer used for streaming the messages.

  • vector – the message to be streamed.

  • chunks – number of elements in each individual Vector message.

void send_matrix(std::unique_ptr<::grpc::ClientReaderWriter<grpcdemo::Matrix, grpcdemo::Matrix>> &reader_writer, const std::vector<std::vector<double>> &matrix, const std::vector<int> &chunks)#

Method in charge of sending a message for stream-based inputs in RPC method. Targeted to Matrix messages.

Parameters:
  • reader_writer – the writer used for streaming the messages.

  • matrix – the message to be streamed.

  • chunks – number of elements in each individual Vector message.

std::vector<double> receive_vector(std::unique_ptr<::grpc::ClientReaderWriter<grpcdemo::Vector, grpcdemo::Vector>> &reader_writer, ::grpc::ClientContext *context)#

Method in charge of providing the resulting Vector of an operation requested to the server from a stream of partial Vector messages.

Parameters:
  • reader_writer – the gRPC reader-writer in the bidirectional stream protocol.

  • context – the gRPC context.

Returns:

std::vector<double>

std::vector<std::vector<double>> receive_matrix(std::unique_ptr<::grpc::ClientReaderWriter<grpcdemo::Matrix, grpcdemo::Matrix>> &reader_writer, ::grpc::ClientContext *context)#

Method in charge of providing the resulting Matrix of an operation requested to the server from a stream of partial Matrix messages.

Parameters:
  • reader_writer – the gRPC reader-writer in the bidirectional stream protocol.

  • context – the gRPC context.

Returns:

std::vector<std::vector<double>>

Private Members

std::unique_ptr<grpcdemo::GRPCDemo::Stub> _stub#

A unique pointer to the stub which defines the gRPC connection (Channel).

bool _debug_log#

Boolean indicating whether to show the debugging logs or not.