00001 00002 // relay just passes messages on. it can be useful if you're trying to ensure 00003 // that a message doesn't get sent twice over a wireless link, by having the 00004 // relay catch the message and then do the fanout on the far side of the 00005 // wireless link. 00006 // 00007 // Copyright (C) 2009, Morgan Quigley 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are met: 00011 // * Redistributions of source code must retain the above copyright notice, 00012 // this list of conditions and the following disclaimer. 00013 // * Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // * Neither the name of Stanford University nor the names of its 00017 // contributors may be used to endorse or promote products derived from 00018 // this software without specific prior written permission. 00019 // 00020 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 // POSSIBILITY OF SUCH DAMAGE. 00032 00033 00034 #include <cstdio> 00035 #include "topic_tools/shape_shifter.h" 00036 #include "topic_tools/parse.h" 00037 00038 using std::string; 00039 using std::vector; 00040 using namespace topic_tools; 00041 00042 ros::NodeHandle *g_node = NULL; 00043 static bool g_advertised = false; 00044 static string g_output_topic; 00045 static ros::Publisher g_pub; 00046 00047 void in_cb(const boost::shared_ptr<ShapeShifter const>& msg) 00048 { 00049 if (!g_advertised) 00050 { 00051 g_pub = msg->advertise(*g_node, g_output_topic, 10); 00052 g_advertised = true; 00053 printf("advertised as %s\n", g_output_topic.c_str()); 00054 } 00055 g_pub.publish(msg); 00056 } 00057 00058 int main(int argc, char **argv) 00059 { 00060 if (argc < 2) 00061 { 00062 printf("\nusage: relay IN_TOPIC [OUT_TOPIC]\n\n"); 00063 return 1; 00064 } 00065 std::string topic_name; 00066 if(!getBaseName(string(argv[1]), topic_name)) 00067 return 1; 00068 ros::init(argc, argv, topic_name + string("_relay"), 00069 ros::init_options::AnonymousName); 00070 if (argc == 2) 00071 g_output_topic = string(argv[1]) + string("_relay"); 00072 else // argc == 3 00073 g_output_topic = string(argv[2]); 00074 ros::NodeHandle n; 00075 g_node = &n; 00076 00077 ros::NodeHandle pnh("~"); 00078 bool unreliable; 00079 pnh.param("unreliable", unreliable, false); 00080 ros::TransportHints th; 00081 if (unreliable) 00082 th.unreliable().reliable(); // Prefers unreliable, but will accept reliable. 00083 00084 ros::Subscriber sub = n.subscribe<ShapeShifter>(string(argv[1]), 10, &in_cb, th); 00085 ros::spin(); 00086 return 0; 00087 } 00088