call_handle.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Willow Garage, Inc.
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  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #ifndef RVIZ_RPC_CALL_HANDLE_H
00031 #define RVIZ_RPC_CALL_HANDLE_H
00032 
00033 #include "exceptions.h"
00034 #include "request_wrapper.h"
00035 #include "response_wrapper.h"
00036 
00037 #include <rve_common/uuid.h>
00038 
00039 #include <string>
00040 #include <boost/shared_ptr.hpp>
00041 #include <boost/function.hpp>
00042 
00043 #include <ros/message_event.h>
00044 
00045 namespace rve_rpc
00046 {
00047 
00048 typedef boost::function<void(const ResponseWrapperConstPtr&)> RespondFn;
00049 
00050 template<typename Req, typename Res>
00051 class CallHandle
00052 {
00053 public:
00054   typedef boost::shared_ptr<Req> ReqPtr;
00055   typedef boost::shared_ptr<Req const> ReqConstPtr;
00056   typedef ros::MessageEvent<Req const> ReqEvent;
00057   typedef boost::shared_ptr<Res> ResPtr;
00058   typedef boost::shared_ptr<Res const> ResConstPtr;
00059 
00060   CallHandle()
00061   {}
00062 
00063   CallHandle(const rve_common::UUID& id, const ReqEvent& evt, const RespondFn& respond_fn)
00064   : data_(new Data)
00065   {
00066     data_->id = id;
00067     data_->request_event = evt;
00068     data_->respond_fn = respond_fn;
00069     data_->responded = false;
00070   }
00071 
00072   ReqConstPtr getRequest() const { return data_->request_event.getMessage(); }
00073   const ReqEvent& getRequestEvent() const { return data_->request_event; }
00074 
00075   void respond(const ResConstPtr& res_msg) const
00076   {
00077     ROS_ASSERT(!data_->responded);
00078     ResponseWrapperPtr res = makeResponse(res_msg);
00079     res->request_id = data_->id;
00080     data_->respond_fn(res);
00081     data_->responded = true;
00082   }
00083 
00084   void except(const std::string& exception_msg)
00085   {
00086     ROS_ASSERT(!data_->responded);
00087     ResponseWrapperPtr res = makeResponse<Res>().first;
00088     res->request_id = data_->id;
00089     res->error_code = Response::EXCEPTION;
00090     res->error_string = exception_msg;
00091     data_->respond_fn(res);
00092     data_->responded = true;
00093   }
00094 
00095   operator void*() const { return data_ ? (void*)1 : (void*)0; }
00096 
00097   bool operator<(const CallHandle<Req, Res>& rhs) const
00098   {
00099     return data_ < rhs.data_;
00100   }
00101 
00102   bool operator==(const CallHandle<Req, Res>& rhs) const
00103   {
00104     return data_ == rhs.data_;
00105   }
00106 
00107   bool operator!=(const CallHandle<Req, Res>& rhs) const
00108   {
00109     return data_ != rhs.data_;
00110   }
00111 
00112 private:
00113   struct Data
00114   {
00115     ~Data()
00116     {
00117       if (!responded)
00118       {
00119         ResponseWrapperPtr res = makeResponse<Res>().first;
00120         res->request_id = id;
00121         res->error_code = Response::EXCEPTION;
00122         res->error_string = "Callee did not respond to the request (handle.respond() or handle.except() never called)";
00123         respond_fn(res);
00124       }
00125     }
00126 
00127     rve_common::UUID id;
00128     ReqEvent request_event;
00129     RespondFn respond_fn;
00130     bool responded;
00131   };
00132   boost::shared_ptr<Data> data_;
00133 };
00134 
00135 } // namespace rve_rpc
00136 
00137 #endif // RVIZ_RPC_CALL_HANDLE_H
00138 


rve_rpc
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:30:53