📄 mytraffictrace3.cc
字号:
#include <sys/types.h>#include <sys/stat.h> #include <stdio.h>#include "config.h"#ifdef HAVE_NETINET_IN_H#include <netinet/in.h>#endif#include "random.h"#include "object.h"#include "trafgen.h"struct tracerec { double trec_time; /* inter-packet time (usec) */ u_int32_t trec_size; /* frame size (bytes) */ u_int32_t trec_type; /* packet type */ };class myTraceFile3 : public NsObject { public: myTraceFile3(); void get_next(int&, struct tracerec&); /* called by TrafficGenerator * to get next record in trace. */ int setup(); /* initialize the trace file */ int command(int argc, const char*const* argv); int get_a(); //added by smallko private: void recv(Packet*, Handler*); /* must be defined for NsObject */ int status_; char *name_; /* name of the file in which the trace is stored */ int nrec_; /* number of records in the trace file */ struct tracerec *trace_; /* array holding the trace */ int a_;};/* instance of a traffic generator. has a pointer to the TraceFile * object and implements the interval() function. */class myTrafficTrace3 : public TrafficGenerator { public: myTrafficTrace3(); int command(int argc, const char*const* argv); virtual double next_interval(int &); protected: void timeout(); myTraceFile3 *tfile_; struct tracerec trec_; int ndx_; int f_; // added by smallko void init();};static class myTraceFile3Class : public TclClass { public: myTraceFile3Class() : TclClass("myTracefile3") {} TclObject* create(int, const char*const*) { return (new myTraceFile3()); }} class_my_tracefile;myTraceFile3::myTraceFile3() : status_(0){ a_=0;}int myTraceFile3::get_a(){ return a_;}int myTraceFile3::command(int argc, const char*const* argv){ if (argc == 3) { if (strcmp(argv[1], "filename") == 0) { name_ = new char[strlen(argv[2])+1]; strcpy(name_, argv[2]); return(TCL_OK); } } return (NsObject::command(argc, argv));}void myTraceFile3::get_next(int& ndx, struct tracerec& t){ t.trec_time = trace_[ndx].trec_time; t.trec_size = trace_[ndx].trec_size; t.trec_type = trace_[ndx].trec_type; if (ndx++ == nrec_){ ndx = 0; a_= 1; }}int myTraceFile3::setup(){ tracerec* t; struct stat buf; int i; unsigned long size, type; double time; FILE *fp; if((fp = fopen(name_, "r")) == NULL) { printf("can't open file %s\n", name_); return -1; } nrec_ = 0; while (!feof(fp)){ fscanf(fp, "%lf%ld%ld", &time, &size, &type); //printf("%lf%\t%ld\t%ld\n", time, size, type); nrec_++; } nrec_=nrec_-2; printf("%d records\n", nrec_); rewind(fp); trace_ = new struct tracerec[nrec_]; for (i = 0, t = trace_; i < nrec_; i++, t++){ fscanf(fp, "%lf%ld%ld", &time, &size, &type); t->trec_time = time; t->trec_size = size; t->trec_type = type; } return 0;}void myTraceFile3::recv(Packet*, Handler*){ /* shouldn't get here */ abort();}/**************************************************************/static class myTrafficTrace3Class : public TclClass { public: myTrafficTrace3Class() : TclClass("Application/Traffic/myTrace3") {} TclObject* create(int, const char*const*) { return(new myTrafficTrace3()); }} class_my_traffictrace;myTrafficTrace3::myTrafficTrace3(){ tfile_ = (myTraceFile3 *)NULL;}void myTrafficTrace3::init(){ if (tfile_) ndx_ = tfile_->setup();}int myTrafficTrace3::command(int argc, const char*const* argv){ Tcl& tcl = Tcl::instance(); if (argc == 3) { if (strcmp(argv[1], "attach-tracefile") == 0) { tfile_ = (myTraceFile3 *)TclObject::lookup(argv[2]); if (tfile_ == 0) { tcl.resultf("no such node %s", argv[2]); return(TCL_ERROR); } return(TCL_OK); } } return (TrafficGenerator::command(argc, argv));}void myTrafficTrace3::timeout(){ if (! running_) return; if (tfile_->get_a()==1){ running_=0; return; } if(f_==1){ agent_->set_prio(10); }else if(f_==2){ agent_->set_prio(11); }else if(f_==3){ agent_->set_prio(12); }else { agent_->set_prio(f_); } agent_->set_pkttype(PT_VIDEO); agent_->set_frametype(f_); agent_->sendmsg(28+size_); /* figure out when to send the next one */ nextPkttime_ = next_interval(size_); //printf("nextPkttime_:%f, size_:%d\n", nextPkttime_, size_); /* schedule it */ timer_.resched(nextPkttime_);}double myTrafficTrace3::next_interval(int& size){ tfile_->get_next(ndx_, trec_); size = trec_.trec_size; f_ = trec_.trec_type; return(((double)trec_.trec_time)/1000000.0); /* usecs->secs */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -