blob: a98f8c15730e1a9ac20b3fa22affca04e2c1a8f2 [file] [log] [blame]
Alexander Afanasyevca6f3292012-04-09 10:03:30 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#ifndef SYNC_SCHEDULER_H
24#define SYNC_SCHEDULER_H
25
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070026#include <ns3/nstime.h>
27#include <ns3/event-id.h>
Alexander Afanasyev3a229132012-04-25 15:07:26 -070028#include <ns3/simulator.h>
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070029#include <list>
30#include <map>
31
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070032#include "sync-event.h"
33
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070034#define TIME_SECONDS(number) ns3::Seconds(number)
35#define TIME_MILLISECONDS(number) ns3::MilliSeconds(number)
Alexander Afanasyev3a229132012-04-25 15:07:26 -070036#define TIME_NOW ns3::Simulator::Now ()
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070037typedef ns3::Time TimeDuration;
Alexander Afanasyev3a229132012-04-25 15:07:26 -070038typedef ns3::Time TimeAbsolute;
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070039
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070040namespace Sync {
41
42/**
43 * @ingroup sync
44 * @brief General purpose event scheduler
45 *
46 * This class internally runs a thread and events can be scheduled by specifying an absolute or relative time of the event
47 */
48class Scheduler
49{
50public:
51 /**
52 * @brief Default constructor. Thread will be created
53 */
54 Scheduler ();
55 /**
56 * @brief Destructor. Thread will be nicely stopped
57 */
58 ~Scheduler ();
59
60 /**
61 * @brief Schedule an event at absolute time 'abstime'
62 * @param abstime Absolute time
63 * @param event function to be called at the time
64 * @param label Label for the event
65 */
66 // void
67 // schedule (const boost::system_time &abstime, Event event, uint32_t label);
68
69 /**
70 * @brief Schedule an event at relative time 'reltime'
71 * @param reltime Relative time
72 * @param event function to be called at the time
73 * @param label Label for the event
74 */
75 void
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070076 schedule (const TimeDuration &reltime, Event event, uint32_t label);
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070077
78 /**
79 * @brief Cancel all events for the label
80 * @param label Label of the event that needs to be cancelled
81 */
82 void
83 cancel (uint32_t label);
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070084
85private:
86 static void
87 eventWrapper (Event event);
88
89private:
90 std::map< uint32_t, std::list< ns3::EventId > > m_labeledEvents;
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070091};
92
93}
94
95#endif // SYNC_SCHEDULER_H