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 #include <topic_proxy/service_client.h>
00030 #include <ros/service_server_link.h>
00031 #include <ros/connection.h>
00032 #include <ros/service_manager.h>
00033 #include <ros/service.h>
00034
00035 #include <ros/poll_manager.h>
00036 #include <ros/transport/transport_tcp.h>
00037 #include <ros/connection_manager.h>
00038
00039 namespace topic_proxy {
00040
00041 using namespace ros;
00042
00043 ServiceClient::Impl::Impl()
00044 { }
00045
00046 ServiceClient::Impl::~Impl()
00047 {
00048 shutdown();
00049 }
00050
00051 void ServiceClient::Impl::shutdown()
00052 {
00053 if (server_link_)
00054 {
00055 server_link_->getConnection()->drop(Connection::Destructing);
00056 server_link_.reset();
00057 }
00058 }
00059
00060 bool ServiceClient::Impl::isValid() const
00061 {
00062 return server_link_ && server_link_->isValid();
00063 }
00064
00065 ServiceClient::ServiceClient()
00066 {}
00067
00068 ServiceClient::ServiceClient(const ServiceClient& rhs)
00069 {
00070 impl_ = rhs.impl_;
00071 }
00072
00073 ServiceClient::~ServiceClient()
00074 {
00075 }
00076
00077 bool ServiceClient::init(const std::string& service_name, const std::string& service_md5sum, std::string host, uint32_t port, const M_string& header_values)
00078 {
00079 if (impl_ && impl_->isValid()) return true;
00080 impl_.reset(new Impl());
00081
00082 impl_->name_ = service_name;
00083
00084 if (host.empty() || port == 0) {
00085 NodeHandle temp;
00086 service::waitForService(service_name);
00087 if (!ServiceManager::instance()->lookupService(service_name, host, port)) return false;
00088 }
00089
00090 TransportTCPPtr transport(new TransportTCP(&PollManager::instance()->getPollSet()));
00091 if (!transport->connect(host, port)) return false;
00092 ConnectionPtr connection(new Connection());
00093 ConnectionManager::instance()->addConnection(connection);
00094
00095 impl_->server_link_.reset(new ServiceServerLink(service_name, true, service_md5sum, service_md5sum, header_values));
00096 connection->initialize(transport, false, HeaderReceivedFunc());
00097 impl_->server_link_->initialize(connection);
00098
00099 return impl_->isValid();
00100 }
00101
00102 bool ServiceClient::call(const SerializedMessage& req, SerializedMessage& resp, const std::string& service_md5sum)
00103 {
00104 if (!isValid()) return false;
00105 return impl_->server_link_->call(req, resp);
00106 }
00107
00108 bool ServiceClient::isValid() const
00109 {
00110 return impl_ && impl_->isValid();
00111 }
00112
00113 void ServiceClient::shutdown()
00114 {
00115 if (impl_)
00116 {
00117 impl_->shutdown();
00118 }
00119 }
00120
00121 std::string ServiceClient::getService()
00122 {
00123 if (impl_)
00124 {
00125 return impl_->name_;
00126 }
00127
00128 return "";
00129 }
00130
00131 }