topic_proxy.cpp
Go to the documentation of this file.
00001 #include <topic_proxy/topic_proxy.h>
00002 
00003 #include <ros/serialization.h>
00004 #include <ros/transport/transport_tcp.h>
00005 #include <ros/service_manager.h>
00006 #include <ros/poll_manager.h>
00007 #include <ros/connection_manager.h>
00008 #include <ros/service_server_link.h>
00009 #include <ros/node_handle.h>
00010 #include <ros/service.h>
00011 
00012 using namespace ros;
00013 
00014 namespace topic_proxy
00015 {
00016   const std::string g_get_message_service     = "/get_message";
00017   const std::string g_publish_message_service = "/publish_message";
00018   const uint32_t g_default_port               = 11322;
00019 
00020   TopicProxy::TopicProxy()
00021   {
00022   }
00023 
00024   TopicProxy::TopicProxy(const std::string& host, uint32_t port)
00025     : host_(host)
00026     , port_(port)
00027   {
00028     if (port_ == 0) port_ = g_default_port;
00029   }
00030 
00031   bool TopicProxy::connect()
00032   {
00033     return get_message_.init<GetMessage>(g_get_message_service, host_, port_)
00034         && publish_message_.init<PublishMessage>(g_publish_message_service, host_, port_);
00035   }
00036 
00037   void TopicProxy::shutdown()
00038   {
00039     get_message_.shutdown();
00040     publish_message_.shutdown();
00041   }
00042 
00043   TopicProxy::~TopicProxy()
00044   {
00045   }
00046 
00047   MessageInstanceConstPtr TopicProxy::send(GetMessage::Request& request)
00048   {
00049     if (!get_message_.isValid() && !get_message_.init<GetMessage>(g_get_message_service, host_, port_)) {
00050       return MessageInstanceConstPtr();
00051     }
00052 
00053     GetMessage::Response response;
00054     if (!get_message_.call(request, response)) {
00055       return MessageInstanceConstPtr();
00056     }
00057 
00058     MessageInstanceConstPtr message;
00059     try {
00060       message.reset(new MessageInstance(response.message));
00061     } catch(Exception& e) {
00062       ROS_ERROR("Catched exception while handling a request for topic %s: %s", request.topic.c_str(), e.what());
00063     }
00064 
00065     return message;
00066   }
00067 
00068   bool TopicProxy::send(PublishMessage::Request& request)
00069   {
00070     if (!publish_message_.isValid() && !publish_message_.init<PublishMessage>(g_publish_message_service, host_, port_)) {
00071       return false;
00072     }
00073 
00074     PublishMessage::Response response;
00075     if (!publish_message_.call(request, response)) {
00076       return false;
00077     }
00078 
00079     return true;
00080   }
00081 
00082 } // namespace topic_proxy


topic_proxy
Author(s): Johannes Meyer
autogenerated on Fri Aug 28 2015 13:23:37