tcp-simple.cc

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Xerox Corporation 1998. All rights reserved.
00003  *
00004  * This program is free software; you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License as published by the
00006  * Free Software Foundation; either version 2 of the License, or (at your
00007  * option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License along
00015  * with this program; if not, write to the Free Software Foundation, Inc.,
00016  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  * Linking this file statically or dynamically with other modules is making
00019  * a combined work based on this file.  Thus, the terms and conditions of
00020  * the GNU General Public License cover the whole combination.
00021  *
00022  * In addition, as a special exception, the copyright holders of this file
00023  * give you permission to combine this file with free software programs or
00024  * libraries that are released under the GNU LGPL and with code included in
00025  * the standard release of ns-2 under the Apache 2.0 license or under
00026  * otherwise-compatible licenses with advertising requirements (or modified
00027  * versions of such code, with unchanged license).  You may copy and
00028  * distribute such a system following the terms of the GNU GPL for this
00029  * file and the licenses of the other code concerned, provided that you
00030  * include the source code of that other code when and as the GNU GPL
00031  * requires distribution of source code.
00032  *
00033  * Note that people who make modified versions of this file are not
00034  * obligated to grant this special exception for their modified versions;
00035  * it is their choice whether to do so.  The GNU General Public License
00036  * gives permission to release a modified version without this exception;
00037  * this exception also makes it possible to release a modified version
00038  * which carries forward this exception.
00039  *
00040  * $Header: /nfs/jade/vint/CVSROOT/ns-2/webcache/tcp-simple.cc,v 1.10 2005/08/26 05:05:31 tomh Exp $
00041  *
00042  */
00043 //
00044 // SimpleTcp: Only share the same interface as FullTcp.
00045 // It's inherited from FullTcp solely for interface reason... :(
00046 //
00047 // If we have interface declaration independent from class type definition,
00048 // we'll be better off.
00049 //
00050 
00051 #include <stdlib.h>
00052 #include "tclcl.h"
00053 #include "packet.h"
00054 #include "ip.h"
00055 #include "app.h"
00056 #include "tcp-simple.h"
00057 
00058 static class SimpleTcpClass : public TclClass {
00059 public:
00060     SimpleTcpClass() : TclClass("Agent/TCP/SimpleTcp") {}
00061     TclObject* create(int, const char*const*) {
00062         return (new SimpleTcpAgent());
00063     }
00064 } class_simple_tcp_agent;
00065 
00066 SimpleTcpAgent::SimpleTcpAgent() : TcpAgent(), seqno_(0)
00067 {
00068 }
00069 
00070 // XXX Do *NOT* support infinite send of TCP (bytes == -1).
00071 void SimpleTcpAgent::sendmsg(int bytes, const char* /*flags*/)
00072 {
00073     if (bytes == -1) {
00074         fprintf(stderr, 
00075 "SimpleTcp doesn't support infinite send. Do not use FTP::start(), etc.\n");
00076         return;
00077     }
00078     // Simply sending out bytes out to target_
00079     curseq_ += bytes;
00080     seqno_ ++;
00081 
00082     Packet *p = allocpkt();
00083     hdr_tcp *tcph = HDR_TCP(p);
00084     tcph->seqno() = seqno_;
00085     tcph->ts() = Scheduler::instance().clock();
00086     tcph->ts_echo() = ts_peer_;
00087         hdr_cmn *th = HDR_CMN(p);
00088     th->size() = bytes + tcpip_base_hdr_size_;
00089     send(p, 0);
00090 }
00091 
00092 void SimpleTcpAgent::recv(Packet *pkt, Handler *)
00093 {
00094         hdr_cmn *th = HDR_CMN(pkt);
00095     int datalen = th->size() - tcpip_base_hdr_size_;
00096     if (app_)
00097         app_->recv(datalen);
00098     // No lastbyte_ callback, because no packet fragmentation.
00099     Packet::free(pkt);
00100 }
00101 
00102 int SimpleTcpAgent::command(int argc, const char*const* argv)
00103 {
00104     // Copy FullTcp's tcl interface
00105 
00106     if (argc == 2) {
00107         if (strcmp(argv[1], "listen") == 0) {
00108             // Do nothing
00109             return (TCL_OK);
00110         }
00111         if (strcmp(argv[1], "close") == 0) {
00112             // Call done{} to match tcp-full's syntax
00113             Tcl::instance().evalf("%s done", name());
00114             return (TCL_OK);
00115         }
00116     }
00117     return (TcpAgent::command(argc, argv));
00118 }

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