YARP  2.3.70.1
Yet Another Robot Platform
An example which shows how to use C++ and DLLS to modify incoming data in an input port
Author
Ali Paikan

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.


simple_dll.png

Requirements

  • Enable and compile portmonitor carrier (ENABLE_yarpcar_portmonitor_carrier=ON in YARP cmake).

Running the example

  • Open a terminal and follow the bellow instruction to compile and build the dll
       $ mkdir $YARP_ROOT/example/portmonitor/simple_dll/build
       $ cd $YARP_ROOT/example/portmonitor/simple_dll/build
       $ cmake ../; make;
    

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

  • Open a terminal and run yarpserver
       $ yarpserver
    
  • Open another terminal (lets call this the sender terminal) and type
       $ yarp write /write
    
  • In the directory where you you built the dll (lets call this the receiver terminal), type
       $ yarp read /read
    
  • In another terminal connect the port as follow:
       $ yarp connect /write /read tcp+recv.portmonitor+type.dll+file.simple_monitor
    

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

class SimpleMonitorObject : public yarp::os::MonitorObject
{
public:
bool create(const yarp::os::Property& options);
void destroy(void);
bool setparam(const yarp::os::Property& params);
void trig(void);
bool accept(yarp::os::Things& thing);
};


#include <stdio.h>
#include <yarp/os/Bottle.h>
#include "Simple.h"
using namespace yarp::os;
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(void)
{
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)
{
Bottle* bt = thing.cast_as<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)
{
Bottle* bt = thing.cast_as<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(void)
{
}