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 
00037 namespace modules { class Module; }
00038 
00039 /*************************************************************************/
00040 
00042 
00043 namespace scheduler
00044 {
00045 
00046 /*************************************************************************/
00047 
00048 class Task;
00049 
00050 /*************************************************************************/
00051 
00053 
00057 class Scheduler
00058 {
00059     private:
00061         typedef std::list<Task *> list_type;
00062     public:
00064         typedef list_type::size_type size_type;
00066         typedef list_type::const_iterator iterator;
00068         typedef list_type::const_iterator const_iterator;
00070         typedef list_type::const_reverse_iterator reverse_iterator;
00072         typedef list_type::const_reverse_iterator const_reverse_iterator;
00073         
00075         Scheduler() : m_tasks(), m_curtask(0) { }
00077         Scheduler(Scheduler const &s) : m_tasks(s.m_tasks),
00078             m_curtask(0) { }
00080         Scheduler &operator=(Scheduler const &right);
00081         
00083 
00091         bool add(Task &task);
00092         
00094 
00100         void remove(Task &task);
00101         
00103 
00113         size_type remove_bymodule(modules::Module const *module, 
00114                 bool free = false);
00115         
00117 
00126         size_type remove_byname(modules::Module const *module, 
00127                 std::string const &name, bool free = false);
00128         
00130 
00137         bool run();
00138         
00140 
00147         size_type run_bymodule(modules::Module const *module);
00148         
00150 
00158         size_type run_byname(modules::Module const *module, 
00159                 std::string const &name);
00160         
00162         Task const *current_task() const { return m_curtask; }
00163         
00165         iterator begin() { return m_tasks.begin(); }
00167         const_iterator begin() const { return m_tasks.begin(); }
00169         iterator end() { return m_tasks.end(); }
00171         const_iterator end() const { return m_tasks.end(); }
00172         
00174         reverse_iterator rbegin() { return m_tasks.rbegin(); }
00176         const_reverse_iterator rbegin() const { return m_tasks.rbegin(); }
00178         reverse_iterator rend() { return m_tasks.rend(); }
00180         const_reverse_iterator rend() const { return m_tasks.rend(); }
00181         
00183         bool empty() const { return m_tasks.empty(); }
00185         list_type::size_type size() const { return m_tasks.size(); }
00186     private:
00187         list_type m_tasks;
00188         Task *m_curtask;
00189         
00190         bool run_task(list_type::iterator &task, list_type::iterator *next);
00191 };
00192 
00193 /*************************************************************************/
00194 
00196 
00205 class Task
00206 {
00207     public:
00209 
00219         explicit Task(modules::Module *module, std::string const &name = "",
00220                 std::string const &desc = "") : m_module(module), m_name(name),
00221                 m_desc(desc) { }
00223         Task(Task const &t) : m_module(t.m_module), m_name(t.m_name),
00224                 m_desc(t.m_desc) { }
00226         Task &operator=(Task const &right);
00228         virtual ~Task() { }
00229         
00231 
00238         virtual bool poll() = 0;
00239         
00241         enum
00242         {
00244             rOK = 0x0,
00246             rSlept = 0x1,
00248             rRemove = 0x2,
00250             rFree = (rRemove | 0x4)
00251         };
00253 
00259         virtual int run() = 0;
00260         
00262         modules::Module *module() const { return m_module; }
00263         
00265 
00272         virtual std::string description() const { return m_desc; }
00273         
00275 
00284         virtual std::string name() const { return m_name; }
00285     protected:
00287         modules::Module *m_module;
00288         
00290         std::string m_name;
00292         std::string m_desc;
00293 };
00294 
00295 /*************************************************************************/
00296 
00298 
00299 class TaskAlways : public Task
00300 {
00301     public:
00303 
00313         TaskAlways(modules::Module *module, std::string const &name = "", 
00314                 std::string const &desc = "") : Task(module, name, desc) { }
00315         
00317         virtual bool poll() { return true; }
00318 };
00319 
00320 /*************************************************************************/
00321 
00323 
00328 class TaskTimeout : public Task
00329 {
00330     public:
00332 
00345         TaskTimeout(time_t timeout, modules::Module *module,
00346                 std::string const &name = "", std::string const &desc = "") : 
00347                 Task(module, name, desc), m_timeout(timeout),
00348                 m_lastrun(time(0)) { }
00350         TaskTimeout(TaskTimeout const &t) : Task(t), 
00351                 m_timeout(t.m_timeout), m_lastrun(t.m_lastrun) { }
00353         TaskTimeout &operator=(TaskTimeout const &right);
00354         
00356         virtual bool poll();
00357         
00359         time_t timeout() const { return m_timeout; }
00361 
00364         time_t timeout(time_t timeout) { return (m_timeout = timeout); }
00365         
00367         time_t last_run() const { return m_lastrun; }
00368     protected:
00370         time_t m_timeout;
00372         time_t m_lastrun;
00373 };
00374 
00375 /*************************************************************************/
00376 
00378 
00394 class TaskStep : public Task
00395 {
00396     public:
00398 
00412         TaskStep(time_t step, time_t shift, modules::Module *module,
00413                 std::string const &name = "", std::string const &desc = "");
00415         TaskStep(TaskStep const &t) : Task(t), m_step(t.m_step),
00416                 m_shift(t.m_shift), m_next(t.m_next) { }
00418         TaskStep &operator=(TaskStep const &right);
00419         
00421         virtual bool poll();
00422         
00424         time_t step() const { return m_step; }
00426 
00430         time_t step(time_t step);
00431         
00433         time_t shift() const { return m_shift; }
00435 
00438         time_t shift(time_t shift);
00439         
00441         time_t next() const { return m_next; } 
00442     protected:
00444         time_t m_step;
00446         time_t m_shift;
00448         time_t m_next;
00449     private:
00450         void calc_next();
00451         time_t timezone_offset(time_t now);
00452 };
00453 
00454 /*************************************************************************/
00455 
00456 } /* namespace scheduler */
00457 
00458 /*************************************************************************/
00459 
00460 #endif /* SCHEDULER_SCHEDULER_H */

Generated on Fri Apr 18 22:03:27 2008 for Epona API by  doxygen 1.5.3