udp.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) Xerox Corporation 1997. All rights reserved.
00004  *  
00005  * This program is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU General Public License as published by the
00007  * Free Software Foundation; either version 2 of the License, or (at your
00008  * option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * 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  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  * Linking this file statically or dynamically with other modules is making
00020  * a combined work based on this file.  Thus, the terms and conditions of
00021  * the GNU General Public License cover the whole combination.
00022  *
00023  * In addition, as a special exception, the copyright holders of this file
00024  * give you permission to combine this file with free software programs or
00025  * libraries that are released under the GNU LGPL and with code included in
00026  * the standard release of ns-2 under the Apache 2.0 license or under
00027  * otherwise-compatible licenses with advertising requirements (or modified
00028  * versions of such code, with unchanged license).  You may copy and
00029  * distribute such a system following the terms of the GNU GPL for this
00030  * file and the licenses of the other code concerned, provided that you
00031  * include the source code of that other code when and as the GNU GPL
00032  * requires distribution of source code.
00033  *
00034  * Note that people who make modified versions of this file are not
00035  * obligated to grant this special exception for their modified versions;
00036  * it is their choice whether to do so.  The GNU General Public License
00037  * gives permission to release a modified version without this exception;
00038  * this exception also makes it possible to release a modified version
00039  * which carries forward this exception.
00040  */
00041 
00042 #ifndef lint
00043 static const char rcsid[] =
00044     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/apps/udp.cc,v 1.21 2005/08/26 05:05:28 tomh Exp $ (Xerox)";
00045 #endif
00046 
00047 #include "udp.h"
00048 #include "rtp.h"
00049 #include "random.h"
00050 #include "address.h"
00051 #include "ip.h"
00052 
00053 
00054 static class UdpAgentClass : public TclClass {
00055 public:
00056     UdpAgentClass() : TclClass("Agent/UDP") {}
00057     TclObject* create(int, const char*const*) {
00058         return (new UdpAgent());
00059     }
00060 } class_udp_agent;
00061 
00062 UdpAgent::UdpAgent() : Agent(PT_UDP), seqno_(-1)
00063 {
00064     bind("packetSize_", &size_);
00065 }
00066 
00067 UdpAgent::UdpAgent(packet_t type) : Agent(type)
00068 {
00069     bind("packetSize_", &size_);
00070 }
00071 
00072 // put in timestamp and sequence number, even though UDP doesn't usually 
00073 // have one.
00074 void UdpAgent::sendmsg(int nbytes, AppData* data, const char* flags)
00075 {
00076     Packet *p;
00077     int n;
00078 
00079     assert (size_ > 0);
00080 
00081     n = nbytes / size_;
00082 
00083     if (nbytes == -1) {
00084         printf("Error:  sendmsg() for UDP should not be -1\n");
00085         return;
00086     }   
00087 
00088     // If they are sending data, then it must fit within a single packet.
00089     if (data && nbytes > size_) {
00090         printf("Error: data greater than maximum UDP packet size\n");
00091         return;
00092     }
00093 
00094     double local_time = Scheduler::instance().clock();
00095     while (n-- > 0) {
00096         p = allocpkt();
00097         hdr_cmn::access(p)->size() = size_;
00098         hdr_rtp* rh = hdr_rtp::access(p);
00099         rh->flags() = 0;
00100         rh->seqno() = ++seqno_;
00101         hdr_cmn::access(p)->timestamp() = 
00102             (u_int32_t)(SAMPLERATE*local_time);
00103         // add "beginning of talkspurt" labels (tcl/ex/test-rcvr.tcl)
00104         if (flags && (0 ==strcmp(flags, "NEW_BURST")))
00105             rh->flags() |= RTP_M;
00106         p->setdata(data);
00107         target_->recv(p);
00108     }
00109     n = nbytes % size_;
00110     if (n > 0) {
00111         p = allocpkt();
00112         hdr_cmn::access(p)->size() = n;
00113         hdr_rtp* rh = hdr_rtp::access(p);
00114         rh->flags() = 0;
00115         rh->seqno() = ++seqno_;
00116         hdr_cmn::access(p)->timestamp() = 
00117             (u_int32_t)(SAMPLERATE*local_time);
00118         // add "beginning of talkspurt" labels (tcl/ex/test-rcvr.tcl)
00119         if (flags && (0 == strcmp(flags, "NEW_BURST")))
00120             rh->flags() |= RTP_M;
00121         p->setdata(data);
00122         target_->recv(p);
00123     }
00124     idle();
00125 }
00126 void UdpAgent::recv(Packet* pkt, Handler*)
00127 {
00128     if (app_ ) {
00129         // If an application is attached, pass the data to the app
00130         hdr_cmn* h = hdr_cmn::access(pkt);
00131         app_->process_data(h->size(), pkt->userdata());
00132     } else if (pkt->userdata() && pkt->userdata()->type() == PACKET_DATA) {
00133         // otherwise if it's just PacketData, pass it to Tcl
00134         //
00135         // Note that a Tcl procedure Agent/Udp recv {from data}
00136         // needs to be defined.  For example,
00137         //
00138         // Agent/Udp instproc recv {from data} {puts data}
00139 
00140         PacketData* data = (PacketData*)pkt->userdata();
00141 
00142         hdr_ip* iph = hdr_ip::access(pkt);
00143                 Tcl& tcl = Tcl::instance();
00144         tcl.evalf("%s process_data %d {%s}", name(),
00145                   iph->src_.addr_ >> Address::instance().NodeShift_[1],
00146               data->data());
00147     }
00148     Packet::free(pkt);
00149 }
00150 
00151 
00152 int UdpAgent::command(int argc, const char*const* argv)
00153 {
00154     if (argc == 4) {
00155         if (strcmp(argv[1], "send") == 0) {
00156             PacketData* data = new PacketData(1 + strlen(argv[3]));
00157             strcpy((char*)data->data(), argv[3]);
00158             sendmsg(atoi(argv[2]), data);
00159             return (TCL_OK);
00160         }
00161     } else if (argc == 5) {
00162         if (strcmp(argv[1], "sendmsg") == 0) {
00163             PacketData* data = new PacketData(1 + strlen(argv[3]));
00164             strcpy((char*)data->data(), argv[3]);
00165             sendmsg(atoi(argv[2]), data, argv[4]);
00166             return (TCL_OK);
00167         }
00168     }
00169     return (Agent::command(argc, argv));
00170 }

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