http://mailman.isi.edu/pipermail/ns-users/2003-February/029983.html
Hi,
I want to broadcastCBR traffic(between ad hoc nodes) in ns through tcl
script,any one knowhow to do this? I need this traffic to evaluate the 802.11
mac broadcastmechanism.
I want one or twonodes broadcast CBR, and all other nodes recv the broadcast
packets.
Charle have evercreated a Agent in the following programs, but have
question,
does anyone can helpsolve it?
Thanks in advance.
Huang Xiaodong
To: <ns-users@ISI.EDU>
Subject: [ns] 2wireless nodes send broadcast packets
From: Charles Hsin<chsin@engin.umich.edu>
Date: Thu, 29 Nov2001 11:45:49 -0500 (EST)
Sender: owner-ns-users@ISI.EDU
------------------------------------------------------------------------------
--
Hi,
I create an agentwhich can send broadcast packets succesfully.
When I use only onewireless node sending broadcast packets, it works.
However, when I usetwo 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 bothnodes send broadcast packets.
I attach my codes.
Could anyone give ahand?
thanks,
Charles
#include"broadcast-base.h"
#include"random.h"
//
// bind C++ to TCL
//
inthdr_broadcastbase::offset_;
static classBroadcastbaseHeaderClass : public PacketHeaderClass {
public:
BroadcastbaseHeaderClass() :PacketHeaderClass("PacketHeader/Broadcastbase",
sizeof(hdr_broadcastbase)){
bind_offset(&(hdr_broadcastbase::offset_));
}
}class_broadcastbasehdr;
static classBroadcastbaseClass : 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()
voidSendTimer::expire(Event*)
{
t_->sendit();
}
// Constructor (alsoinitialize instances of timers)
BroadcastbaseAgent::BroadcastbaseAgent():
Agent(PT_BROADCASTBASE),snd_timer_(this)
{
bind("packetSize_", &size_);
//bind("off_broadcastbase_",&off_broadcastbase_);
}
// OTcl commandinterpreter
intBroadcastbaseAgent::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));
}
voidBroadcastbaseAgent::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 datapacket transmission time
double BroadcastbaseAgent::next_snd_time()
{
double next_time_ = 5;
next_time_ += 5 * Random::uniform(-0.5, 0.5);
return next_time_;
}
#ifndefns_broadcast_base_h
#definens_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;
//Basestationbroadcast beacon header structure
structhdr_broadcastbase {
nsaddr_t src; //Source IP Address
// Packet header access functions
static int offset_;
//inline static int& offset() {returnoffset_; }
inline static hdr_broadcastbase* access(constPacket* p) {
return (hdr_broadcastbase*)p->access(offset_);
}
};
classBroadcastbaseAgent;
// Sender uses thistimer to
// schedule next appdata packet transmission time
class SendTimer :public TimerHandler {
public:
SendTimer(BroadcastbaseAgent* t) :TimerHandler(), t_(t) {}
inline virtual void expire(Event*);
protected:
BroadcastbaseAgent* t_;
};
//Basestationbroadcast beacon agent class
classBroadcastbaseAgent : 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) 1997Regents of the University of California.
# All rightsreserved.
#
# Redistribution anduse in source and binary forms, with or without
# modification, arepermitted provided that the following conditions
# are met:
# 1. Redistributionsof source code must retain the above copyright
# notice, this list of conditions and thefollowing disclaimer.
# 2. Redistributionsin binary form must reproduce the above copyright
# notice, this list of conditions and thefollowing disclaimer in the
# documentation and/or other materialsprovided with the distribution.
# 3. All advertisingmaterials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developedby the Computer Systems
# Engineering Group at Lawrence BerkeleyLaboratory.
# 4. Neither the nameof the University nor of the Laboratory may be used
# to endorse or promote products derived fromthis software without
# specific prior written permission.
#
# THIS SOFTWARE ISPROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS ORIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIESOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# AREDISCLAIMED. IN NO EVENT SHALL THEREGENTS 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; LOSSOF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED ANDON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OFTHIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# simple-wireless.tcl
# A simple examplefor 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 ofmobilenodes
set val(rp) AODV ;# routing protocol
#======================================================================
# Main Program
#======================================================================
#
# Initialize GlobalVariables
#
set ns_ [new Simulator]
set tracefd [open base.tr w]
$ns_ use-newtrace
$ns_ trace-all$tracefd
# set up topographyobject
set topo [new Topography]
$topo load_flatgrid500 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) andnode(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) } {incri} {
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 somesimple node movements
# Node_(2) starts tomove towards node_(1)
#
$ns_ at 50.0"$node_(2) setdest 401.0 401.0 10.0"
# Node_(1) thenstarts to move away from node_(0)
#$ns_ at 100.0"$node_(1) setdest 490.0 480.0 15.0"
# Setup traffic flowbetween nodes
set b0 [newAgent/Broadcastbase]
$node_(0) attach $b0
set ll [$node_(0) setll_(0)]
$ns_ at 0.0 "$b0set-ll $ll"
$ns_ at 10.0"$b0 send"
set b1 [newAgent/Broadcastbase]
$node_(1) attach $b1
set ll2 [$node_(1)set ll_(0)]
$ns_ at 0.5 "$b1set-ll $ll2"
$ns_ at 10.5"$b1 send"
#
# Tell nodes when thesimulation 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 "StartingSimulation..."
$ns_ run