00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef ns_pagepool_h
00023 #define ns_pagepool_h
00024
00025 #include <stdio.h>
00026 #include <limits.h>
00027 #include <tcl.h>
00028 #include <ranvar.h>
00029 #include <tclcl.h>
00030 #include "config.h"
00031
00032 enum WebPageType { HTML, MEDIA };
00033
00034 class Page {
00035 public:
00036 Page(int size) : size_(size) {}
00037 int size() const { return size_; }
00038 int& id() { return id_; }
00039 virtual WebPageType type() const = 0;
00040
00041 protected:
00042 int size_;
00043 int id_;
00044 };
00045
00046 class ServerPage : public Page {
00047 public:
00048 ServerPage(int size, int id) : Page(size) {
00049 id_ = id, mtime_ = NULL, num_mtime_ = 0;
00050 }
00051 virtual ~ServerPage() {
00052 if (mtime_ != NULL)
00053 delete []mtime_;
00054 }
00055
00056 virtual WebPageType type() const { return HTML; }
00057
00058 int& size() { return size_; }
00059 int& mtime(int n) { return mtime_[n]; }
00060 int& num_mtime() { return num_mtime_; }
00061 void set_mtime(int *mt, int n);
00062
00063 protected:
00064 int *mtime_;
00065 int num_mtime_;
00066 };
00067
00068 class HttpApp;
00069
00070
00071 const int HTTP_PAGE_STATE_MASK = 0x00FF;
00072 const int HTTP_ALL_PAGE_STATES = 0x00ff;
00073 const int HTTP_VALID_PAGE = 0x01;
00074 const int HTTP_SERVER_DOWN = 0x02;
00075
00076 const int HTTP_VALID_HEADER = 0x04;
00077 const int HTTP_UNREAD_PAGE = 0x08;
00078
00079
00080
00081 const int HTTP_UNCACHEABLE = 0x10;
00082
00083
00084 const int HTTP_PAGE_ACTION_MASK = 0xFF00;
00085 const int HTTP_MANDATORY_PUSH = 0x1000;
00086
00087 struct PageID {
00088 PageID() : s_(NULL), id_(0) {}
00089 PageID(int* t) {
00090 s_ = (HttpApp*)t[0];
00091 id_ = t[1];
00092 }
00093 HttpApp* s_;
00094 int id_;
00095 };
00096
00097 class ClientPage : public Page {
00098 public:
00099 ClientPage(const char *n, int s, double mt, double et, double a);
00100 virtual ~ClientPage() {}
00101
00102 virtual WebPageType type() const { return HTML; }
00103 virtual void print_info(char* buf);
00104
00105 void name(char* buf);
00106 double& mtime() { return mtime_; }
00107 double& etime() { return etime_; }
00108 double& age() { return age_; }
00109 HttpApp* server() { return server_; }
00110
00111
00112 void validate(double mtime) {
00113 if (mtime_ >= mtime)
00114 abort();
00115
00116 clear_page_state(HTTP_SERVER_DOWN);
00117 set_page_state(HTTP_VALID_PAGE);
00118 mtime_ = mtime;
00119 }
00120 void invalidate(double mtime) {
00121 if (mtime_ >= mtime)
00122 return;
00123 clear_page_state(HTTP_VALID_PAGE);
00124 clear_page_state(HTTP_VALID_HEADER);
00125 mtime_ = mtime;
00126 }
00127 int is_valid() const {
00128 return (status_ & HTTP_VALID_PAGE);
00129 }
00130 int is_header_valid() const {
00131 return ((status_ & HTTP_VALID_PAGE) ||
00132 (status_ & HTTP_VALID_HEADER));
00133 }
00134 inline void set_valid_hdr() {
00135
00136 clear_page_state(HTTP_SERVER_DOWN);
00137 clear_page_state(HTTP_VALID_PAGE);
00138 set_page_state(HTTP_VALID_HEADER);
00139 }
00140
00141 inline void set_uncacheable() {
00142 set_page_state(HTTP_UNCACHEABLE);
00143 }
00144 inline int is_uncacheable() {
00145 return (status_ & HTTP_UNCACHEABLE);
00146 }
00147
00148
00149
00150 inline void set_unread() {
00151 set_page_state(HTTP_UNREAD_PAGE);
00152 }
00153 inline void set_read() {
00154 clear_page_state(HTTP_UNREAD_PAGE);
00155 }
00156 inline int is_unread() { return (status_ & HTTP_UNREAD_PAGE); }
00157
00158 inline int is_server_down() { return (status_ & HTTP_SERVER_DOWN); }
00159 inline void server_down() {
00160
00161
00162 clear_page_state(HTTP_VALID_PAGE);
00163 clear_page_state(HTTP_VALID_HEADER);
00164 set_page_state(HTTP_SERVER_DOWN);
00165 }
00166
00167
00168
00169 static int PUSHALL_;
00170 inline int& counter() {
00171 if (PUSHALL_) counter_ = INT_MAX;
00172 return counter_;
00173 }
00174 inline int count_inval(int a, int th) {
00175 if (PUSHALL_)
00176 return INT_MAX;
00177 else {
00178 counter_ -= a;
00179 if (counter_ < th)
00180 counter_ = th;
00181 return counter_;
00182 }
00183 }
00184 inline int count_request(int b, int th) {
00185 if (PUSHALL_)
00186 return INT_MAX;
00187 else {
00188 counter_ += b;
00189 if (counter_ > th)
00190 counter_ = th;
00191 return counter_;
00192 }
00193 }
00194 inline void set_mpush(double time) {
00195 set_page_action(HTTP_MANDATORY_PUSH), mpushTime_ = time;
00196 }
00197 inline void clear_mpush() { clear_page_action(HTTP_MANDATORY_PUSH); }
00198 inline int is_mpush() { return status_ & HTTP_MANDATORY_PUSH; }
00199 inline double mpush_time() { return mpushTime_; }
00200
00201
00202 static void split_name(const char* name, PageID& id);
00203 static void print_name(char* name, PageID& id);
00204
00205 protected:
00206 void set_page_state(int state) {
00207 status_ |= state;
00208 }
00209 void clear_page_state(int state) {
00210 status_ = status_ & ~state;
00211 }
00212 void set_page_action(int action) {
00213 status_ |= action;
00214 }
00215 void clear_page_action(int action) {
00216 status_ = status_ & ~action;
00217 }
00218
00219 HttpApp* server_;
00220 double age_;
00221 double mtime_;
00222 double etime_;
00223 int status_;
00224 int counter_;
00225 double mpushTime_;
00226 };
00227
00228
00229
00230 class PagePool : public TclObject {
00231 public:
00232 PagePool() : num_pages_(0), start_time_(INT_MAX), end_time_(INT_MIN) {}
00233 int num_pages() const { return num_pages_; }
00234 protected:
00235 virtual int command(int argc, const char*const* argv);
00236 int num_pages_;
00237 double start_time_;
00238 double end_time_;
00239 int duration_;
00240
00241
00242 TclObject* lookup_obj(const char* name) {
00243 TclObject* obj = Tcl::instance().lookup(name);
00244 if (obj == NULL)
00245 fprintf(stderr, "Bad object name %s\n", name);
00246 return obj;
00247 }
00248 };
00249
00250
00251
00252 const int TRACEPAGEPOOL_MAXBUF = 4096;
00253
00254
00255 class TracePagePool : public PagePool {
00256 public:
00257 TracePagePool(const char *fn);
00258 virtual ~TracePagePool();
00259 virtual int command(int argc, const char*const* argv);
00260
00261 protected:
00262 Tcl_HashTable *namemap_, *idmap_;
00263 RandomVariable *ranvar_;
00264
00265 ServerPage* load_page(FILE *fp);
00266 void change_time();
00267 int add_page(const char* pgname, ServerPage *pg);
00268
00269 ServerPage* get_page(int id);
00270 };
00271
00272
00273
00274 class MathPagePool : public PagePool {
00275 public:
00276
00277 MathPagePool() : rvSize_(0), rvAge_(0) { num_pages_ = 1; }
00278
00279 protected:
00280 virtual int command(int argc, const char*const* argv);
00281
00282 RandomVariable *rvSize_;
00283 RandomVariable *rvAge_;
00284 };
00285
00286
00287 class CompMathPagePool : public PagePool {
00288 public:
00289 CompMathPagePool();
00290
00291 protected:
00292 virtual int command(int argc, const char*const* argv);
00293 RandomVariable *rvMainAge_;
00294 RandomVariable *rvCompAge_;
00295 int main_size_, comp_size_;
00296 };
00297
00298 class ClientPagePool : public PagePool {
00299 public:
00300 ClientPagePool();
00301 virtual ~ClientPagePool();
00302
00303 virtual ClientPage* enter_page(int argc, const char*const* argv);
00304 virtual ClientPage* enter_metadata(int argc, const char*const* argv);
00305 virtual ClientPage* enter_page(const char *name, int size, double mt,
00306 double et, double age);
00307 virtual ClientPage* enter_metadata(const char *name, int size,
00308 double mt, double et, double age);
00309 virtual int remove_page(const char *name);
00310
00311 void invalidate_server(int server_id);
00312
00313 ClientPage* get_page(const char *name);
00314 int get_mtime(const char *name, double &mt);
00315 int set_mtime(const char *name, double mt);
00316 int exist_page(const char *name) { return (get_page(name) != NULL); }
00317 int get_size(const char *name, int &size);
00318 int get_age(const char *name, double &age);
00319 int get_etime(const char *name, double &et);
00320 int set_etime(const char *name, double et);
00321 int get_pageinfo(const char *name, char *buf);
00322
00323 virtual int command(int argc, const char*const* argv);
00324
00325 protected:
00326
00327 int add_page(ClientPage *pg);
00328 Tcl_HashTable *namemap_;
00329 };
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 class ProxyTracePagePool : public PagePool {
00342 public:
00343 ProxyTracePagePool();
00344
00345
00346
00347 virtual ~ProxyTracePagePool();
00348 virtual int command(int argc, const char*const* argv);
00349
00350 protected:
00351
00352
00353 int init_req(const char *fn);
00354 int init_page(const char *fn);
00355 int find_info();
00356
00357 RandomVariable *rvDyn_, *rvStatic_;
00358 int br_;
00359 int *size_;
00360 FILE *reqfile_;
00361
00362 struct ClientRequest {
00363 ClientRequest() : seq_(0), nrt_(0), nurl_(0), fpos_(0)
00364 {}
00365 int seq_;
00366
00367 double nrt_;
00368 int nurl_;
00369 long fpos_;
00370 };
00371 Tcl_HashTable *req_;
00372 int nclient_, lastseq_;
00373 ClientRequest* load_req(int cid);
00374 };
00375
00376 class EPATracePagePool : public ProxyTracePagePool {
00377 public:
00378 virtual int command(int argc, const char*const* argv);
00379 };
00380
00381 #endif //ns_pagepool_h