webserver.cc

Go to the documentation of this file.
00001 
00002 /*
00003  * webserver.cc
00004  * Copyright (C) 1999 by the University of Southern California
00005  * $Id: webserver.cc,v 1.6 2005/08/25 18:58:13 johnh Exp $
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License,
00009  * version 2, as published by the Free Software Foundation.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along
00017  * with this program; if not, write to the Free Software Foundation, Inc.,
00018  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00019  *
00020  *
00021  * The copyright of this module includes the following
00022  * linking-with-specific-other-licenses addition:
00023  *
00024  * In addition, as a special exception, the copyright holders of
00025  * this module give you permission to combine (via static or
00026  * dynamic linking) this module with free software programs or
00027  * libraries that are released under the GNU LGPL and with code
00028  * included in the standard release of ns-2 under the Apache 2.0
00029  * license or under otherwise-compatible licenses with advertising
00030  * requirements (or modified versions of such code, with unchanged
00031  * license).  You may copy and distribute such a system following the
00032  * terms of the GNU GPL for this module and the licenses of the
00033  * other code concerned, provided that you include the source code of
00034  * that other code when and as the GNU GPL requires distribution of
00035  * source code.
00036  *
00037  * Note that people who make modified versions of this module
00038  * are not obligated to grant this special exception for their
00039  * modified versions; it is their choice whether to do so.  The GNU
00040  * General Public License gives permission to release a modified
00041  * version without this exception; this exception also makes it
00042  * possible to release a modified version which carries forward this
00043  * exception.
00044  *
00045  */
00046 
00047 //
00048 // Incorporation Polly's web traffic module into the PagePool framework
00049 //
00050 // Simple web server implementation
00051 // Two server scheduling policies supported: fcfs and stf
00052 // Xuan Chen (xuanc@isi.edu)
00053 //
00054 #include "webserver.h"
00055 
00056 void
00057 WebServer::WebServer_init(WebTrafPool *webpool) {
00058   web_pool_ = webpool;
00059   
00060   // clean busy flag
00061   busy_ = 0;
00062   
00063   // Initialize function flag:
00064   // 0: there's no server processing delay
00065   // 1: server processing delay from FCFS scheduling policy
00066   // 2: server processing delay from STF scheduling policy
00067   set_mode(0);
00068   
00069   // Initialize server processing raste
00070   set_rate(1);
00071   
00072   // initialize the job queue
00073   head = tail = NULL;
00074   queue_size_ = 0;
00075   queue_limit_ = 0;
00076 
00077   //cancel();
00078 }
00079 
00080 // Set server processing rate
00081 void WebServer::set_rate(double s_rate) {
00082   rate_ = s_rate;
00083 }
00084 
00085 // Set server function mode
00086 void WebServer::set_mode(int s_mode) {
00087   mode_ = s_mode;
00088 }
00089 
00090 // Set the limit for job queue
00091 void WebServer::set_queue_limit(int limit) {
00092   queue_limit_ = limit;
00093 }
00094 
00095 
00096 // Return server's node id
00097 int WebServer::get_nid() {
00098   return(node->nodeid());
00099 }
00100 
00101 // Assign node to server
00102 void WebServer::set_node(Node *n) {
00103   node = n;
00104 }
00105 // Return server's node
00106 Node* WebServer::get_node() {
00107   return(node);
00108 }
00109 
00110 double WebServer::job_arrival(int obj_id, Node *clnt, Agent *tcp, Agent *snk, int size, void *data) {
00111   // There's no server processing delay
00112   if (! mode_) {
00113     web_pool_->launchResp(obj_id, node, clnt, tcp, snk, size, data);
00114 
00115     return 1;
00116   }
00117 
00118   //printf("%d %d\n", queue_limit_, queue_size_);
00119   if (!queue_limit_ || queue_size_ < queue_limit_) {
00120     // Insert the new job to the job queue
00121     job_s *new_job = new(job_s);
00122     new_job->obj_id = obj_id;
00123     new_job->clnt = clnt;
00124     new_job->tcp = tcp;
00125     new_job->snk = snk;
00126     new_job->size = size;
00127     new_job->data = data;
00128     new_job->next = NULL; 
00129 
00130     // always insert the new job to the tail.
00131     if (tail)
00132       tail->next = new_job;
00133     else
00134       head = new_job;
00135     tail = new_job;
00136 
00137     queue_size_++;
00138   } else {
00139     // drop the incoming job
00140     //printf("server drop job\n");
00141     return 0;
00142   }
00143 
00144   // Schedule the dequeue time when there's no job being processed
00145   if (!busy_) 
00146     schedule_next_job();
00147   
00148   return 1;
00149 }
00150 
00151 
00152 double WebServer::job_departure() {
00153   if (head) {
00154     web_pool_->launchResp(head->obj_id, node, head->clnt, head->tcp, head->snk, head->size, head->data);
00155     
00156     // delete the first job
00157     job_s *p = head;
00158     if (head->next)
00159       head = head->next;
00160     else 
00161       head = tail = NULL;
00162     
00163     delete(p);
00164     queue_size_--;
00165   }
00166   
00167   // Schedule next job
00168   schedule_next_job();
00169   return 0;
00170 }
00171 
00172 void WebServer::schedule_next_job() {
00173   job_s *p, *q;
00174   
00175   if (head) {
00176     // do shortest task first scheduling
00177     if (mode_ == STF_DELAY) { 
00178       // find the shortest task
00179       p = q = head;
00180       while (q) {
00181     if (p->size > q->size)
00182       p = q;
00183     
00184     q = q->next;
00185       }
00186       
00187       // exchange the queue head with the shortest job
00188       int obj_id = p->obj_id;
00189       Node *clnt = p->clnt;
00190       int size = p->size;
00191       void *data = p->data;
00192       
00193       p->obj_id = head->obj_id;
00194       p->clnt = head->clnt;
00195       p->size = head->size;
00196       p->data = head->data;
00197 
00198       head->obj_id = obj_id;
00199       head->clnt = clnt;
00200       head->size = size;
00201       head->data = data;
00202     }
00203     
00204     // Schedule the processing timer
00205     double delay_ = head->size / rate_;
00206     resched(delay_);
00207     busy_ = 1;
00208     //  printf("%d, %f, %f\n", head->size, rate_, delay_);
00209   } else
00210     busy_ = 0;
00211 }
00212 
00213 // Processing finished
00214 void WebServer::expire(Event *e) {
00215   job_departure();
00216 }

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