Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <ros/ros.h>
00020
00021 #include <variant_topic_tools/Message.h>
00022
00023 ros::NodeHandlePtr nodeHandle;
00024
00025 ros::Subscriber subscriber;
00026 std::string subscriberTopic;
00027 size_t subscriberQueueSize = 100;
00028 ros::TransportHints subscriberTransportHints;
00029
00030 ros::Publisher publisher;
00031 std::string publisherTopic;
00032 size_t publisherQueueSize = 100;
00033
00034 bool lazy = false;
00035
00036 void connectCallback(const ros::SingleSubscriberPublisher&);
00037 void callback(const ros::MessageEvent<variant_topic_tools::Message>&
00038 messageEvent);
00039
00040 bool getTopicBase(const std::string& topic, std::string& topicBase) {
00041 std::string tmp = topic;
00042 int i = tmp.rfind('/');
00043
00044 while( (tmp.size() > 0) && (i >= (int)(tmp.size()-1))) {
00045 tmp = tmp.substr(0,tmp.size()-1);
00046 i = tmp.rfind('/');
00047 }
00048
00049 if (tmp.size() == 0)
00050 return false;
00051
00052 if(i < 0)
00053 topicBase = tmp;
00054 else
00055 topicBase = tmp.substr(i+1, tmp.size()-i-1);
00056
00057 return true;
00058 }
00059
00060 void subscribe() {
00061 subscriber = nodeHandle->subscribe(subscriberTopic, subscriberQueueSize,
00062 &callback, subscriberTransportHints);
00063 }
00064
00065 void connectCallback(const ros::SingleSubscriberPublisher&) {
00066 if(lazy && !subscriber)
00067 subscribe();
00068 }
00069
00070 void callback(const ros::MessageEvent<variant_topic_tools::Message>&
00071 messageEvent) {
00072 boost::shared_ptr<const variant_topic_tools::Message> message =
00073 messageEvent.getConstMessage();
00074 boost::shared_ptr<const ros::M_string> connectionHeader =
00075 messageEvent.getConnectionHeaderPtr();
00076
00077 if (!publisher) {
00078 bool latch = false;
00079
00080 if (connectionHeader) {
00081 ros::M_string::const_iterator it = connectionHeader->find("latching");
00082 if ((it != connectionHeader->end()) && (it->second == "1"))
00083 latch = true;
00084 }
00085
00086 ros::AdvertiseOptions options(publisherTopic, publisherQueueSize,
00087 message->getType().getMD5Sum(), message->getType().getDataType(),
00088 message->getType().getDefinition(), connectCallback);
00089 options.latch = latch;
00090
00091 publisher = nodeHandle->advertise<variant_msgs::Variant>(publisherTopic,
00092 publisherQueueSize, connectCallback, ros::SubscriberStatusCallback(),
00093 ros::VoidConstPtr(), latch);
00094 }
00095
00096 if(!lazy || publisher.getNumSubscribers()) {
00097 boost::shared_ptr<const variant_msgs::Variant> variantMessage =
00098 message->toVariantMessage();
00099 publisher.publish(variantMessage);
00100 }
00101 else
00102 subscriber = ros::Subscriber();
00103 }
00104
00105 int main(int argc, char **argv) {
00106 if (argc < 2) {
00107 printf("\nusage: relay TOPIC [VARIANT_TOPIC]\n\n");
00108 return 1;
00109 }
00110
00111 if (!getTopicBase((argv[1]), publisherTopic)) {
00112 ROS_ERROR("Failed to extract topic base from [%s]", argv[1]);
00113 return 1;
00114 }
00115
00116 ros::init(argc, argv, publisherTopic+"_relay",
00117 ros::init_options::AnonymousName);
00118
00119 if (argc == 2)
00120 publisherTopic = std::string(argv[1])+"_relay";
00121 else
00122 publisherTopic = argv[2];
00123 subscriberTopic = argv[1];
00124
00125 nodeHandle.reset(new ros::NodeHandle("~"));
00126
00127 bool unreliable = false;
00128 nodeHandle->getParam("unreliable", unreliable);
00129 nodeHandle->getParam("lazy", lazy);
00130
00131 if (unreliable)
00132 subscriberTransportHints.unreliable().reliable();
00133
00134 subscribe();
00135 ros::spin();
00136
00137 return 0;
00138 }