Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
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
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
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
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 }