job scheduler works with run once and run periodic schedules. Stop logic is not fully thought through.

Dependencies:   LinkedList

Dependents:   JobSchedulerDemo Borsch

schedules.h

Committer:
sgnezdov
Date:
2017-07-11
Revision:
8:4ead1f4ab741
Parent:
2:9bf5366ad5a2
Child:
16:f61b62b119dd

File content as of revision 8:4ead1f4ab741:

#pragma once

#include "scheduler.h"

namespace JobScheduler {

    class RunOnceSchedule: public ISchedule {
        public:
            RunOnceSchedule(time_t time): _time(time) {};
            
            virtual ~RunOnceSchedule() {};
            
            virtual time_t NextRunTime(time_t from) {
                time_t current = _time;
                _time = 0;
                return current;
            };
        
        private:
            time_t _time;
    };
    
    class RunPeriodicSchedule: public ISchedule {
        public:
            RunPeriodicSchedule(time_t period, int limit = 0)
            : _period(period), _limit(limit), _counter(0) {};
            
            virtual ~RunPeriodicSchedule() {};
            
            virtual time_t NextRunTime(time_t from) {
                if (_limit > 0 && _counter == _limit) {
                    // no next run
                    return 0;
                }
                _counter++;
                time_t next = from + _period;
                return next;
            };
        
        private:
            time_t _period;
            /** limit limits number of times to execute NextRunTime.
            If set to zero, then it is unlimitted.
            */
            int _limit;
            int _counter;
    };

} // end of namespace