搜索此博客

2011年4月26日星期二

[自己钻研]ns2修改c++ 如何使用tcl对c++的变量进行初始化


背景:队列需要设置一个变量 nofstart_ql_non-overflow期的开始参数。新加变量,需要用TCL脚本初始化,类似于$ns queue-limit $n0 $n1 40 (设置队长为40)

实现方法:新建mydrop-tail.hmydrop-tail.cc, 标榜 drop_front_  summarystats_  queue_in_bytes_  mean_pktsize_  进行效仿。

step1 mydrop-tail.h里面声明变量nofstart_ql_
_______________________________________________________________
mydrop-tail.h
...

~MyDropTail() {
delete q_;
}
  protected:
void reset();
int command(int argc, const char*const* argv); 
void enque(Packet*);
Packet* deque();
void shrink_queue(); // To shrink queue and drop excessive packets.

PacketQueue *q_;
int drop_front_;
int summarystats;
void print_summarystats();
int qib_;      
int mean_pktsize_;
int nofstart_ql_;


...
_______________________________________________________________

step2: tcl变量和c++绑定
_______________________________________________________________
mydrop-tail.h


class MyDropTail : public Queue {
  public:
MyDropTail() { 
q_ = new PacketQueue; 
pq_ = q_;
bind_bool("drop_front_", &drop_front_);
bind_bool("summarystats_", &summarystats);
bind_bool("queue_in_bytes_", &qib_);  // boolean: q in bytes?
bind("mean_pktsize_", &mean_pktsize_);
bind("nofstart_ql_",&nofstart_ql_); //start of non-of queue len..
// _RENAMED("drop-front_", "drop_front_");
.....................................................................................................
可以看到bind或者是bind_bool,這兩個函數主要的目的是要把TCL的變數和C++的變數產生互連的關係,這樣使用者就可以在TCL檔內去設定或者去查看目前C++的變數值。(bind_bool是用在布林變數,bind是用在一般實數;而第一個參數是TCL的變數,第二個參數是C++內的變數。筆者以在基本工具內的範例為例,教大家如何去改變或查看C++的變數值。[ref: http://hpds.ee.ncku.edu.tw/~smallko/ns2/module.htm]

step3: 修改ns-default.tcl檔,設定初始內定值。
a.       cd  ns-allinone-2.27/ns-2.27/tcl/lib/
b.      使用文字編輯軟體打開ns-default.tcl
c.       使用搜尋的功能找到Queue/DropTail
d.      把每個初始設定值都再設一份給Queue/myfifo
……………………………………………..
Queue/DropTail set drop_front_ false
Queue/DropTail set summarystats_ false
Queue/DropTail set queue_in_bytes_ false
Queue/DropTail set mean_pktsize_ 500

Queue/MyDropTail set drop_front_ false
Queue/MyDropTail set summarystats_ false
Queue/MyDropTail set queue_in_bytes_ false
Queue/MyDropTail set mean_pktsize_ 500
Queue/MyDropTail set nofstart_ql_ 5
……………………………………………..

step4: 在C++里面可以使用变量nofstart_ql_ (我们新增的变量)

step5: 在tcl脚本里面的使用

set q [[$ns link $n2 $n3] queue]
$q set nofstart_ql_ 5

笨笨小玲玲debug系列——注意循环次数啊

for {set i 1} {$i <= 65} {incr i} {  # 这里一共设置了65个source
        set b0($i) [new Application/Traffic/Exponential]
        $b0($i) set packetSize_ 100
        $b0($i) set burst_time_ 0.96
        $b0($i) set idle_time_ 1.69
        $b0($i) set rate_ 136k
        $b0($i) attach-agent $udp_src0
        $ns at 10.0 "$b0($i) start"
}

set udp_sink0 [new Agent/Null]
$ns attach-agent $n1 $udp_sink0

$ns connect $udp_src0 $udp_sink0

$ns at 0 "Show_time"
$ns at 0.01 "recordQL"

#$ns at 100 "$q ResetData"

for {set i 1} {$i <= 10} {incr i} { # 只停了其中10个啊!剩下55个可不是还在发么!多废话啊!
        $ns at 50.0 "$b0($i) stop"
}

2011年4月25日星期一

笨笨小玲玲debug系列——什么叫return

背景: ns2, c++改源代码

伤不起啊 伤不起!知道什么叫return不???return完就离开了!下面的语句都不执行了啊!有木有。。。555 这你都不知道,把加的代码写return后面,能运行才怪!

笨死!害自己又郁闷了一晚上!哼!

无论如何,它还是继续下去了!鼓掌~ 撒花儿庆祝!

2011年4月22日星期五

[转]ns2问题解决经验-争取经常更新


1) ns2的安装
 
 i: 一般安装,进入 ns-allinone-2.xx目录后,执行: ./install 即可
 
 ii: 安装完毕后,要设置环境变量,在.bashrc的文件末尾(ubuntu 版本中在用户目录下,如/home/weizai)增加以下语句(假设在/home/weizai/ns2work 下安装了ns-allinone-2.30):
 export NS_HOME=/home/weizai/ns2work/ns-allinone-2.30
 export PATH=$NS_HOME/tcl8.4.13/unix:$NS_HOME/tk8.4.13/unix:$NS_HOME/bin:$PATH
 export LD_LIBRARY_PATH=$NS_HOME/tcl8.4.13/unix:$NS_HOME/tk8.4.13/unix:$NS_HOME/otcl-1.12:$NS_HOME/lib:$LD_LIBRARY_PATH
 export TCL_LIBRARY=$NS_HOME/tcl8.4.13/library
 保存文件退出,重新打开终端,这时在终端键入 ns,应该出现%,可用./validate检查安装是否成功。
 
 iii: 要记得安装tk和tcl, ns-allinone-2.26(含2.26)以前对应的版本是8.3,以后是8.4
 以ubuntu为例,在新立德软件包管理器中就可以选择,强烈建议上网不方便或网速不快的用户使用ubuntu DVD进行安装,不少基本软件在DVD中都有,安装方便快捷。

 iv: nam安装不上 
 * 要安装libxmu-dev包,不然无法编译通过nam-1.11,会报“没有X11/Xmu/WinUtil.h”的错误。
 在ubuntu中选择安装libxmu-dev包时,libxt-dev包会关联安装。(libxt-dev 这个包也是必须的。)
 * 在ubuntu8.x版本的dvd光盘中希望安装libxmu-dev,但会提示不能安装。
 解决方法1:到网上下载libxmu-dev包,按照一般软件包安装步骤-configure, make, sudo make install 安装。
 解决方法2: 尝试用低版本的ubuntu DVD,比如用ubuntu7.0,在新立得中添加此光盘源,而后在终端用sudo apt-get install libxmu-dev安装
 v: 解决因为reldi'`,安装ns2,提示Syntax error: Unterminated quoted string,导致tcl,tk,otcl不能安装的问题(这个问题是因gcc对语法要求更加严格):
 分别进入以下3个文件夹:
 */ns-allinone-2.29/tcl8.4.11/unix/
 */ns-allinone-2.29/tcl8.4.11/tk8.4.11/unix/
 */ns-allinone-2.29/otcl-1.11/
 执行命令: cp configure{,.orig} && sed "s/relid'/relid/" configure.orig > configure 
 更直接的该发,例如安装中提示如下错误
 checking system version (for dynamic loading)... ./configure: 1: Syntax error: Unterminated quoted string
 tcl8.3.2 configuration failed! Exiting
 这时,进入 ns-allinone/tcl8.3/unix ,打开 configure文件,查找relid字符,会找到如下语句:
 if test -r /etc/.relid -a "X`uname -n`" = "X`uname -s`" ; then
 system=MP-RAS-`awk '{print }' /etc/.relid'`
 .........................................................
 if test -r /etc/.relid -a "X`uname -n`" = "X`uname -s`" ; then
 system=MP-RAS-`awk '{print }' /etc/.relid'`
 把 relid'`中的 '符号去掉即可,tk和otcl安装也会遇到类似问题,方法同上 
 补充说明:
 关于2.26和2.27版本中的configure文件中“relid”字符问题,
 只有tcl和tk文件夹下的unix文件夹里configure文件,以及otcl文件夹的configure有relid后面多一个单引号’的问题,去掉单引号即可
 即搜索到relid'`,而后改成relid`
 
 
2) ns-allinone有一些软件包可以单独安装,比如nam-1.x等,进入相应目录后安装命令如下:
./configure;
make
sudo make install
3) 关于gcc, g++版本修改(ubuntu)
gcc -v-显示现在实用gcc版本;
gcc 按tab键,显示现在系统中已有gcc;
* 可以改变现在系统使用gcc的版本(假设原来是 gcc-4.1);
 首先,先删除原有的gcc连接,假设原来是 gcc-4.1,则进入安装gcc的目录后,假设安装在 /usr/bin下
 (查看原来的gcc所在的路径:% which gcc)
 cd /usr/bin
 /usr/bin sudo rm gcc (这只是删除快捷方式)
 第二,建立新的gcc连接 (建立新的快捷方式)
 /usr/bin sudo ln -s gcc-3.4 gcc (ln -s 表示建立符号链接)
 (注:分别使用gcc34、g++34、gcj34来调用GCC 3.4.0的gcc、g++、gcj完成对C、C++、JAVA程序的编译了)
 相当于建立到各种gcc版本的快捷方式
 g++版本修改同上
4) 关于gcc和g++安装
 如果用ubuntu live cd 安装,这两个东东实际上都没有装,需要上网装或者用live dvd通过dvd安装光盘安装
5) 如果nam不能直接启动(但是在ubuntu8.x下我没有遇到这个问题,这个问题不是必然出现的)
sudo ln -s /home/wei/nsallione-2.30/ns-2.30/nam-1.12 nam /usr/bin nam
即在/usr/bin下建立一个名位nam的快捷方式指向可执行文件nam
6) 关于不同版本ns2的安装,特别是关于编译起gcc的选择
6.1)ns-2.26安装
用gcc 3.3,g++-3.3
6.2)ns-2.27安装
用gcc-3.3 和g++-3.0,在ubuntu下安装ok
6.3)ns-2.29 安装
用gcc-3.3和gcc-3.4编译不能通过,在遇到wpan模块时出错
-----------------------------
用gcc-3.4,gcc-4.1,编译不能通过,“./sctp/sctp.h:705:错误,有多余的限定'SctpAgent::'
在成员'DumpSendBuffer’'上make:***[trace/trace.o]错误1.
-----------------------------
用gcc-4.3编译不能通过,
“./sctp/sctp.h:705:错误,有多余的限定'SctpAgent::'
在成员'DumpSendBuffer’'上...
/trace/trace.cc:185警告:不建议使用从字符串常量到"char*"的转换make:***[trace/trace.o]错误1
6.4)ns-2.30安装
用gcc-3.4编译不能通过,在遇到wpan模块时出错
用gcc 4.3,g++-4.3可安装,但安装过程中显示若干警告条目,不知会有什么具体影响;
6.5)ns-2.31安装
用gcc 4.1,g++-4.1可安装,安装过程中警告条目不如用gcc4.3装ns2.30时多
6.6)ns-2.33安装
用gcc 4.3,g++-4.3可安装,但安装过程中显示若干警告条目,不知会有什么具体影响;

7)关于wpan(802.15.4模块)
ns2.30和ns2.31中均有此模块,用gcc3.3编译会出错,用gcc4则不会
出错提示如下
tag.o diffusion3/filters/rmst/rmst.o diffusion3/filters/rmst/rmst_filter.o gen/version.o gen/ns_tcl.o gen/ptypes.o common/win32.o -L/root/nsroot/ns-allinone-2.27/tclcl-1.15 -ltclcl -L/root/nsroot/ns-allinone-2.27/otcl-1.8 -lotcl -L/root/nsroot/ns-allinone-2.27/lib -ltk8.4 -L/root/nsroot/ns-allinone-2.27/lib -ltcl8.4 -lXext -lX11 -lnsl -ldl -lm
`.gnu.linkonce.t._ZN11taskPending10taskStatusEh' referenced in section `.rodata' of wpan/p802_15_4mac.o: defined in discarded section `.gnu.linkonce.t._ZN11taskPending10taskStatusEh' of wpan/p802_15_4mac.o
collect2: ld returned 1 exit status
make: *** [ns] 错误 1
8) 关于ubuntu 源
 i: 源文件位置
 /etc/apt/sources.list ,在这个文件里可以手工添加各种源
 ii: 如何选择源
 一种方法是把网上别人提供的源,添加到sources.list 文件中
 二(推荐),在“软件源”,选择 “下载自”- “其他...”,点击“选择最佳服务器”,这时系统会帮你搜索最快的服务器(建议国家选择中国,如果你装的是英文界面,默认是美国)。

9) 在ns2中修改或添加文件后
i:修改.cc文件后
 修改.cc文件后,重新make即可
i::修改.h文件后,或者默认的tcl文件(这里不是指自己写的仿真脚本)
 修改.h文件后,整个ns-2.xx要重新编译,命令如下
 make clean
 ./configure
 make
 sudo make install
 或者以下过程更妥当 
 ./configure
 make clean
 ./configure
 make
 sudo make install
111) 其他问题描述
 
 i:我新增完一個C++和OTCL的鏈結變數後,跑模擬都會出現warning: no class variable xx:variable_。  
解決方案:
 在~ns2/tcl/lib中的ns-default.tcl加入預設值後,重新編譯ns2。

[转]同时安装两个版本NS2方法


有时候我们可能会遇到要同时安装两个ns版本的问题,比如我研究wimax/802.16,因为协议太复杂,用的是长庚大学和nistwimax补丁。长庚大学的wimax补丁是在ns2.29下开发的,nistwimax补丁虽说可以在多个ns版本上使用,但是在ns2.31上移植最简单。因此如果能够两个版本同时使用当然最好,事实上这也是可以实现的。
   我们用./install安装好后,没有设置环境变量在终端上是不能使用ns命令的,除非你在ns-2~/文件夹下。呵呵,其实这样已经告诉我们一种解决两个版本同时使用的方案了,那就是不设置环境变量,当我们需要执行仿真代码a.tcl时,就指定ns命令的路径:/home/ns-allinone-2.29/ns-2.29/ns a.tcl/home/all-inone-2.31/ns-2.31/ns a.tcl(我的ns是安装在home文件夹下)。
   如果在.bashrc上设定其中一个版本的环境变量,比如说ns2.31则你的终端就可以使用ns2.31版本的ns命令了,另外一个则仍旧要指定路径全称才能使用。
   没有没有其它更好的办法?有的!其实我们的ns2安装后并不是安装到Linux系统内部了,ns命令是通过链接ns-allinone-2.~/ns-2~/ns实现的。这样就好办了,因为怕就怕在两个ns版本都设定了环境变量的情况下,我们在终端敲入ns命令只能识别其中一个罢了。知道了原委,我们就可以通过重新创建链接的方式来改变命令ns的名称,比如我们可以把2.29版本的ns命令改成ns2.29,把2.31版本的ns命令改成ns2.31,这样就可以避免名称的冲突了。
具体的实现是:假若我们先安装ns2.29,目录是/home/ns-allinone-2.29/linux下安装完ns2.29并设置好环境变量以后,打开ns-allinone-2.29/bin目录,用file ns查看链接关系,得到bin文件夹下的ns链接是链接到了:/home/ns-allinone-2.29/ns-2.29/ns上,你可以删除这个链接:rm ns,重建一个连接:ln -s /home/ns-allinone-2.29/ns-2.29/ns   ns2.29,这样ns2.29版本的仿真命令就变成了ns2.29了,你再在终端敲入ns看看是不是一个未知命令呢?然后再敲个ns2.29看看,有个%号出来了吧!用这个方式重建namxgraph的链接,将它们改为nam2.29xgraph2.29。接着安装ns2.31,环境变量可以同时设置好,完毕后你在终端敲ns命令其实就是执行2.31namxgraph也是2.31的,为方便记忆,你也可以仿照上面的方式将nsnamxgraph的连接改成ns2.31nam2.31xgraph2.31。然后要执行脚本的话,想用ns2.29版本的ns就用:ns2.29 a.tcl就可以了。
用这个方法你可以装很多个ns版本,是不是更好些呢?
note1:我没有用过cygwin,不过听说在cygwin也是可以通过上述的方法安装多个ns版本的。
note2:增补一个说明,当为两个版本的ns2设置环境变量的时候,只有一个LD_LIBRARY_PATH的设置能够生效,一般是最先设置的一个,不过可能是2.29和2.31的库文件没有很多变化,其中一个的库另一个也能正常调用,在我的使用过程中没有发现问题。

ns- configure and make

I met a problem when I make

first, go to directory ns-2.33, run "configure" instead of './configure'. (work for mac os)

second, you need to 'make'. If failed reported that no CXX@ directory, then you edit 'makefile' (under ns-2.33), search 'CXX@' and then change it to g++

ref:
Regarding ns2.33 (or any ns2.XX)
  take a fresh ns-allinone-2.33, go to the ns-2.33 folder,
  In the 'Makefile.in' replace,

    CC=@CC@
    CPP=@CXX@
       
       by

    CC=gcc34
    CPP=g++34

  then return to the 'ns-allinone-2.33' directory and start the normal installation process. 



In my case, it is 'g++', not 'g++34'.



2011年4月21日星期四

matlab - pdf or ksdensity

废话不多说 ksdensity就是连续版本的hist. 怎么搞得也搞不明白 面壁去吧

2011年4月11日星期一

latex - 如何引用equation

first step:    \usepackage{amsmath}

second step:


\begin{equation}\label{eq}
...

\end{equation}


need to reference it, using \eqref{eq}。

latex - 公式转行等号对齐


\begin{equation}
    \begin{array}{rcl}
        a+b+c & = & d\\
                  & = & e\\
        f+g & = & h\\
             & = &i+j+k
    \end{array}
\end{equation}

ref:http://bbs.ctex.org/viewthread.php?tid=38804

latex subscript 下角标

1)
% In your preamble, add:
\usepackage{fixltx2e}
...

% In your document:
It is found that height\textsubscript{apple tree} is
different than height\textsubscript{orange tree}.



gives


\text{It is found that height}_\text{apple tree}\text{ is different than height}_\text{orange tree}\text{.}\,




2)



If you do not load a package that supports \textsubscript{}, the math mode must be used. This is easily accomplished in running text by bracketing your text with the $ symbol. In math mode subscripting is done using the underscore: _{}.
For example, the formula for water is written as:
H$_2$O is the formula for water
H2O is the formula for water


3)

Note that in math mode text will appear in a font suitable for mathematical variables. In math mode, to generate roman text, for example, one would use the \mathrmcommand:
This is $\mathrm{normal\ roman\ and}_\mathrm{subscript\ roman}$text

4)

Note the use of \ to insert a space in math mode.
Similarly, you can superscript using:
This is $\mathrm{normal\ roman\ and}^\mathrm{superscript\ roman}$ text
\text{This is }\mathrm{normal\ roman\ and}^\mathrm{superscript\ roman}\text{  text}

Latex - quotes


How to quotes in Latex? see belows
ref: http://en.wikibooks.org/wiki/LaTeX/Formatting#Quote-marks

To `quote' in Latex
Quote1.png
To ``quote'' in Latex
Quote2.png
To ``quote" in Latex
Quote2.png
To ,,quote'' in Latex
Quote4.png
``Please press the `x' key.''
Latex quote 3.png
,,Proszę, naciśnij klawisz <>''.
Latex quote 4.png