service_client.h
Go to the documentation of this file.
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 
00132   bool isPersistent() const;
00133 
00142   void shutdown();
00143 
00150   bool waitForExistence(ros::Duration timeout = ros::Duration(-1));
00151 
00156   bool exists();
00157 
00161   std::string getService();
00162 
00163   operator void*() const { return isValid() ? (void*)1 : (void*)0; }
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   bool operator!=(const ServiceClient& rhs) const
00175   {
00176     return impl_ != rhs.impl_;
00177   }
00178 
00179 private:
00180   // This works around a problem with the OSX linker that causes the static variable declared by
00181   // ROS_ERROR to error with missing symbols when it's used directly in the templated call() method above
00182   // This for some reason only showed up in the rxtools package
00183   void deserializeFailed(const std::exception& e)
00184   {
00185     ROS_ERROR("Exception thrown while while deserializing service call: %s", e.what());
00186   }
00187 
00188   struct Impl
00189   {
00190     Impl();
00191     ~Impl();
00192 
00193     void shutdown();
00194     bool isValid() const;
00195 
00196     ServiceServerLinkPtr server_link_;
00197     std::string name_;
00198     bool persistent_;
00199     M_string header_values_;
00200     std::string service_md5sum_;
00201     bool is_shutdown_;
00202   };
00203   typedef boost::shared_ptr<Impl> ImplPtr;
00204   typedef boost::weak_ptr<Impl> ImplWPtr;
00205 
00206   ImplPtr impl_;
00207 
00208   friend class NodeHandle;
00209   friend class NodeHandleBackingCollection;
00210 };
00211 typedef boost::shared_ptr<ServiceClient> ServiceClientPtr;
00212 
00213 }
00214 
00215 #endif


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Tue Mar 7 2017 03:44:47