diffrtg.cc

Go to the documentation of this file.
00001 
00002 /*
00003  * Copyright (C) 2004-2005 by the University of Southern California
00004  * $Id: diffrtg.cc,v 1.16 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 Routing Agent - a wrapper class for core diffusion agent, ported from SCADDS's directed diffusion software. --Padma, nov 2001.
00048 
00049 #ifdef NS_DIFFUSION
00050 
00051 #include "diffrtg.h"
00052 #include "address.h"
00053 #include "scheduler.h"
00054 #include "diffagent.h"
00055 
00056 
00057 static class DiffRoutingAgentClass : public TclClass {
00058 public:
00059     DiffRoutingAgentClass() : TclClass("Agent/DiffusionRouting") {}
00060     TclObject* create(int argc, const char*const* argv) {
00061         if (argc == 5)
00062             return(new DiffRoutingAgent(atoi(argv[4])));
00063         
00064         fprintf(stderr, "Insufficient number of args for creating DiffRtgAgent");
00065         return (NULL);
00066     }
00067 } class_diffusion_routing_agent;
00068 
00069 
00070 void LocalApp::sendPacket(DiffPacket msg, int len, int dst) {
00071   agent_->sendPacket(msg, len, dst); 
00072 }
00073 
00074 
00075 DiffPacket LocalApp::recvPacket(int fd) {
00076   DiffPacket p;
00077   
00078   fprintf(stderr, "This function should not get called; call DiffRoutingAgent::recv(Packet *, Handler *) instead\n\n");
00079   exit(1);
00080   return (p);  // to keep the compiler happy
00081 }
00082 
00083 
00084 void LinkLayerAbs::sendPacket(DiffPacket dp, int len, int dst) {
00085   Packet *p;
00086   hdr_cmn *ch;
00087   hdr_ip *iph;
00088   Message *msg;
00089   
00090   msg = (Message *)dp;
00091   p = agent_->createNsPkt(msg, len, dst); 
00092   iph = HDR_IP(p);
00093   ch = HDR_CMN(p);
00094   iph->saddr() = agent_->addr();
00095   iph->sport() = agent_->port();    //RT_PORT;
00096   iph->daddr() = msg->next_hop_;  // Use diffusion next_hop_
00097   iph->dport() = agent_->port();    //RT_PORT;
00098   ch->next_hop_ = msg->next_hop_;  // populate nexthop in cmn hdr
00099   agent_->send(p, 0);
00100 
00101 }
00102 
00103 
00104 DiffPacket LinkLayerAbs::recvPacket(int fd) {
00105   DiffPacket p;
00106   
00107   fprintf(stderr, "This function should not get called; call DiffRoutingAgent::recv(Packet *, Handler *) instead\n\n");
00108   exit(1);
00109   return (p);  // to keep the compiler happy
00110 }
00111 
00112 
00113 
00114 DiffRoutingAgent::DiffRoutingAgent(int nodeid) : Agent(PT_DIFF) {
00115     agent_ = new DiffusionCoreAgent(this, nodeid);
00116 
00117 }
00118 
00119 
00120 void DiffRoutingAgent::sendPacket(DiffPacket dp, int len, int dst) {
00121     Packet *p;
00122     hdr_ip *iph;
00123     Message *msg;
00124 
00125     msg = (Message *)dp;
00126     p = createNsPkt(msg, len, dst); 
00127     iph = HDR_IP(p);
00128     iph->dport() = dst;
00129     
00130     // schedule for a realistic delay : 0 sec for now
00131     (void)Scheduler::instance().schedule(port_dmux(), p, 0.000001);
00132     
00133 }
00134 
00135 void
00136 DiffRoutingAgent::initpkt(Packet* p, Message* msg, int len)
00137 {
00138     hdr_cmn* ch = HDR_CMN(p);
00139     hdr_ip* iph = HDR_IP(p);
00140     AppData *diffdata;
00141         
00142     diffdata  = new DiffusionData(msg, len);
00143     p->setdata(diffdata);
00144     
00145     // initialize pkt
00146     ch->uid() = msg->pkt_num_; /* copy pkt_num from diffusion msg */
00147     ch->ptype() = type_;
00148     ch->size() = size_;
00149     ch->timestamp() = Scheduler::instance().clock();
00150     ch->iface() = UNKN_IFACE.value(); // from packet.h (agent is local)
00151     ch->direction() = hdr_cmn::NONE;
00152     ch->error() = 0;    /* pkt not corrupt to start with */
00153 
00154     iph->saddr() = addr();
00155     iph->sport() = port(); // RT_PORT
00156     iph->daddr() = addr();
00157     iph->flowid() = fid_;
00158     iph->prio() = prio_;
00159     iph->ttl() = defttl_;
00160     
00161     hdr_flags* hf = hdr_flags::access(p);
00162     hf->ecn_capable_ = 0;
00163     hf->ecn_ = 0;
00164     hf->eln_ = 0;
00165     hf->ecn_to_echo_ = 0;
00166     hf->fs_ = 0;
00167     hf->no_ts_ = 0;
00168     hf->pri_ = 0;
00169     hf->cong_action_ = 0;
00170     
00171 }
00172 
00173 
00174 Packet* 
00175 DiffRoutingAgent::createNsPkt(Message *msg, int len, int dst) {
00176     Packet *p;
00177     
00178     p = Packet::alloc();
00179     initpkt(p, msg, len);
00180     return p;
00181 }
00182 
00183 void DiffRoutingAgent::recv(Packet *p, Handler *h) {
00184     Message *msg;
00185     DiffusionData *diffdata;
00186     
00187     diffdata = (DiffusionData *)(p->userdata());
00188     msg = diffdata->data();
00189     
00190     agent_->recvMessage(msg);
00191     
00192     //delete msg;
00193     Packet::free(p);
00194 }
00195 
00196 int DiffRoutingAgent::command(int argc, const char*const* argv) {
00197     if (argc == 2) {
00198         if (strcasecmp(argv[1], "start")==0) {
00199             //start();
00200             //eq = new DiffusionCoreEQ(this);
00201             //eq->eq_new();
00202             // Add timers to the eventQueue
00203             //eq->eq_addAfter(NEIGHBORS_TIMER, NULL, NEIGHBORS_DELAY);
00204             //eq->eq_addAfter(FILTER_TIMER, NULL, FILTER_DELAY);
00205             
00206             return TCL_OK;
00207         }
00208         
00209     }
00210     else if (argc == 3) {
00211         if (strcasecmp(argv[1], "addr") == 0) {
00212             addr_ = (Address::instance().str2addr(argv[2]));
00213             return TCL_OK;
00214         }
00215         if (strcasecmp(argv[1], "stop-time")==0) {
00216             // add stop-event which when fired dumps statistical data
00217             // at end of ns simulation
00218             TimerCallback *callback;
00219             callback = new DiffusionStopTimer(agent_);
00220             agent_->timers_manager_->addTimer(atoi(argv[2])*1000, callback);
00221             return TCL_OK;
00222         }
00223         TclObject *obj;
00224         if ((obj = TclObject::lookup (argv[2])) == 0) {
00225             fprintf(stderr, "_Diffusion Node_ %s lookup of %s failed\n", argv[1], argv[2]);
00226             return TCL_ERROR;
00227         }
00228         if (strcasecmp(argv[1], "port-dmux") == 0) {
00229             port_dmux_ = (PortClassifier *)obj;
00230             return TCL_OK;
00231         }
00232         if (strcasecmp(argv[1], "add-ll") == 0) {
00233             target_ = (LL*)obj;
00234             return TCL_OK;
00235         }
00236         if (strcasecmp(argv[1], "tracetarget") == 0) {
00237             tracetarget_ = (Trace *)obj;
00238             return TCL_OK;
00239         }
00240     }
00241     return Agent::command(argc, argv);
00242 }
00243 
00244 
00245 void DiffRoutingAgent::trace(char *fmt, ...) {
00246     va_list ap;
00247     if (!tracetarget_)
00248         return;
00249     
00250     va_start (ap, fmt);
00251     vsprintf (tracetarget_->pt_->buffer (), fmt, ap);
00252     tracetarget_->pt_->dump ();
00253     va_end (ap);
00254 }
00255     
00256 
00257 #endif // NS

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