分享

Broadcasting packets through tcl script

 幸福的乐土 2013-01-31
Huang Xiaodong Huang Xiaodong" <xdhuang_ns@eyou.com 
Wed Feb 19 05:15:16 2003


Hi,

I want to broadcast CBR traffic(between ad hoc nodes) in ns through tcl
script,any one know how to do this? I need this traffic to evaluate the 802.11
mac broadcast mechanism.

I want one or two nodes broadcast CBR, and all other nodes recv the broadcast
packets.

Charle have ever created a Agent in the following programs, but have
question,
does anyone can help solve it?

Thanks in advance.
Huang Xiaodong

To: <ns-users@ISI.EDU> 
Subject: [ns] 2 wireless nodes send broadcast packets 
From: Charles Hsin <chsin@engin.umich.edu> 
Date: Thu, 29 Nov 2001 11:45:49 -0500 (EST) 
Sender: owner-ns-users@ISI.EDU 

------------------------------------------------------------------------------
--

Hi,

I create an agent which can send broadcast packets succesfully.
When I use only one wireless node sending broadcast packets, it works.
However, when I use two wireless nodes attached with two such agents, only
the last created node (or the node last attached with the agent) sends
broadcast packets.
I cannot make both nodes send broadcast packets.
I attach my codes.

Could anyone give a hand?

thanks,
Charles

#include "broadcast-base.h"
#include "random.h"

//
// bind C++ to TCL
//
int hdr_broadcastbase::offset_;

static class BroadcastbaseHeaderClass : public PacketHeaderClass {
 public:
  BroadcastbaseHeaderClass() : PacketHeaderClass("PacketHeader/Broadcastbase",
sizeof(hdr_broadcastbase)) {
    bind_offset(&(hdr_broadcastbase::offset_));
  }
} class_broadcastbasehdr;

static class BroadcastbaseClass : public TclClass {
 public:
  BroadcastbaseClass() : TclClass("Agent/Broadcastbase"){}
  TclObject* create(int, const char*const*) {
    return (new BroadcastbaseAgent() );
  }
} class_broadbastbase;
// end of binding

// When snd_timer_ expires call BroadcastbaseAgent:sendit()
void SendTimer::expire(Event*)
{
  t_->sendit();
}

// Constructor (also initialize instances of timers)
BroadcastbaseAgent::BroadcastbaseAgent() :
Agent(PT_BROADCASTBASE),snd_timer_(this)
{
  bind("packetSize_", &size_);
  //bind("off_broadcastbase_", &off_broadcastbase_);
}  

// OTcl command interpreter
int BroadcastbaseAgent::command(int argc, const char*const* argv)
{
  if (argc == 3) {
    if (strcmp(argv[1], "set-ll")==0) {
         if( (obj = TclObject::lookup(argv[2])) == 0) {
        	fprintf(stderr, " Broadcastbase(set-ll): %s lookup of %s failed \n",
argv[1],argv[2]);
           return(TCL_ERROR);
         }
      ll = (NsObject *) obj;
      return (TCL_OK);
    }
  }
  if (argc == 2) {
    if (strcmp(argv[1], "send")==0) {
      sendit();
      return (TCL_OK);
    }
  }
  return (Agent::command(argc, argv));
}

void BroadcastbaseAgent::sendit()
{
  Packet *p = Packet::alloc();
  struct hdr_cmn *ch = HDR_CMN(p);
  struct hdr_ip *ih = HDR_IP(p);
 
  ch->ptype() = PT_BROADCASTBASE;
  ch->next_hop_ = IP_BROADCAST;

  ih->saddr() = Agent::addr();
  ih->daddr() = IP_BROADCAST;
  ih->sport() = RT_PORT;
  ih->dport() = RT_PORT;
  ih->ttl_ = 1;
 
  Scheduler::instance().schedule(ll,p,0.0); 
 
  // Reschedule the send_pkt timer
  double next_time_ = next_snd_time();
  if(next_time_ > 0) snd_timer_.resched(next_time_);
}

// Schedule next data packet transmission time
double BroadcastbaseAgent::next_snd_time()
{    
  double next_time_ = 5;  
  next_time_ += 5 * Random::uniform(-0.5, 0.5);
  return next_time_;
}









#ifndef ns_broadcast_base_h
#define ns_broadcast_base_h

#include "agent.h"
#include "tclcl.h"
#include "packet.h"
#include "address.h"
#include "ip.h"
#include "timer-handler.h"

NsObject *ll;
TclObject *obj;

//Basestation broadcast beacon header structure
struct hdr_broadcastbase {
  nsaddr_t  src;  //Source IP Address
  
  // Packet header access functions
  static int offset_;
  //inline static int& offset() {return offset_; }
  inline static hdr_broadcastbase* access(const Packet* p) {
    return (hdr_broadcastbase*) p->access(offset_);
  }
};

class BroadcastbaseAgent;

// Sender uses this timer to 
// schedule next app data packet transmission time
class SendTimer : public TimerHandler {
 public:
        SendTimer(BroadcastbaseAgent* t) : TimerHandler(), t_(t) {}
        inline virtual void expire(Event*);
 protected:
        BroadcastbaseAgent* t_;
};

//Basestation broadcast beacon agent class
class BroadcastbaseAgent : public Agent {
 friend class SendTimer;
 public:
  BroadcastbaseAgent();
  int command(int argc, const char*const* argv);
  //protected:
  //int off_broadcastbase_;
 private:
  void sendit();
  inline double next_snd_time();
  
  SendTimer snd_timer_;  // SendTimer
};

#endif  








# Copyright (c) 1997 Regents of the University of California.

# All rights reserved.

#

# Redistribution and use in source and binary forms, with or without

# modification, are permitted provided that the following conditions

# are met:

# 1. Redistributions of source code must retain the above copyright

#    notice, this list of conditions and the following disclaimer.

# 2. Redistributions in binary form must reproduce the above copyright

#    notice, this list of conditions and the following disclaimer in the

#    documentation and/or other materials provided with the distribution.

# 3. All advertising materials mentioning features or use of this software

#    must display the following acknowledgement:

#      This product includes software developed by the Computer Systems

#      Engineering Group at Lawrence Berkeley Laboratory.

# 4. Neither the name of the University nor of the Laboratory may be used

#    to endorse or promote products derived from this software without

#    specific prior written permission.

#

# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND

# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE

# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE

# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL

# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS

# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)

# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT

# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY

# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF

# SUCH DAMAGE.

#

# simple-wireless.tcl

# A simple example for wireless simulation



# ======================================================================

# Define options

# ======================================================================

set val(chan)           Channel/WirelessChannel    ;# channel type

set val(prop)           Propagation/TwoRayGround   ;# radio-propagation
model

set val(netif)          Phy/WirelessPhy            ;# network interface type

set val(mac)            Mac/802_11                 ;# MAC type

set val(ifq)            Queue/DropTail/PriQueue    ;# interface queue type

set val(ll)             LL                         ;# link layer type

set val(ant)            Antenna/OmniAntenna        ;# antenna model

set val(ifqlen)         50                         ;# max packet in ifq

set val(nn)             6                          ;# number of mobilenodes

set val(rp)             AODV                       ;# routing protocol



# ======================================================================

# Main Program

# ======================================================================





#

# Initialize Global Variables

#

set ns_		[new Simulator]

set tracefd     [open base.tr w]

$ns_ use-newtrace

$ns_ trace-all $tracefd



# set up topography object

set topo       [new Topography]



$topo load_flatgrid 500 500



#

# Create God

#

create-god $val(nn)



#

#  Create the specified number of mobilenodes [$val(nn)] and "attach" them

#  to the channel. 

#  Here two nodes are created : node(0) and node(1)



# configure node



        $ns_ node-config -adhocRouting $val(rp) 
			 -llType $val(ll) 
			 -macType $val(mac) 
			 -ifqType $val(ifq) 
			 -ifqLen $val(ifqlen) 
			 -antType $val(ant) 
			 -propType $val(prop) 
			 -phyType $val(netif) 
			 -channelType $val(chan) 
			 -topoInstance $topo 
			 -agentTrace ON 
			 -routerTrace ON 
			 -macTrace ON 
			 -movementTrace OFF			

			 

	for {set i 0} {$i < $val(nn) } {incr i} {

		set node_($i) [$ns_ node]	

		$node_($i) random-motion 0   ;# disable random motion

	}



#

# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes

#

$node_(0) set X_ 2.0

$node_(0) set Y_ 2.0

$node_(0) set Z_ 0.0



$node_(1) set X_ 300.0

$node_(1) set Y_ 300.0

$node_(1) set Z_ 0.0



$node_(2) set X_ 4.0

$node_(2) set Y_ 4.0

$node_(2) set Z_ 0.0



$node_(3) set X_ 5.0

$node_(3) set Y_ 5.0

$node_(3) set Z_ 0.0



$node_(4) set X_ 400.0

$node_(4) set Y_ 400.0

$node_(4) set Z_ 0.0



$node_(5) set X_ 150.0

$node_(5) set Y_ 150.0

$node_(5) set Z_ 0.0



#

# Now produce some simple node movements

# Node_(2) starts to move towards node_(1)

#

$ns_ at 50.0 "$node_(2) setdest 401.0 401.0 10.0"



# Node_(1) then starts to move away from node_(0)

#$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0" 



# Setup traffic flow between nodes



set b0 [new Agent/Broadcastbase]

$node_(0) attach $b0

set ll [$node_(0) set ll_(0)]

$ns_ at 0.0 "$b0 set-ll $ll"

$ns_ at 10.0 "$b0 send" 



set b1 [new Agent/Broadcastbase]

$node_(1) attach $b1

set ll2 [$node_(1) set ll_(0)]

$ns_ at 0.5 "$b1 set-ll $ll2"

$ns_ at 10.5 "$b1 send"



#

# Tell nodes when the simulation ends

#

for {set i 0} {$i < $val(nn) } {incr i} {

    $ns_ at 150.0 "$node_($i) reset";

}

$ns_ at 150.0 "stop"

$ns_ at 150.01 "puts \"NS EXITING...\" ; $ns_ halt"

proc stop {} {

    global ns_ tracefd

    $ns_ flush-trace

    close $tracefd

}



puts "Starting Simulation..."

$ns_ run

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多