This example demonstrates how to use the port the portmonitor object at both side of a connection to encode and decode the data. The port '/write' from 'yarp write' module is connected to the '/read' port of 'yarp read' using two portmonitors plugged into the sender and receiver side. Both portmonitors load the same Lua script ('codec.lua') which can act as data coder or decoder depending on the which side of the connection it is attached.
$ yarpserver
$ cd $YARP_ROOT/example/portmonitor/coder_decoder $ yarp write /write
$ cd $YARP_ROOT/example/portmonitor/coder_decoder $ yarp read /read
$ yarp connect /write /read tcp+send.portmonitor+type.lua+file.codec
Now if you write something in the 'sender' terminal, you will see the original text is encoded with a simple base64 encoder and transmitted to the receiver. For example:
[sender terminal] Hello [receiver terminal] "SGVsbG8="
Now try to plug the ‘codec.lua’ to the receiver side too.
$ yarp connect /write /read tcp+send.portmonitor+file.codec+recv.portmonitor+file.codec
You will see the data gets decoded and the original text will be shown in the receiver terminal:
[sender terminal] Hello [receiver terminal] "Hello"
Notice that codec.lua acts as coder or decoder depending to which side of the connection it is attached. This is checked in the 'PortMonitor.create(options)' callback.