2011.11.07 开始正式研究 nam 格式及源码。经验总结如下:
1. Nam 下的 fprintf 是可以打印信息的,但是有时候打印到指定文件中的字符不
知道是哪种编码的,打不开。
2. Nam 中文手册中提到的 label-color 和 label-at 是可以用的,但是要注意位
置,可以按下图所示应用。
set node(0) [$ns node]
$node(0) random-motion 0
if { $val(protocol) == "PRIME" } {
Mac/PrimeMac set isPAN 0
}
if { $val(protocol) == "G3" } {
Mac/G3Mac set isPAN 0
}
for {set i 1} {$i < $val(node) } {incr i} {
set node($i) [$ns node]
$node($i) random-motion 0
}
$node(1) add-mark "mark1" "red" "square"
$ns at 1 "$node(1) delete-mark mark1"
$ns at 100 "$node(1) label liuyi"
$ns at 100 "$node(1) label-color gold"
$ns at 100 "$node(1) label-at EAST"
否则会导致节点 1 的位置发生变化。
3. Node 的形状可以在 ns2.34\tcl\lib\ns-lib.tcl 中的 initial_node_pos 函数中修
改 -v 参数。
4. 第 2 条中提到的 label-color 和 label-at 节点位置移动的方法实际解决不了问
题。但是可以在调用这两个 proc 的时候传递进去位置参数。它们两个在
ns2.34\tcl\lib\ns-namsupp.tcl 中。调用的时候这样:
void PrimeNam::labelInfo(double atTime, double x, double y, double z, const char
*name, const char *color, const char *location)
{
Tcl& tcl = Tcl::instance();
tcl.evalf("$node(%d) label \"%s\"", mac->index_, name);
tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {n
//
-t %.9f -s %d -x %f -y %f -z %f -S DCOLOR -e %s}}",
//
atTime,atTime,mac->index_,x, y, z, color);
tcl.evalf("$node(%d) label-color \"%f\" \"%f\" \"%f\" \"%s\"",mac->index_, x, y,
tcl.evalf("$node(%d) label-at \"%f\" \"%f\" \"%f\" \"%s\"", mac->index_, x, y, z,
z, color);
location);
}
同时也要修改那两个 proc 如下:
Node instproc label-color {x y z str} {
$self instvar attr_ id_
set ns [Simulator instance]
if [info exists attr_(DCOLOR)] {
$ns puts-nam-config "n -t [$ns now] -s $id_ -S DCOLOR
-e \"$str\" -E $attr_(DCOLOR)"
} else {
DCOLOR -e \"$str\" -E \"\""
}
set attr_(DCOLOR) \"$str\"
}
$ns puts-nam-config "n -t [$ns now] -s $id_ -x $x -y $y -S
5. 同一个节点,可以有多个 mark 但只能有一个 label
6. 两个节点间建立链接使用 duplex-link 这个 proc,它在 ns2.34\tcl\lib\ns-lib.tcl
中,如下所示:
Simulator instproc duplex-link { n1 n2 bw delay type args } {
$self instvar link_
set i1 [$n1 id]
set i2 [$n2 id]
if [info exists link_($i1:$i2)] {
$self remove-nam-linkconfig $i1 $i2
}
eval $self simplex-link $n1 $n2 $bw $delay $type $args
eval $self simplex-link $n2 $n1 $bw $delay $type $args
# Modified by GFR for nix-vector routing
if { [Simulator set nix-routing] } {
# Inform nodes of neighbors
$n1 set-neighbor [$n2 id]
$n2 set-neighbor [$n1 id]
}
从中可以看到它调用了 simplex-link 这个 proc,它也在该文件中
Simulator instproc simplex-link { n1 n2 bw delay qtype args } 其中有这样一句:
default {
set link_($sid:$did) [new SimpleLink
\
$n1 $n2 $bw $delay $q]
(注意 tcl 中 switch 语法的用法)可以看出是创建了 SimpleLink。而 SimpleLink
则是由 Link 生成的,详见 ns2.34\tcl\lib\ns-link.tcl。
SimpleLink 和 Link 都包含 color 属性,但是从这些地方修改却不能起作用。
ns2.34\tcl\lib\ns-namsupp.tcl 中的 SimpleLink instproc dump-namconfig {} 这个
proc 才是真正起作用的地方。
if ![info exists attr_(COLOR)] {
#
set attr_(COLOR) "black"
set attr_(COLOR) "red"
;#liuyi modify at 2011.11.30
}
}
}
修改这个 color 才能真正的起作用。
ns2.34\tcl\lib\ns-lib.tcl 中有获取 link 的 proc 如下:
7.
Simulator instproc link { n1 n2 } {
$self instvar Node_ link_
if { ![catch "$n1 info class Node"] } {
set n1 [$n1 id]
if { ![catch "$n2 info class Node"] } {
set n2 [$n2 id]
}
if [info exists link_($n1:$n2)] {
return $link_($n1:$n2)
}
return ""
}
需要给出两个参数。通过这个获取到 link 变量后便可以使用
ns2.34\tcl\lib\ns-namsupp.tcl 中的关于 link 的 proc,如 label、label-color、label-at、
color、link-color 等
有个奇怪的问题是 通过 $ns duplex-link $node(0) $node(1) 5Kb 2ms DropTail 创
建的链接 如果获取链接时 set link_ [$ns link $node(2) $node(0)] 再使用后面的
label-color 则不起作用,原因正在查找。