$search
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 00035 #ifndef ROSCPP_SERVICE_SERVER_LINK_H 00036 #define ROSCPP_SERVICE_SERVER_LINK_H 00037 00038 #include "ros/common.h" 00039 00040 #include <boost/thread/mutex.hpp> 00041 #include <boost/shared_array.hpp> 00042 #include <boost/enable_shared_from_this.hpp> 00043 #include <boost/thread.hpp> 00044 00045 #include <queue> 00046 00047 namespace ros 00048 { 00049 class Header; 00050 class Message; 00051 class Connection; 00052 typedef boost::shared_ptr<Connection> ConnectionPtr; 00053 00058 class ROSCPP_DECL ServiceServerLink : public boost::enable_shared_from_this<ServiceServerLink> 00059 { 00060 private: 00061 struct CallInfo 00062 { 00063 SerializedMessage req_; 00064 SerializedMessage* resp_; 00065 00066 bool finished_; 00067 boost::condition_variable finished_condition_; 00068 boost::mutex finished_mutex_; 00069 boost::thread::id caller_thread_id_; 00070 00071 bool success_; 00072 bool call_finished_; 00073 }; 00074 typedef boost::shared_ptr<CallInfo> CallInfoPtr; 00075 typedef std::queue<CallInfoPtr> Q_CallInfo; 00076 00077 public: 00078 typedef std::map<std::string, std::string> M_string; 00079 ServiceServerLink(const std::string& service_name, bool persistent, const std::string& request_md5sum, const std::string& response_md5sum, const M_string& header_values); 00080 virtual ~ServiceServerLink(); 00081 00082 // 00083 bool initialize(const ConnectionPtr& connection); 00084 00088 bool isValid() const; 00092 bool isPersistent() const { return persistent_; } 00093 00094 const ConnectionPtr& getConnection() const { return connection_; } 00095 00096 const std::string& getServiceName() const { return service_name_; } 00097 const std::string& getRequestMD5Sum() const { return request_md5sum_; } 00098 const std::string& getResponseMD5Sum() const { return response_md5sum_; } 00099 00106 bool call(const SerializedMessage& req, SerializedMessage& resp); 00107 00108 private: 00109 void onConnectionDropped(const ConnectionPtr& conn); 00110 bool onHeaderReceived(const ConnectionPtr& conn, const Header& header); 00111 00116 void callFinished(); 00121 void processNextCall(); 00125 void clearCalls(); 00129 void cancelCall(const CallInfoPtr& info); 00130 00131 void onHeaderWritten(const ConnectionPtr& conn); 00132 void onRequestWritten(const ConnectionPtr& conn); 00133 void onResponseOkAndLength(const ConnectionPtr& conn, const boost::shared_array<uint8_t>& buffer, uint32_t size, bool success); 00134 void onResponse(const ConnectionPtr& conn, const boost::shared_array<uint8_t>& buffer, uint32_t size, bool success); 00135 00136 ConnectionPtr connection_; 00137 std::string service_name_; 00138 bool persistent_; 00139 std::string request_md5sum_; 00140 std::string response_md5sum_; 00141 00142 M_string extra_outgoing_header_values_; 00143 bool header_written_; 00144 bool header_read_; 00145 00146 Q_CallInfo call_queue_; 00147 boost::mutex call_queue_mutex_; 00148 00149 CallInfoPtr current_call_; 00150 00151 bool dropped_; 00152 }; 00153 typedef boost::shared_ptr<ServiceServerLink> ServiceServerLinkPtr; 00154 00155 } // namespace ros 00156 00157 #endif // ROSCPP_SERVICE_SERVER_LINK_H 00158 00159 00160