Skip to content

Examples of C++-Python interoperability using Thrift services based on SWIG.

License

Notifications You must be signed in to change notification settings

xEnVrE/thrift-cpp-python-interop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

thrift-cpp-python-interop

Examples of C++-Python interoperability using Thrift services based on SWIG and YARP.

Requirements

  • YARP compiled with Python bindings
  • SWIG

Thrift service definition used in this repository

The test case considered in this repository is related to the following Thrift service definition:

struct YarpMatrix
{
}
(
yarp.name="yarp::sig::Matrix"
yarp.includefile="yarp/sig/Matrix.h"
)
struct YarpVector
{
}
(
yarp.name="yarp::sig::Vector"
yarp.includefile="yarp/sig/Vector.h"
)
service Service
{
list<YarpMatrix> get_poses();
YarpVector get_position();
double get_distance();
}

where the service Service offers three methods:

  • get_poses returns a list of yarp::sig::Matrix 4x4 matrices representing poses
  • get_position returns a yarp::sig::Vector vector representing a 3D position
  • get_distance return a double representing a distance

Builds the C++ library that exports the Service class

git clone https://github.com/xEnVrE/thrift-cpp-python-interop
cd thrift-cpp-python-interop/cpp-library
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=<your_install_prefix> ../
make install

Builds the Python library that exports the Service class

Note: it is required to build and install the C++ library first.

Warning: It is suggested to build and install the library within a virtual environment.

(env) git clone https://github.com/xEnVrE/thrift-cpp-python-interop
(env) cd thrift-cpp-python-interop/python-library
(env) mkdir build && cd build
(env) cmake ../
(env) make install

Use the service in a C++ CMake-based project

find_package(ThriftCppPythonInterop REQUIRED)
ThriftCppPythonInterop::ThriftCppPythonInterop

Note: the names of the target can be changed in the following:

project(ThriftCppPythonInterop
set(LIBRARY_TARGET_NAME ThriftCppPythonInterop)

Write a client/server node in C++

In order to build and run the client:

git clone https://github.com/xEnVrE/thrift-cpp-python-interop
cd thrift-cpp-python-interop/samples/cpp-client
mkdir build && cd build
make
./tcpi-cpp-client

The client opens an RPC port /tcpi/cpp-client/rpc:o.

In order to build and run the server:

git clone https://github.com/xEnVrE/thrift-cpp-python-interop
cd thrift-cpp-python-interop/samples/cpp-server
mkdir build && cd build
make
./tcpi-cpp-server

The server opens an RPC port /tcpi/cpp-server/rpc:i.

Use the service in a Python project

Note: the names of the library can be changed in the following:

%module(directors="1", package="tcpi") tcpi
set(LIBRARY_TARGET_NAME tcpi)

Write a client/server node in Python

In order to build and run the client:

git clone https://github.com/xEnVrE/thrift-cpp-python-interop
cd thrift-cpp-python-interop/samples/python-client
python client.py

The client opens an RPC port /tcpi/python-client/rpc:o.

In order to build and run the server:

git clone https://github.com/xEnVrE/thrift-cpp-python-interop
cd thrift-cpp-python-interop/samples/python-server
python server.py

The server opens an RPC port /tcpi/python-server/rpc:i.

Connection between C++ and Python nodes

It is as easy as connecting the corresponding YARP ports using yarp connect.

About

Examples of C++-Python interoperability using Thrift services based on SWIG.

Topics

Resources

License

Stars

Watchers

Forks