⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bandwidth_time_filter.cc

📁 这是一个著名的应用层组播中间件的源码
💻 CC
字号:
//Copyright (c) 2004, Charles Killian, Adolfo Rodriguez, Dejan Kostic, Sooraj Bhat, and Amin Vahdat//All rights reserved.////Redistribution and use in source and binary forms, with or without//modification, are permitted provided that the following conditions are met:////   * Redistributions of source code must retain the above copyright//     notice, this list of conditions and the following disclaimer.//   * Redistributions in binary form must reproduce the above copyright//     notice, this list of conditions and the following disclaimer in//     the documentation and/or other materials provided with the//     distribution.//   * Neither the names of Duke University nor The University of//     California, San Diego, nor the names of its contributors//     may be used to endorse or promote products derived from//     this software without specific prior written permission.////THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"//AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE//IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE//FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL//DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,//OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE//USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#include "bandwidth_time_filter.h"#include <stdio.h>#include "scheduler.h"#include "macedon.h"bandwidth_time_filter::bandwidth_time_filter( ){  value = 0.0;}bandwidth_time_filter::~bandwidth_time_filter(){}// ---------------------------------------------- // get_value// ---------------------------------------------- double  bandwidth_time_filter::get_value (){  check();  return  value;}// ---------------------------------------------- // clear// ---------------------------------------------- void  bandwidth_time_filter::clear (){  history.clear ();  value = 0.0;}// ---------------------------------------------- // start_update// ---------------------------------------------- void  bandwidth_time_filter::start_update (){  // techinically we could start two updates without finishing one,  // so be careful and don't do this  double now =  Scheduler::instance().clock();  // just add an entry with the start time  if(!history.empty() && history.back().stop != 0 && history.back().size != 0) {     // This is a hack to add a "finish" block if we happen to start a segment    // without finishing one.  This can happen if we add source as a neighbor    // later on when we were recording bandwidth to him without having    // requested anything.    history.push_back(bandwidth_triplet(0, history.back().stop, 0));  }  history.push_back (bandwidth_triplet (now, 0, 0));}// ---------------------------------------------- // update// ---------------------------------------------- void bandwidth_time_filter::update(int size1) {  double now =  Scheduler::instance().clock();  history.push_back(bandwidth_triplet(0, now, size1));}// ---------------------------------------------- // finish_update// ---------------------------------------------- void bandwidth_time_filter::finish_update (){  double now =  Scheduler::instance().clock();  history.push_back(bandwidth_triplet(0, now, 0));}// ---------------------------------------------- // check// ---------------------------------------------- void bandwidth_time_filter::check (){  double elapsed_time = 0;  double stop_time = -1;  double end = 0;  int total_bytes = 0;  double seg_start, seg_stop;  if(history.size() <= 1) {    value = 0;//    printf("Returning because not enough bw filter entries\n");    return;  }  for(list<bandwidth_triplet>::reverse_iterator it = history.rbegin();      it != history.rend(); ++it) {    //printf("Entry is (%f %f %d)\n", it->start, it->stop, it->size);    if(it->stop > 0 && end == 0) {      /*if(Scheduler::instance().clock() - it->stop >= BANDWIDTH_WINDOW) {	//printf("First entry is outside bandwidth window\n");	value = 0;	return;      }*/      end = it->stop;      seg_stop = end;      //printf("End time is %f\n", end);    }    if(it->stop > 0) {      total_bytes += it->size;      if(it->size == 0)	seg_stop = it->stop;      if(end - it->stop >= BANDWIDTH_WINDOW) {	stop_time = it->stop;	elapsed_time += (seg_stop - it->stop);	break;      }          }    else if(end != 0) {      ASSERT(it->start != 0);      seg_start = it->start;      //printf("A segment from %f to %f has been added\n", seg_start, seg_stop);      elapsed_time += (seg_stop - seg_start);      if(elapsed_time >= BANDWIDTH_WINDOW) {	stop_time = it->start;	break;      }    }  }  //  if(stop_time == -1)  //  stop_time = (history.front().stop > 0 ? history.front().stop : history.front().start);  if(stop_time != -1) {    //printf("Stop time is %f\n", stop_time);    while((history.front().start > 0 && history.front().start != stop_time)	  || (history.front().stop > 0 && history.front().stop != stop_time)) {      history.pop_front();    }    if(history.front().start == 0) {      history.front().start = history.front().stop;      history.front().stop = 0;      history.front().size = 0;    }  }  if(history.front().start == 0) {    history.front().start = history.front().stop;    history.front().stop = 0;    history.front().size = 0;  }  value = total_bytes*8/elapsed_time;  //printf("Counted %d bytes over %f time. BW = %f\n", total_bytes, elapsed_time, value);  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -