You are on page 1of 4

SimpleSimulationExample

ThissectionshowsasimpleNSsimulationscriptandexplainswhateachlinedoes.Example3isanOTcl
scriptthatcreatesthesimplenetworkconfigurationandrunsthesimulationscenarioinFigure4.Torunthis
simulation,download"nssimple.tcl"andtype"nsnssimple.tcl"atyourshellprompt.

Figure4.ASimpleNetworkTopologyandSimulationScenario
Thisnetworkconsistsof4nodes(n0,n1,n2,n3)asshowninabovefigure.Theduplexlinksbetweenn0and
n2,andn1andn2have2Mbpsofbandwidthand10msofdelay.Theduplexlinkbetweenn2andn3has1.7
Mbpsofbandwidthand20msofdelay.EachnodeusesaDropTailqueue,ofwhichthemaximumsizeis10.
A"tcp"agentisattachedton0,andaconnectionisestablishedtoatcp"sink"agentattachedton3.As
default,themaximumsizeofapacketthata"tcp"agentcangenerateis1KByte.Atcp"sink"agentgenerates
andsendsACKpacketstothesender(tcpagent)andfreesthereceivedpackets.A"udp"agentthatis
attachedton1isconnectedtoa"null"agentattachedton3.A"null"agentjustfreesthepacketsreceived.A
"ftp"anda"cbr"trafficgeneratorareattachedto"tcp"and"udp"agentsrespectively,andthe"cbr"is
configuredtogenerate1KBytepacketsattherateof1Mbps.The"cbr"issettostartat0.1secandstopat
4.5sec,and"ftp"issettostartat1.0secandstopat4.0sec.


Example3.ASimpleNSSimulationScript
Thefollowingistheexplanationofthescriptabove.Ingeneral,anNSscriptstartswithmakingaSimulator
objectinstance.
setns[newSimulator]:generatesanNSsimulatorobjectinstance,andassignsittovariablens(italics
isusedforvariablesandvaluesinthissection).Whatthislinedoesisthefollowing:

Initializethepacketformat(ignorethisfornow)
Createascheduler(defaultiscalendarscheduler)
Selectthedefaultaddressformat(ignorethisfornow)

The"Simulator"objecthasmemberfunctionsthatdothefollowing:

Createcompoundobjectssuchasnodesandlinks(describedlater)
Connectnetworkcomponentobjectscreated(ex.attachagent)
Setnetworkcomponentparameters(mostlyforcompoundobjects)
Createconnectionsbetweenagents(ex.makeconnectionbetweena"tcp"and"sink")
SpecifyNAMdisplayoptions
Etc.

Mostofmemberfunctionsareforsimulationsetup(referredtoasplumbingfunctionsintheOverview
section)andscheduling,howeversomeofthemarefortheNAMdisplay.The"Simulator"object
memberfunctionimplementationsarelocatedinthe"ns2/tcl/lib/nslib.tcl"file.

$nscolorfidcolor:istosetcolorofthepacketsforaflowspecifiedbytheflowid(fid).Thismember
functionof"Simulator"objectisfortheNAMdisplay,andhasnoeffectontheactualsimulation.

$nsnamtraceallfiledescriptor:Thismemberfunctiontellsthesimulatortorecordsimulationtraces
inNAMinputformat.Italsogivesthefilenamethatthetracewillbewrittentolaterbythecommand
$nsflushtrace.Similarly,thememberfunctiontraceallisforrecordingthesimulationtraceina
generalformat.

procfinish{}:iscalledafterthissimulationisoverbythecommand$nsat5.0"finish".Inthis
function,postsimulationprocessesarespecified.

setn0[$nsnode]:Thememberfunctionnodecreatesanode.AnodeinNSiscompoundobjectmade
ofaddressandportclassifiers(describedinalatersection).Userscancreateanodebyseparately
creatinganaddressandaportclassifierobjectsandconnectingthemtogether.However,thismember
functionofSimulatorobjectmakesthejobeasier.Toseehowanodeiscreated,lookatthefiles:"ns
2/tcl/libs/nslib.tcl"and"ns2/tcl/libs/nsnode.tcl".

$nsduplexlinknode1node2bandwidthdelayqueuetype:createstwosimplexlinksofspecified
bandwidthanddelay,andconnectsthetwospecifiednodes.InNS,theoutputqueueofanodeis
implementedasapartofalink,thereforeusersshouldspecifythequeuetypewhencreatinglinks.In
theabovesimulationscript,DropTailqueueisused.IfthereaderwantstouseaREDqueue,simply

replacethewordDropTailwithRED.TheNSimplementationofalinkisshowninalatersection.
Likeanode,alinkisacompoundobject,anduserscancreateitssubobjectsandconnectthemandthe
nodes.Linksourcecodescanbefoundin"ns2/tcl/libs/nslib.tcl"and"ns2/tcl/libs/nslink.tcl"files.
Onethingtonoteisthatyoucaninserterrormodulesinalinkcomponenttosimulatealossylink
(actuallyuserscanmakeandinsertanynetworkobjects).RefertotheNSdocumentationtofindout
howtodothis.

$nsqueuelimitnode1node2number:Thislinesetsthequeuelimitofthetwosimplexlinksthat
connectnode1andnode2tothenumberspecified.Atthispoint,theauthorsdonotknowhowmanyof
thesekindsofmemberfunctionsofSimulatorobjectsareavailableandwhattheyare.Pleasetakea
lookat"ns2/tcl/libs/nslib.tcl"and"ns2/tcl/libs/nslink.tcl",orNSdocumentationformore
information.

$nsduplexlinkopnode1node2...:ThenextcoupleoflinesareusedfortheNAMdisplay.Toseethe
effectsoftheselines,userscancommenttheselinesoutandtrythesimulation.
Nowthatthebasicnetworksetupisdone,thenextthingtodoistosetuptrafficagentssuchasTCPand
UDP,trafficsourcessuchasFTPandCBR,andattachthemtonodesandagentsrespectively.
settcp[newAgent/TCP]:ThislineshowshowtocreateaTCPagent.Butingeneral,userscancreate
anyagentortrafficsourcesinthisway.Agentsandtrafficsourcesareinfactbasicobjects(not
compoundobjects),mostlyimplementedinC++andlinkedtoOTcl.Therefore,therearenospecific
Simulatorobjectmemberfunctionsthatcreatetheseobjectinstances.Tocreateagentsortraffic
sources,ausershouldknowtheclassnamestheseobjects(Agent/TCP,Agnet/TCPSink,
Application/FTPandsoon).ThisinformationcanbefoundintheNSdocumentationorpartlyinthis
documentation.Butoneshortcutistolookatthe"ns2/tcl/libs/nsdefault.tcl"file.Thisfilecontainsthe
defaultconfigurableparametervaluesettingsforavailablenetworkobjects.Therefore,itworksasa
goodindicatorofwhatkindofnetworkobjectsareavailableinNSandwhataretheconfigurable
parameters.

$nsattachagentnodeagent:Theattachagentmemberfunctionattachesanagentobjectcreatedtoa
nodeobject.Actually,whatthisfunctiondoesiscalltheattachmemberfunctionofspecifiednode,
whichattachesthegivenagenttoitself.Therefore,ausercandothesamethingby,forexample,$n0
attach$tcp.Similarly,eachagentobjecthasamemberfunctionattachagentthatattachesatraffic
sourceobjecttoitself.

$nsconnectagent1agent2:Aftertwoagentsthatwillcommunicatewitheachotherarecreated,the
nextthingistoestablishalogicalnetworkconnectionbetweenthem.Thislineestablishesanetwork
connectionbysettingthedestinationaddresstoeachothers'networkandportaddresspair.
Assumingthatallthenetworkconfigurationisdone,thenextthingtodoiswriteasimulationscenario(i.e.
simulationscheduling).TheSimulatorobjecthasmanyschedulingmemberfunctions.However,theonethat
ismostlyusedisthefollowing:
$nsattime"string":ThismemberfunctionofaSimulatorobjectmakesthescheduler(scheduler_is
thevariablethatpointstheschedulerobjectcreatedby[newScheduler]commandatthebeginningof
thescript)toscheduletheexecutionofthespecifiedstringatgivensimulationtime.Forexample,$ns
at0.1"$cbrstart"willmaketheschedulercallastartmemberfunctionoftheCBRtrafficsource
object,whichstartstheCBRtotransmitdata.InNS,usuallyatrafficsourcedoesnottransmitactual
data,butitnotifiestheunderlyingagentthatithassomeamountofdatatotransmit,andtheagent,just
knowinghowmuchofthedatatotransfer,createspacketsandsendsthem.
Afterallnetworkconfiguration,schedulingandpostsimulationprocedurespecificationsaredone,theonly
thingleftistorunthesimulation.Thisisdoneby$nsrun.

You might also like