/**
\page type_modification An example which shows how to use a Lua script to create a new data type in a port

\tableofcontents

\section type_modification_desc Description
This example demonstrates how to use the port monitor carrier to substitute a data type with another one.
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 Lua script (\c 'type_modifier.lua') which expects to see a command such as 'random N' (where N is an integer number). The command is, then, completely substituted with a vector of N random values which will be delivered to the 'yarp read'. The port monitor also checks for the validity of the command



\image html type_modification.png ""


\section type_modification_req Requirements
\li Enable and compile portmonitor carrier (ENABLE_yarpcar_portmonitor_carrier=ON in YARP cmake).

\li Compile YARP with Lua binding support (see \ref yarp_swig "Using YARP from python, java, ruby, C#, and other languages").

\li Set LUA_CPATH to include YARP-Lua binding library  (e.g., export LUA_CPATH=";;;$YARP_ROOT/build/lib/lua/?.so")


\section type_modification_running Running the example

\li Open a terminal and run yarpserver
\verbatim
   $ yarpserver
\endverbatim

\li Open another terminal (lets call this the receiver terminal) and change to the 'type_modification' directory:
\verbatim
   $ cd $YARP_ROOT/example/portmonitor/type_modification
   $ yarp read /read
\endverbatim

\li Open another terminal (lets call this the sender terminal) and type
\verbatim
   $ yarp write /write
\endverbatim

\li In another terminal connect the port as follow
\verbatim
   $ yarp connect /write /read tcp+recv.portmonitor+type.lua+file.type_modifier
\endverbatim

Now if you write a valid command in the 'sender' terminal (e.g., random 3), you will see the command is completely substituted by a vector of 3 random values. For example:

\verbatim
[sender terminal]
   random 3
\endverbatim

\verbatim
[receiver terminal]
   0.513401 0.95223 0.916195
\endverbatim


\section type_modification_scripts Scripts

\subsection type_modification_type_modifier type_modifier.lua

~~~{.lua}
-- loading lua-yarp binding library
require("yarp")

--
-- accept is called when the port receives new data
-- @param thing The Things abstract data type
-- @return Boolean
-- if false is returned, the data will be ignored
-- and update() will never be called
PortMonitor.accept = function(thing)
     bt = thing:asBottle()
     if bt:size() &lt; 2 or bt:get(0):asString() ~= "random" then
        print("type_modifier: invalid command! (e.g., random 10)")
        return false;
     end
    return true
end

--
-- update is called when the port receives new data
-- @param thing The Things abstract data type
-- @return Things
PortMonitor.update = function(thing)
    bt = thing:asBottle()
    th = yarp.Things()
    vec = yarp.Vector()
    for i=1,bt:get(1):asInt32() do
        vec:push_back(math.random())
    end
    th:setPortWriter(vec)
    return th
end
~~~

*/
