📄 ns-lib.tcl
字号:
## Copyright (c) 1996 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 MASH Research# Group at the University of California Berkeley.# 4. Neither the name of the University nor of the Research Group 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.## @(#) $Header: /usr/src/mash/repository/vint/ns-2/tcl/lib/ns-lib.tcl,v 1.186 2000/01/24 17:58:19 klan Exp $### Word of warning to developers:# this code (and all it sources) is compiled into the# ns executable. You need to rebuild ns or explicitly# source this code to see changes take effect.#proc warn {msg} { global warned_ if {![info exists warned_($msg)]} { puts stderr "warning: $msg" set warned_($msg) 1 }}if {[info commands debug] == ""} { proc debug args { warn {Script debugging disabled. Reconfigure with --with-tcldebug, and recompile.} }}proc assert args { if [catch "expr $args" ret] { set ret [eval $args] } if {! $ret} { error "assertion failed: $args" }}proc find-max list { set max 0 foreach val $list { if {$val > $max} { set max $val } } return $max}## Create the core OTcl class called "Simulator".# This is the principal interface to the simulation engine.#Class Simulatorsource ns-autoconf.tclsource ns-address.tclsource ns-node.tclsource ns-hiernode.tclsource ns-mobilenode.tclsource ns-bsnode.tclsource ns-link.tclsource ns-source.tclsource ns-compat.tclsource ns-nam.tclsource ns-packet.tclsource ns-queue.tclsource ns-trace.tclsource ns-random.tclsource ns-agent.tclsource ns-route.tclsource ns-errmodel.tclsource ns-intserv.tclsource ns-cmutrace.tclsource ns-mip.tclsource ns-sat.tcl#source ns-wireless-mip.tclsource ../rtp/session-rtp.tclsource ../interface/ns-iface.tclsource ../lan/ns-mac.tclsource ../lan/ns-ll.tclsource ../lan/vlan.tclsource ../mcast/timer.tclsource ../mcast/ns-mcast.tclsource ../mcast/McastProto.tclsource ../mcast/DM.tclsource ../ctr-mcast/CtrMcast.tclsource ../ctr-mcast/CtrMcastComp.tclsource ../ctr-mcast/CtrRPComp.tclsource ../mcast/BST.tclsource ../mcast/srm.tclsource ../mcast/srm-ssm.tclsource ../mcast/mftp_snd.tclsource ../mcast/mftp_rcv.tclsource ../mcast/mftp_rcv_stat.tclsource ../mcast/McastMonitor.tclsource ../rlm/rlm.tclsource ../rlm/rlm-ns.tclsource ../session/session.tclsource ../webcache/http-server.tclsource ../webcache/http-cache.tclsource ../webcache/http-agent.tclsource ../webcache/http-mcache.tclsource ../webcache/webtraf.tclsource ns-namsupp.tcl# JCWsource ../mobility/noah.tclsource ../mobility/dsdv.tclsource ../mobility/dsr.tclsource ../mobility/com.tclsource ns-default.tclsource ../emulate/ns-emulate.tclSimulator instproc init args { $self create_packetformat #the calendar scheduler doesn't work on big mobile network runs #it dies around 240 secs... #$self use-scheduler List $self use-scheduler Calendar $self set nullAgent_ [new Agent/Null] $self set-address-format def eval $self next $args}Simulator instproc nullagent {} { $self instvar nullAgent_ return $nullAgent_}Simulator instproc use-scheduler type { $self instvar scheduler_ if [info exists scheduler_] { if { [$scheduler_ info class] == "Scheduler/$type" } { return } else { delete $scheduler_ } } set scheduler_ [new Scheduler/$type] $scheduler_ now}## A simple method to wrap any object around# a trace object that dumps to stdout#Simulator instproc dumper obj { set t [$self alloc-trace hop stdout] $t target $obj return $t}# New node structure# Define global node configuration# $ns_ node-config -addressType flat/hierarchical# -adhocRouting DSDV/DSR/TORA/NOAH# -llType# -macType# -propType# -ifqType# -ifqLen# -phyType# -antType# -channelType# -topologyInstance# -wiredRouting ON/OFF# -mobileIP ON/OFF# -energyModel "EnergyModel"# -initialEnergy (in Joules)# -rxPower (in W)# -txPower (in W)# -agentTrace ON# -routerTrace ON # -macTrace OFF # -toraDebug OFF # -movementTrace OFFSimulator set routingAgent_ ""Simulator set addressType_ ""#Simulator set llType_ ""#Simulator set macType_ ""#Simulator set propType_ ""#Simulator set ifqType_ ""#Simulator set phyType_ ""#Simulator set antType_ ""#Simulator set ifqlen_ ""Simulator set nodefactory_ NodeSimulator set MovementTrace OFFSimulator instproc addressType {val} { $self set addressType_ $val }Simulator instproc adhocRouting {val} { $self set routingAgent_ $val }Simulator instproc llType {val} { $self set llType_ $val }Simulator instproc macType {val} { $self set macType_ $val }Simulator instproc propType {val} { $self set propType_ $val }Simulator instproc ifqType {val} { $self set ifqType_ $val }Simulator instproc ifqLen {val} { $self set ifqlen_ $val }Simulator instproc phyType {val} { $self set phyType_ $val }Simulator instproc antType {val} { $self set antType_ $val }Simulator instproc channelType {val} {$self set channelType_ $val}Simulator instproc topoInstance {val} {$self set topoInstance_ $val}Simulator instproc wiredRouting {val} {$self set wiredRouting_ $val}Simulator instproc mobileIP {val} {$self set mobileIP_ $val}Simulator instproc energyModel {val} { $self set energyModel_ $val }Simulator instproc initialEnergy {val} { $self set initialEnergy_ $val }Simulator instproc txPower {val} { $self set txPower_ $val }Simulator instproc rxPower {val} { $self set rxPower_ $val }Simulator instproc agentTrace {val} { $self set agentTrace_ $val }Simulator instproc routerTrace {val} { $self set routerTrace_ $val }Simulator instproc macTrace {val} { $self set macTrace_ $val }Simulator instproc movementTrace {val} { $self set movementTrace_ $val }Simulator instproc toraDebug {val} {$self set toraDebug_ $val }Simulator instproc get-nodetype {} { $self instvar addressType_ routingAgent_ wiredRouting_ set val "" if { [info exists addressType_] && $addressType_ == "hierarchical" } { set val Hier } if { [info exists routingAgent_] && $routingAgent_ != "" } { set val Mobile } if { [info exists wiredRouting_] && $wiredRouting_ == "ON" } { set val Base } if { [info exists wiredRouting_] && $wiredRouting_ == "OFF"} { set val Base } if { [Simulator set mobile_ip_] } { if { $val == "Base" && $wiredRouting_ == "ON" } { set val MIPBS } if { $val == "Base" && $wiredRouting_ == "OFF" } { set val MIPMH } } return $val}Simulator instproc node-config args { set args [eval $self init-vars $args] $self instvar addressType_ routingAgent_ nodefactory_ propType_ $self instvar macTrace_ routerTrace_ agentTrace_ movementTrace_ $self instvar channelType_ topoInstance_ propInstance_ chan mobileIP_ $self instvar rxPower_ txPower_ if [info exists macTrace_] { Simulator set MacTrace_ $macTrace_ } if [info exists routerTrace_] { Simulator set RouterTrace_ $routerTrace_ } if [info exists agentTrace_] { Simulator set AgentTrace_ $agentTrace_ } if [info exists movementTrace_] { Simulator set MovementTrace_ $movementTrace_ } # hacking for matching old cmu add-interface # not good style, for back-compability ONLY # Only create 1 instance of prop if {[info exists propType_] && ![info exists propInstance_]} { set propInstance_ [new $propType_] } if {[info exists channelType_] && ![info exists chan]} { set chan [new $channelType_] } if [info exists topoInstance_] { $propType_ topography $topoInstance_ }# set address type, hierarchical or expanded if {[string compare $addressType_ ""] != 0} { $self set-address-format $addressType_ }# set mobileIP flag if { [info exists mobileIP_] && $mobileIP_ == "ON"} { Simulator set mobile_ip_ 1 } else { if { [info exists mobileIP_] } { Simulator set mobile_ip_ 0 } }}# Default behavior is changed: consider nam as not initialized if # no shape OR color parameter is givenSimulator instproc node args { $self instvar Node_ routingAgent_ wiredRouting_ if { [Simulator info vars EnableMcast_] != "" } { warn "Flag variable Simulator::EnableMcast_ discontinued.\n\t\ Use multicast methods as:\n\t\t\ % set ns \[new Simulator -multicast on]\n\t\t\ % \$ns multicast" $self multicast Simulator unset EnableMcast_ } if { [Simulator info vars NumberInterfaces_] != "" } { warn "Flag variable Simulator::NumberInterfaces_ discontinued.\n\t\ Setting (or not) this variable will not affect the simulations." Simulator unset NumberInterfaces_ } # wireless-ready node if [info exists routingAgent_] { if {[string compare $routingAgent_ ""] != 0} { set node [$self create-wireless-node $args] # for base node # JCW #if {[info exists wiredRouting_] && $wiredRouting_ == "ON"} { set Node_([$node id]) $node #} return $node } } set node [new [Simulator set node_factory_] $args] set Node_([$node id]) $node $node set ns_ $self if [$self multicast?] { $node enable-mcast $self } $self check-node-num return $node}Simulator instproc imep-support {} { $self instvar imepflag_ if [info exists imepflag_] { return $imepflag_ } return ""}Simulator instproc create-wireless-node { args } { $self instvar routingAgent_ wiredRouting_ $self instvar propInstance_ llType_ macType_ ifqType_ ifqlen_ phyType_ chan $self instvar antType_ energyModel_ initialEnergy_ txPower_ rxPower_ $self instvar imepflag_ topoInstance_ $self instvar namtraceAllFile_ $self instvar level1_ level2_ set imepflag_ OFF # create node instance set node [$self create-node-instance $args] if { [info exist namtraceAllFile_] } { $node get-nam-traceall $namtraceAllFile_ } # basestation address setting if { [info exist wiredRouting_] && $wiredRouting_ == "ON" } { $node base-station [AddrParams set-hieraddr [$node node-addr]] } $node nodeid [$node id] switch -exact $routingAgent_ { DSDV { set ragent [$self create-dsdv-agent $node] } DSR { $self at 0.0 "$node start-dsr" } AODV { set ragent [$self create-aodv-agent $node] } TORA { set imepflag_ ON set ragent [$self create-tora-agent $node] } # JCW NOAH { set ragent [$self create-noah-agent $node] } default { puts "Wrong node routing agent!" exit } } # add main node interface $node add-interface $chan $propInstance_ $llType_ $macType_ \ $ifqType_ $ifqlen_ $phyType_ $antType_ # attach agent if {$routingAgent_ != "DSR"} { $node attach $ragent 255 } # bind routing agent and mip agent if existing # basestation address setting if { [info exist wiredRouting_] && $wiredRouting_ == "ON" } { if { $routingAgent_ != "DSR" } { $node mip-call $ragent } } # # This Trace Target is used to log changes in direction # and velocity for the mobile node. # set tracefd [$self get-ns-traceall] if {$tracefd != "" } { $node nodetrace $tracefd $node agenttrace $tracefd } set namtracefd [$self get-nam-traceall] if {$namtracefd != "" } { $node namattach $namtracefd } if [info exists energyModel_] { if [info exists level1_] { set l1 $level1_ } else { set l1 0.5 } if [info exists level2_] { set l2 $level2_ } else { set l2 0.2 } $node addenergymodel [new $energyModel_ $initialEnergy_ $l1 $l2] } if [info exists txPower_] { $node setPt $txPower_ } if [info exists rxPower_] { $node setPr $rxPower_ } $node topography $topoInstance_ return $node}#Class BaseNode -superclass {HierNode Node/MobileNode}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -