difftimer.cc

Go to the documentation of this file.
00001 
00002 /*
00003  * Copyright (C) 2004-2005 by the University of Southern California
00004  * $Id: difftimer.cc,v 1.4 2005/09/13 20:47:34 johnh Exp $
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License,
00008  * version 2, as published by the Free Software Foundation.
00009  *
00010  * This program 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 along
00016  * with this program; if not, write to the Free Software Foundation, Inc.,
00017  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00018  *
00019  *
00020  * The copyright of this module includes the following
00021  * linking-with-specific-other-licenses addition:
00022  *
00023  * In addition, as a special exception, the copyright holders of
00024  * this module give you permission to combine (via static or
00025  * dynamic linking) this module with free software programs or
00026  * libraries that are released under the GNU LGPL and with code
00027  * included in the standard release of ns-2 under the Apache 2.0
00028  * license or under otherwise-compatible licenses with advertising
00029  * requirements (or modified versions of such code, with unchanged
00030  * license).  You may copy and distribute such a system following the
00031  * terms of the GNU GPL for this module and the licenses of the
00032  * other code concerned, provided that you include the source code of
00033  * that other code when and as the GNU GPL requires distribution of
00034  * source code.
00035  *
00036  * Note that people who make modified versions of this module
00037  * are not obligated to grant this special exception for their
00038  * modified versions; it is their choice whether to do so.  The GNU
00039  * General Public License gives permission to release a modified
00040  * version without this exception; this exception also makes it
00041  * possible to release a modified version which carries forward this
00042  * exception.
00043  *
00044  */
00045 
00046 //
00047 // Diffusion-event handler class, Padma, nov 2001. 
00048 
00049 #ifdef NS_DIFFUSION
00050 
00051 #include "difftimer.h"
00052 #include "diffagent.h"
00053 #include "diffrtg.h"
00054 
00055 DiffEvent::DiffEvent(d_handle hdl, void *payload, int time) {
00056     handle_ = hdl;
00057     payload_ = payload;
00058     GetTime(&tv_);
00059     TimevalAddusecs(&tv_, time*1000);
00060 }
00061 
00062 void DiffEventQueue::eqAddAfter(d_handle hdl, void *payload, int delay_msec) {
00063     DiffEvent* de;
00064     
00065     de = new DiffEvent(hdl, payload, delay_msec);
00066     DiffEventHandler *dh = timerHandler_;
00067     double delay = ((double)delay_msec)/1000;   //convert msec to sec
00068     
00069     (void)Scheduler::instance().schedule(dh, de, delay);
00070     scheduler_uid_t uid = ((Event *)de)->uid_;
00071     
00072     setUID(hdl, uid);
00073 }
00074 
00075 
00076 DiffEvent *DiffEventQueue::eqFindEvent(d_handle hdl) {
00077     scheduler_uid_t uid = getUID(hdl);
00078     if (uid != -1) {
00079         Event *p = Scheduler::instance().lookup(uid);
00080         if (p != 0) {
00081             return ((DiffEvent *)p);
00082         } else {
00083             fprintf(stderr, "Error: Can't find event in scheduler queue\n");
00084             return NULL;
00085         }
00086     } else {
00087         fprintf(stderr, "Error: Can't find event in uidmap!\n");
00088         return NULL;
00089     }
00090 }
00091 
00092 bool DiffEventQueue::eqRemove(d_handle hdl) {
00093     // first lookup UID
00094     scheduler_uid_t uid = getUID(hdl);
00095     if (uid != -1) {
00096         // next look for event in scheduler queue
00097         Event *p = Scheduler::instance().lookup(uid);
00098         if (p != 0) {
00099             // remove event from scheduler
00100             Scheduler::instance().cancel(p);
00101             TimerEntry* te = (TimerEntry *)((DiffEvent *)p)->payload();
00102             delete te;
00103             delete p;
00104             return 1;
00105         } else {
00106             fprintf(stderr, "Error: Can't find event in scheduler queue\n");
00107             return 0;
00108         }
00109     } else {
00110         fprintf(stderr, "Error: Can't find event in uidmap!\n");
00111         return 0;
00112     }
00113 }
00114 
00115 // sets value of UID and matching handle into uidmap
00116 void DiffEventQueue::setUID(d_handle hdl, scheduler_uid_t uid) {
00117     MapEntry *me = new MapEntry;
00118     me->hdl_ = hdl;
00119     me->uid_ = uid;
00120     uidmap_.push_back(me);
00121 }
00122 
00123 // finds UID for matching handle; removes entry from uidmap and returns uid
00124 scheduler_uid_t DiffEventQueue::getUID(d_handle hdl) {
00125     UIDMap_t::iterator itr;
00126     MapEntry* entry;
00127     for (itr = uidmap_.begin(); itr != uidmap_.end(); ++itr) {
00128         entry = *itr;
00129         if (entry->hdl_ == hdl) { // found handle
00130             // don't need this entry, take it out of list
00131             scheduler_uid_t uid = entry->uid_;
00132             itr = uidmap_.erase(itr);
00133             delete entry;
00134             return uid;
00135         }
00136     }
00137     return (-1);
00138 }
00139 
00140 // event handler that gets called at expiry time of event
00141 void DiffEventHandler::handle(Event *e) {
00142     DiffEvent *de = (DiffEvent *)e;
00143     
00144     d_handle hdl = de->getHandle();
00145     queue_->getUID(hdl); // only removes entry from uidmap
00146     a_->diffTimeout(de);
00147 }
00148 
00149 
00150 #endif // NS

Generated on Tue Mar 6 16:47:44 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6