forked from robotology/yarp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport_monitor_simple_dll.dox
157 lines (114 loc) · 4.03 KB
/
port_monitor_simple_dll.dox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
\page simple_dll An example which shows how to use C++ and DLLS to modify incoming data in an input port
\tableofcontents
\section simple_dll_desc 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 (\c 'libsimple_monitor.so') in which we access and modify the data going through the port.
<br>
\image html simple_dll.png ""
\section simple_dll_req Requirements
\li Enable and compile portmonitor carrier (ENABLE_yarpcar_portmonitor_carrier=ON in YARP cmake).
\section simple_dll_running Running the example
\li Open a terminal and follow the below instruction to compile and build the dll
\verbatim
$ mkdir $YARP_ROOT/example/portmonitor/simple_dll/build
$ cd $YARP_ROOT/example/portmonitor/simple_dll/build
$ cmake ../; make;
\endverbatim
you should see the \c 'libsimple_monitor.so' after the compilation (the generated dll can have
different names on windows or mac os).
\li Open a terminal and run yarpserver
\verbatim
$ yarpserver
\endverbatim
\li Open another terminal (lets call this the sender terminal) and type
\verbatim
$ yarp write /write
\endverbatim
\li In the directory where you you built the dll (lets call this the receiver terminal), type
\verbatim
$ yarp read /read
\endverbatim
\li In another terminal connect the port as follow:
\verbatim
$ yarp connect /write /read tcp+recv.portmonitor+type.dll+file.simple_monitor
\endverbatim
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:
\verbatim
[sender terminal]
Hello
\endverbatim
\verbatim
[receiver terminal]
Hello "Modified in DLL"
\endverbatim
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.
\section simple_dll_sample Code Samples
\subsection simple_dll_sample_h SimpleMonitorObject.h
~~~{.cpp}
#include <yarp/os/MonitorObject.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;
yarp::os::Things& update(yarp::os::Things& thing) override;
};
~~~
\subsection simple_dll_sample_cpp SimpleMonitorObject.cpp
~~~{.cpp}
#include <stdio.h>
#include <yarp/os/Bottle.h>
#include <yarp/os/SharedLibraryClass.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()
{
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()
{
}
~~~
*/