simple_receiver.cc
Go to the documentation of this file.
00001 /*
00002  * This file is part of the rc_dynamics_api package.
00003  *
00004  * Copyright (c) 2017 Roboception GmbH
00005  * All rights reserved
00006  *
00007  * Author: Christian Emmerich
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions are met:
00011  *
00012  * 1. Redistributions of source code must retain the above copyright notice,
00013  * this list of conditions and the following disclaimer.
00014  *
00015  * 2. Redistributions in binary form must reproduce the above copyright notice,
00016  * this list of conditions and the following disclaimer in the documentation
00017  * and/or other materials provided with the distribution.
00018  *
00019  * 3. Neither the name of the copyright holder nor the names of its contributors
00020  * may be used to endorse or promote products derived from this software without
00021  * specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  * POSSIBILITY OF SUCH DAMAGE.
00034  */
00035 
00036 #include <rc_dynamics_api/remote_interface.h>
00037 
00038 #include <signal.h>
00039 
00040 #ifdef WIN32
00041 #include <winsock2.h>
00042 #undef min
00043 #undef max
00044 #endif
00045 
00046 using namespace std;
00047 namespace rcdyn = rc::dynamics;
00048 
00052 static bool caught_signal = false;
00053 void signal_callback_handler(int signum)
00054 {
00055   printf("Caught signal %d, stopping program!\n", signum);
00056   caught_signal = true;
00057 }
00058 
00062 void printUsage(char* arg)
00063 {
00064   cout << "\nRequests a data stream from the specified rc_visard IP "
00065           "\nand simply prints received data to std out."
00066        << "\n\nUsage: \n"
00067        << arg << " -v <rcVisardIP> -s <stream> [-i <networkInterface>][-n <numMessages>]" << endl;
00068 }
00069 
00070 int main(int argc, char* argv[])
00071 {
00072 #ifdef WIN32
00073   WSADATA wsaData;
00074   WSAStartup(MAKEWORD(2, 2), &wsaData);
00075 #endif
00076 
00077   // Register signals and signal handler for proper program escape
00078   signal(SIGINT, signal_callback_handler);
00079   signal(SIGTERM, signal_callback_handler);
00080 
00084   string visardIP, networkIface = "", streamName = "";
00085   unsigned int maxNumMsgs = 50, cntMsgs = 0;
00086   bool userSetIp = false;
00087   bool userSetStreamType = false;
00088 
00089   int i = 1;
00090   while (i < argc)
00091   {
00092     std::string p = argv[i++];
00093 
00094     if (p == "-s" && i < argc)
00095     {
00096       streamName = string(argv[i++]);
00097       userSetStreamType = true;
00098     }
00099     else if (p == "-i" && i < argc)
00100     {
00101       networkIface = string(argv[i++]);
00102     }
00103     else if (p == "-v" && i < argc)
00104     {
00105       visardIP = string(argv[i++]);
00106       userSetIp = true;
00107     }
00108     else if (p == "-n" && i < argc)
00109     {
00110       maxNumMsgs = (unsigned int)std::max(0, atoi(argv[i++]));
00111     }
00112     else if (p == "-h")
00113     {
00114       printUsage(argv[0]);
00115       return EXIT_SUCCESS;
00116     }
00117     else
00118     {
00119       printUsage(argv[0]);
00120       return EXIT_FAILURE;
00121     }
00122   }
00123   if (!userSetIp)
00124   {
00125     cerr << "Please specify rc_visard IP." << endl;
00126     printUsage(argv[0]);
00127     return EXIT_FAILURE;
00128   }
00129   if (!userSetStreamType)
00130   {
00131     cerr << "Please specify stream type." << endl;
00132     printUsage(argv[0]);
00133     return EXIT_FAILURE;
00134   }
00135 
00139   cout << "connecting rc_visard " << visardIP << "..." << endl;
00140   auto rcvisardDynamics = rcdyn::RemoteInterface::create(visardIP);
00141 
00142   try
00143   {
00144     // start the rc::dynamics module on the rc_visard
00145     cout << "starting rc_dynamics module on rc_visard..." << endl;
00146     rcvisardDynamics->start();
00147   }
00148   catch (exception& e)
00149   {
00150     cout << "ERROR! Could not start rc_dynamics module on rc_visard: " << e.what() << endl;
00151     return EXIT_FAILURE;
00152   }
00153 
00154   try
00155   {
00156     // easy-to-use creation of a data receiver, parameterized via stream type
00157     cout << "creating receiver and waiting for first messages to arrive..." << endl;
00158     auto receiver = rcvisardDynamics->createReceiverForStream(streamName, networkIface);
00159     receiver->setTimeout(250);
00160 
00161     // receive rc_dynamics protobuf msgs and print them
00162     for (; cntMsgs < maxNumMsgs && !caught_signal; ++cntMsgs)
00163     {
00164       auto msg = receiver->receive(rcvisardDynamics->getPbMsgTypeOfStream(streamName));
00165       if (msg)
00166       {
00167         cout << "received msg " << endl << msg->DebugString() << endl;
00168       }
00169     }
00170   }
00171   catch (exception& e)
00172   {
00173     cout << "ERROR during streaming: " << e.what() << endl;
00174   }
00175 
00179   try
00180   {
00181     cout << "stopping rc_dynamics module on rc_visard..." << endl;
00182     rcvisardDynamics->stop();
00183   }
00184   catch (exception& e)
00185   {
00186     cout << "ERROR! Could not start rc_dynamics module on rc_visard: " << e.what() << endl;
00187   }
00188 
00189   cout << "Received  " << cntMsgs << " " << streamName << " messages." << endl;
00190 
00191 #ifdef WIN32
00192   ::WSACleanup();
00193 #endif
00194 
00195   return EXIT_SUCCESS;
00196 }


rc_dynamics_api
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Endres
autogenerated on Thu May 9 2019 02:13:50