Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

webserver.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (c) 1999 by the University of Southern California
00003 // All rights reserved.
00004 //
00005 // Permission to use, copy, modify, and distribute this software and its
00006 // documentation in source and binary forms for non-commercial purposes
00007 // and without fee is hereby granted, provided that the above copyright
00008 // notice appear in all copies and that both the copyright notice and
00009 // this permission notice appear in supporting documentation. and that
00010 // any documentation, advertising materials, and other materials related
00011 // to such distribution and use acknowledge that the software was
00012 // developed by the University of Southern California, Information
00013 // Sciences Institute.  The name of the University may not be used to
00014 // endorse or promote products derived from this software without
00015 // specific prior written permission.
00016 //
00017 // THE UNIVERSITY OF SOUTHERN CALIFORNIA makes no representations about
00018 // the suitability of this software for any purpose.  THIS SOFTWARE IS
00019 // PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
00020 // INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00021 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00022 //
00023 // Other copyrights might apply to parts of this software and are so
00024 // noted when applicable.
00025 //
00026 // Incorporation Polly's web traffic module into the PagePool framework
00027 //
00028 // Simple web server implementation
00029 // Two server scheduling policies supported: fcfs and stf
00030 // Xuan Chen (xuanc@isi.edu)
00031 //
00032 #include "webserver.h"
00033 
00034 WebServer::WebServer(WebTrafPool *webpool) {
00035   web_pool_ = webpool;
00036   
00037   // clean busy flag
00038   busy_ = 0;
00039   
00040   // Initialize function flag:
00041   // 0: there's no server processing delay
00042   // 1: server processing delay from FCFS scheduling policy
00043   // 2: server processing delay from STF scheduling policy
00044   set_mode(0);
00045   
00046   // Initialize server processing raste
00047   set_rate(1);
00048   
00049   // initialize the job queue
00050   head = tail = NULL;
00051   queue_size_ = 0;
00052   queue_limit_ = 0;
00053 
00054   //cancel();
00055 }
00056 
00057 // Set server processing rate
00058 void WebServer::set_rate(double s_rate) {
00059   rate_ = s_rate;
00060 }
00061 
00062 // Set server function mode
00063 void WebServer::set_mode(int s_mode) {
00064   mode_ = s_mode;
00065 }
00066 
00067 // Set the limit for job queue
00068 void WebServer::set_queue_limit(int limit) {
00069   queue_limit_ = limit;
00070 }
00071 
00072 
00073 // Return server's node id
00074 int WebServer::get_nid() {
00075   return(node->nodeid());
00076 }
00077 
00078 // Assign node to server
00079 void WebServer::set_node(Node *n) {
00080   node = n;
00081 }
00082 // Return server's node
00083 Node* WebServer::get_node() {
00084   return(node);
00085 }
00086 
00087 double WebServer::job_arrival(int obj_id, Node *clnt, Agent *tcp, Agent *snk, int size, void *data) {
00088   // There's no server processing delay
00089   if (! mode_) {
00090     web_pool_->launchResp(obj_id, node, clnt, tcp, snk, size, data);
00091 
00092     return 1;
00093   }
00094 
00095   //printf("%d %d\n", queue_limit_, queue_size_);
00096   if (!queue_limit_ || queue_size_ < queue_limit_) {
00097     // Insert the new job to the job queue
00098     job_s *new_job = new(job_s);
00099     new_job->obj_id = obj_id;
00100     new_job->clnt = clnt;
00101     new_job->tcp = tcp;
00102     new_job->snk = snk;
00103     new_job->size = size;
00104     new_job->data = data;
00105     new_job->next = NULL; 
00106 
00107     // always insert the new job to the tail.
00108     if (tail)
00109       tail->next = new_job;
00110     else
00111       head = new_job;
00112     tail = new_job;
00113 
00114     queue_size_++;
00115   } else {
00116     // drop the incoming job
00117     //printf("server drop job\n");
00118     return 0;
00119   }
00120 
00121   // Schedule the dequeue time when there's no job being processed
00122   if (!busy_) 
00123     schedule_next_job();
00124   
00125   return 1;
00126 }
00127 
00128 
00129 double WebServer::job_departure() {
00130   if (head) {
00131     web_pool_->launchResp(head->obj_id, node, head->clnt, head->tcp, head->snk, head->size, head->data);
00132     
00133     // delete the first job
00134     job_s *p = head;
00135     if (head->next)
00136       head = head->next;
00137     else 
00138       head = tail = NULL;
00139     
00140     delete(p);
00141     queue_size_--;
00142   }
00143   
00144   // Schedule next job
00145   schedule_next_job();
00146   return 0;
00147 }
00148 
00149 void WebServer::schedule_next_job() {
00150   job_s *p, *q;
00151   
00152   if (head) {
00153     // do shortest task first scheduling
00154     if (mode_ == STF_DELAY) { 
00155       // find the shortest task
00156       p = q = head;
00157       while (q) {
00158         if (p->size > q->size)
00159           p = q;
00160         
00161         q = q->next;
00162       }
00163       
00164       // exchange the queue head with the shortest job
00165       int obj_id = p->obj_id;
00166       Node *clnt = p->clnt;
00167       int size = p->size;
00168       void *data = p->data;
00169       
00170       p->obj_id = head->obj_id;
00171       p->clnt = head->clnt;
00172       p->size = head->size;
00173       p->data = head->data;
00174 
00175       head->obj_id = obj_id;
00176       head->clnt = clnt;
00177       head->size = size;
00178       head->data = data;
00179     }
00180     
00181     // Schedule the processing timer
00182     double delay_ = head->size / rate_;
00183     resched(delay_);
00184     busy_ = 1;
00185     //  printf("%d, %f, %f\n", head->size, rate_, delay_);
00186   } else
00187     busy_ = 0;
00188 }
00189 
00190 // Processing finished
00191 void WebServer::expire(Event *e) {
00192   job_departure();
00193 }

Generated on Tue Apr 20 12:14:38 2004 for NS2.26SourcesOriginal by doxygen 1.3.3