$search
00001 00002 /*************************************************************************** 00003 * state_publisher.cpp - Node monitoring state publisher 00004 * 00005 * Created: Fri Apr 08 11:37:52 2011 00006 * Copyright 2011 Tim Niemueller [www.niemueller.de] 00007 * 2011 SRI International 00008 * 2011 Carnegie Mellon University 00009 * 2011 Intel Labs Pittsburgh 00010 * 2011 Columbia University in the City of New York 00011 * 00012 ****************************************************************************/ 00013 00014 /* This program is free software; you can redistribute it and/or modify 00015 * it under the terms of the 3-clase BSD License. 00016 * 00017 * This program is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU Library General Public License for more details. 00021 * 00022 * Read the full text in the LICENSE file in the root directory. 00023 */ 00024 00025 #include <nodemon/state_publisher.h> 00026 00027 #include <cstdarg> 00028 #include <cstdio> 00029 #include <cstring> 00030 00054 NodeStatePublisher::NodeStatePublisher(const char *package_name, 00055 const char *node_type, 00056 ros::NodeHandle &nh) 00057 : __nh(nh) 00058 { 00059 __state_pub = __nh.advertise<nodemon_msgs::NodeState>("/nodemon/state", 5); 00060 00061 __heartbeat_timer = __nh.createWallTimer(ros::WallDuration(1.0), 00062 &NodeStatePublisher::heartbeat_timer_cb, 00063 this); 00064 00065 __state_msg.nodename = ros::this_node::getName(); 00066 __state_msg.package = package_name; 00067 #ifdef _GNU_SOURCE 00068 __state_msg.nodetype = basename(node_type); 00069 #else 00070 char *tmp = strdup(node_type); 00071 __state_msg.nodetype = basename(node_type); 00072 free(tmp); 00073 #endif 00074 __state_msg.state = nodemon_msgs::NodeState::STARTING; 00075 __state_msg.time = ros::Time::now(); 00076 __state_msg.machine_message = ""; 00077 __state_msg.human_message = ""; 00078 } 00079 00080 00082 NodeStatePublisher::~NodeStatePublisher() 00083 { 00084 __state_msg.state = nodemon_msgs::NodeState::STOPPING; 00085 __state_msg.time = ros::Time::now(); 00086 __state_msg.machine_message = ""; 00087 __state_msg.human_message = ""; 00088 publish_state(); 00089 } 00090 00091 00093 inline void 00094 NodeStatePublisher::publish_state() 00095 { 00096 __state_pub.publish(__state_msg); 00097 } 00098 00099 00104 void 00105 NodeStatePublisher::set_running() 00106 { 00107 __state_msg.state = nodemon_msgs::NodeState::RUNNING; 00108 __state_msg.time = ros::Time::now(); 00109 __state_msg.machine_message = ""; 00110 __state_msg.human_message = ""; 00111 publish_state(); 00112 } 00113 00114 00124 void 00125 NodeStatePublisher::set_fatal(std::string machine_msg, 00126 std::string human_msg) 00127 { 00128 __state_msg.state = nodemon_msgs::NodeState::FATAL; 00129 __state_msg.time = ros::Time::now(); 00130 __state_msg.machine_message = machine_msg; 00131 __state_msg.human_message = human_msg; 00132 publish_state(); 00133 } 00134 00135 00148 void 00149 NodeStatePublisher::set_error(std::string machine_msg, 00150 std::string human_msg) 00151 { 00152 __state_msg.state = nodemon_msgs::NodeState::ERROR; 00153 __state_msg.time = ros::Time::now(); 00154 __state_msg.machine_message = machine_msg; 00155 __state_msg.human_message = human_msg; 00156 publish_state(); 00157 } 00158 00159 00170 void 00171 NodeStatePublisher::set_recovering(std::string machine_msg, 00172 std::string human_msg) 00173 { 00174 __state_msg.state = nodemon_msgs::NodeState::RECOVERING; 00175 __state_msg.time = ros::Time::now(); 00176 __state_msg.machine_message = machine_msg; 00177 __state_msg.human_message = human_msg; 00178 publish_state(); 00179 } 00180 00181 00193 void 00194 NodeStatePublisher::send_warning(std::string machine_msg, 00195 std::string human_msg) 00196 { 00197 nodemon_msgs::NodeState old_state = __state_msg; 00198 __state_msg.state = nodemon_msgs::NodeState::WARNING; 00199 __state_msg.time = ros::Time::now(); 00200 __state_msg.machine_message = machine_msg; 00201 __state_msg.human_message = human_msg; 00202 publish_state(); 00203 __state_msg = old_state; 00204 publish_state(); 00205 } 00206 00212 void 00213 NodeStatePublisher::vset_fatal(const char *machine_msg, 00214 const char *human_format, ...) 00215 { 00216 va_list arg; 00217 va_start(arg, human_format); 00218 char *human_msg; 00219 if (vasprintf(&human_msg, human_format, arg) != -1) { 00220 // only fails on out of memory 00221 __state_msg.state = nodemon_msgs::NodeState::FATAL; 00222 __state_msg.time = ros::Time::now(); 00223 __state_msg.machine_message = machine_msg; 00224 __state_msg.human_message = human_msg; 00225 publish_state(); 00226 free(human_msg); 00227 } 00228 va_end(arg); 00229 } 00230 00231 00237 void 00238 NodeStatePublisher::vset_error(const char *machine_msg, 00239 const char *human_format, ...) 00240 { 00241 va_list arg; 00242 va_start(arg, human_format); 00243 char *human_msg; 00244 if (vasprintf(&human_msg, human_format, arg) != -1) { 00245 // only fails on out of memory 00246 __state_msg.state = nodemon_msgs::NodeState::ERROR; 00247 __state_msg.time = ros::Time::now(); 00248 __state_msg.machine_message = machine_msg; 00249 __state_msg.human_message = human_msg; 00250 publish_state(); 00251 free(human_msg); 00252 } 00253 va_end(arg); 00254 } 00255 00256 00262 void 00263 NodeStatePublisher::vset_recovering(const char *machine_msg, 00264 const char *human_format, ...) 00265 { 00266 va_list arg; 00267 va_start(arg, human_format); 00268 char *human_msg; 00269 if (vasprintf(&human_msg, human_format, arg) != -1) { 00270 // only fails on out of memory 00271 __state_msg.state = nodemon_msgs::NodeState::RECOVERING; 00272 __state_msg.time = ros::Time::now(); 00273 __state_msg.machine_message = machine_msg; 00274 __state_msg.human_message = human_msg; 00275 publish_state(); 00276 free(human_msg); 00277 } 00278 va_end(arg); 00279 } 00280 00281 00287 void 00288 NodeStatePublisher::vsend_warning(const char *machine_msg, 00289 const char *human_format, ...) 00290 { 00291 nodemon_msgs::NodeState old_state = __state_msg; 00292 00293 va_list arg; 00294 va_start(arg, human_format); 00295 char *human_msg; 00296 if (vasprintf(&human_msg, human_format, arg) != -1) { 00297 // only fails on out of memory 00298 __state_msg.state = nodemon_msgs::NodeState::WARNING; 00299 __state_msg.time = ros::Time::now(); 00300 __state_msg.machine_message = machine_msg; 00301 __state_msg.human_message = human_msg; 00302 publish_state(); 00303 free(human_msg); 00304 } 00305 va_end(arg); 00306 00307 __state_msg = old_state; 00308 publish_state(); 00309 } 00310 00311 00315 void 00316 NodeStatePublisher::heartbeat_timer_cb(const ros::WallTimerEvent& event) 00317 { 00318 if (__state_msg.state == nodemon_msgs::NodeState::RUNNING) { 00319 // we set the time automatically only when running! 00320 __state_msg.time = 00321 ros::Time(event.current_real.sec, event.current_real.nsec); 00322 } 00323 publish_state(); 00324 }