scheduler.h

Go to the documentation of this file.
00001 /* Vaste programme.
00002  *
00003  * PegSoft scheduler library (c) 2005 PegSoft
00004  * Contact us at pegsoft@pegsoft.net
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License version 2 as
00008  * published by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this software (the COPYING file); if not, write to the
00017  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
00018  * USA.
00019  *
00020  */
00021 
00027 #ifndef SCHEDULER_SCHEDULER_H
00028 #define SCHEDULER_SCHEDULER_H
00029 
00030 /*************************************************************************/
00031 
00032 #include <list>
00033 #include <string>
00034 
00035 /*************************************************************************/
00036 
00038 
00039 namespace scheduler
00040 {
00041 
00042 /*************************************************************************/
00043 
00044 class Task;
00045 
00046 /*************************************************************************/
00047 
00049 
00053 class Scheduler
00054 {
00055     public:
00057         typedef std::list<Task *> list_type;
00058         
00060         Scheduler() : m_tasks(), m_curtask(0), m_tasktype(0) { }
00062         Scheduler(Scheduler const &s) : m_tasks(s.m_tasks),
00063             m_curtask(0), m_tasktype(s.m_tasktype) { }
00065         Scheduler &operator=(Scheduler const &right);
00066         
00068 
00071         int register_type() { return m_tasktype++; }
00072         
00074 
00078         bool add(Task &task);
00080 
00086         bool remove(Task &task);
00088 
00094         list_type::size_type remove_bytype(int type);
00096 
00103         list_type::size_type remove_byname(int type, std::string const &name);
00104         
00106 
00113         bool run();
00114         
00116 
00123         list_type::size_type run_bytype(int type);
00125 
00133         list_type::size_type run_byname(int type, std::string const &name);
00134         
00136         list_type const &tasks() const { return m_tasks; }
00138         Task const *current_task() const { return m_curtask; }
00139     private:
00140         list_type m_tasks;
00141         Task *m_curtask;
00142         
00143         int m_tasktype;
00144         
00145         bool run_task(list_type::iterator &task, list_type::iterator *next);
00146 };
00147 
00148 /*************************************************************************/
00149 
00151 
00160 class Task
00161 {
00162     public:
00164         explicit Task(int type) : m_type(type) { }
00166         explicit Task(Task const &t) : m_type(t.m_type) { }
00168         virtual Task &operator=(Task const &right);
00170         virtual ~Task() { }
00171         
00173 
00180         virtual bool poll() = 0;
00181         
00183         enum
00184         {
00186             rOK = 0x0,
00188             rSlept = 0x1,
00190             rRemove = 0x2,
00192             rFree = (rRemove | 0x4)
00193         };
00195 
00201         virtual int run() = 0;
00202         
00204 
00207         virtual std::string description() const = 0;
00209 
00213         virtual std::string name() const = 0;
00215         virtual int type() const { return m_type; }
00217 
00220         virtual int type(int type) { return (m_type = type); }
00221     protected:
00223         int m_type;
00224 };
00225 
00226 /*************************************************************************/
00227 
00229 
00230 class TaskAlways : public Task
00231 {
00232     public:
00234         explicit TaskAlways(int type) : Task(type) { }
00235         
00237         virtual bool poll() { return true; }
00238 };
00239 
00240 /*************************************************************************/
00241 
00243 
00248 class TaskTimeout : public Task
00249 {
00250     public:
00252 
00258         TaskTimeout(int type, time_t timeout) : Task(type), m_timeout(timeout),
00259                 m_lastrun(time(NULL)) { }
00261         explicit TaskTimeout(TaskTimeout const &t) : Task(t), m_timeout(t.m_timeout),
00262                 m_lastrun(t.m_lastrun) { }
00264         virtual TaskTimeout &operator=(TaskTimeout const &right);
00265         
00267         virtual bool poll();
00268         
00270         time_t timeout() const { return m_timeout; }
00272 
00275         time_t timeout(time_t timeout) { return (m_timeout = timeout); }
00276         
00278         time_t last_run() const { return m_lastrun; }
00279     protected:
00281         time_t m_timeout;
00283         time_t m_lastrun;
00284 };
00285 
00286 /*************************************************************************/
00287 
00289 
00305 class TaskStep : public Task
00306 {
00307     public:
00309 
00316         TaskStep(int type, time_t step, time_t shift);
00318         explicit TaskStep(TaskStep const &t) : Task(t), m_step(t.m_step),
00319                 m_shift(t.m_shift), m_next(t.m_next) { }
00321         virtual TaskStep &operator=(TaskStep const &right);
00322         
00324         virtual bool poll();
00325         
00327         time_t step() const { return m_step; }
00329 
00333         time_t step(time_t step);
00334         
00336         time_t shift() const { return m_shift; }
00338 
00341         time_t shift(time_t shift);
00342         
00344         time_t next() const { return m_next; } 
00345     protected:
00347         time_t m_step;
00349         time_t m_shift;
00351         time_t m_next;
00352     private:
00353         void calc_next();
00354         time_t timezone_offset(time_t now);
00355 };
00356 
00357 /*************************************************************************/
00358 
00359 } /* namespace scheduler */
00360 
00361 /*************************************************************************/
00362 
00363 #endif /* SCHEDULER_SCHEDULER_H */

Generated on Wed Aug 15 00:37:22 2007 for Epona API by  doxygen 1.5.2