00001 00042 #include "simstream.h" 00043 00044 #define NOW Scheduler::instance().clock() 00045 #define TIMER_TIMEOUT 5 00046 #define STARTUP_TIMEOUT 5 00047 #define RATE_FACTOR 0.7 00048 #define UP_LEVEL_FACTOR 1.2 00049 #define DOWN_LEVEL_FACTOR 0.8 00050 00051 static class SimStreamClass : public TclClass { 00052 public: 00053 SimStreamClass() : TclClass("SimStream/RTP") {} 00054 TclObject* create(int, const char*const*) { 00055 return (new SimStream()); 00056 } 00057 } class_sim_stream; 00058 00062 SimStream::SimStream() 00063 : sessions_(0),streamid_(-1) 00064 { 00065 bind_bool("isSender",&isSender); 00066 bind_bool("isReceiver",&isReceiver); 00067 bind("level_",&level_); 00068 bind("rcv_rate_",&rcv_rate_); 00069 inst_rx_rate_ = 0; 00070 avg_rx_ = 0; 00071 bind("low_tx_",&low_tx_); 00072 bind("medium_tx_",&medium_tx_); 00073 bind("high_tx_",&high_tx_); 00074 bind("low_threshold_", &low_threshold_); 00075 bind("medium_threshold_", &medium_threshold_); 00076 bind("high_threshold_", &high_threshold_); 00077 00078 00079 } 00080 00084 SimStream::~SimStream() 00085 { 00086 while (sessions_ != 0) { 00087 RTPSession* s = sessions_; 00088 sessions_ = sessions_->next; 00089 delete s; 00090 } 00091 rx_lst.~list<double>(); 00092 00093 00094 } 00095 00100 void SimStream::enter(RTPSession* s) 00101 { 00102 00103 printf("Time:%f Stream:%d ...I add session %d \n", NOW,streamid_ , s->srcid()); 00104 s->next = sessions_; 00105 sessions_ = s; 00106 00107 } 00108 00109 00114 int SimStream::command(int argc, const char*const* argv) 00115 { 00116 if (argc == 3) { 00117 if (strcmp(argv[1], "session") == 0) { 00118 RTPSession* s = (RTPSession*)TclObject::lookup(argv[2]); 00119 enter(s); 00120 return (TCL_OK); 00121 } 00122 if (strcmp(argv[1], "stream-id") == 0) { 00123 streamid_ = atoi(argv[2]); 00124 return (TCL_OK); 00125 } 00126 } 00127 00128 return (TclObject::command(argc, argv)); 00129 } 00130 00131 00136 void SimStream::setInst_rx_rate(double theValue) 00137 { 00138 avg_rx_ = avg_rx_ * 0.5 + 0.5 * theValue; 00139 00140 } 00141 00147 void SimStream::setAvg_tx(double theValue, int level) 00148 { 00149 if(level==1) { 00150 low_tx_ = theValue; 00151 } else if(level==2) { 00152 medium_tx_ = theValue; 00153 } else high_tx_ = theValue; 00154 00155 00156 } 00157 00158 00163 void SimStream::compare() 00164 { 00165 00166 switch(level_) { 00167 case 1 : 00168 00169 if(avg_rx_ >= UP_LEVEL_FACTOR * medium_threshold_ && avg_rx_ >= medium_tx_ * RATE_FACTOR) { 00170 00171 go_up(level_); 00172 } 00173 break; 00174 case 2 : 00175 00176 if(avg_rx_ <= DOWN_LEVEL_FACTOR * medium_threshold_) { 00177 00178 go_down(level_); 00179 } else if(avg_rx_ >= UP_LEVEL_FACTOR * high_threshold_ && avg_rx_ >= high_tx_ * RATE_FACTOR) { 00180 00181 go_up(level_); 00182 } 00183 break; 00184 case 3 : 00185 00186 if(avg_rx_ <= DOWN_LEVEL_FACTOR * high_threshold_ ) { 00187 00188 go_down(level_); 00189 } 00190 break; 00191 default : 00192 printf("Error...no level found\n"); 00193 } 00194 00195 } 00196 00201 void SimStream::go_up(int level) 00202 { 00203 Tcl::instance().evalf("%s go-up %d",name(),level); 00204 printf("Time:%f Stream:%d level:%d I go UP Avg_rx: %f Avg_Tx_Low:%f Avg_Tx_Medium:%f Avg_Tx_High:%f \n", NOW,streamid_ , level, avg_rx_ * 8, low_tx_ * 8, medium_tx_ * 8, high_tx_ * 8 ); 00205 00206 } 00207 00212 void SimStream::go_down(int level) 00213 { 00214 Tcl::instance().evalf("%s go-down %d",name(),level); 00215 printf("Time:%f Stream:%d level:%d I go DOWN Avg_rx: %f Avg_Tx_Low:%f Avg_Tx_Medium:%f Avg_Tx_High:%f \n", NOW,streamid_ , level, avg_rx_ * 8, low_tx_ * 8, medium_tx_ * 8, high_tx_ * 8 ); 00216 } 00217 00218 00219 00220 00221 00222 00223 00224