From 978a84744ea1f46591a9acf371219ad18e7571eb Mon Sep 17 00:00:00 2001 From: Chris Denniston Date: Tue, 1 Apr 2014 16:17:15 -0500 Subject: [PATCH 1/6] started work on watchdog functor --- wesley/ros/src/commander/src/main.cpp | 4 +++- wesley/ros/src/commander/src/watchdog.cpp | 9 +++++++++ wesley/ros/src/commander/src/watchdog.h | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 wesley/ros/src/commander/src/watchdog.cpp create mode 100644 wesley/ros/src/commander/src/watchdog.h diff --git a/wesley/ros/src/commander/src/main.cpp b/wesley/ros/src/commander/src/main.cpp index d0eda1a..b881c6d 100644 --- a/wesley/ros/src/commander/src/main.cpp +++ b/wesley/ros/src/commander/src/main.cpp @@ -2,7 +2,7 @@ #include "exit_handlers.h" #include #include - +#include using std::string; #define grasp() executeBinary("rostopic pub -1 /arm/put/point wesley/arm_point '{direct_mode: false, cmd: grasp}'", ""); @@ -126,3 +126,5 @@ int executeBinary(string binaryName, string prefix, string mode ){ // man 2 waitpid) did not shed any extra light on this. return pclose(f)/256; } + + diff --git a/wesley/ros/src/commander/src/watchdog.cpp b/wesley/ros/src/commander/src/watchdog.cpp new file mode 100644 index 0000000..89542af --- /dev/null +++ b/wesley/ros/src/commander/src/watchdog.cpp @@ -0,0 +1,9 @@ +#include "watchdog.h" +#include +using std::stringstream; +void WatchDOg::execute_pkill(string name){ + stringstream ss; + ss << "pkill " << name; + FILE * f = popen(ss.str().c_str(),"r"); + pclose(f); +} diff --git a/wesley/ros/src/commander/src/watchdog.h b/wesley/ros/src/commander/src/watchdog.h new file mode 100644 index 0000000..8878219 --- /dev/null +++ b/wesley/ros/src/commander/src/watchdog.h @@ -0,0 +1,15 @@ +#include +#include +#include +using std::string; + +class WatchDog{ + int timesCalled; + void callback(string binaryName) + void execute_pkill(string name); +public: + WatchDog(){timesCalled = 0;} + WatchDog(int times){timesCalled=times;}; + void operator()(string binaryName){ + signal(SIGALARM,callback(binaryName); + From 0b4907969062a7681a663f44aadabd22096957f0 Mon Sep 17 00:00:00 2001 From: octopuscabbage Date: Wed, 2 Apr 2014 01:51:49 -0500 Subject: [PATCH 2/6] Finished watchdog --- wesley/ros/src/commander/src/main.cpp | 7 +++++- wesley/ros/src/commander/src/watchdog.cpp | 23 ++++++++++++++++- wesley/ros/src/commander/src/watchdog.h | 30 ++++++++++++++++++----- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/wesley/ros/src/commander/src/main.cpp b/wesley/ros/src/commander/src/main.cpp index b881c6d..7421233 100644 --- a/wesley/ros/src/commander/src/main.cpp +++ b/wesley/ros/src/commander/src/main.cpp @@ -1,8 +1,8 @@ #include #include "exit_handlers.h" #include -#include #include +#include using std::string; #define grasp() executeBinary("rostopic pub -1 /arm/put/point wesley/arm_point '{direct_mode: false, cmd: grasp}'", ""); @@ -116,6 +116,11 @@ int executeBinary(string binaryName, string prefix, string mode ){ if(f == 0){ return -2; } + /** + * Uncomment this to turn on watchdogging (memory is deallocated in the class) + //WatchDog* watch = new WatchDog; + //std::thread(watch(binaryName)); + */ //Get return value, don't ask why it's this but it is. It's from the stack overflow on popen. // // http://bytes.com/topic/c/answers/131694-pclose-returning-termination-status-command#post472837 diff --git a/wesley/ros/src/commander/src/watchdog.cpp b/wesley/ros/src/commander/src/watchdog.cpp index 89542af..6667b7e 100644 --- a/wesley/ros/src/commander/src/watchdog.cpp +++ b/wesley/ros/src/commander/src/watchdog.cpp @@ -1,9 +1,30 @@ #include "watchdog.h" +#include +#include #include +#include +#include using std::stringstream; -void WatchDOg::execute_pkill(string name){ + +void WatchDog::execute_pkill(string name){ stringstream ss; ss << "pkill " << name; FILE * f = popen(ss.str().c_str(),"r"); pclose(f); } + +void WatchDog::tick(){ + amountCalled++; +} + +void WatchDog::operator()(string binaryName){ + amountCalled = 0; + while(amountCalled < checkAmount){ + tick(); + std::this_thread::sleep_for(std::chrono::seconds(seconds_to_sleep)); + } + execute_pkill(binaryName); + /**Need to delete the memory allocated for this because it's a suicide thread*/ + delete this; +} + diff --git a/wesley/ros/src/commander/src/watchdog.h b/wesley/ros/src/commander/src/watchdog.h index 8878219..1cf498f 100644 --- a/wesley/ros/src/commander/src/watchdog.h +++ b/wesley/ros/src/commander/src/watchdog.h @@ -1,15 +1,33 @@ #include #include #include +#include +#include +#include +using std::map; using std::string; +#ifndef WATCHDOG_H +#define WATCHDOG_H +/** + * Watch dog is a functor object that should be called as such, don't worry it suicides itself and deallocates it's own memory + * WatchDog* watch = new WatchDog; + * std::thread(watch(binaryName)); + */ class WatchDog{ - int timesCalled; - void callback(string binaryName) + string name; + int amountCalled; + int checkAmount; /* Date: Wed, 2 Apr 2014 01:51:49 -0500 Subject: [PATCH 3/6] Finished watchdog --- wesley/ros/src/commander/src/main.cpp | 6 +++++ wesley/ros/src/commander/src/watchdog.cpp | 23 ++++++++++++++++- wesley/ros/src/commander/src/watchdog.h | 30 ++++++++++++++++++----- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/wesley/ros/src/commander/src/main.cpp b/wesley/ros/src/commander/src/main.cpp index b881c6d..5d45ca6 100644 --- a/wesley/ros/src/commander/src/main.cpp +++ b/wesley/ros/src/commander/src/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using std::string; #define grasp() executeBinary("rostopic pub -1 /arm/put/point wesley/arm_point '{direct_mode: false, cmd: grasp}'", ""); @@ -116,6 +117,11 @@ int executeBinary(string binaryName, string prefix, string mode ){ if(f == 0){ return -2; } + /** + * Uncomment this to turn on watchdogging (memory is deallocated in the class) + //WatchDog* watch = new WatchDog; + //std::thread(watch(binaryName)); + */ //Get return value, don't ask why it's this but it is. It's from the stack overflow on popen. // // http://bytes.com/topic/c/answers/131694-pclose-returning-termination-status-command#post472837 diff --git a/wesley/ros/src/commander/src/watchdog.cpp b/wesley/ros/src/commander/src/watchdog.cpp index 89542af..6667b7e 100644 --- a/wesley/ros/src/commander/src/watchdog.cpp +++ b/wesley/ros/src/commander/src/watchdog.cpp @@ -1,9 +1,30 @@ #include "watchdog.h" +#include +#include #include +#include +#include using std::stringstream; -void WatchDOg::execute_pkill(string name){ + +void WatchDog::execute_pkill(string name){ stringstream ss; ss << "pkill " << name; FILE * f = popen(ss.str().c_str(),"r"); pclose(f); } + +void WatchDog::tick(){ + amountCalled++; +} + +void WatchDog::operator()(string binaryName){ + amountCalled = 0; + while(amountCalled < checkAmount){ + tick(); + std::this_thread::sleep_for(std::chrono::seconds(seconds_to_sleep)); + } + execute_pkill(binaryName); + /**Need to delete the memory allocated for this because it's a suicide thread*/ + delete this; +} + diff --git a/wesley/ros/src/commander/src/watchdog.h b/wesley/ros/src/commander/src/watchdog.h index 8878219..1cf498f 100644 --- a/wesley/ros/src/commander/src/watchdog.h +++ b/wesley/ros/src/commander/src/watchdog.h @@ -1,15 +1,33 @@ #include #include #include +#include +#include +#include +using std::map; using std::string; +#ifndef WATCHDOG_H +#define WATCHDOG_H +/** + * Watch dog is a functor object that should be called as such, don't worry it suicides itself and deallocates it's own memory + * WatchDog* watch = new WatchDog; + * std::thread(watch(binaryName)); + */ class WatchDog{ - int timesCalled; - void callback(string binaryName) + string name; + int amountCalled; + int checkAmount; /* Date: Wed, 2 Apr 2014 01:57:19 -0500 Subject: [PATCH 4/6] Added unistd.h back in --- wesley/ros/src/commander/src/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/wesley/ros/src/commander/src/main.cpp b/wesley/ros/src/commander/src/main.cpp index 7421233..5d45ca6 100644 --- a/wesley/ros/src/commander/src/main.cpp +++ b/wesley/ros/src/commander/src/main.cpp @@ -1,6 +1,7 @@ #include #include "exit_handlers.h" #include +#include #include #include using std::string; From dfe6bc67060090cd201ad2f20e2cf0b9154d0e12 Mon Sep 17 00:00:00 2001 From: octopuscabbage Date: Wed, 2 Apr 2014 02:00:16 -0500 Subject: [PATCH 5/6] Fixed error in thread call --- wesley/ros/src/commander/src/main.cpp | 2 +- wesley/ros/src/commander/src/watchdog.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/wesley/ros/src/commander/src/main.cpp b/wesley/ros/src/commander/src/main.cpp index 5d45ca6..4768144 100644 --- a/wesley/ros/src/commander/src/main.cpp +++ b/wesley/ros/src/commander/src/main.cpp @@ -120,7 +120,7 @@ int executeBinary(string binaryName, string prefix, string mode ){ /** * Uncomment this to turn on watchdogging (memory is deallocated in the class) //WatchDog* watch = new WatchDog; - //std::thread(watch(binaryName)); + //std::thread(watch,binaryName); */ //Get return value, don't ask why it's this but it is. It's from the stack overflow on popen. // diff --git a/wesley/ros/src/commander/src/watchdog.h b/wesley/ros/src/commander/src/watchdog.h index 1cf498f..d196ee2 100644 --- a/wesley/ros/src/commander/src/watchdog.h +++ b/wesley/ros/src/commander/src/watchdog.h @@ -12,7 +12,7 @@ using std::string; /** * Watch dog is a functor object that should be called as such, don't worry it suicides itself and deallocates it's own memory * WatchDog* watch = new WatchDog; - * std::thread(watch(binaryName)); + * std::thread(watch,binaryName); */ class WatchDog{ string name; From e2cc49ae9c5532b6618c9dbeb2db73ae5a6190e2 Mon Sep 17 00:00:00 2001 From: Chris Denniston Date: Wed, 2 Apr 2014 13:11:29 -0500 Subject: [PATCH 6/6] Fixed an error in watchdog --- wesley/ros/src/commander/src/main.cpp | 5 +++-- wesley/ros/src/commander/src/watchdog.cpp | 2 -- wesley/ros/src/commander/src/watchdog.h | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/wesley/ros/src/commander/src/main.cpp b/wesley/ros/src/commander/src/main.cpp index 4768144..10c096d 100644 --- a/wesley/ros/src/commander/src/main.cpp +++ b/wesley/ros/src/commander/src/main.cpp @@ -4,6 +4,7 @@ #include #include #include +//#include Include if using watchdog using std::string; #define grasp() executeBinary("rostopic pub -1 /arm/put/point wesley/arm_point '{direct_mode: false, cmd: grasp}'", ""); @@ -118,8 +119,8 @@ int executeBinary(string binaryName, string prefix, string mode ){ return -2; } /** - * Uncomment this to turn on watchdogging (memory is deallocated in the class) - //WatchDog* watch = new WatchDog; + * Uncomment this to turn on watchdogging + //WatchDog watch; //std::thread(watch,binaryName); */ //Get return value, don't ask why it's this but it is. It's from the stack overflow on popen. diff --git a/wesley/ros/src/commander/src/watchdog.cpp b/wesley/ros/src/commander/src/watchdog.cpp index 6667b7e..548320a 100644 --- a/wesley/ros/src/commander/src/watchdog.cpp +++ b/wesley/ros/src/commander/src/watchdog.cpp @@ -24,7 +24,5 @@ void WatchDog::operator()(string binaryName){ std::this_thread::sleep_for(std::chrono::seconds(seconds_to_sleep)); } execute_pkill(binaryName); - /**Need to delete the memory allocated for this because it's a suicide thread*/ - delete this; } diff --git a/wesley/ros/src/commander/src/watchdog.h b/wesley/ros/src/commander/src/watchdog.h index d196ee2..d983660 100644 --- a/wesley/ros/src/commander/src/watchdog.h +++ b/wesley/ros/src/commander/src/watchdog.h @@ -10,8 +10,8 @@ using std::string; #ifndef WATCHDOG_H #define WATCHDOG_H /** - * Watch dog is a functor object that should be called as such, don't worry it suicides itself and deallocates it's own memory - * WatchDog* watch = new WatchDog; + * Watch dog is a functor object that should be called as such + * WatchDog watch; * std::thread(watch,binaryName); */ class WatchDog{