diffagent.cc

Go to the documentation of this file.
00001 
00002 /*
00003  * Copyright (C) 2004-2005 by the University of Southern California
00004  * $Id: diffagent.cc,v 1.13 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 // DiffAppAgent - Wrapper Class for diffusion transport agent DR, ported from SCADDS's directed diffusion software. --Padma, nov 2001.  
00047 
00048 #ifdef NS_DIFFUSION
00049 
00050 #include "diffagent.h"
00051 #include "diffrtg.h"
00052 
00053 
00054 static class DiffAppAgentClass : public TclClass {
00055 public:
00056   DiffAppAgentClass() : TclClass("Agent/DiffusionApp") {}
00057   TclObject* create(int , const char*const* ) {
00058     return(new DiffAppAgent());
00059   }
00060 } class_diffusion_app_agent;
00061 
00062 
00063 void NsLocal::sendPacket(DiffPacket p, int len, int dst) {
00064   agent_->sendPacket(p, len, dst);
00065 }
00066 
00067 DiffPacket NsLocal::recvPacket(int fd) {
00068   DiffPacket p;
00069   fprintf(stderr, "This function should not get called; call DiffAppAgent::recv(Packet *, Handler *) instead\n\n");
00070   exit(1);
00071   return (p);  // to keep the compiler happy
00072 }
00073 
00074 
00075 DiffAppAgent::DiffAppAgent() : Agent(PT_DIFF) {
00076   dr_ = NR::create_ns_NR(DEFAULT_DIFFUSION_PORT, this);
00077 }
00078 
00079 
00080 int DiffAppAgent::command(int argc, const char*const* argv) {
00081   //Tcl& tcl = Tcl::instance();
00082   
00083   if (argc == 3) {
00084       if (strcmp(argv[1], "node") == 0) {
00085           MobileNode *node = (MobileNode *)TclObject::lookup(argv[2]);
00086           ((DiffusionRouting *)dr_)->getNode(node);
00087           return TCL_OK;
00088       } 
00089       if (strcmp(argv[1], "agent-id") == 0) {
00090           int id = atoi(argv[2]);
00091           ((DiffusionRouting *)dr_)->getAgentId(id);
00092           return TCL_OK;
00093       }
00094   }
00095   return Agent::command(argc, argv);
00096 }
00097 
00098 
00099 
00100 void DiffAppAgent::recv(Packet* p, Handler* h) 
00101 {
00102     Message *msg;
00103     DiffusionData *diffdata;
00104     
00105     diffdata = (DiffusionData *)(p->userdata());
00106     msg = diffdata->data();
00107     
00108     DiffusionRouting *dr = (DiffusionRouting*)dr_;
00109     dr->recvMessage(msg);
00110     
00111     Packet::free(p);
00112     
00113 }
00114 
00115 void
00116 DiffAppAgent::initpkt(Packet* p, Message* msg, int len)
00117 {
00118     hdr_cmn* ch = HDR_CMN(p);
00119     hdr_ip* iph = HDR_IP(p);
00120     AppData *diffdata;
00121         
00122     diffdata  = new DiffusionData(msg, len);
00123     p->setdata(diffdata);
00124     
00125     // initialize pkt
00126     ch->uid() = msg->pkt_num_; /* copy pkt_num from diffusion msg */
00127     ch->ptype() = type_;
00128     ch->size() = size_;
00129     ch->timestamp() = Scheduler::instance().clock();
00130     ch->iface() = UNKN_IFACE.value(); // from packet.h (agent is local)
00131     ch->direction() = hdr_cmn::NONE;
00132     ch->error() = 0;    /* pkt not corrupt to start with */
00133 
00134     iph->saddr() = addr();
00135     iph->sport() = ((DiffusionRouting*)dr_)->getAgentId();
00136     iph->daddr() = addr();
00137     iph->flowid() = fid_;
00138     iph->prio() = prio_;
00139     iph->ttl() = defttl_;
00140     
00141     hdr_flags* hf = hdr_flags::access(p);
00142     hf->ecn_capable_ = 0;
00143     hf->ecn_ = 0;
00144     hf->eln_ = 0;
00145     hf->ecn_to_echo_ = 0;
00146     hf->fs_ = 0;
00147     hf->no_ts_ = 0;
00148     hf->pri_ = 0;
00149     hf->cong_action_ = 0;
00150     
00151 }
00152 
00153 Packet* 
00154 DiffAppAgent::createNsPkt(Message *msg, int len) 
00155 {
00156     Packet *p;
00157   
00158     p =  Packet::alloc();
00159     initpkt(p, msg, len);
00160     return (p);
00161 }
00162 
00163 
00164 void DiffAppAgent::sendPacket(DiffPacket dp, int len, int dst) {
00165   Packet *p;
00166   hdr_ip *iph;
00167   Message *msg;
00168 
00169   msg = (Message *)dp;
00170   p = createNsPkt(msg, len); 
00171   iph = HDR_IP(p);
00172   iph->dport() = dst;
00173 
00174   // schedule for realistic delay : set to 0 sec for now
00175   (void)Scheduler::instance().schedule(target_, p, 0.000001);
00176 
00177 }
00178 
00179 
00180 #endif // NS
00181 
00182 
00183 

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