logweb.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  * logweb.cc
00005  * Copyright (C) 2001 by the University of Southern California
00006  * $Id: logweb.cc,v 1.5 2005/08/25 18:58:13 johnh Exp $
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License,
00010  * version 2, as published by the Free Software Foundation.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with this program; if not, write to the Free Software Foundation, Inc.,
00019  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00020  *
00021  *
00022  * The copyright of this module includes the following
00023  * linking-with-specific-other-licenses addition:
00024  *
00025  * In addition, as a special exception, the copyright holders of
00026  * this module give you permission to combine (via static or
00027  * dynamic linking) this module with free software programs or
00028  * libraries that are released under the GNU LGPL and with code
00029  * included in the standard release of ns-2 under the Apache 2.0
00030  * license or under otherwise-compatible licenses with advertising
00031  * requirements (or modified versions of such code, with unchanged
00032  * license).  You may copy and distribute such a system following the
00033  * terms of the GNU GPL for this module and the licenses of the
00034  * other code concerned, provided that you include the source code of
00035  * that other code when and as the GNU GPL requires distribution of
00036  * source code.
00037  *
00038  * Note that people who make modified versions of this module
00039  * are not obligated to grant this special exception for their
00040  * modified versions; it is their choice whether to do so.  The GNU
00041  * General Public License gives permission to release a modified
00042  * version without this exception; this exception also makes it
00043  * possible to release a modified version which carries forward this
00044  * exception.
00045  *
00046  */
00047 
00048 //
00049 // Generate web traffic based on HTTP log
00050 // Xuan Chen (xuanc@isi.edu)
00051 //
00052 #include <tclcl.h>
00053 
00054 #include "logweb.h"
00055 
00056 // Timer to send requests
00057 RequestTimer::RequestTimer(LogWebTrafPool* pool) {
00058     lwp = pool;
00059 };
00060 
00061 void RequestTimer::expire(Event *e) {
00062     //if (e) 
00063     //  Packet::free((Packet *)e);
00064 
00065     lwp->run();
00066 }
00067 
00068 
00069 static class LogWebTrafPoolClass : public TclClass {
00070 public:
00071         LogWebTrafPoolClass() : TclClass("PagePool/WebTraf/Log") {}
00072         TclObject* create(int, const char*const*) {
00073         return (new LogWebTrafPool());
00074     }
00075 } class_logwebtrafpool;
00076 
00077 LogWebTrafPool::LogWebTrafPool() {
00078     num_obj = 0;
00079     // initialize next request
00080     next_req.time = 0;
00081     next_req.client = 0;
00082     next_req.server = 0;
00083     next_req.size = 0;
00084 
00085     // initialize request timer
00086     req_timer = new RequestTimer(this);
00087     start_t = 0;
00088 }
00089 
00090 LogWebTrafPool::~LogWebTrafPool() {
00091     if (fp)
00092         fclose(fp);
00093     if (req_timer) delete req_timer;
00094 }
00095 
00096 int LogWebTrafPool::loadLog(const char* filename) {
00097     fp = fopen(filename, "r");
00098     if (fp == 0)
00099         return(0);
00100     
00101     return(1);
00102 }
00103 
00104 int LogWebTrafPool::start() {
00105     start_t = Scheduler::instance().clock();
00106     processLog();
00107     return(1);
00108 }
00109 
00110 int LogWebTrafPool::processLog() {
00111     int time, client, server, size;
00112 
00113     if (!feof(fp)) {
00114         fscanf(fp, "%d %d %d %d\n", &time, &client, &server, &size);
00115         // save information for next request
00116         next_req.time = time;
00117         next_req.client = client;
00118         next_req.server = server;
00119         next_req.size = size;   
00120 
00121         double now = Scheduler::instance().clock();
00122         double delay = time + start_t - now;
00123         req_timer->resched(delay);
00124 
00125         return(1);
00126 
00127     } else 
00128         return(0);
00129 }
00130 
00131 int LogWebTrafPool::run() {
00132     launchReq(next_req.client, next_req.server, next_req.size);
00133     processLog();
00134     return(1);
00135 }
00136 
00137 Node* LogWebTrafPool::picksrc(int id) {
00138     int n = id % nClient_;
00139     assert((n >= 0) && (n < nClient_));
00140     return client_[n];
00141 }
00142 
00143 Node* LogWebTrafPool::pickdst(int id) {
00144     int n = id % nServer_;
00145     assert((n >= 0) && (n < nServer_));
00146     return server_[n].get_node();
00147 }
00148 
00149 int LogWebTrafPool::launchReq(int cid, int sid, int size) {
00150     TcpAgent *tcp;
00151     Agent *snk;
00152     
00153     // Allocation new TCP connections for both directions
00154     pick_ep(&tcp, &snk);
00155 
00156     // pick client and server nodes
00157     Node* client = picksrc(cid);
00158     Node* server = pickdst(sid);
00159 
00160     int num_pkt = size / 1000 + 1;
00161 
00162     // Setup TCP connection and done
00163     Tcl::instance().evalf("%s launch-req %d %d %s %s %s %s %d %d", 
00164                   name(), num_obj++, num_obj,
00165                   client->name(), server->name(),
00166                   tcp->name(), snk->name(), num_pkt, NULL);
00167 
00168     return(1);
00169 }
00170 
00171 int LogWebTrafPool::command(int argc, const char*const* argv) {
00172     if (argc == 2) {
00173         if (strcmp(argv[1], "start") == 0) {
00174             if (start())
00175                 return (TCL_OK);
00176             else
00177                 return (TCL_ERROR);
00178             
00179         }
00180         
00181     } else if (argc == 3) {
00182         if (strcmp(argv[1], "loadLog") == 0) {
00183             if (loadLog(argv[2]))
00184                 return (TCL_OK);
00185             else
00186                 return (TCL_ERROR);
00187 
00188         } else if (strcmp(argv[1], "doneObj") == 0) {
00189             return (TCL_OK);
00190         } 
00191     }
00192     return WebTrafPool::command(argc, argv);
00193 }

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