📄 sample4.tcl
字号:
## Copyright (c) 2007 Regents of the SIGNET lab, University of Padova.# 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. Neither the name of the University of Padova (SIGNET lab) nor the # names of its contributors may be used to endorse or promote products # derived from this software without specific prior written permission.## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.## ****** SAMPLE 4 *******# In this sample we planned to create two nodes with tree layers with one # module per layer and a PlugIn connected to NodeCore. Module called SendModule # has to send periodically a packet, which will be received by ReceiveModule # in node 2. SandBoxPlugin1 has to discover the SendModule in the architecture # (i.e., learn its internal address in order to communicate directly) by# means of discover cross layer message (SANDBOXPLG1MSG) sent a broadcast fashion.# After SendModule received this packet, it has to answer in order to # publicize its id in the architecture. Finally SandBoxPlugin1 may now# set the size of the packet thanks to SANDBOXPLG1MSGS cross layer message# sent to SendModule.source dynlibutils.tcl# load the necessary libraries, firstly the ns-Miracle librarydynlibload Miracle# then the packets trace librarydynlibload Trace# then the cross layer messages trace librarydynlibload ClTrace# and finally the Sandbox librarydynlibload MiracleSandbox ../src/.libs/# this is the procedure which defines all the intructions to be done when# the simulation is finishedproc finish {} { global ns opt puts "---> SIMULATION DONE." close $opt(dtfd) close $opt(cltfd) puts "Data Trace File: $opt(dtf) " puts "Cross-layer Trace File: $opt(cltf)"}# Initialization of ambient variables# period_ : is the period between each two packet transmissionsModule/SendModule set period_ 0.1# destPort_: is the destination port value to be set in the packet headerModule/SendModule set destPort_ 0# destAddr_: is the destination address value to be set in the packet headerModule/SendModule set destAddr_ 0# debug_ (for all the bottom definitions): are the intenal c++ Class variables used to # manage the sections of code to be processed for debugging purposesChannelTest set debug_ 0Module/SendModule set debug_ 0Module/MiddleModule set debug_ 0Module/ReceiveModule set debug_ 0SandboxPlugIn1 set debug_ 0ConnectorTrace/ChSAP set debug_ 0ConnectorTrace/Bin set debug_ 0set ns [new Simulator]# Command "use-Miracle" : initialize internal ns-Miracle library variables$ns use-Miracle# define the type of channel to use ("ChannelTest" is the one within the Sandbox library)set channel [new ChannelTest]# define tracefile namesset opt(dtf) "/tmp/data.tr"set opt(cltf) "/tmp/crosslayer.tr"# open trace files and set file descriptor variablesset opt(dtfd) [open $opt(dtf) w ]set opt(cltfd) [open $opt(cltf) w ]# Begin of the sequence of actions that allows to create a node, firstly you have to define # the structure you want to generate# "create-M_Node" : initialize internal node variables set node1 [$ns create-M_Node $opt(dtfd) $opt(cltfd)]# instantiates a new module SendModuleset sm1 [new Module/SendModule]# instantiates a new module MiddleModuleset mm1 [new Module/MiddleModule]# instantiates a new plug-in SandboxPlugIn1set plg1 [new SandboxPlugIn1]# Command "addModule" : adds the module referenced by "$sm1" (SendModule) in the protocol # stack in layer 2 with detph 3 in packet tracing and set "SEND1" tag in trace file.set i [$node1 addModule 2 $sm1 1 "SEND1"]# Command "addModule" : adds the module referenced by "$mm1" (MiddleModule) in the protocol # stack in layer 1 with detph 3 in packet tracing and set "MIDDLE1" tag in trace file.set j [$node1 addModule 1 $mm1 1 "MIDDLE1"]# Command "addplugin" : attaches the plug-in referenced by "$plg1" (SandboxPlugin) to the NodeCore# ans sets its depth value in tracing to 3 and set "PLGIN1" tag in trace file.set p1 [$node1 addPlugin $plg1 1 "PLGIN1"]# Command "setConnection": adds a SAP below level 2 between module refereced by "$i" and "$j", # where $i has to be the one in the above layer (2 in this case) and sets the depth of tracing# to level 1$node1 setConnection $sm1 $mm1 1# Command "addToChannel": attach the channel defined above ($channel) to the module(s) in layer 1# and set depth at level 2 in the ChSAP(s)$node1 addToChannel $channel $mm1 1puts "Node 1 is generated."# repeat the same operations for node 2set node2 [$ns create-M_Node $opt(dtfd) $opt(cltfd)]set rm2 [new Module/ReceiveModule]set mm2 [new Module/MiddleModule]set i [$node2 addModule 2 $rm2 0]set j [$node2 addModule 1 $mm2 0]$node2 setConnection $rm2 $mm2 1$node2 addToChannel $channel $mm2 1puts "Node 2 is generated."puts "---> BEGIN SIMULATION"# Next step is schedule the sequance of actions to be executed during the simulation# 1) send the "start" command to the SendModule, "$sm1", so it starts to generate and transmit packets# each period_ secs (see def above)$ns at 0 "$sm1 start"# 2) SandboxPlugin1 has to send SANDBOXPLG1MSG cross layer message in order to discover the # SendModule within the architecture and then it has to send to it the new packet size by # means of the SANDBOXPLG1MSGS cross layer message$ns at 1 "$plg1 discover"# 3) stop the packets generation$ns at 1.9 "$sm1 stop"# 4) finish the simulation (i.e., run the procedure define above in this script)$ns at 2 "finish; $ns halt"# Finally, run the simulation$ns run
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -