echo.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * Copyright (C) 2014 by Ralf Kaestner                                        *
00003  * ralf.kaestner@gmail.com                                                    *
00004  *                                                                            *
00005  * This program is free software; you can redistribute it and/or modify       *
00006  * it under the terms of the Lesser GNU General Public License as published by*
00007  * the Free Software Foundation; either version 3 of the License, or          *
00008  * (at your option) any later version.                                        *
00009  *                                                                            *
00010  * This program is distributed in the hope that it will be useful,            *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *
00013  * Lesser GNU General Public License for more details.                        *
00014  *                                                                            *
00015  * You should have received a copy of the Lesser GNU General Public License   *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.       *
00017  ******************************************************************************/
00018 
00019 #include <ros/ros.h>
00020 
00021 #include <variant_topic_tools/Subscriber.h>
00022 
00023 ros::NodeHandlePtr nodeHandle;
00024 
00025 variant_topic_tools::Subscriber subscriber;
00026 std::string subscriberTopic;
00027 size_t subscriberQueueSize = 100;
00028 
00029 void callback(const variant_topic_tools::MessageVariant& variant, const
00030   ros::Time& receiptTime);
00031 
00032 bool getTopicBase(const std::string& topic, std::string& topicBase) {
00033   std::string tmp = topic;
00034   int i = tmp.rfind('/');
00035 
00036   while( (tmp.size() > 0) && (i >= (int)(tmp.size()-1))) {
00037     tmp = tmp.substr(0,tmp.size()-1);
00038     i = tmp.rfind('/');
00039   }
00040 
00041   if (tmp.size() == 0)
00042     return false;
00043 
00044   if(i < 0)
00045     topicBase = tmp;
00046   else
00047     topicBase = tmp.substr(i+1, tmp.size()-i-1);
00048 
00049   return true;
00050 }
00051 
00052 void subscribe() {
00053   variant_topic_tools::MessageType type;
00054   subscriber = type.subscribe(*nodeHandle, subscriberTopic,
00055     subscriberQueueSize, &callback);
00056 }
00057 
00058 void callback(const variant_topic_tools::MessageVariant& variant, const
00059     ros::Time& receiptTime) {
00060   std::cout << variant << "\n---\n";
00061 }
00062 
00063 int main(int argc, char **argv) {
00064   if (argc < 2) {
00065     printf("\nusage: echo TOPIC\n\n");
00066     return 1;
00067   }
00068   
00069   if (!getTopicBase((argv[1]), subscriberTopic)) {
00070     ROS_ERROR("Failed to extract topic base from [%s]", argv[1]);
00071     return 1;
00072   }
00073   
00074   ros::init(argc, argv, subscriberTopic+"_echo",
00075     ros::init_options::AnonymousName);
00076   
00077   subscriberTopic = argv[1];
00078   
00079   nodeHandle.reset(new ros::NodeHandle("~"));
00080  
00081   subscribe();
00082   ros::spin();
00083   
00084   return 0;
00085 }


variant_topic_tools
Author(s): Ralf Kaestner
autogenerated on Tue Jul 9 2019 03:18:42