service_client.cpp
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 #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; // required for service::waitForService
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 } // namespace topic_proxy


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