service_client.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef _ROS_SERVICE_CLIENT_H_
19 #define _ROS_SERVICE_CLIENT_H_
20 
22 
23 #include "publisher.h"
24 #include "subscriber.h"
25 
26 namespace ros {
27 
28  template<typename MReq , typename MRes>
29  class ServiceClient : public Subscriber_ {
30  public:
31  ServiceClient(const char* topic_name) :
32  pub(topic_name, &req, rosserial_msgs::TopicInfo::ID_SERVICE_CLIENT + rosserial_msgs::TopicInfo::ID_PUBLISHER)
33  {
34  this->topic_ = topic_name;
35  this->waiting = true;
36  }
37 
38  virtual void call(const MReq & request, MRes & response)
39  {
40  if(!pub.nh_->connected()) return;
41  ret = &response;
42  waiting = true;
43  pub.publish(&request);
44  while(waiting && pub.nh_->connected())
45  if(pub.nh_->spinOnce() < 0) break;
46  }
47 
48  // these refer to the subscriber
49  virtual void callback(unsigned char *data){
50  ret->deserialize(data);
51  waiting = false;
52  }
53  virtual const char * getMsgType(){ return this->resp.getType(); }
54  virtual const char * getMsgMD5(){ return this->resp.getMD5(); }
56 
57  MReq req;
58  MRes resp;
59  MRes * ret;
60  bool waiting;
61  Publisher pub;
62  };
63 
64 }
65 
66 #endif
void publish(const boost::shared_ptr< M > &message) const
bool call(MReq &req, MRes &res)
virtual bool connected()=0
virtual int spinOnce()=0
const char * topic_
Definition: subscriber.h:37


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Tue Oct 20 2020 03:35:57