YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
An example which shows how to use C++ and DLLS to modify incoming data in an input port
+ Collaboration diagram for An example which shows how to use C++ and DLLS to modify incoming data in an input port:

Description

This example demonstrates how to simply use the port monitor carrier to modify data going through a connection. The port '/write' from 'yarp write' module is connected to the '/read' port of 'yarp read' using a portmonitor plugged into the receiver side. The portmoniotr loads a dll ('libsimple_monitor.so') in which we access and modify the data going through the port.


Requirements

Running the example

you should see the 'libsimple_monitor.so' after the compilation (the generated dll can have different names on windows or mac os).

Now if you write something in the 'sender' terminal, you will see the text "Modified in DLL" will be added to the original message. For example:

[sender terminal]
 Hello
[receiver terminal]
 Hello "Modified in DLL"

As it is constrained in ‘SimpleMonitorObject::accept()’ method from ‘Simple.cpp’, if you type "ignore", the message will be ignored by the portmonitor and it never be delivered to the input port.

Code Samples

SimpleMonitorObject.h

class SimpleMonitorObject : public yarp::os::MonitorObject
{
public:
bool create(const yarp::os::Property& options) override;
void destroy() override;
bool setparam(const yarp::os::Property& params) override;
bool getparam(yarp::os::Property& params) override;
void trig() override;
bool accept(yarp::os::Things& thing) override;
};
virtual void trig()
This will be called when one of the peer connections to the same import port receives data.
virtual bool setparam(const yarp::os::Property &params)
This will be called when the portmonitor carrier parameters are set via YARP admin port.
virtual yarp::os::Things & update(yarp::os::Things &thing)
After data get accpeted in the accept() callback, an instance of that is given to the update function...
virtual bool getparam(yarp::os::Property &params)
This will be called when the portmonitor carrier parameters are requested via YARP admin port.
virtual void destroy()
This will be called when the portmonitor object destroyes.
virtual bool accept(yarp::os::Things &thing)
This will be called when the data reach the portmonitor object.
virtual bool create(const yarp::os::Property &options)
This will be called when the dll is properly loaded by the portmonitor carrier.
A class for storing options and configuration information.
Definition Property.h:33
Base class for generic things.
Definition Things.h:18

SimpleMonitorObject.cpp

#include "Simple.h"
#include <yarp/os/Bottle.h>
#include <yarp/os/Things.h>
#include <stdio.h>
YARP_DEFINE_SHARED_SUBCLASS(MonitorObject_there, SimpleMonitorObject, MonitorObject);
bool SimpleMonitorObject::create(const yarp::os::Property& options)
{
printf("created!\n");
printf("I am attached to the %s\n",
(options.find("sender_side").asBool()) ? "sender side" : "receiver side");
return true;
}
void SimpleMonitorObject::destroy()
{
printf("destroyed!\n");
}
bool SimpleMonitorObject::setparam(const yarp::os::Property& params)
{
return false;
}
bool SimpleMonitorObject::getparam(yarp::os::Property& params)
{
return false;
}
bool SimpleMonitorObject::accept(yarp::os::Things& thing)
{
auto* bt = thing.cast_as<yarp::os::Bottle>();
if (bt == NULL) {
printf("SimpleMonitorObject: expected type Bottle but got wrong data type!\n");
return false;
}
if (bt->toString() == "ignore")
return false;
return true;
}
yarp::os::Things& SimpleMonitorObject::update(yarp::os::Things& thing)
{
auto* bt = thing.cast_as<yarp::os::Bottle>();
if (bt == NULL) {
printf("SimpleMonitorObject: expected type Bottle but got wrong data type!\n");
return thing;
}
bt->addString("Modified in DLL");
return thing;
}
void SimpleMonitorObject::trig()
{
}
#define YARP_DEFINE_SHARED_SUBCLASS(factoryname, classname, basename)
Macro to create a bunch of functions with undecorated names that can be found within a plugin library...
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
T * cast_as()
Definition Things.h:53
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:186