290 likes | 379 Vues
Learn how to implement applications in NS using classes like Application and TrafficGenerator. Attach agents, send and receive data, and manage traffic generators in NS simulations.
E N D
Applications and transport agent API 695430050 許庭瑋
Outline • Application in NS • Class Application • Attaching applications • Class TrafficGenerator • Simulated Applications
Application in NS • On top of transport agents • Two basic types • Traffic generators • Simulated applications
Class Application class Application : public TclObject { public: Application(); virtual void send(int nbytes); virtual void recv(int nbytes); virtual void resume(); protected: int command(int argc, const char*const* argv); virtual void start(); virtual void stop(); Agent *agent_; int enableRecv_; // call OTcl recv or not int enableResume_; // call OTcl resume or not }; *see trafgen.cc/.h
Attaching transport agent set src [new Agent/TCP/FullTcp] set sink [new Agent/TCP/FullTcp] $ns_ attach-agent $node_(s1) $src $ns_ attach-agent $node_(k1) $sink $ns_ connect $src $sink
Attaching applications • Use attach-agent function set ftp1 [new Application/FTP] $ftp1 attach-agent $src • Set agent_ • Call attachApp() [agent.cc] • An alternative Set ftp1 [$src attach-app FTP]
System calls to use transport agent • send(int nbytes) • send nbytes of data to peer • if nbytes==-1 , infinite send • sendmsg(int nbytes, const char* flags = 0) • Identical to send(int bytes) • Additional string flags • MSG_EOF
System calls to use transport agent • close() • Requests the agent to close the connection (only applicable for TCP) • listen() • Requests the agent to listen for new connections (only applicable for Full TCP) • set_pkttype(int pkttype) • This function sets the type_ variable in the agent to pkttype.
Agent upcalls to application • In NS, no actual data transfering between applications • Agents upcalls application to notify events • Incoming number of bytes of data • Two types of upcalls • recv(int nbytes) • resume()
Done() • All data has been transferred, and ACKed • Done does nothing by default • An example set myagent [new Agent/TCP/FullTcp] $myagent proc done ... code you want ...
Example for FTP set src [new Agent/TCP/FullTcp] set sink [new Agent/TCP/FullTcp] $ns_ attach-agent $node_(s1) $src $ns_ attach-agent $node_(k1) $sink $ns_ connect $src $sink # set up TCP-level connections $sink listen; $src set window_ 100 set ftp1 [new Application/FTP] $ftp1 attach-agent $src $ns_ at 0.0 "$ftp1 start"
Class TrafficGenerator class TrafficGenerator : public Application { public: TrafficGenerator(); virtual double next_interval(int &) = 0; virtual void init() {} virtual double interval() { return 0; } virtual int on() { return 0; } virtual void timeout(); virtual void recv() {} virtual void resume() {} protected: virtual void start(); virtual void stop(); double nextPkttime_; int size_; int running_; TrafficTimer timer_; };
Class TrafficGenerator • Four classes derived from TrafficGenerator • EXPOO_Traffic • Exponential on/off duration • Fixed rate,packet size • POO_Traffic • Pareto on/off distribution • CBR_Traffic • TrafficTrace • Generates traffic according to trace file
packetSize_ the constant size of the packets generated burst_time_ the average “on” time for the generator idle_time_ the average “off” time for the generator rate_ the sending rate during “on” times Next_packet_time=tr_time+idle_time Example: set e [new Application/Traffic/Exponential] $e set packetSize_ 210 $e set burst_time_ 500ms $e set idle_time_ 500ms $e set rate_ 100k Exponential On/Off
Pareto Distribution • α,β>0 • Shape in NS is β
packetSize_ the constant size of the packets generated burst_time_ the average "on" time for the generator idle_time_ the average "off" time for the generator rate_ the sending rate during "on" times shape_ the "shape" parameter used by the pareto distribution Example: set p [new Application/Traffic/Pareto] $p set packetSize_ 210 $p set burst_time_ 500ms $p set idle_time_ 500ms $p set rate_ 200k $p set shape_ 1.5 Pareto On/Off
rate_ the sending rate interval_ (Optional) interval between packets packetSize_ the constant size of the packets generated random_ flag indicating whether or not to introduce random “noise” in the scheduled departure times (default isoff) maxpkts_ the maximum number of packets to send (default is (228) Example: set e [new Application/Traffic/CBR] $e set packetSize_ 48 $e set rate_ 64Kb $e set random_ 1 CBR
Enable multiple Traffic/Trace associated with one trace file 2 32-bit fields in trace file Next packet generates time (ms) Length of next packet (bytes) Example: set tfile [new Tracefile] $tfile filename example-trace set t1 [new Application/Traffic/Trace] $t1 attach-tracefile $tfile set t2 [new Application/Traffic/Trace] $t2 attach-tracefile $tfile Traffic Trace
Random variable in NS • Pareto Distribution • Constant Distribution • Uniform Distribution • Exponential Distribution • HyperExponential Distribution
Pareto Distribution • # Pareto 分佈,柏拉圖分佈set r1 [new RandomVariable/Pareto] $r1 use-rng $rng $r1 set avg_ 10.0 $r1 set shape_ 1.2 for {set i 1} {$i<=3} {incr i} { puts [$r1 value] }
Constant Distribution • set r2 [new RandomVariable/Constant] $r2 use-rng $rng $r2 set avg_ 5 for {set i 1} {$i<=3} {incr i} { puts [$r2 value] }
Uniform Distribution • Set min & max • set r3 [new RandomVariable/Uniform] $r3 use-rng $rng $r3 set min_ 0.0 $r3 set max_ 10.0 for {set i 1} {$i<=3} {incr i} { puts [$r3 value] }
Exponential Distribution • set r4 [new RandomVariable/Exponential] $r4 use-rng $rng $r4 set avg_ 5.0 for {set i 1} {$i<=3} {incr i} { puts [$r4 value] }
Example-Exponential traffic set src [new Agent/UDP] set sink [new Agent/UDP] $ns_ attach-agent $node_(s1) $src $ns_ attach-agent $node_(k1) $sink $ns_ connect $src $sink set e [new Application/Traffic/Exponential] $e attach-agent $src $e set packetSize_ 210 $e set burst_time_ 500ms $e set idle_time_ 500ms $e set rate_ 100k $ns_ at 0.0 "$e start"
Simulated applications • Two simulated applications : FTP & Telnet • FTP methods: • attach-agent • start • stop • produce n • producemore n • send n
Simulated applications • Telnet • Packet inter-packet time: • Chose from exponential distribution , with average=interval_ ; (if interval not zero) • Chose according tcplib distribution (see ns-2.28\tcp\tcplib-telnet.cc)
CBR + FTP Node 1 CBR Node 2 FTP Example