delay.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1996-1997 The Regents of the University of California.
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the Network Research
00017  *  Group at Lawrence Berkeley National Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 
00035 #ifndef lint
00036 static const char rcsid[] =
00037     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/link/delay.cc,v 1.28 2005/07/13 03:51:25 tomh Exp $ (LBL)";
00038 #endif
00039 
00040 #include "delay.h"
00041 #include "mcast_ctrl.h"
00042 #include "ctrMcast.h"
00043 
00044 static class LinkDelayClass : public TclClass {
00045 public:
00046     LinkDelayClass() : TclClass("DelayLink") {}
00047     TclObject* create(int /* argc */, const char*const* /* argv */) {
00048         return (new LinkDelay);
00049     }
00050 } class_delay_link;
00051 
00052 LinkDelay::LinkDelay() 
00053     : dynamic_(0), 
00054       latest_time_(0),
00055       itq_(0)
00056 {
00057     bind_bw("bandwidth_", &bandwidth_);
00058     bind_time("delay_", &delay_);
00059     bind_bool("avoidReordering_", &avoidReordering_);
00060 }
00061 
00062 int LinkDelay::command(int argc, const char*const* argv)
00063 {
00064     if (argc == 2) {
00065         if (strcmp(argv[1], "isDynamic") == 0) {
00066             dynamic_ = 1;
00067             itq_ = new PacketQueue();
00068             return TCL_OK;
00069         }
00070     } else if (argc == 6) {
00071         if (strcmp(argv[1], "pktintran") == 0) {
00072             int src = atoi(argv[2]);
00073             int grp = atoi(argv[3]);
00074             int from = atoi(argv[4]);
00075             int to = atoi(argv[5]);
00076             pktintran (src, grp);
00077             Tcl::instance().evalf("%s puttrace %d %d %d %d %d %d %d %d", name(), total_[0], total_[1], total_[2], total_[3], src, grp, from, to);
00078             return TCL_OK;
00079         }
00080     }
00081 
00082     return Connector::command(argc, argv);
00083 }
00084 
00085 void LinkDelay::recv(Packet* p, Handler* h)
00086 {
00087     double txt = txtime(p);
00088     Scheduler& s = Scheduler::instance();
00089     if (dynamic_) {
00090         Event* e = (Event*)p;
00091         e->time_= txt + delay_;
00092         itq_->enque(p); // for convinience, use a queue to store packets in transit
00093         s.schedule(this, p, txt + delay_);
00094     } else if (avoidReordering_) {
00095         // code from Andrei Gurtov, to prevent reordering on
00096         //   bandwidth or delay changes
00097         double now_ = Scheduler::instance().clock();
00098         if (txt + delay_ < latest_time_ - now_ && latest_time_ > 0) {
00099             latest_time_+=txt;
00100             s.schedule(target_, p, latest_time_ - now_ );
00101         } else {
00102             latest_time_ = now_ + txt + delay_;
00103             s.schedule(target_, p, txt + delay_);
00104         }
00105 
00106     } else {
00107         s.schedule(target_, p, txt + delay_);
00108     }
00109     s.schedule(h, &intr_, txt);
00110 }
00111 
00112 void LinkDelay::send(Packet* p, Handler*)
00113 {
00114     target_->recv(p, (Handler*) NULL);
00115 }
00116 
00117 void LinkDelay::reset()
00118 {
00119     Scheduler& s= Scheduler::instance();
00120 
00121     if (itq_ && itq_->length()) {
00122         Packet *np;
00123         // walk through packets currently in transit and kill 'em
00124         while ((np = itq_->deque()) != 0) {
00125             s.cancel(np);
00126             drop(np);
00127         }
00128     }
00129 }
00130 
00131 void LinkDelay::handle(Event* e)
00132 {
00133     Packet *p = itq_->deque();
00134     assert(p->time_ == e->time_);
00135     send(p, (Handler*) NULL);
00136 }
00137 
00138 void LinkDelay::pktintran(int src, int group)
00139 {
00140     int reg = 1;
00141     int prune = 30;
00142     int graft = 31;
00143     int data = 0;
00144     for (int i=0; i<4; i++) {
00145         total_[i] = 0;
00146     }
00147 
00148     if (! dynamic_)
00149         return;
00150 
00151     int len = itq_->length();
00152     while (len) {
00153         len--;
00154         Packet* p = itq_->lookup(len);
00155         hdr_ip* iph = hdr_ip::access(p);
00156         if (iph->flowid() == prune) {
00157             if (iph->saddr() == src && iph->daddr() == group) {
00158                 total_[0]++;
00159             }
00160         } else if (iph->flowid() == graft) {
00161             if (iph->saddr() == src && iph->daddr() == group) {
00162                 total_[1]++;
00163             }
00164         } else if (iph->flowid() == reg) {
00165             hdr_CtrMcast* ch = hdr_CtrMcast::access(p);
00166             if (ch->src() == src+1 && ch->group() == group) {
00167                 total_[2]++;
00168             }
00169         } else if (iph->flowid() == data) {
00170             if (iph->saddr() == src+1 && iph->daddr() == group) {
00171                 total_[3]++;
00172             }
00173         }
00174     }
00175         //printf ("%f %d %d %d %d\n", Scheduler::instance().clock(), total_[0], total_[1], total_[2],total_[3]);
00176 }

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