info.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/Message.h>
00022 #include <variant_topic_tools/MessageDefinition.h>
00023 
00024 ros::NodeHandlePtr nodeHandle;
00025 
00026 ros::Subscriber subscriber;
00027 std::string subscriberTopic;
00028 size_t subscriberQueueSize = 100;
00029 
00030 variant_topic_tools::MessageDefinition messageDefinition;
00031 
00032 void callback(const ros::MessageEvent<variant_topic_tools::Message>&
00033   messageEvent);
00034 
00035 bool getTopicBase(const std::string& topic, std::string& topicBase) {
00036   std::string tmp = topic;
00037   int i = tmp.rfind('/');
00038 
00039   while( (tmp.size() > 0) && (i >= (int)(tmp.size()-1))) {
00040     tmp = tmp.substr(0,tmp.size()-1);
00041     i = tmp.rfind('/');
00042   }
00043 
00044   if (tmp.size() == 0)
00045     return false;
00046 
00047   if(i < 0)
00048     topicBase = tmp;
00049   else
00050     topicBase = tmp.substr(i+1, tmp.size()-i-1);
00051 
00052   return true;
00053 }
00054 
00055 void subscribe() {
00056   subscriber = nodeHandle->subscribe(subscriberTopic, subscriberQueueSize,
00057     &callback);
00058 }
00059 
00060 void callback(const ros::MessageEvent<variant_topic_tools::Message>&
00061     messageEvent) {
00062   boost::shared_ptr<const variant_topic_tools::Message> message =
00063     messageEvent.getConstMessage();
00064 
00065   if (!messageDefinition.isValid()) {
00066     messageDefinition.setMessageType(message->getType());
00067     
00068     std::cout << "Topic: " << message->getHeader().getTopic() << "\n";
00069     std::cout << "Publisher: " << message->getHeader().getPublisher() << "\n";
00070     std::cout << "Latched: " << (message->getHeader().isLatched() ?
00071       "yes" : "no") << "\n";
00072     std::cout << "---\n";
00073     std::cout << "Type: " << message->getType().getDataType() << "\n";
00074     std::cout << "MD5 Sum: " << message->getType().getMD5Sum() << "\n";
00075     std::cout << "Definition:\n";    
00076     std::cout << "---\n";
00077     std::cout << messageDefinition;
00078     std::cout << "---\n";
00079     
00080     ros::shutdown();
00081   }
00082 }
00083 
00084 int main(int argc, char **argv) {
00085   if (argc < 2) {
00086     printf("\nusage: info TOPIC\n\n");
00087     return 1;
00088   }
00089   
00090   if (!getTopicBase((argv[1]), subscriberTopic)) {
00091     ROS_ERROR("Failed to extract topic base from [%s]", argv[1]);
00092     return 1;
00093   }
00094   
00095   ros::init(argc, argv, subscriberTopic+"_info",
00096     ros::init_options::AnonymousName);
00097   
00098   subscriberTopic = argv[1];
00099   
00100   nodeHandle.reset(new ros::NodeHandle("~"));
00101  
00102   subscribe();
00103   ros::spin();
00104   
00105   return 0;
00106 }


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