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 #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
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
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
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
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 }