Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/sched/Scheduler.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/Scheduler.h>
00020 #include <marsyas/sched/EvExpr.h>
00021 #include <marsyas/sched/TmTimerManager.h>
00022 
00023 using namespace std;
00024 //using namespace Marsyas;
00025 
00026 namespace Marsyas //lmartins: hack: should work without this [?][!]
00027 {
00028 
00029 Scheduler::Scheduler()
00030 {
00031   timers_=NULL;
00032   timers_count_=0;
00033 }
00034 
00035 Scheduler::~Scheduler()
00036 {
00037   removeAll();
00038 }
00039 
00040 void
00041 Scheduler::tick()
00042 {
00043   for (int i=0; i<timers_count_; ++i) { timers_[i]->tick(); }
00044 }
00045 
00046 bool
00047 Scheduler::eventPending()
00048 {
00049   for (int i=0; i<timers_count_; ++i) {
00050     if (timers_[i]->eventPending()) { return true; }
00051   }
00052   return false;
00053 }
00054 
00055 void
00056 Scheduler::addTimer(TmTimer* t)
00057 {
00058   if (t == NULL) return;
00059   // look for schedulers with same name to ensure only one of each name
00060   if (findTimer(t->getPrefix())!=NULL) {
00061     MRSWARN("Scheduler::addTimer(TmTimer)  refusing to add timer with name already in use");
00062   }
00063   else
00064     appendTimer(t);
00065 }
00066 
00067 void
00068 Scheduler::addTimer(std::string class_name, std::string identifier)
00069 {
00070   // look for schedulers with same name to ensure only one of each name
00071   if (findTimer(class_name+"/"+identifier)!=NULL) {
00072     MRSWARN("Scheduler::addTimer(\""+class_name+"\",\""+identifier+"\")  refusing to add timer with name already in use");
00073   }
00074   else {
00075     addTimer(TmTimerManager::getInstance()->make(class_name,identifier));
00076   }
00077 }
00078 
00079 void
00080 Scheduler::addTimer(std::string class_name, std::string identifier, std::vector<TmParam> params)
00081 {
00082   // look for schedulers with same name to ensure only one of each name
00083   if (findTimer(class_name+"/"+identifier)!=NULL) {
00084     MRSWARN("Scheduler::addTimer(\""+class_name+"\",\""+identifier+"\",TmParams)  refusing to add timer with name already in use");
00085   }
00086   else {
00087     addTimer(TmTimerManager::getInstance()->make(class_name,identifier,params));
00088   }
00089 }
00090 
00091 void
00092 Scheduler::split_cname(std::string cname, std::string* head, std::string* tail)
00093 {
00094   bool second=false;
00095   for (size_t i=0; i<cname.length(); ++i) {
00096     if (cname[i]=='/') {
00097       if (!second) {
00098 //                scheduler_type = cname.substr(0,i);
00099         second=true;
00100       }
00101       else {
00102         *head = cname.substr(0,i);
00103         *tail = cname.substr(i+1,cname.length());
00104         break;
00105       }
00106     }
00107   }
00108 }
00109 
00110 void
00111 Scheduler::updtimer(std::string tmr_id, TmControlValue value)
00112 {
00113   mrs_string timer_ident="";
00114   mrs_string timer_control="";
00115   split_cname(tmr_id,&timer_ident,&timer_control);
00116   TmTimer* s = findTimer(timer_ident);
00117   if (s==NULL) {
00118     MRSWARN("Scheduler::updtimer(std::string,TmControlValue)  no timer: "+timer_ident);
00119   }
00120   else {
00121     s->updtimer(timer_control,value);
00122   }
00123 }
00124 void
00125 Scheduler::updtimer(std::string tmr_id, TmParam& param)
00126 {
00127   TmTimer* s = findTimer(tmr_id);
00128   if (s==NULL) {
00129     MRSWARN("Scheduler::updtimer(std::string,TmControlValue)  no timer: "+tmr_id);
00130   }
00131   else {
00132     s->updtimer(param);
00133   }
00134 }
00135 
00136 void
00137 Scheduler::updtimer(std::string tmr_id, std::vector<TmParam> params)
00138 {
00139   mrs_string timer_ident="";
00140   mrs_string timer_control="";
00141   split_cname(tmr_id,&timer_ident,&timer_control);
00142   TmTimer* s = findTimer(timer_ident);
00143   if (s==NULL) {
00144     MRSWARN("Scheduler::updtimer(std::string,TmControlValue)  no timer: "+timer_ident);
00145   }
00146   else {
00147     s->updtimer(params);
00148   }
00149 }
00150 
00151 void
00152 Scheduler::appendTimer(TmTimer* s)
00153 {
00154   timers_ = (TmTimer**)realloc(timers_,sizeof(TmTimer*)*(timers_count_+1));
00155   timers_[timers_count_] = s;
00156   timers_count_ = timers_count_ + 1;
00157 }
00158 
00159 TmTimer*
00160 Scheduler::findTimer(std::string name)
00161 {
00162   for (int i=0; i<timers_count_; ++i) {
00163     TmTimer* s = timers_[i];
00164     if (s->getPrefix()==name) {
00165       return s;
00166     }
00167   }
00168   return NULL;
00169 }
00170 
00171 bool
00172 Scheduler::removeTimer(std::string name)
00173 {
00174   for (int i=0; i<timers_count_; ++i) {
00175     if (timers_[i]->getPrefix()==name) {
00176       delete(timers_[i]);
00177       for (int j=i+1; j<timers_count_; j++) {
00178         timers_[j-1]=timers_[j];
00179         timers_[j]=NULL;
00180       }
00181       timers_=(TmTimer**)realloc(timers_,sizeof(TmTimer*)*timers_count_);
00182       return true;
00183     }
00184   }
00185   return false;
00186 }
00187 
00188 void
00189 Scheduler::removeAll()
00190 {
00191   if (timers_count_>0) {
00192     for (int i=0; i<timers_count_; ++i) {
00193       delete timers_[i];
00194     }
00195     free(timers_);
00196     timers_=NULL;
00197     timers_count_=0;
00198   }
00199 }
00200 
00201 void
00202 Scheduler::post(std::string time, std::string tmname, Repeat r, EvEvent* me)
00203 {
00204   TmTimer* s = findTimer(tmname);
00205   if (s!=NULL) {
00206     if (me!=NULL) {
00207       // EvExpr supports querying of the scheduler environment in expressions
00208       EvExpr* e=dynamic_cast<EvExpr*>(me);
00209       if (e!=NULL) {
00210         MRSWARN("Scheduler::post(mrs_string time, mrs_string tmname, Repeat r, EvEvent* me) : setScheduler is not working yet");
00211         e->getExpression()->setScheduler(this);
00212       }
00213       s->post(time,r,me);
00214     }
00215     else {
00216       MRSWARN("Scheduler::post(mrs_string,mrs_string,Repeat,EvEvent*)  NULL event");
00217     }
00218   }
00219   else { MRSWARN("Scheduler::post(mrs_string,mrs_string,Repeat,EvEvent*)  unknown timer name: "+tmname); }
00220 }
00221 
00222 void
00223 Scheduler::post(TmTime t, Repeat r, EvEvent* me)
00224 {
00225   // pass the buck
00226   post(t.getTime(),t.getTimeName(),r,me);
00227 }
00228 
00229 void
00230 Scheduler::post(std::string event_time, Repeat rep, EvEvent* me)
00231 {
00232   if (timers_[0]!=NULL) {
00233     post(event_time,timers_[0]->getPrefix(),rep,me);
00234   }
00235 }
00236 
00237 void
00238 Scheduler::post(std::string event_time, EvEvent* me)
00239 {
00240   if (timers_[0]!=NULL) {
00241     post(event_time,Repeat(),me);
00242   }
00243 }
00244 
00245 mrs_natural
00246 Scheduler::getTime(std::string timer)
00247 {
00248   TmTimer* s = findTimer(timer);
00249   if (s!=NULL)
00250     return s->getTime();
00251   MRSWARN("Scheduler::getTime(string)  unknown timer '"+timer+"'");
00252   return 0;
00253 }
00254 
00255 //ostream&
00256 //Marsyas::operator<< (ostream& o, Scheduler& sys) {
00258 //    return o;
00259 //}
00260 
00261 }//namespace Marsyas