relay.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2014 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <ros/ros.h>
20 
22 
24 
26 std::string subscriberTopic;
27 size_t subscriberQueueSize = 100;
29 
31 std::string publisherTopic;
32 size_t publisherQueueSize = 100;
33 
34 bool lazy = false;
35 
38  messageEvent);
39 
40 bool getTopicBase(const std::string& topic, std::string& topicBase) {
41  std::string tmp = topic;
42  int i = tmp.rfind('/');
43 
44  while( (tmp.size() > 0) && (i >= (int)(tmp.size()-1))) {
45  tmp = tmp.substr(0,tmp.size()-1);
46  i = tmp.rfind('/');
47  }
48 
49  if (tmp.size() == 0)
50  return false;
51 
52  if(i < 0)
53  topicBase = tmp;
54  else
55  topicBase = tmp.substr(i+1, tmp.size()-i-1);
56 
57  return true;
58 }
59 
60 void subscribe() {
61  subscriber = nodeHandle->subscribe(subscriberTopic, subscriberQueueSize,
62  &callback, subscriberTransportHints);
63 }
64 
66  if(lazy && !subscriber)
67  subscribe();
68 }
69 
71  messageEvent) {
73  messageEvent.getConstMessage();
75  messageEvent.getConnectionHeaderPtr();
76 
77  if (!publisher) {
78  bool latch = false;
79 
80  if (connectionHeader) {
81  ros::M_string::const_iterator it = connectionHeader->find("latching");
82  if ((it != connectionHeader->end()) && (it->second == "1"))
83  latch = true;
84  }
85 
87  message->getType().getMD5Sum(), message->getType().getDataType(),
88  message->getType().getDefinition(), connectCallback);
89  options.latch = latch;
90 
91  publisher = nodeHandle->advertise<variant_msgs::Variant>(publisherTopic,
93  ros::VoidConstPtr(), latch);
94  }
95 
96  if(!lazy || publisher.getNumSubscribers()) {
98  message->toVariantMessage();
99  publisher.publish(variantMessage);
100  }
101  else
102  subscriber = ros::Subscriber();
103 }
104 
105 int main(int argc, char **argv) {
106  if (argc < 2) {
107  printf("\nusage: relay TOPIC [VARIANT_TOPIC]\n\n");
108  return 1;
109  }
110 
111  if (!getTopicBase((argv[1]), publisherTopic)) {
112  ROS_ERROR("Failed to extract topic base from [%s]", argv[1]);
113  return 1;
114  }
115 
116  ros::init(argc, argv, publisherTopic+"_relay",
118 
119  if (argc == 2)
120  publisherTopic = std::string(argv[1])+"_relay";
121  else
122  publisherTopic = argv[2];
123  subscriberTopic = argv[1];
124 
125  nodeHandle.reset(new ros::NodeHandle("~"));
126 
127  bool unreliable = false;
128  nodeHandle->getParam("unreliable", unreliable);
129  nodeHandle->getParam("lazy", lazy);
130 
131  if (unreliable)
132  subscriberTransportHints.unreliable().reliable();
133 
134  subscribe();
135  ros::spin();
136 
137  return 0;
138 }
boost::shared_ptr< void const > VoidConstPtr
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
TransportHints & reliable()
void publish(const boost::shared_ptr< M > &message) const
Header file providing the Message class interface.
size_t publisherQueueSize
Definition: relay.cpp:32
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
const boost::shared_ptr< M_string > & getConnectionHeaderPtr() const
std::string subscriberTopic
Definition: relay.cpp:26
void connectCallback(const ros::SingleSubscriberPublisher &)
Definition: relay.cpp:65
void subscribe()
Definition: relay.cpp:60
const boost::shared_ptr< ConstMessage > & getConstMessage() const
ros::Publisher publisher
Definition: relay.cpp:30
TransportHints & unreliable()
ros::NodeHandlePtr nodeHandle
Definition: relay.cpp:23
bool lazy
Definition: relay.cpp:34
void callback(const ros::MessageEvent< variant_topic_tools::Message > &messageEvent)
Definition: relay.cpp:70
bool getTopicBase(const std::string &topic, std::string &topicBase)
Definition: relay.cpp:40
ROSCPP_DECL void spin()
int main(int argc, char **argv)
Definition: relay.cpp:105
std::string publisherTopic
Definition: relay.cpp:31
ros::TransportHints subscriberTransportHints
Definition: relay.cpp:28
size_t subscriberQueueSize
Definition: relay.cpp:27
uint32_t getNumSubscribers() const
ros::Subscriber subscriber
Definition: relay.cpp:25
#define ROS_ERROR(...)


variant_topic_tools
Author(s): Ralf Kaestner
autogenerated on Sat Jan 9 2021 03:56:50