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 | #include "sync-scheduler.h" |
| 24 | #include "sync-log.h" |
| 25 | |
| 26 | #include "ns3/simulator.h" |
| 27 | |
| 28 | using namespace boost; |
| 29 | using namespace std; |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame^] | 30 | using namespace ns3; |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 31 | |
| 32 | INIT_LOGGER ("Scheduler"); |
| 33 | |
| 34 | namespace Sync { |
| 35 | |
| 36 | Scheduler::Scheduler () |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | Scheduler::~Scheduler () |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | void |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame^] | 45 | Scheduler::eventWrapper (Event event) |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 46 | { |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame^] | 47 | event (); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | Scheduler::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 Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void |
| 73 | Scheduler::cancel (uint32_t label) |
| 74 | { |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame^] | 75 | 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 Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | } // Sync |