blob: 7f39f38c839bf043f23e83b16ac81861c5320541 [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#include "sync-scheduler.h"
24#include "sync-log.h"
25
26#include "ns3/simulator.h"
27
28using namespace boost;
29using namespace std;
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070030using namespace ns3;
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070031
32INIT_LOGGER ("Scheduler");
33
34namespace Sync {
35
36Scheduler::Scheduler ()
37{
38}
39
40Scheduler::~Scheduler ()
41{
42}
43
44void
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070045Scheduler::eventWrapper (Event event)
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070046{
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070047 event ();
48}
49
50void
51Scheduler::schedule (const TimeDuration &reltime, Event event, uint32_t label)
52{
53 list< EventId > &eventsForLabel = m_labeledEvents [label];
54 list< EventId >::iterator i = eventsForLabel.begin ();
55 while (i != eventsForLabel.end ())
56 {
57 if (i->IsExpired ())
58 {
59 list< EventId >::iterator next = i;
60 next ++;
61 eventsForLabel.erase (i);
62 i = next;
63 }
64 else
65 i ++;
66 }
67
68 ns3::EventId eventId = ns3::Simulator::Schedule (reltime, Scheduler::eventWrapper, event);
69 eventsForLabel.push_back (eventId);
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070070}
71
72void
73Scheduler::cancel (uint32_t label)
74{
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070075 list< EventId > &eventsForLabel = m_labeledEvents [label];
76
77 for (list< EventId >::iterator i = eventsForLabel.begin ();
78 i != eventsForLabel.end ();
79 i++)
80 {
81 i->Cancel ();
82 }
83
84 eventsForLabel.clear ();
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070085}
86
87
88} // Sync