## Set some parameters for the page #pragma section-numbers 2 #pragma keywords HATP, hatp, htn planning, hatpconsole, hatptester #pragma description Homepage for HATP software, an open-source HTN task planner = --DEPRECATED-- msgconnector, HATP's middleware --DEPRECATED-- = || This module is now deprecated ! HATP relies on ROS to transport messages. || This page presents msgconnector's usage. msgconnector is responsible to carry messages form a module to another. It is also responsible to the connection to other middlewares: OPRS, ORO, YARP. || <>|| /!\ '''The way to install msgconnector is described in [[HATP/install]].''' == Principle == msgconnector is composed of a server (`MsgSever`) that once started allows the different clients to connect. When a client connects it gives its name. This name is then used to carry a message from a client to another. It is possible to declare several clients with the same name, then all the message are repeated to all the clients with the same name. The message passed are usually using the JSON format (see [[http://en.wikipedia.org/wiki/JSON|JSON]]) but it is not mandatory. Finally msgconnector can be connected to other middleware to extend the reach of HATP's messages. To do so bridges need to be started. So far the implemented bridge are: * OPRS * ORO * YARP * print (which is in fact just a backend to print all messages that it receives). ''If you need an additional bridge put the request on the tracker, see the troubleshooting section at the bottom of this page.'' == Start the server == <> The default usage for the server is simply: {{{ MsgServer }}} It starts the server on `localhost` and on the port `5500`. The help gives: {{{ MsgServer --help Usage: Default: MsgServer Compact: MsgServer address:port [max_time] Standard: MsgServer address port [max_time] Complete: MsgServer [-a address] [-p port] [-t max_time] [-m max_connections] Allowed options: --help Produce help message -a [ --address ] arg The address of the server (default : localhost) -p [ --port ] arg The port number of the server (default : 5500) -t [ --time ] arg The maximum inactivity time in second before the automatic-shutdown of the server (default : 3600) -n [ --noLimit ] Deactivate the time limit, the server runs until shutdown by hand (opposite of time) -m [ --max-clients ] arg Maximum number of clients that can connect at the same time (default : 10) }}} It is indeed possible to change the following parameters: * `adrress`, `port` on which the server starts * `time` which is how long the server will wait after the last message before automatically quitting * `max_clients` which is the number of clients that can be connected at the same time. To connect the !MsgServer over the network the address should be ":" or nothing to connect over the network, not localhost. == Use a bridge == === Use OPRS bridge === <> {{{ msgconnector-OPRS-bridge }}} The parameters are: * ``: Registered name of hatponboard, usually "`HATP`". * ``: Name of this bridge on OPRS's side. * ``: The address of msgconnector's server, usually "`localhost`".* * ``: Name of this bridge on HATP's side, should be the name of the OPRS module you want to talk to. * ``: The address of OPRS's server.* * ``: Can be `0` or `1` to toggle the verbose mode (all message processed are printed out). *: The address can be a hostname like `localhost`. === Use ORO bridge === <> {{{ msgconnector-ORO-bridge }}} The parameters are: * ``: The name of this bridge on HATP's side. * ``: The address of msgconnector's server, usually "`localhost`". * ``: The address of ORO's server. * ``: Can be `0` or `1` to toggle the verbose mode (all message processed are printed out). === Use YARP bridge === <> {{{ msgconnector-YARP-bridge }}} The parameters are: * ``: Name of the HATP's module that will be the target of the bridge messages. * ``: The address of msgconnector's server, usually "`localhost`". * ``: The name of this bridge on HATP's side. * ``: The name of this bridge on YARP's side. * ``: Can be `0` or `1` to toggle the verbose mode (all message processed are printed out). === Use the print bridge === <> {{{ msgconnector-PRINT-bridge }}} The parameters are: * ``: Name of this bridge. * ``: The address of msgconnector's server, usually "`localhost`". == Introduction to the API == This section shows some basic usages to send and receive messages using msgconnector. === Create a client === The msgconnector API is client based, the server is responsible of transferring the messages. If several clients have the same name they will all receive the messages sent to one, the message transfer is based on the name of the agents. ==== Add msgconnector to a project ==== There are two ways to integrate msgconnector to a project: (1) using CMake and find_package, or (2) using pkgconfig. In this section we only show the CMake fashion. Here is the extract of the CMakeLists.txt: {{{#!highlight cmake numbers=disable find_package(msgconnector REQUIRED) include_directories(${msgconnector_INCLUDE_DIRS}) target_link_libraries( ${msgconnector_LIBRARIES}) }}} However if msgconnector was not installed system wide, for instance if it was installed using robotpkg you will have to specifiy the path to the `Findmsgconnector.cmake`. To do so, invoke the CMake executable as follow: {{{ cmake [] -DCMAKE_MODULES_PATH= }}} If msgconnetor has been installed using robotpkg (as advised) then the path is `-DCMAKE_MODULES_PATH=${ROBOTPKG_BASE}/share/cmake/Modules`. ==== Connect a new client ==== Here is the minimum code to connect to msgconnector with a new client: {{{#!highlight c++ numbers=disable #include #include int main(int argc, char ** argv){ msgClient client; client.connect(, , ); } }}} When calling `connect`, the `name` corresponds to the name of this client, the `server` can be: an IP address or a hostname (by default use `"localhsot"`) finally the `server_port` is 5500 by default but can be any port the server listens to. ==== Send messages and broadcast messages ==== To send a message there are two options: send a message to a recipient or send a broadcast message. When the message is sent to a recipient, the message is transferred by the server to '''ALL''' client with the target name. The broadcast is simply sent to all the clients. If a message is sent to a recipient not connected, it does not trigger an error, it is supposed a normal behaviour. Reminder: the message are usually using the [[http://en.wikipedia.org/wiki/JSON|JSON format]]. To send a message to a recipient: {{{#!highlight c++ numbers=disable client.sendMessage(, ); }}} To send a broadcast message: {{{#!highlight c++ numbers=disable client.sendBroadcastMessage(); }}} The parameters for both send functions are `std::string`. ==== Receive messages ==== Now that message can be sent, it is time to receive messages. There are three ways to retrieve messages: it is possible to wait for the message (blocking), or wait for the message for a given time (blocking with a timeout) and finally it is possible to directly get a message if present. ===== Wait for a message ===== Using this solution the code will wait indefinitely for a message to arrive, the only things that can stop it are: a message or the server exiting. {{{#!highlight c++ numbers=disable while(client.isConnected()){ std::pair result = client.getBlockingMessage(); std::cout<<"#### Message from "<`. However if the `sender` is the empty string it means an error occurred, then the `message` will contain `"NOT_CONNECTED"` (defined as `#define STATUS_DISCONNECTED "NOT_CONNECTED"`. ===== Wait for a message (with a timeout) ===== This solution allows to wait for a message for a given time, if the time is exceeded a timeout is returned. {{{#!highlight c++ numbers=disable while(client.isConnected()){ std::pair result = client.getMessageTimeout(