All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tutorial: Remote Procedure Calls (RPC)


This tutorial extends the "FloatProducer" unit from Tutorial: Creating a Unit that Publishes Data.


Beside Channels, Remote Procedure Calls (RPC) are the second mechanism that can be used to communicate with other modules outside of your Unit. Remote Procedure Calls are suitable for "request - reply" interactions since they allow to call a procedure or method.

Adding the Service Method

So we like to provide a service method that computes the mean on a vector of float values. First we need to add a method that does exactly that:

float calculateMean(const std::vector<float>& values)
if (values.size() == 0)
MIRA_THROW(XInvalidParameter, "The vector contains no data");
float sum = 0.0;
foreach(float f, values)
sum += f;
return sum / values.size();

Nothing special here. Please note that we throw an exception if the vector is emtpy. Throwing exceptions within RPC methods is not a problem. They are transported to the caller side and are rethrown there, if necessary.

Publishing the Service

Now lets expose this method as a service function that can be called by other modules using RPC. For this purpose, we need to extend our reflect method and add an include that provides the reflect mechanism for std::vectors, since such a vector is used as parameter of the method and needs to be serialized.

#include <serialization/adapters/std/vector>
template<typename Reflector>
void reflect(Reflector& r)
r.method("calculateMean", &MeanCalculator::calculateMean, this,
"This method calculates the mean of a vector of floats",
"values", "container with values", std::vector<float>({3,4,5}));

Finally we need to publish our unit as a service:

virtual void initialize()

And we are done. Now other modules can call our "calculateMean" method via RPC and will be presented with a nice and clean mean - or an exception if they pass an empty vector.

Testing the Method in miracenter


For more information on how to use RPC, please refer to Services and Remote Procedure Calls.