node_handle.hpp
Go to the documentation of this file.
00001 /* 
00002  *  node_handle.hpp - micros node handle
00003  *  Copyright (C) 2015 Zaile Jiang
00004  *  
00005  *  This program is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU General Public License
00007  *  as published by the Free Software Foundation; either version 2
00008  *  of the License, or (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  *  GNU General Public License for more details.
00014  *  
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 */
00019 
00020 #ifndef MICROSRTT_NODE_HANDLE_HPP
00021 #define MICROSRTT_NODE_HANDLE_HPP
00022 
00023 #include "ros/node_handle.h"
00024 #include "ros/message_traits.h"
00025 #include "micros_rtt/publisher.h"
00026 #include "micros_rtt/subscriber.h"
00027 #include "micros_rtt/topic_manager.h"
00028 #include "micros_rtt/oro/connection_factory.hpp"
00029 
00030 namespace micros_rtt {
00031 
00032 class NodeHandle
00033 {
00034 public:
00035   NodeHandle(const std::string& ns = std::string(), const ros::M_string& remappings = ros::M_string())
00036   {
00037     //get ros node handle first
00038     ros_nh_ = ros::NodeHandle(ns, remappings);
00039     //maybe need other initialization later
00040     
00041   }
00042   ~NodeHandle()
00043   {
00044     //maybe something need to release the resources
00045   }
00046 
00047   //ros node handle method, leave for compatibility
00048   void setCallbackQueue(ros::CallbackQueueInterface* queue);
00049   ros::CallbackQueueInterface* getCallbackQueue() const;
00050   const std::string& getNamespace() const ;
00051   const std::string& getUnresolvedNamespace();
00052   std::string resolveName(const std::string& name, bool remap = true) const;
00053 
00054 
00055   //micros node handle method from here
00056   
00079   template <class M>
00080   Publisher advertise(const std::string& topic, uint32_t queue_size, bool latch = false)
00081   {
00082     // resolveName in ros namespace
00083     const std::string real_topic = resolveName(topic);
00084     // resolve datatype
00085     const std::string data_type = ros::message_traits::datatype<M>();
00086 
00087     // try to publish the topic in ros first, it will also make the necessary parameter check.
00088     ros::Publisher ros_pub = ros_nh_.advertise<M>(topic, queue_size, latch);
00089 
00090     if (ros_pub) 
00091     {
00092       //topic get published in ros, do the advertise work in micros
00093       ROS_INFO("micros has published topic %s on ros with %s.", topic.c_str(), ros_pub.getTopic().c_str());
00094       
00095       //ros has done the necessary parameter check, so we know the topic is new.
00096       //and we know the subscribers (if there are) have the same data type.
00097       ConnectionBasePtr pub_connection(new Publication<M>(topic));
00098       TopicManager::instance()->addPubConnection(pub_connection);
00099         
00100       //create remote message queue for inter-process transport.
00101       ConnFactory::createStream<M>(pub_connection, true);
00102 
00103       //check if local subscription exists and make the connection.
00104       ConnectionBasePtr local_sub = TopicManager::instance()->findSubConnection(topic);
00105       if (local_sub)
00106       {
00107         //make local connection
00108         ConnFactory::createConnection<M>(pub_connection, local_sub);
00109       }
00110       
00111       Publisher pub(ros_pub, pub_connection);
00112       return pub;
00113     }
00114     else 
00115     {
00116       ROS_WARN("micros publishes topic %s on ros failed", topic.c_str());
00117       return Publisher();
00118     }
00119   }
00120 
00153   template <class M>
00154   Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(*fp)(M), const ros::TransportHints& transport_hints = ros::TransportHints())
00155   {
00156     //try to subscribe the topic in ros, it will also make the necessary parameter check.
00157     //we don't use ros for transport, so register NULL.
00158     ros::Subscriber ros_sub = ros_nh_.subscribe<M>(topic, queue_size, NULL, transport_hints);
00159     
00160     if (ros_sub) 
00161     {
00162       //topic get subscribed in ros, do the subscribe work in micros
00163       ROS_INFO("micros has subscribed topic %s on ros with %s.", topic.c_str(), ros_sub.getTopic().c_str());
00164 
00165       //ros has done the necessary parameter check, so we know the topic is new.
00166       //and we know the publisher (if there are) have the same data type.
00167       ConnectionBasePtr sub_connection(new Subscription<M>(topic, fp));
00168       TopicManager::instance()->addSubConnection(sub_connection);
00169       
00170       //create remote message queue for inter-process transport.
00171       ConnFactory::createStream<M>(sub_connection, false);
00172       
00173       //check if local subscription exists and make the connection.
00174       ConnectionBasePtr local_pub = TopicManager::instance()->findPubConnection(topic);
00175       if (local_pub)
00176       {
00177         //make local connection
00178         ConnFactory::createConnection<M>(local_pub, sub_connection);
00179       }
00180       
00181       Subscriber sub(ros_sub, sub_connection);
00182       return sub;
00183     }
00184     else 
00185     {
00186       ROS_WARN("micros subscribes topic %s on ros failed", topic.c_str());
00187       return Subscriber();
00188     }
00189 
00190   }
00191 
00192 private:
00193   ros::NodeHandle ros_nh_;
00194 
00195 };
00196 
00197 }
00198 
00199 #endif


micros_rtt
Author(s): Zaile Jiang , Xiaodong Yi , Minglong Li
autogenerated on Sat Jun 8 2019 19:02:21