Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/sched/TmTimer.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2005 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include <marsyas/sched/TmTimer.h>
00020 #include "../common_source.h"
00021 
00022 using namespace std;
00023 using namespace Marsyas;
00024 /*
00025 TmTimer::TmTimer()
00026 {
00027     init();
00028 }
00029 TmTimer::TmTimer(mrs_string name)
00030 {
00031     init();
00032     name_=name;
00033 }
00034 */
00035 TmTimer::TmTimer(std::string type, std::string name)
00036 {
00037   type_=type;
00038   name_=name;
00039   cur_time_=0;
00040   init();
00041 }
00042 TmTimer::TmTimer(const TmTimer& t)
00043 {
00044   name_=t.name_;
00045 //    granularity_=t.granularity_;
00046   type_=t.type_;        // Type of MarSystem
00047   name_=t.name_;        // Name of instance
00048 //    next_trigger_=t.next_trigger_;
00049   cur_time_=t.cur_time_;
00050 //    scheduler=t.scheduler;
00051 }
00052 
00053 TmTimer::~TmTimer() { }
00054 
00055 void
00056 TmTimer::init()
00057 {
00058 //    cur_time_=0;
00059 //    granularity_=0;
00060 //    next_trigger_=0;
00061 }
00062 
00063 mrs_string
00064 TmTimer::getName()
00065 {
00066   return name_;
00067 }
00068 
00069 mrs_string
00070 TmTimer::getType()
00071 {
00072   return type_;
00073 }
00074 
00075 mrs_string
00076 TmTimer::getPrefix()
00077 {
00078   return type_ + "/" + name_;
00079 }
00080 
00081 mrs_natural
00082 TmTimer::getTime()
00083 {
00084   return cur_time_;
00085 }
00086 
00087 void
00088 TmTimer::tick()
00089 {
00090   updtime();
00091   trigger();
00092 }
00093 
00094 void
00095 TmTimer::updtime()
00096 {
00097   // now update the time for the end of this time slot
00098   mrs_natural adj_time = readTimeSrc();
00099   if (adj_time<1)
00100     return;
00101   cur_time_ += adj_time;
00102 }
00103 
00104 void TmTimer::trigger()
00105 {
00106   dispatch();
00107 }
00108 
00109 void
00110 TmTimer::updtimer(std::string cname, TmControlValue value)
00111 {
00112   (void)cname; // FIXME These values are unused
00113   (void)value;
00114   std::string x = "TmTimer::updtimer(\""+cname+"\","+value.getSType()+")  updtimer not supported for this timer";
00115   MRSWARN(x);
00116 }
00117 
00118 void
00119 TmTimer::updtimer(TmParam& param)
00120 {
00121   updtimer(param.cname(),param.value());
00122 }
00123 
00124 void
00125 TmTimer::updtimer(std::vector<TmParam> params)
00126 {
00127   vector<TmParam>::const_iterator tvi;
00128   for(tvi=params.begin(); tvi!=params.end(); ++tvi) {
00129     TmParam p = *tvi;
00130     updtimer(p);
00131   }
00132   MRSWARN("TmTimer::updtimer(mrs_string,TmControlValue)  updtimer not supported for this timer");
00133 }
00134 
00135 void
00136 TmTimer::post(std::string event_time, Repeat rep, EvEvent* ev)
00137 {
00138   rep--;
00139   // should probably check if rep.count==0
00140   mrs_natural stime = getTime() + intervalsize(event_time);
00141   ev->setTime(stime);
00142   ev->setRepeat(rep);
00143   post(ev);
00144 }
00145 
00146 void
00147 TmTimer::post(std::string event_time, EvEvent* ev)
00148 {
00149   mrs_natural stime = getTime() + intervalsize(event_time);
00150   ev->setTime(stime);
00151   ev->setRepeat(Repeat());
00152   post(ev);
00153 }
00154 
00155 void
00156 TmTimer::post(EvEvent* ev)
00157 {
00158   ev->setTimer(this); // for EvEpr type events that want to read the timer
00159   // add pointer to map
00160   events_[ev->getPrefix()] = ev;
00161   // add to heap
00162   pq_.push(ev);
00163 }
00164 
00165 bool
00166 TmTimer::eventPending()
00167 {
00168   return (!pq_.empty() && pq_.top()->getTime()<getTime());
00169 }
00170 
00171 void
00172 TmTimer::dispatch()
00173 { //if (getPrefix()=="TmVirtualTime/neil") cout << getTime() << endl;
00174   while (eventPending()) {
00175     // dispatch
00176     EvEvent* ev = pq_.pop();
00177     ev->dispatch();
00178     // handle repetition
00179     if (ev->repeat()) {
00180       ev->doRepeat();
00181       post(ev);
00182     }
00183     else {
00184       // delete handle to event
00185       events_iter_ = events_.find(ev->getPrefix());
00186       if (events_iter_ != events_.end()) {
00187         events_.erase(events_iter_);
00188       }
00189       delete(ev);
00190     }
00191   }
00192 }
00193 
00194 
00195 /*
00196 ostream&
00197 Marsyas::operator<< (ostream& o, Scheduler& sys) {
00198 //    sys.put(o);
00199 
00200     return o;
00201 }
00202 */