Clock.cc

00001 // Copyright 2008 Michael Marsh, University of Maryland.
00002 //
00003 // This file is part of pydtn.
00004 //
00005 // pydtn is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // pydtn is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with pydtn.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // The views and conclusions contained in the software and documentation
00019 // are those of the authors and should not be interpreted as representing
00020 // official policies, either expressed or implied, of the University
00021 // of Maryland.
00022 //
00023 // pydtn extends and embeds the Python interpreter, which is
00024 // Copyright 2001-2006 Python Software Foundation, All Rights Reserved,
00025 // and is released under the PSF License Agreement.
00026 //
00027 // RANLUX random number generation uses the Boost library,
00028 // Copyright 1994-2006 by various authors (details in individual files),
00029 // which is released under the Boost Software License, Version 1.0.
00030 
00031 #include "Clock.h"
00032 #include "Event.h"
00033 
00034 #include <iostream>
00035 
00036 Time Clock::m_time;
00037 Clock::EventList Clock::m_events;
00038 Clock::EventList Clock::m_tentative;
00039 bool Clock::m_running = true;
00040 
00041 bool
00042 EventCmp::operator()( const Event* a, const Event* b ) const
00043 {
00044    if ( 0 == a )
00045    {
00046       return false;
00047    }
00048    if ( 0 == b )
00049    {
00050       return false;
00051    }
00052    return a->time() < b->time();
00053 }
00054 
00055 void
00056 Clock::schedule( Event* e )
00057 {
00058    if ( 0 == e )
00059    {
00060       std::cerr << "Attempted to schedule a NULL Event" << std::endl;
00061       return;
00062    }
00063    if ( e->time() < m_time )
00064    {
00065       std::cerr << "Cannot schedule an event for the past!\n"
00066                 << "    current time: " << m_time << "\n"
00067                 << "   schedule time: " << e->time() << std::endl;
00068       delete e;
00069       return;
00070    }
00071    m_events.insert(e);
00072 }
00073 
00074 void
00075 Clock::tentative( Event* e )
00076 {
00077    if ( 0 == e )
00078    {
00079       std::cerr << "Attempted to schedule a NULL Event" << std::endl;
00080       return;
00081    }
00082    if ( e->time() < m_time )
00083    {
00084       std::cerr << "Cannot schedule an event for the past!\n"
00085                 << "    current time: " << m_time << "\n"
00086                 << "   schedule time: " << e->time() << std::endl;
00087       delete e;
00088       return;
00089    }
00090    m_tentative.insert(e);
00091 }
00092 
00093 void
00094 Clock::tick()
00095 {
00096    EventList::iterator eli = m_events.begin();
00097    EventList::iterator teli = m_tentative.begin();
00098 
00099    if ( 0 == m_events.size() )
00100    {
00101       // run the earliest tentative event
00102       if ( 0 != m_tentative.size() )
00103       {
00104          Event* te = *teli;
00105          m_tentative.erase(teli);
00106          m_time = te->time();
00107          te->handle();
00108          delete te;
00109       }
00110       m_running = false;
00111       return;
00112    }
00113    if ( 0 == m_tentative.size() )
00114    {
00115          Event* e = *eli;
00116          m_events.erase(eli);
00117          m_time = e->time();
00118          e->handle();
00119          delete e;
00120          return;
00121    }
00122    Event* te = *teli;
00123    Event* e = *eli;
00124 
00125    // This biases towards running e
00126    if ( te->time() < e->time() )
00127    {
00128       m_tentative.erase(teli);
00129       m_time = te->time();
00130       te->handle();
00131       delete te;
00132    }
00133    else
00134    {
00135       m_events.erase(eli);
00136       m_time = e->time();
00137       e->handle();
00138       delete e;
00139    }
00140 }

Generated on Mon Mar 24 11:15:46 2008 for Pydtn Simulator by  doxygen 1.5.4