User Manual, Developers Guide and API Documentation

PeriodicTimeout.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 
00029 #include <WNS/simulator/ISimulator.hpp>
00030 #include <WNS/events/scheduler/Interface.hpp>
00031 
00032 #include <WNS/events/PeriodicTimeout.hpp>
00033 
00034 using namespace wns::events;
00035 
00036 PeriodicTimeout::PeriodicTimeoutFunctor::PeriodicTimeoutFunctor(PeriodicTimeout* _dest, wns::simulator::Time _period) :
00037     period_(_period),
00038     dest_(_dest)
00039 {}
00040 
00041 
00042 void
00043 PeriodicTimeout::PeriodicTimeoutFunctor::operator()()
00044 {
00045     this->dest_->periodicEv_ =
00046         wns::simulator::getEventScheduler()->scheduleDelay(*this, this->period_);
00047     this->dest_->periodically();
00048 }
00049 
00050 simTimeType
00051 PeriodicTimeout::PeriodicTimeoutFunctor::getPeriod() const
00052 {
00053     return this->period_;
00054 }
00055 
00056 void
00057 PeriodicTimeout::PeriodicTimeoutFunctor::print(std::ostream& aStreamRef) const
00058 {
00059     aStreamRef << "<" << TypeInfo::create(*this) <<" instance at "
00060            << static_cast<const void* const>(this) << ">";
00061 
00062     aStreamRef << ", target: " << wns::TypeInfo::create(*this->dest_);
00063 }
00064 
00065 
00066 PeriodicTimeout::PeriodicTimeout() :
00067     period_(-1),
00068     periodicEv_(scheduler::IEventPtr())
00069 {
00070 }
00071 
00072 
00073 PeriodicTimeout::~PeriodicTimeout()
00074 {
00075     if(this->hasPeriodicTimeoutSet())
00076     {
00077         this->cancelPeriodicTimeout();
00078     }
00079 }
00080 
00081 
00082 PeriodicTimeout::PeriodicTimeout(const PeriodicTimeout& other) :
00083     period_(other.period_),
00084     periodicEv_(scheduler::IEventPtr())
00085 {
00086     if(other.hasPeriodicTimeoutSet())
00087     {
00088         this->startPeriodicTimeout(other.period_,
00089                        other.periodicEv_->getScheduled() - wns::simulator::getEventScheduler()->getTime());
00090 
00091         assure(this->periodicEv_ != other.periodicEv_,
00092                "PeriodicTimeout(const PeriodicTimeout& other): "
00093                "Events of source and destination "
00094                "must not be the same");
00095     }
00096 }
00097 
00098 
00099 void
00100 PeriodicTimeout::startPeriodicTimeout(wns::simulator::Time _period, wns::simulator::Time delay)
00101 {
00102     assure(_period > 0,
00103            "The peroid must be >0, otherwise PeriodicTimeout will get stuck in an endless event loop!");
00104 
00105     if (this->hasPeriodicTimeoutSet())
00106     {
00107         this->cancelPeriodicTimeout();
00108     }
00109 
00110     this->period_ = _period;
00111 
00112     this->periodicEv_ = wns::simulator::getEventScheduler()->
00113         scheduleDelay(PeriodicTimeoutFunctor(this, this->period_), delay);
00114 }
00115 
00116 
00117 bool
00118 PeriodicTimeout::hasPeriodicTimeoutSet() const
00119 {
00120     return this->periodicEv_ != NULL;
00121 }
00122 
00123 
00124 void
00125 PeriodicTimeout::cancelPeriodicTimeout()
00126 {
00127     if (!hasPeriodicTimeoutSet())
00128     {
00129         return;
00130     }
00131     wns::simulator::getEventScheduler()->cancelEvent(this->periodicEv_);
00132     this->periodicEv_ = scheduler::IEventPtr();
00133 
00134     // invalidate period
00135     this->period_ = -1;
00136 }
00137 
00138 
00139 

Generated on Fri May 25 03:31:38 2012 for openWNS by  doxygen 1.5.5