00001 #include "mrta/sample_holder.h" 00002 #include "mrta/state_monitor.h" 00003 #include <ros/console.h> 00004 00005 namespace mrta 00006 { 00007 SampleHolder::SampleHolder(StateMonitor* monitor, const ros::Duration& timeout) 00008 : QObject(monitor), timer_(new QTimer(this)) 00009 { 00010 connect(timer_, SIGNAL(timeout()), this, SLOT(expired())); 00011 setUpToDate(false); 00012 setTimeout(timeout); 00013 } 00014 00015 SampleHolder::~SampleHolder() 00016 { 00017 ROS_INFO_STREAM("[~SampleHolder] before"); 00018 timer_->stop(); 00019 if (timer_) 00020 { 00021 delete timer_; 00022 timer_ = NULL; 00023 } 00024 ROS_INFO_STREAM("[~SampleHolder] after"); 00025 } 00026 00027 bool SampleHolder::isUpToDate() const { return up_to_date_; } 00028 00029 ros::Duration SampleHolder::getTimeout() const { return timeout_; } 00030 00031 void SampleHolder::setUpToDate(bool up_to_date) 00032 { 00033 timer_->stop(); 00034 if (up_to_date) 00035 { 00036 timer_->start(1e3 * timeout_.toSec()); 00037 } 00038 if (up_to_date != up_to_date_) 00039 { 00040 up_to_date_ = up_to_date; 00041 emit updated(up_to_date); 00042 emit changed(); 00043 } 00044 } 00045 00046 void SampleHolder::setTimeout(const ros::Duration& timeout) 00047 { 00048 if (timeout != timeout_) 00049 { 00050 timer_->stop(); 00051 if (up_to_date_) 00052 { 00053 timer_->start(1e3 * timeout_.toSec()); 00054 } 00055 timeout_ = timeout; 00056 emit timeoutChanged(timeout); 00057 emit changed(); 00058 } 00059 } 00060 00061 void SampleHolder::update() { setUpToDate(true); } 00062 00063 void SampleHolder::expired() { setUpToDate(false); } 00064 }