blob: d5d57aabe6c59d23921bd4e6f21654a78ee67dbe [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (C) 2014 Named Data Networking Project
* See COPYING for copyright and distribution information.
*/
#ifndef NFD_CORE_SCHEDULER_HPP
#define NFD_CORE_SCHEDULER_HPP
#include "common.hpp"
#include "monotonic_deadline_timer.hpp"
namespace ndn {
struct EventIdImpl; ///< \brief Private storage of information about the event
/**
* \brief Opaque type (shared_ptr) representing ID of the scheduled event
*/
typedef shared_ptr<EventIdImpl> EventId;
/**
* \brief Generic scheduler
*/
class Scheduler
{
public:
typedef function<void()> Event;
Scheduler(boost::asio::io_service& ioService);
/**
* \brief Schedule one time event after the specified delay
* \returns EventId that can be used to cancel the scheduled event
*/
EventId
scheduleEvent(const time::Duration& after, const Event& event);
/**
* \brief Schedule periodic event that should be fired every specified period.
* First event will be fired after the specified delay.
* \returns EventId that can be used to cancel the scheduled event
*/
EventId
schedulePeriodicEvent(const time::Duration& after,
const time::Duration& period,
const Event& event);
/**
* \brief Cancel scheduled event
*/
void
cancelEvent(const EventId& eventId);
private:
void
onEvent(const boost::system::error_code& code);
private:
boost::asio::io_service& m_ioService;
struct EventInfo
{
EventInfo(const time::Duration& after,
const time::Duration& period,
const Event& event);
EventInfo(const time::Point& when, const EventInfo& previousEvent);
bool
operator <=(const EventInfo& other) const
{
return this->m_scheduledTime <= other.m_scheduledTime;
}
bool
operator <(const EventInfo& other) const
{
return this->m_scheduledTime < other.m_scheduledTime;
}
time::Duration
expiresFromNow() const;
time::Point m_scheduledTime;
time::Duration m_period;
Event m_event;
mutable EventId m_eventId;
};
typedef std::multiset<EventInfo> EventQueue;
friend struct EventIdImpl;
EventQueue m_events;
EventQueue::iterator m_scheduledEvent;
boost::asio::monotonic_deadline_timer m_deadlineTimer;
};
} // namespace ndn
#endif // NFD_CORE_SCHEDULER_HPP