1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
* mcache.h
* Copyright (C) 1997 by the University of Southern California
* $Id: mcache.h,v 1.8 2005/09/18 23:33:35 tomh Exp $
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
*
* The copyright of this module includes the following
* linking-with-specific-other-licenses addition:
*
* In addition, as a special exception, the copyright holders of
* this module give you permission to combine (via static or
* dynamic linking) this module with free software programs or
* libraries that are released under the GNU LGPL and with code
* included in the standard release of ns-2 under the Apache 2.0
* license or under otherwise-compatible licenses with advertising
* requirements (or modified versions of such code, with unchanged
* license). You may copy and distribute such a system following the
* terms of the GNU GPL for this module and the licenses of the
* other code concerned, provided that you include the source code of
* that other code when and as the GNU GPL requires distribution of
* source code.
*
* Note that people who make modified versions of this module
* are not obligated to grant this special exception for their
* modified versions; it is their choice whether to do so. The GNU
* General Public License gives permission to release a modified
* version without this exception; this exception also makes it
* possible to release a modified version which carries forward this
* exception.
*
*/
//
// Multimedia caches
//
// $Header: /cvsroot/nsnam/ns-2/webcache/mcache.h,v 1.8 2005/09/18 23:33:35 tomh Exp $
#ifndef ns_mcache_h
#define ns_mcache_h
#include "config.h"
#include "agent.h"
#include "pagepool.h"
#include "http.h"
#include "rap/media-app.h"
//----------------------------------------------------------------------
// Priority list for hit counts at each layer
//----------------------------------------------------------------------
class HitCount : public DoubleListElem {
public:
HitCount(ClientPage *pg, short layer) :
DoubleListElem(), pg_(pg), layer_(layer), hc_(0) {}
void update(float hc) { hc_ += hc; }
float hc() { return hc_; }
void reset() { hc_ = 0; }
ClientPage* page() { return pg_; }
short layer() { return layer_; }
HitCount* next() { return (HitCount *)DoubleListElem::next(); }
HitCount* prev() { return (HitCount *)DoubleListElem::prev(); }
private:
ClientPage* pg_; // page
short layer_; // layer id
float hc_; // hit count
};
class HitCountList : public DoubleList {
public:
HitCountList() : DoubleList() {}
void update(HitCount *h); // Update layer hit count
void add(HitCount *h); // Add a new layer
HitCount* detach_tail() {
HitCount* tmp = (HitCount *)tail_;
if (tmp) {
tail_ = tail_->prev();
tmp->detach();
}
return tmp;
}
// Debug only
void print();
void check_integrity();
};
//----------------------------------------------------------------------
// Multimedia web objects
//----------------------------------------------------------------------
class MediaPage : public ClientPage {
public:
MediaPage(const char *n, int s, double mt, double et, double a, int l);
virtual ~MediaPage();
virtual WebPageType type() const { return MEDIA; }
virtual void print_info(char *buf);
int num_layer() const { return num_layer_; }
HitCount* get_hit_count(int layer) {
assert((layer >= 0) && (layer < num_layer_));
return hc_[layer];
}
void hit_layer(int layer) {
assert((layer >= 0) && (layer < num_layer_));
hc_[layer]->update((double)(layer_[layer].length()*num_layer_)
/ (double)size_);
}
int layer_size(int layer) {
assert((layer >= 0) && (layer < num_layer_));
return layer_[layer].length();
}
void add_segment(int layer, const MediaSegment& s);
int evict_tail_segment(int layer, int size);
void add_layer(int layer) {
assert((layer >= 0) && (layer < num_layer_));
num_layer_ = (num_layer_ < layer) ? layer : num_layer_;
}
char* print_layer(int layer) {
assert((layer >= 0) && (layer < num_layer_));
return layer_[layer].dump2buf();
}
MediaSegmentList is_available(int layer, const MediaSegment& s) {
assert((layer >= 0) && (layer < MAX_LAYER));
return layer_[layer].check_holes(s);
}
// Return a media segment which is the closest one after 's'.
// Used by MediaApps to request data.
// Do NOT check if layer >= num_layer_. If it's empty,
// an empty MediaSegment will be returned.
MediaSegment next_available(int layer, const MediaSegment& s) {
assert((layer >= 0) && (layer < MAX_LAYER));
return layer_[layer].get_nextseg(s);
}
MediaSegment next_overlap(int layer, const MediaSegment& s) {
assert((layer >= 0) && (layer < MAX_LAYER));
MediaSegment s1 = layer_[layer].get_nextseg(s);
if ((s1.end() <= s.start()) || (s1.start() >= s.end())) {
MediaSegment s;
if (s1.is_last())
s.set_last();
return s;
} else
return s1;
}
enum {FETCHLOCK = 1, XMITLOCK = 2};
// 1st type of lock: it is being fetched from the server
void lock() { locked_ |= FETCHLOCK; }
void unlock() { locked_ &= ~FETCHLOCK; }
int is_locked() { return (locked_ & FETCHLOCK); }
// 2nd type of lock: it is being transmitted to a client
void tlock() { locked_ |= XMITLOCK; }
void tunlock() { locked_ &= ~XMITLOCK; }
int is_tlocked() { return (locked_ & XMITLOCK); }
// Whether all layers are marked as "completed"
int is_complete();
// Set all layers as "completed"
void set_complete();
// Given the number of layers, evenly distribute the size among all
// layers and create all segments.
void create();
int realsize() const { return realsize_; }
protected:
void set_complete_layer(int layer) {
flags_[layer] = 1;
}
int is_complete_layer(int layer) {
return flags_[layer] == 1;
}
MediaSegmentList layer_[MAX_LAYER];
int flags_[MAX_LAYER]; // If 1, marks the layer as completed
HitCount* hc_[MAX_LAYER];
int num_layer_;
int locked_; // If 1, then no segment can be replaced
int realsize_; // The size of stream data in this page.
};
// XXX Later we should add a generic replacement algorithm hook into
// ClientPagePool. But now we'll leave it there and get this media
// page pool work first.
// ClientPagePool enhanced with support for multimedia objects, and
// with replacement algorithms
class MClientPagePool : public ClientPagePool {
public:
MClientPagePool();
virtual ClientPage* enter_page(int argc, const char*const* argv);
virtual int remove_page(const char *name);
virtual int force_remove(const char *name);
int add_segment(const char *name, int layer, const MediaSegment& s);
void hc_update(const char *name, int max_layer);
int maxsize() { return max_size_; }
int usedsize() { return used_size_; }
void fill_page(const char* pgname);
// Debug only
void dump_hclist() { hclist_.print(); }
protected:
virtual int command(int argc, const char*const* argv);
virtual int cache_replace(ClientPage* page, int size);
// Fine-grain replacement
int repl_finegrain(ClientPage* p, int size);
int repl_atomic(ClientPage* p, int size);
// XXX Should change to quad_t, or use MB as unit
int max_size_; // PagePool size
int used_size_; // Available space size
HitCountList hclist_;
// Replacement style
enum { FINEGRAIN, ATOMIC } repl_style_;
};
// Provide page data and generate requests for servers and clients
class MediaPagePool : public PagePool {
public:
MediaPagePool();
protected:
virtual int command(int argc, const char*const* argv);
int layer_; // Number of layers of pages
int *size_; // Page sizes
RandomVariable *rvReq_;
};
//--------------------
// Multimedia caches
//--------------------
// Plain multimedia cache, which can interface with RapAgent
class MediaCache : public HttpCache {
public:
MediaCache();
~MediaCache();
// Handle app-specific data in C++
virtual void process_data(int size, AppData* data);
// Handle data request from RAP
virtual AppData* get_data(int& size, AppData* data);
protected:
virtual int command(int argc, const char*const* argv);
// Do we need to maintain links to MediaApp?? I doubt not
// MediaApp* mapp_; // An array of incoming/outgoing RAP agents
MClientPagePool* mpool() { return (MClientPagePool *)pool_; }
// Information and statistics related to clients
struct RegInfo {
RegInfo() : client_(NULL), hl_(-1) {
memset(pb_, 0, sizeof(unsigned int)*MAX_LAYER);
memset(db_, 0, sizeof(unsigned int)*MAX_LAYER);
memset(eb_, 0, sizeof(unsigned int)*MAX_LAYER);
}
~RegInfo() {
for (int i = 0; i < MAX_LAYER; i++)
pref_[i].destroy();
}
char name_[20];
HttpApp* client_;
int hl_; // Highest layer this client has asked
// Prefetched bytes
unsigned int pb_[MAX_LAYER];
// Prefetched bytes that were delivered
unsigned int eb_[MAX_LAYER];
// Total delivered bytes
unsigned int db_[MAX_LAYER];
MediaSegmentList pref_[MAX_LAYER];
// Return the number of prefetched bytes in the given segment
void add_pref(int layer, const MediaSegment& s) {
assert((layer >= 0) && (layer < MAX_LAYER));
pref_[layer].add(s);
}
int pref_size(int layer, const MediaSegment &s) const {
assert((layer >= 0) && (layer < MAX_LAYER));
return pref_[layer].overlap_size(s);
}
};
Tcl_HashTable *cmap_; // client map
// Prefetching/No-prefetching/Offline-prefetching flag
enum {NOPREF, ONLINE_PREF, OFFLINE_PREF} pref_style_;
};
//----------------------------------------------------------------------
// Multimedia web client
//----------------------------------------------------------------------
class MediaClient : public HttpClient {
public:
MediaClient() : HttpClient() {}
virtual void process_data(int size, AppData* data);
virtual int command(int argc, const char*const* argv);
private:
MClientPagePool* mpool() { return (MClientPagePool *)pool_; }
};
// Helper data structure
template <class T> class Queue; // forward declaration
template <class T> class QueueElem {
public:
QueueElem(T* d) : next_(0), data_(d) {}
QueueElem<T>* next() const { return next_; }
T* data() const { return data_; }
void detachData() { data_ = NULL; }
void append(QueueElem<T>* e) {
e->next_ = next_;
next_ = e;
}
protected:
QueueElem<T>* next_;
T* data_;
friend class Queue<T>;
};
template <class T> class Queue {
public:
Queue() : head_(0), tail_(0), size_(0) {}
virtual ~Queue() {
QueueElem<T> *p = head_, *q;
while (p != NULL) {
q = p;
p = p->next();
delete q;
}
head_ = NULL;
}
virtual void reset() {
QueueElem<T> *p = head_, *q;
while (p != NULL) {
q = p;
p = p->next();
delete q;
}
head_ = NULL;
}
virtual void destroy() {
QueueElem<T> *p = head_, *q;
while (p != NULL) {
q = p;
p = p->next();
delete q->data();
delete q;
}
head_ = NULL;
}
void enqueue(QueueElem<T> *e) {
if (tail_ == 0)
head_ = tail_ = e;
else {
tail_->append(e);
tail_ = e;
}
size_++;
}
QueueElem<T>* dequeue() {
QueueElem<T> *p = head_;
if (head_ != 0)
head_ = head_->next();
if (head_ == 0)
tail_ = 0;
p->next_ = 0;
size_--;
if (size_ == 0)
assert((head_ == 0) && (tail_ == 0));
return p;
}
void detach(QueueElem<T>* e) {
assert(head_ != 0);
if (head_ == e) {
dequeue();
return;
}
QueueElem<T> *p = head_;
while (p != NULL) {
if (p->next_ != e)
p = p->next_;
else
break;
}
assert(p != NULL);
p->next_ = e->next_;
if (tail_ == e)
tail_ = p;
size_--;
if (size_ == 0)
assert((head_ == 0) && (tail_ == 0));
}
QueueElem<T>* getHead() { return head_; }
int is_empty() const { return (size_ == 0); }
int size() const { return size_; }
protected:
QueueElem<T> *head_, *tail_;
int size_;
};
//----------------------------------------------------------------------
// Multimedia server
//----------------------------------------------------------------------
class MediaServer : public HttpServer {
public:
MediaServer();
~MediaServer();
virtual AppData* get_data(int& size, AppData* d);
protected:
virtual int command(int argc, const char*const* argv);
MediaSegment get_next_segment(MediaRequest *r, Application*& ci);
// Prefetching list
struct RegInfo {
char name_[20];
HttpApp* client_;
};
struct PrefInfo {
MediaSegmentList* sl_;
Application* conid_;
};
typedef Queue<PrefInfo> PrefInfoQ;
typedef QueueElem<PrefInfo> PrefInfoE;
PrefInfoE* find_prefinfo(PrefInfoQ* q, Application* conid) {
for (PrefInfoE *e = q->getHead(); e != NULL; e = e->next())
if (e->data()->conid_ == conid)
return e;
return NULL;
}
Tcl_HashTable *pref_; // Mapping <cache>:<pagenum> to PrefInfoQ
Tcl_HashTable *cmap_; // Mapping MediaApps to clients
PrefInfoQ* get_piq(const char* pgname, HttpApp* client) {
PageID id;
ClientPage::split_name(pgname, id);
id.s_ = client;
Tcl_HashEntry* he =
Tcl_FindHashEntry(pref_, (const char*)&id);
if (he == NULL)
return NULL;
return (PrefInfoQ*)Tcl_GetHashValue(he);
}
RegInfo* get_reginfo(Application* app) {
Tcl_HashEntry *he =
Tcl_FindHashEntry(cmap_, (const char *)app);
if (he == NULL) {
fprintf(stderr, "Unknown connection!\n");
abort();
}
return (RegInfo *)Tcl_GetHashValue(he);
}
};
#endif // ns_mcache_h
|