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

Dependencies:   LinkedList

Dependents:   JobSchedulerDemo Borsch

scheduler.h

Committer:
sgnezdov
Date:
2017-07-11
Revision:
0:806403f3d0d1
Child:
1:ec6a1d054065

File content as of revision 0:806403f3d0d1:

#pragma once

#include "mbed.h"
#include "LinkedList.h"
#include "jobService.h"

namespace JobScheduler {
    
    typedef int JobID;
    typedef int ActionType;
    
    const ActionType ActionJobAdd(1);
    
    /**
    Declares concept of the schedule.
    For example, run once, run periodically, run at the top of the hour,
    never run, run weekly, monthly, etc.
    */
    class ISchedule {
        public:
            virtual ~ISchedule() = 0;
    };
    
    struct JobRes {
        Error error;
        JobID jobID;
        JobRes(Error err, JobID id): error(err), jobID(id) {}
    };
    
    struct Action {
        ActionType type;
        void *data;
        Queue<JobRes, 1> response;
        Action(ActionType t, void *d): type(t), data(d) {}
    };
       
    /**
    Job describes the job and its parameters, but not the job's schedule.
    */
    class Job {
        public:
        
        Job(JobID id, int typeID): _id(id), _typeID(typeID) {};

        JobID GetID() const {
            return _id;
        };
        
        int GetTypeID() const {
            return _typeID;
        };
        
        private:
            JobID _id;
            int _typeID;
    };
    
   
    /**
    Scheduler is responsible for maintaining job schedules and running jobs
    in a serial manner.  For example, next job appointments can be:
    14:05, 14:05, 15:00, 16:00 and scheduler will run jobs even one after
    another even if job run times collide.  
    
    The scheduler has no means of stopping running job.
    
    The order of execution is preserved if job runs for a long time.

    */
    class Scheduler {
        public:
            Scheduler(JobService *_jobService);
            
            void Start();
            void Stop();
            void WaitToStop();
            
            /** JobAdd adds job of typeID and returns ID of added job. */
            JobRes* JobAdd(int typeID);
            void JobRemove(int jobID);
        private:
            static void updateAdapter(void *target);
            void updateHandler();
            void process(Action *action);
        private:
            Thread _updater;
            bool _quit;
            JobService *_jobService;
            JobID _nextJobID;
            /** _actions contains incoming action queue to handle. */
            Queue<Action, 5> _actions;
            
    };  
}