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 using namespace std;
00041 namespace rcdyn = rc::dynamics;
00042 
00043 
00047 static bool caught_signal = false;
00048 void signal_callback_handler(int signum)
00049 {
00050   printf("Caught signal %d, stopping program!\n",signum);
00051   caught_signal = true;
00052 }
00053 
00054 
00058 void printUsage(char *arg)
00059 {
00060   cout << "\nRequests a data stream from the specified rc_visard IP "
00061           "\nand simply prints received data to std out."
00062        << "\n\nUsage: \n\t"
00063        << arg
00064        << " -v rcVisardIP -s stream [-i networkInterface][-n numMessages]"
00065        << endl;
00066 }
00067 
00068 
00069 int main(int argc, char *argv[])
00070 {
00071   // Register signals and signal handler for proper program escape
00072   signal(SIGINT, signal_callback_handler);
00073   signal(SIGTERM, signal_callback_handler);
00074 
00075 
00079   string visardIP, networkIface = "", streamName = "";
00080   unsigned int maxNumMsgs = 50, cntMsgs = 0;
00081   bool userSetIp = false;
00082   bool userSetStreamType = false;
00083 
00084   int opt;
00085   while ((opt = getopt(argc, argv, "hn:v:i:s:")) != -1)
00086   {
00087     switch (opt)
00088     {
00089       case 's':
00090         streamName = string(optarg);
00091         userSetStreamType = true;
00092         break;
00093       case 'i':
00094         networkIface = string(optarg);
00095         break;
00096       case 'v':
00097         visardIP = string(optarg);
00098         userSetIp = true;
00099         break;
00100       case 'n':
00101         maxNumMsgs = (unsigned int) max(0, atoi(optarg));
00102         break;
00103       case 'h':
00104         printUsage(argv[0]);
00105         return EXIT_SUCCESS;
00106       default: /* '?' */
00107         printUsage(argv[0]);
00108         return EXIT_FAILURE;
00109     }
00110   }
00111   if (!userSetIp)
00112   {
00113     cerr << "Please specify rc_visard IP." << endl;
00114     printUsage(argv[0]);
00115     return EXIT_FAILURE;
00116   }
00117   if (!userSetStreamType)
00118   {
00119     cerr << "Please specify stream type." << endl;
00120     printUsage(argv[0]);
00121     return EXIT_FAILURE;
00122   }
00123 
00124 
00128   cout << "connecting rc_visard " << visardIP << "..." << endl;
00129   auto rcvisardDynamics = rcdyn::RemoteInterface::create(visardIP);
00130 
00131   try
00132   {
00133     // start the rc::dynamics module on the rc_visard
00134     cout << "starting rc_dynamics module on rc_visard..." << endl;
00135     rcvisardDynamics->start();
00136   }
00137   catch (exception &e)
00138   {
00139     cout << "ERROR! Could not start rc_dynamics module on rc_visard: "
00140          << e.what() << endl;
00141     return EXIT_FAILURE;
00142   }
00143 
00144   try
00145   {
00146     // easy-to-use creation of a data receiver, parameterized via stream type
00147     cout << "creating receiver and waiting for first messages to arrive..."
00148          << endl;
00149     auto receiver = rcvisardDynamics->createReceiverForStream(streamName, networkIface);
00150     receiver->setTimeout(250);
00151 
00152     // receive rc_dynamics protobuf msgs and print them
00153     for (; cntMsgs < maxNumMsgs && !caught_signal; ++cntMsgs)
00154     {
00155       auto msg = receiver->receive(
00156               rcvisardDynamics->getPbMsgTypeOfStream(streamName));
00157       if (msg)
00158       {
00159         cout << "received msg " << endl << msg->DebugString() << endl;
00160       }
00161     }
00162   }
00163   catch (exception &e)
00164   {
00165     cout << "ERROR during streaming: " << e.what() << endl;
00166   }
00167 
00168 
00172   try
00173   {
00174     cout << "stopping rc_dynamics module on rc_visard..." << endl;
00175     rcvisardDynamics->stop();
00176   }
00177   catch (exception &e)
00178   {
00179     cout << "ERROR! Could not start rc_dynamics module on rc_visard: "
00180          << e.what() << endl;
00181   }
00182 
00183   cout << "Received  " << cntMsgs << " " << streamName << " messages." << endl;
00184   return EXIT_SUCCESS;
00185 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06