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 #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 }
00100
00101 #endif // TOPIC_PROXY_H