tpm.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 //
00003 
00004 /*
00005  * tpm.cc
00006  * Copyright (C) 2001 by the University of Southern California
00007  * $Id: tpm.cc,v 1.2 2005/08/25 18:58:02 johnh Exp $
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU General Public License,
00011  * version 2, as published by the Free Software Foundation.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License along
00019  * with this program; if not, write to the Free Software Foundation, Inc.,
00020  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00021  *
00022  *
00023  * The copyright of this module includes the following
00024  * linking-with-specific-other-licenses addition:
00025  *
00026  * In addition, as a special exception, the copyright holders of
00027  * this module give you permission to combine (via static or
00028  * dynamic linking) this module with free software programs or
00029  * libraries that are released under the GNU LGPL and with code
00030  * included in the standard release of ns-2 under the Apache 2.0
00031  * license or under otherwise-compatible licenses with advertising
00032  * requirements (or modified versions of such code, with unchanged
00033  * license).  You may copy and distribute such a system following the
00034  * terms of the GNU GPL for this module and the licenses of the
00035  * other code concerned, provided that you include the source code of
00036  * that other code when and as the GNU GPL requires distribution of
00037  * source code.
00038  *
00039  * Note that people who make modified versions of this module
00040  * are not obligated to grant this special exception for their
00041  * modified versions; it is their choice whether to do so.  The GNU
00042  * General Public License gives permission to release a modified
00043  * version without this exception; this exception also makes it
00044  * possible to release a modified version which carries forward this
00045  * exception.
00046  *
00047  */
00048 //
00049 // Other copyrights might apply to parts of this software and are so
00050 // noted when applicable.
00051 //
00052 // Management function to playback TCPDump trace file (after post precssing)
00053 // Xuan Chen (xuanc@isi.edu)
00054 //
00055 #include "tpm.h"
00056 
00057 // Timer to send requests
00058 TPMTimer::TPMTimer(TPM* t) {
00059     tpm = t;
00060 };
00061 
00062 void TPMTimer::expire(Event *e) {
00063     //if (e) 
00064     //  Packet::free((Packet *)e);
00065 
00066     tpm->run();
00067 }
00068 
00069 
00070 static class TPMClass : public TclClass {
00071 public:
00072         TPMClass() : TclClass("TPM") {}
00073         TclObject* create(int, const char*const*) {
00074         return (new TPM());
00075     }
00076 } class_tpm;
00077 
00078 TPM::TPM() {
00079     tp_default_ = tp_num_ = 0;
00080     tp = NULL;
00081     
00082     // initialize next request
00083     next_p.time = 0;
00084     next_p.saddr = 0;
00085     next_p.sport = 0;
00086     next_p.daddr = 0;
00087     next_p.dport = 0;
00088     next_p.len = 0;
00089 
00090     // initialize request timer
00091     timer = new TPMTimer(this);
00092     start_t = 0;
00093 }
00094 
00095 TPM::~TPM() {
00096     if (fp)
00097         fclose(fp);
00098     if (timer)
00099         delete timer;
00100 }
00101 
00102 int TPM::loadLog(const char* filename) {
00103     fp = fopen(filename, "r");
00104     if (fp == 0)
00105         return(0);
00106     
00107     return(1);
00108 }
00109 
00110 int TPM::start() {
00111     start_t = Scheduler::instance().clock();
00112     processLog();
00113     return(1);
00114 }
00115 
00116 int TPM::processLog() {
00117     int time, sport, dport, len;
00118     unsigned int saddr, daddr;
00119 
00120     while (!feof(fp)) {
00121         fscanf(fp, "%d %u %d %u %d %d\n",
00122                &time, &saddr, &sport, &daddr, &dport, &len);
00123 
00124         if (time > 0) {
00125             // save information for next request
00126             next_p.time = time;
00127             next_p.saddr = saddr;
00128             next_p.sport = sport;
00129             next_p.daddr = daddr;
00130             next_p.dport = dport;
00131             next_p.len = len;   
00132             
00133             //double now = Scheduler::instance().clock();
00134             //double delay = time + start_t - now;
00135             double delay = time * 1.0 / 1000000.0;
00136             timer->resched(delay);
00137             
00138             return(1);
00139         } else {
00140             genPkt(saddr, sport, daddr, dport, len);
00141         }
00142     }
00143     return(1);
00144 }
00145 
00146 int TPM::run() {
00147     genPkt(next_p.saddr, next_p.sport, next_p.daddr, next_p.dport, next_p.len);
00148     processLog();
00149     return(1);
00150 }
00151 
00152 int TPM::genPkt(unsigned int saddr, int sport, unsigned int daddr, int dport, int len) {
00153     // we can send packets to anybody with twinks...
00154     if (saddr == daddr) {
00155            return(0);
00156     }
00157 
00158     // use the default TP agent unless specified.
00159     unsigned int tp_id = tp_default_;
00160 
00161     if (saddr < (unsigned int)tp_num_) {
00162         tp_id = saddr;
00163     }
00164     printf("tpid %d\n", tp_id);
00165     tp[tp_id]->sendto(len, saddr, sport, daddr, dport);
00166     
00167     return(1);
00168 }
00169 
00170 int TPM::command(int argc, const char*const* argv) {
00171     if (argc == 2) {
00172         if (strcmp(argv[1], "start") == 0) {
00173             if (start())
00174                 return(TCL_OK);
00175             else
00176                 return(TCL_ERROR);
00177             
00178         }
00179         
00180     } else if (argc == 3) {
00181         if (strcmp(argv[1], "loadLog") == 0) {
00182             if (loadLog(argv[2]))
00183                 return(TCL_OK);
00184             else
00185                 return(TCL_ERROR);
00186 
00187         } else if (strcmp(argv[1], "tp-num") == 0) {
00188             tp_num_ = atoi(argv[2]);
00189             tp_default_ = tp_num_ - 1;
00190             if (tp_default_ < 0)
00191               tp_default_ = 0;
00192             tp = new TPAgent*[tp_num_];
00193             return(TCL_OK);
00194         } else if (strcmp(argv[1], "tp-default") == 0) {
00195                 tp_default_ = atoi(argv[2]);
00196             return(TCL_OK);
00197         }
00198     } else if (argc == 4) {
00199         if (strcmp(argv[1], "tp-reg") == 0) {
00200             int index = atoi(argv[2]);
00201             if (index >= tp_num_)
00202                 return(TCL_ERROR);
00203 
00204             tp[index] = (TPAgent *) TclObject::lookup(argv[3]);
00205             return(TCL_OK);
00206         }
00207     }
00208 
00209     printf("Command specified does not exist\n");
00210     return(TCL_ERROR);
00211 }

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