topic_proxy.h
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2013, Johannes Meyer, TU Darmstadt
00003 // All rights reserved.
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //     * Redistributions of source code must retain the above copyright
00008 //       notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above copyright
00010 //       notice, this list of conditions and the following disclaimer in the
00011 //       documentation and/or other materials provided with the distribution.
00012 //     * Neither the name of the Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //=================================================================================================
00028 
00029 #ifndef TOPIC_PROXY_H
00030 #define TOPIC_PROXY_H
00031 
00032 #include <topic_proxy/service_client.h>
00033 
00034 #include <topic_proxy/GetMessage.h>
00035 #include <topic_proxy/PublishMessage.h>
00036 
00037 namespace topic_proxy
00038 {
00039 
00040 extern const uint32_t g_default_port;
00041 extern const std::string g_get_message_service;
00042 extern const std::string g_publish_message_service;
00043 
00044 class TopicProxy
00045 {
00046 private:
00047   std::string host_;
00048   uint16_t port_;
00049 
00050 public:
00051   TopicProxy();
00052   TopicProxy(const std::string& host, uint32_t port = 0);
00053   virtual ~TopicProxy();
00054 
00055   bool connect();
00056   void shutdown();
00057 
00058   const std::string& getHost() const { return host_; }
00059   uint16_t getTCPPort() const { return port_; }
00060 
00061   template <class M> boost::shared_ptr<const M> getMessage(const std::string& topic, ros::Duration timeout = ros::Duration(), bool compressed = false);
00062   template <class M> void publishMessage(const M& message, const std::string& topic, bool compressed = false);
00063 
00064 protected:
00065   ServiceClient get_message_;
00066   MessageInstanceConstPtr send(GetMessage::Request&);
00067 
00068   ServiceClient publish_message_;
00069   bool send(PublishMessage::Request&);
00070 };
00071 
00072 template <class M>
00073 boost::shared_ptr<const M> TopicProxy::getMessage(const std::string& topic, ros::Duration timeout, bool compressed)
00074 {
00075   GetMessage::Request request;
00076   request.topic = topic;
00077   request.compressed = compressed;
00078   request.timeout = timeout;
00079 
00080   MessageInstanceConstPtr result = send(request);
00081   if (!result) return boost::shared_ptr<const M>();
00082   return result ? result->blob.instantiate<const M>() : boost::shared_ptr<const M>();
00083 }
00084 
00085 template <class M>
00086 void TopicProxy::publishMessage(const M& message, const std::string& topic, bool compressed)
00087 {
00088   PublishMessage::Request request;
00089   request.message.topic = topic;
00090   request.message.md5sum = ros::message_traits::md5sum<M>(message);
00091   request.message.type = ros::message_traits::datatype<M>(message);
00092   request.message.message_definition = ros::message_traits::definition<M>(message);
00093   request.message.blob.setCompressed(compressed);
00094   request.message.blob.serialize(message);
00095 
00096   return send(request);
00097 }
00098 
00099 } // namespace topic_proxy
00100 
00101 #endif // TOPIC_PROXY_H


topic_proxy
Author(s): Johannes Meyer
autogenerated on Mon Oct 6 2014 07:55:47