Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 26 | #include <ns3/nstime.h> |
| 27 | #include <ns3/event-id.h> |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame^] | 28 | #include <ns3/simulator.h> |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 29 | #include <list> |
| 30 | #include <map> |
| 31 | |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 32 | #include "sync-event.h" |
| 33 | |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 34 | #define TIME_SECONDS(number) ns3::Seconds(number) |
| 35 | #define TIME_MILLISECONDS(number) ns3::MilliSeconds(number) |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame^] | 36 | #define TIME_NOW ns3::Simulator::Now () |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 37 | typedef ns3::Time TimeDuration; |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame^] | 38 | typedef ns3::Time TimeAbsolute; |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 40 | namespace 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 | */ |
| 48 | class Scheduler |
| 49 | { |
| 50 | public: |
| 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 Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 76 | schedule (const TimeDuration &reltime, Event event, uint32_t label); |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 77 | |
| 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 Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | static void |
| 87 | eventWrapper (Event event); |
| 88 | |
| 89 | private: |
| 90 | std::map< uint32_t, std::list< ns3::EventId > > m_labeledEvents; |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | } |
| 94 | |
| 95 | #endif // SYNC_SCHEDULER_H |