Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

TimerEventProxy.ct

Go to the documentation of this file.
00001 // $Id: TimerEventProxy.ct 1786 2011-05-09 15:46:52Z mtk $
00002 //
00003 // Copyright (C) 2010
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Mathias Kretschmer <mtk@berlios.de>
00007 //     Jens Moedeker <jens.moedeker@fit.fraunhofer.de>
00008 //
00009 // This program is free software; you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation; either version 2 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the
00021 // Free Software Foundation, Inc.,
00022 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00027 // Custom includes
00028 #include <senf/Utils/membind.hh>
00029 
00030 #define prefix_
00031 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00032 
00033 template<typename IdType>
00034 prefix_ senf::scheduler::TimerEventProxy<IdType>::TimerEventProxy(std::string const & description)
00035     : entrySetById( entrySet.template get<Id>()),
00036       entrySetByTimeout( entrySet.template get<Timeout> ()),
00037       timer( "TimerEventProxy " + description,
00038               membind(&TimerEventProxy<IdType>::timerEvent, this), 0, false)
00039 { }
00040 
00041 template<typename IdType>
00042 prefix_ void senf::scheduler::TimerEventProxy<IdType>::timerEvent()
00043 {
00044     ClockService::clock_type now = senf::scheduler::now();
00045     typename EntrySetByTimeout_t::iterator it = entrySetByTimeout.begin();
00046     while (it != entrySetByTimeout.end() && it->timeout <= now) {
00047         Entry item (*it);
00048         // remove due entry from set
00049         entrySetByTimeout.erase(it);
00050         // call callback
00051         item.cb(now, item.id);
00052         it = entrySetByTimeout.begin();
00053     }
00054     if (entrySet.size() > 0)
00055         timer.timeout(entrySetByTimeout.begin()->timeout);
00056 }
00057 
00058 template<typename IdType>
00059 prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
00060         ClockService::clock_type timeout, IdType const & id, Callback cb)
00061 {
00062     // insert new entry or replace the timeout of an entry already indexed
00063     typename EntrySetById_t::iterator i = entrySetById.find(id);
00064     if(i == entrySetById.end())
00065         entrySetByTimeout.insert( Entry(timeout, id, cb));
00066         else{
00067                 Entry tmp = *i;
00068                 tmp.timeout = timeout;
00069                 entrySetById.replace(i,tmp);
00070         }
00071     // the scheduler time to the first earliest timeout (ordered index)
00072     timer.timeout( entrySetByTimeout.begin()->timeout);
00073 }
00074 
00075 template<typename IdType>
00076 prefix_ bool senf::scheduler::TimerEventProxy<IdType>::remove(IdType const & id)
00077 {
00078     bool removed (entrySetById.erase( id) > 0);
00079     if (entrySet.size() > 0)
00080         timer.timeout(entrySetByTimeout.begin()->timeout);
00081     else
00082         timer.disable();
00083     return removed;
00084 }
00085 
00086 template<typename IdType>
00087 prefix_ senf::ClockService::clock_type senf::scheduler::TimerEventProxy<IdType>::timeout(IdType const & id)
00088     const
00089 {
00090     typename EntrySetById_t::const_iterator i ( entrySetById.find( id));
00091     return i == entrySetById.end() ? 0 : i->timeout;
00092 }
00093 
00094 
00095 template<typename IdType>
00096 prefix_ std::vector<std::pair<senf::ClockService::clock_type, IdType> > senf::scheduler::TimerEventProxy<IdType>::list()
00097     const
00098 {
00099     std::vector<std::pair<ClockService::clock_type, IdType> > tmp;
00100 
00101     typename EntrySetByTimeout_t::const_iterator it;
00102     for (it = entrySetByTimeout.begin(); it != entrySetByTimeout.end(); ++it) {
00103         tmp.push_back(std::make_pair<ClockService::clock_type, IdType>( it->timeout, it->id));
00104     }
00105     return tmp;
00106 }
00107 
00108 template<typename IdType>
00109 prefix_ unsigned senf::scheduler::TimerEventProxy<IdType>::numEvents()
00110   const
00111 {
00112     return entrySetByTimeout.size();
00113 }
00114 
00115 template<typename IdType>
00116 prefix_ void senf::scheduler::TimerEventProxy<IdType>::clear()
00117 {
00118     entrySetByTimeout.clear();
00119 }
00120 
00121 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00122 #undef prefix_
00123 
00124 
00125 // Local Variables:
00126 // mode: c++
00127 // fill-column: 100
00128 // c-file-style: "senf"
00129 // indent-tabs-mode: nil
00130 // ispell-local-dictionary: "american"
00131 // compile-command: "scons -u test"
00132 // comment-column: 40
00133 // End:
00134 
00135 

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research