service_client.cpp
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2013, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
31 #include <ros/connection.h>
32 #include <ros/service_manager.h>
33 #include <ros/service.h>
34 
35 #include <ros/poll_manager.h>
37 #include <ros/connection_manager.h>
38 
39 namespace topic_proxy {
40 
41 using namespace ros;
42 
44 { }
45 
47 {
48  shutdown();
49 }
50 
52 {
53  if (server_link_)
54  {
55  server_link_->getConnection()->drop(Connection::Destructing);
56  server_link_.reset();
57  }
58 }
59 
61 {
62  return server_link_ && server_link_->isValid();
63 }
64 
66 {}
67 
69 {
70  impl_ = rhs.impl_;
71 }
72 
74 {
75 }
76 
77 bool ServiceClient::init(const std::string& service_name, const std::string& service_md5sum, std::string host, uint32_t port, const M_string& header_values)
78 {
79  if (impl_ && impl_->isValid()) return true;
80  impl_.reset(new Impl());
81 
82  impl_->name_ = service_name;
83 
84  if (host.empty() || port == 0) {
85  NodeHandle temp; // required for service::waitForService
86  service::waitForService(service_name);
87  if (!ServiceManager::instance()->lookupService(service_name, host, port)) return false;
88  }
89 
90  TransportTCPPtr transport(new TransportTCP(&PollManager::instance()->getPollSet()));
91  if (!transport->connect(host, port)) return false;
92  ConnectionPtr connection(new Connection());
93  ConnectionManager::instance()->addConnection(connection);
94 
95  impl_->server_link_.reset(new ServiceServerLink(service_name, true, service_md5sum, service_md5sum, header_values));
96  connection->initialize(transport, false, HeaderReceivedFunc());
97  impl_->server_link_->initialize(connection);
98 
99  return impl_->isValid();
100 }
101 
102 bool ServiceClient::call(const SerializedMessage& req, SerializedMessage& resp, const std::string& service_md5sum)
103 {
104  if (!isValid()) return false;
105  return impl_->server_link_->call(req, resp);
106 }
107 
108 bool ServiceClient::isValid() const
109 {
110  return impl_ && impl_->isValid();
111 }
112 
114 {
115  if (impl_)
116  {
117  impl_->shutdown();
118  }
119 }
120 
121 std::string ServiceClient::getService()
122 {
123  if (impl_)
124  {
125  return impl_->name_;
126  }
127 
128  return "";
129 }
130 
131 } // namespace topic_proxy
bool call(MReq &req, MRes &res)
boost::function< bool(const ConnectionPtr &, const Header &)> HeaderReceivedFunc
static const ServiceManagerPtr & instance()
std::map< std::string, std::string > M_string
static const ConnectionManagerPtr & instance()
bool isValid() const
static const PollManagerPtr & instance()
ROSCPP_DECL void shutdown()
std::string getService()
ROSCPP_DECL bool waitForService(const std::string &service_name, int32_t timeout)


topic_proxy
Author(s): Johannes Meyer
autogenerated on Sat Jul 27 2019 03:35:25