如何在NS2中产生和使用Poisson Traffic

来源:互联网 发布:运营的数据分析 编辑:程序博客网 时间:2024/06/18 06:58

转载地址:http://140.116.164.80/~smallko/ns2/poisson_ch.htm

1.請把底下的程式碼拷貝到一個新的檔案(poisson.cc),並且把poisson.cc放在~//tools的目錄下

(若是在make時有問題,請自己用手輸入底下的程式碼)

 

#include <stdlib.h>

 

#include "random.h"

#include "trafgen.h"

#include "ranvar.h"

 

class Poisson_Traffic : public TrafficGenerator {

 public:

        Poisson_Traffic();

        virtual void timeout();

        virtual double next_interval(int&);

 

 protected:

       virtual void start();

        void init();

        double rate_;     /* Mean sending rate (b/s) */

        double interval_; /* Mean time between each packet generation (sec) */

        int seqno_;       /* Each generated packet has a unique sequence number */

        int maxpkts_;     /* No source can generate more than maxpkts_ packets */

};

 

static class PoissonTrafficClass : public TclClass {

 public:

        PoissonTrafficClass() : TclClass("Application/Traffic/Poisson") {}

        TclObject* create(int, const char*const*) {

                return (new Poisson_Traffic());

        }

} class_poisson_traffic;

 

Poisson_Traffic::Poisson_Traffic() : seqno_(0)

{

        bind_bw("rate_", &rate_);

        bind("interval_", &interval_);

        bind("packetSize_", &size_);

        bind("maxpkts_", &maxpkts_);

}

 

void Poisson_Traffic::init()

{

  /*

   * If the user did not specify a mean packet inter-generation time,

   * then calculate it based on the rate_ and the packetSize_

   */

  if (interval_ < 0.0)

    interval_ = (double)(size_ << 3) / (double)rate_;

 

  /*

   * Assign unique packet type ID to each packet sent by a Poisson

   * source.

   */

  if (agent_)

    agent_->set_pkttype(PT_POISSON);

}

 

void Poisson_Traffic::start()

{

  init();

  running_ = 1;

  timeout(); 

}

 

 

double Poisson_Traffic::next_interval(int& size)

{

   size = size_;

   if (++seqno_ < maxpkts_)

     return(Random::exponential(interval_));

   else

     return(-1);

}

 

void Poisson_Traffic::timeout()

{

        if(! running_)

                return;

        agent_->sendmsg(size_);

        nextPkttime_ = next_interval(size_);

        timer_.resched(nextPkttime_);

}

 

2.修改~/tcl/lib/ns-default.tcl

請加入底下的程式碼

Application/Traffic/Poisson set interval_ -1.0

Application/Traffic/Poisson set rate_ 1Mb

Application/Traffic/Poisson set packetSize_ 500

Application/Traffic/Poisson set maxpkts_ 268435456

 

3.修改~/trace/cmu-trace.cc

………………………..

case PT_GAF:

case PT_PING:

                        break;

case PT_POISSON:

                        break;

………………………

 

4.修改~/trace/trace.cc

……………………..

/* UDP's now have seqno's too */

        if (t == PT_RTP || t == PT_CBR || t == PT_UDP || t == PT_EXP ||

            t == PT_PARETO || t == PT_POISSON)

                seqno = rh->seqno();

……………………..

 

5.修改packet.h

……………………………

PT_FTP,

        PT_PARETO,

        PT_EXP,

        PT_POISSON,

        PT_INVAL,

 

……………………………

name_[PT_EXP]= "exp";

        name_[PT_POISSON]= "poisson";

………………………………..

 

6.修改Makefile

………………………………………

tools/expoo.o tools/cbr_traffic.o tools/poisson.o \

………………………………………

7.重新編譯

make clean; make

 

 

測試的tcl script

#Create a simulator object

set ns [new Simulator]

 

#Define different colors for data flows

$ns color 1 Blue

$ns color 2 Red

 

#Open the nam trace file

set nf [open out.nam w]

$ns namtrace-all $nf

 

set nd [open out.tr w]

$ns trace-all $nd

 

#Define a 'finish' procedure

proc finish {} {

        global ns nf nd

        $ns flush-trace

        #Close the trace file

        close $nf

        close $nd

        #Execute nam on the trace file

        #exec nam out.nam &

        exit 0

}

 

#Create four nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

 

#Create links between the nodes

$ns duplex-link $n0 $n2 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n3 $n2 1Mb 10ms SFQ

 

$ns duplex-link-op $n0 $n2 orient right-down

$ns duplex-link-op $n1 $n2 orient right-up

$ns duplex-link-op $n2 $n3 orient right

 

#Monitor the queue for the link between node 2 and node 3

$ns duplex-link-op $n2 $n3 queuePos 0.5

 

#Create a UDP agent and attach it to node n0

set udp0 [new Agent/UDP]

$udp0 set packetSize_ 1500

$udp0 set class_ 1

$ns attach-agent $n0 $udp0

 

# Create a Poisson traffic source and attach it to udp0

set Poi0 [new Application/Traffic/Poisson]

$Poi0 set packetSize_ 1500

$Poi0 set rate_ 1Mb

$Poi0 attach-agent $udp0

 

#Create a UDP agent and attach it to node n1

set udp1 [new Agent/UDP]

$udp1 set class_ 2

$ns attach-agent $n1 $udp1

 

# Create a CBR traffic source and attach it to udp1

set cbr1 [new Application/Traffic/CBR]

$cbr1 set packetSize_ 500

$cbr1 set interval_ 0.005

$cbr1 attach-agent $udp1

 

#Create a Null agent (a traffic sink) and attach it to node n3

set null0 [new Agent/Null]

$ns attach-agent $n3 $null0

 

#Connect the traffic sources with the traffic sink

$ns connect $udp0 $null0 

$ns connect $udp1 $null0

 

#Schedule events for the CBR agents

$ns at 0.5 "$Poi0 start"

$ns at 1.0 "$cbr1 start"

$ns at 4.0 "$cbr1 stop"

$ns at 4.5 "$Poi0 stop"

#Call the finish procedure after 5 seconds of simulation time

$ns at 5.0 "finish"

 

#Run the simulation

$ns run

 

參考資料

1.      http://www.cs.stonybrook.edu/%7Ekostas/src/poisson/install-poisson-ns-2.27.html

原创粉丝点击