00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "BandwidthTimeFilter.h"
00035 #include "TimeUtil.h"
00036 #include "massert.h"
00037
00038
00039 using std::list;
00040
00041 const double BandwidthTimeFilter::DEFAULT_BANDWIDTH_WINDOW;
00042
00043 BandwidthTimeFilter::BandwidthTimeFilter(double wnd) {
00044 value = 0;
00045 window = wnd;
00046 }
00047
00048 BandwidthTimeFilter::~BandwidthTimeFilter() {
00049 }
00050
00051 double BandwidthTimeFilter::getValue() {
00052 check();
00053 return value;
00054 }
00055
00056 void BandwidthTimeFilter::clear() {
00057 history.clear();
00058 value = 0;
00059 }
00060
00061 void BandwidthTimeFilter::startUpdate() {
00062
00063
00064 double now = TimeUtil::timed();
00065
00066
00067 if(!history.empty() && history.back().stop != 0 && history.back().size != 0) {
00068
00069
00070
00071
00072 history.push_back(BandwidthTriplet(0, history.back().stop, 0));
00073 }
00074 history.push_back (BandwidthTriplet (now, 0, 0));
00075 }
00076
00077 void BandwidthTimeFilter::update(int size) {
00078 double now = TimeUtil::timed();
00079
00080 history.push_back(BandwidthTriplet(0, now, size));
00081 }
00082
00083 void BandwidthTimeFilter::finishUpdate() {
00084 double now = TimeUtil::timed();
00085
00086 history.push_back(BandwidthTriplet(0, now, 0));
00087 }
00088
00089 void BandwidthTimeFilter::check() {
00090 double elapsed_time = 0;
00091 double stop_time = -1;
00092 double end = 0;
00093 int total_bytes = 0;
00094 double seg_start, seg_stop = 0.0;
00095
00096 if(history.size() <= 1) {
00097 value = 0;
00098
00099 return;
00100 }
00101
00102 for(list<BandwidthTriplet>::reverse_iterator it = history.rbegin();
00103 it != history.rend(); ++it) {
00104
00105 if(it->stop > 0 && end == 0) {
00106
00107
00108
00109
00110
00111 end = it->stop;
00112 seg_stop = end;
00113
00114 }
00115 if(it->stop > 0) {
00116 total_bytes += it->size;
00117 if(it->size == 0)
00118 seg_stop = it->stop;
00119 if(end - it->stop >= window) {
00120 stop_time = it->stop;
00121 elapsed_time += (seg_stop - it->stop);
00122 break;
00123 }
00124
00125 }
00126 else if(end != 0) {
00127 ASSERTMSG(it->start != 0, "Segment added, but start is non zero");
00128 seg_start = it->start;
00129
00130 elapsed_time += (seg_stop - seg_start);
00131 if(elapsed_time >= window) {
00132 stop_time = it->start;
00133 break;
00134 }
00135 }
00136 }
00137
00138
00139 if(stop_time != -1) {
00140
00141 while((history.front().start > 0 && history.front().start != stop_time)
00142 || (history.front().stop > 0 && history.front().stop != stop_time)) {
00143 history.pop_front();
00144 }
00145 if(history.front().start == 0) {
00146 history.front().start = history.front().stop;
00147 history.front().stop = 0;
00148 history.front().size = 0;
00149 }
00150 }
00151 if(history.front().start == 0) {
00152 history.front().start = history.front().stop;
00153 history.front().stop = 0;
00154 history.front().size = 0;
00155 }
00156 value = total_bytes / elapsed_time;
00157
00158
00159 }