drop.cpp
Go to the documentation of this file.
1 // drop will (intentionally) drop X out of every Y messages that hits it
3 //
4 // Copyright (C) 2009, Morgan Quigley
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Stanford University nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #include <cstdlib>
31 #include <cstdio>
33 #include "topic_tools/parse.h"
34 
35 using std::string;
36 using std::vector;
37 using namespace topic_tools;
38 
39 static ros::NodeHandle *g_node = NULL;
40 static int g_x = 0, g_y = 1;
41 static bool g_advertised = false;
42 static string g_output_topic;
44 
46 {
47  static int s_count = 0;
48  if (!g_advertised)
49  {
50  g_pub = msg->advertise(*g_node, g_output_topic, 10);
51  g_advertised = true;
52  printf("advertised as %s\n", g_output_topic.c_str());
53  }
54  if (s_count >= g_x)
55  g_pub.publish(msg);
56  ++s_count;
57  if (s_count >= g_y)
58  s_count = 0;
59 }
60 
61 #define USAGE "\nusage: drop IN_TOPIC X Y [OUT_TOPIC]\n\n" \
62  " This program will drop X out of every Y messages from IN_TOPIC,\n" \
63  " forwarding the rest to OUT_TOPIC if given, else to a topic \n" \
64  " named IN_TOPIC_drop\n\n"
65 int main(int argc, char **argv)
66 {
67  if(argc < 2)
68  {
69  puts(USAGE);
70  return 1;
71  }
72  std::string topic_name;
73  if(!getBaseName(string(argv[1]), topic_name))
74  return 1;
75  ros::init(argc, argv, topic_name + string("_drop"),
77  if ((argc != 4 && argc != 5) || atoi(argv[2]) < 0 || atoi(argv[3]) < 1)
78  {
79  puts(USAGE);
80  return 1;
81  }
82  if (argc == 4)
83  g_output_topic = string(argv[1]) + string("_drop");
84  else // argc == 5
85  g_output_topic = string(argv[4]);
87  g_node = &n;
88  g_x = atoi(argv[2]);
89  g_y = atoi(argv[3]);
90  ros::Subscriber sub = n.subscribe<ShapeShifter>(string(argv[1]), 10, in_cb);
91  ros::spin();
92  return 0;
93 }
static string g_output_topic
Definition: drop.cpp:42
void publish(const boost::shared_ptr< M > &message) const
static int g_y
Definition: drop.cpp:40
int main(int argc, char **argv)
Definition: drop.cpp:65
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
static int g_x
Definition: drop.cpp:40
static ros::Publisher g_pub
Definition: drop.cpp:43
static ros::NodeHandle * g_node
Definition: drop.cpp:39
std::string topic_name
Definition: demux.cpp:60
TOPIC_TOOLS_DECL bool getBaseName(const std::string &full_name, std::string &base_name)
Definition: parse.cpp:40
#define USAGE
Definition: drop.cpp:61
ROSCPP_DECL void spin()
static bool g_advertised
Definition: drop.cpp:41
void in_cb(const boost::shared_ptr< ShapeShifter const > &msg)
Definition: drop.cpp:45


topic_tools
Author(s): Morgan Quigley, Brian Gerkey
autogenerated on Sun Feb 3 2019 03:30:24