$search
00001 /* 00002 * Copyright (C) 2009, Willow Garage, Inc. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * * Redistributions of source code must retain the above copyright notice, 00007 * this list of conditions and the following disclaimer. 00008 * * Redistributions in binary form must reproduce the above copyright 00009 * notice, this list of conditions and the following disclaimer in the 00010 * documentation and/or other materials provided with the distribution. 00011 * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its 00012 * contributors may be used to endorse or promote products derived from 00013 * this software without specific prior written permission. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00016 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00017 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00019 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00020 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00021 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00023 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00024 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00025 * POSSIBILITY OF SUCH DAMAGE. 00026 */ 00027 00028 #ifndef ROSCPP_SERVICE_CLIENT_H 00029 #define ROSCPP_SERVICE_CLIENT_H 00030 00031 #include "ros/forwards.h" 00032 #include "ros/common.h" 00033 #include "ros/service_traits.h" 00034 #include "ros/serialization.h" 00035 00036 namespace ros 00037 { 00038 00042 class ROSCPP_DECL ServiceClient 00043 { 00044 public: 00045 ServiceClient() {} 00046 ServiceClient(const std::string& service_name, bool persistent, const M_string& header_values, const std::string& service_md5sum); 00047 ServiceClient(const ServiceClient& rhs); 00048 ~ServiceClient(); 00049 00054 template<class MReq, class MRes> 00055 bool call(MReq& req, MRes& res) 00056 { 00057 namespace st = service_traits; 00058 00059 if (!isValid()) 00060 { 00061 return false; 00062 } 00063 00064 if (strcmp(st::md5sum(req), st::md5sum(res))) 00065 { 00066 ROS_ERROR("The request and response parameters to the service " 00067 "call must be autogenerated from the same " 00068 "server definition file (.srv). your service call " 00069 "for %s appeared to use request/response types " 00070 "from different .srv files. (%s vs. %s)", impl_->name_.c_str(), st::md5sum(req), st::md5sum(res)); 00071 return false; 00072 } 00073 00074 return call(req, res, st::md5sum(req)); 00075 } 00076 00080 template<class Service> 00081 bool call(Service& service) 00082 { 00083 namespace st = service_traits; 00084 00085 if (!isValid()) 00086 { 00087 return false; 00088 } 00089 00090 return call(service.request, service.response, st::md5sum(service)); 00091 } 00092 00096 template<typename MReq, typename MRes> 00097 bool call(const MReq& req, MRes& resp, const std::string& service_md5sum) 00098 { 00099 namespace ser = serialization; 00100 SerializedMessage ser_req = ser::serializeMessage(req); 00101 SerializedMessage ser_resp; 00102 bool ok = call(ser_req, ser_resp, service_md5sum); 00103 if (!ok) 00104 { 00105 return false; 00106 } 00107 00108 try 00109 { 00110 ser::deserializeMessage(ser_resp, resp); 00111 } 00112 catch (std::exception& e) 00113 { 00114 deserializeFailed(e); 00115 return false; 00116 } 00117 00118 return true; 00119 } 00120 00121 bool call(const SerializedMessage& req, SerializedMessage& resp, const std::string& service_md5sum); 00122 00127 bool isValid() const; 00128 00137 void shutdown(); 00138 00145 bool waitForExistence(ros::Duration timeout = ros::Duration(-1)); 00146 00151 bool exists(); 00152 00156 std::string getService(); 00157 00158 operator void*() const { return isValid() ? (void*)1 : (void*)0; } 00159 bool operator<(const ServiceClient& rhs) const 00160 { 00161 return impl_ < rhs.impl_; 00162 } 00163 00164 bool operator==(const ServiceClient& rhs) const 00165 { 00166 return impl_ == rhs.impl_; 00167 } 00168 00169 bool operator!=(const ServiceClient& rhs) const 00170 { 00171 return impl_ != rhs.impl_; 00172 } 00173 00174 private: 00175 // This works around a problem with the OSX linker that causes the static variable declared by 00176 // ROS_ERROR to error with missing symbols when it's used directly in the templated call() method above 00177 // This for some reason only showed up in the rxtools package 00178 void deserializeFailed(const std::exception& e) 00179 { 00180 ROS_ERROR("Exception thrown while while deserializing service call: %s", e.what()); 00181 } 00182 00183 struct Impl 00184 { 00185 Impl(); 00186 ~Impl(); 00187 00188 void shutdown(); 00189 bool isValid() const; 00190 00191 ServiceServerLinkPtr server_link_; 00192 std::string name_; 00193 bool persistent_; 00194 M_string header_values_; 00195 std::string service_md5sum_; 00196 bool is_shutdown_; 00197 }; 00198 typedef boost::shared_ptr<Impl> ImplPtr; 00199 typedef boost::weak_ptr<Impl> ImplWPtr; 00200 00201 ImplPtr impl_; 00202 00203 friend class NodeHandle; 00204 friend class NodeHandleBackingCollection; 00205 }; 00206 typedef boost::shared_ptr<ServiceClient> ServiceClientPtr; 00207 00208 } 00209 00210 #endif