00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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 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 }
00156
00157 #endif // ROSCPP_SERVICE_SERVER_LINK_H
00158
00159
00160