37 #include "std_msgs/String.h"
38 #include "topic_tools/MuxSelect.h"
39 #include "topic_tools/MuxAdd.h"
40 #include "topic_tools/MuxList.h"
41 #include "topic_tools/MuxDelete.h"
71 static list<struct sub_info_t>
g_subs;
86 topic_tools::MuxSelect::Response &res )
94 ROS_DEBUG(
"Unsubscribing to %s, lazy", res.prev_topic.c_str());
102 res.prev_topic = string(
"");
107 ROS_INFO(
"mux selected to no input.");
114 ROS_INFO(
"trying to switch mux to %s", req.topic.c_str());
116 for (list<struct sub_info_t>::iterator it =
g_subs.begin();
123 ROS_INFO(
"mux selected input: [%s]", it->topic_name.c_str());
144 topic_tools::MuxSelect::Response &res )
146 ROS_WARN(
"the <topic>_select service is deprecated; use mux/select instead");
161 std::this_thread::sleep_for(std::chrono::microseconds(
static_cast<int>(
g_wait_second * 1000000)));
167 for (
static list<struct sub_info_t>::iterator it =
g_subs.begin(); it !=
g_subs.end(); ++it) {
169 ROS_INFO(
"Unregistering from %s", it->topic_name.c_str());
184 ROS_INFO(
"lazy mode; unsubscribing");
194 topic_tools::MuxList::Response& res)
197 for (list<struct sub_info_t>::iterator it =
g_subs.begin();
201 res.topics.push_back(it->topic_name);
208 topic_tools::MuxAdd::Response& res)
212 ROS_INFO(
"trying to add %s to mux", req.topic.c_str());
217 ROS_WARN(
"failed to add topic %s to mux, because it's reserved for special use",
223 for (list<struct sub_info_t>::iterator it =
g_subs.begin();
229 ROS_WARN(
"tried to add a topic that mux was already listening to: [%s]",
230 it->topic_name.c_str());
247 ROS_WARN(
"failed to add topic %s to mux, because it's an invalid name: %s",
248 req.topic.c_str(), e.what());
252 g_subs.push_back(sub_info);
254 ROS_INFO(
"added %s to mux", req.topic.c_str());
260 topic_tools::MuxDelete::Response& res)
264 ROS_INFO(
"trying to delete %s from mux", req.topic.c_str());
266 for (list<struct sub_info_t>::iterator it =
g_subs.begin();
275 ROS_WARN(
"tried to delete currently selected topic %s from mux", req.topic.c_str());
283 ROS_INFO(
"deleted topic %s from mux", req.topic.c_str());
288 ROS_WARN(
"tried to delete non-subscribed topic %s from mux", req.topic.c_str());
292 int main(
int argc,
char **argv)
299 printf(
"\nusage: mux OUT_TOPIC IN_TOPIC1 [IN_TOPIC2 [...]]\n\n");
307 vector<string> topics;
308 for (
unsigned int i = 2; i <
args.size(); i++)
309 topics.push_back(
args[i]);
319 g_pub_selected = mux_nh.advertise<std_msgs::String>(string(
"selected"), 1,
true);
321 for (
size_t i = 0; i < topics.size(); i++)
328 g_subs.push_back(sub_info);
332 std::string initial_topic;
333 pnh.
getParam(
"initial_topic", initial_topic);
335 if (initial_topic.empty())
342 ROS_INFO(
"mux selected to no input.");
348 ROS_INFO(
"trying to switch mux to %s", initial_topic.c_str());
350 for (list<struct sub_info_t>::iterator it =
g_subs.begin();
357 t.data = initial_topic;
358 ROS_INFO(
"mux selected input: [%s]", it->topic_name.c_str());
383 for (list<struct sub_info_t>::iterator it =
g_subs.begin();