blob: 025eb3e9c57d528cbd41c1b4a3d51ba0dec2f8af [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{
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -070053 _LOG_DEBUG ("Schedule event for " << (Simulator::Now () +reltime).ToDouble (Time::S) << "s for label " << label);
Alexander Afanasyev89571f12012-04-23 13:44:57 -070054
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070055 list< EventId > &eventsForLabel = m_labeledEvents [label];
56 list< EventId >::iterator i = eventsForLabel.begin ();
57 while (i != eventsForLabel.end ())
58 {
59 if (i->IsExpired ())
60 {
61 list< EventId >::iterator next = i;
62 next ++;
63 eventsForLabel.erase (i);
64 i = next;
65 }
66 else
67 i ++;
68 }
69
70 ns3::EventId eventId = ns3::Simulator::Schedule (reltime, Scheduler::eventWrapper, event);
71 eventsForLabel.push_back (eventId);
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070072}
73
74void
75Scheduler::cancel (uint32_t label)
76{
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070077 list< EventId > &eventsForLabel = m_labeledEvents [label];
Alexander Afanasyev0ac399e2012-04-27 13:28:28 -070078 _LOG_DEBUG ("Canceling events for label " << label << " (" << eventsForLabel.size () << " events)");
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070079
80 for (list< EventId >::iterator i = eventsForLabel.begin ();
81 i != eventsForLabel.end ();
82 i++)
83 {
84 i->Cancel ();
85 }
86
87 eventsForLabel.clear ();
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070088}
89
90
91} // Sync