目标:如下的tcl测试脚本,知道每句话最后执行到哪里。
set ns [new Simulator]
set test [new Agent/test_newprotocol]
$test set para_in_tcl 7
$test get_para
$test test_fun
=================================
开始
=================================
第1步:
在/usr/local/ns2/ns-allinone-2.34/ns-2.34下添加自己的文件夹,名字随意 我的叫xdw
第2步:
每一个新协议都对应两个文件。比如这个新协议叫test_newprotocol.则创建newprotocol.h和newprotocol.cc两个文件。。h对应的是协议的c++类。。cc文件对应的是otcl类
第3步:
test_newprotocol.h文件内容如下
#ifndef test_newprotocol_h
#define test_newprotocol_h
#include "agent.h"
#include "tclcl.h"
#include "packet.h"
#include "address.h"
#include "ip.h"
class test_newprotocol : public Agent
{
public :
test_newprotocol();
void get_para();
protected:
int command(int argc, const char*const*argv);
int test_para1;
void test_fun();
};
#endif
第4步:
test_newprotocol.cc内容如下
#include "test_newprotocol.h"
static class Test_newprotocolTclClass:public TclClass
{
public:
Test_newprotocolTclClass():TclClass("Agent/test_newprotocol"){};
TclObject *create(int,const char* const*)
{
return (new test_newprotocol());
}
}class_my_agent;
test_newprotocol::test_newprotocol():Agent(PT_UDP) //!!!!!!!!!!!!!!!!
{
bind("para_in_tcl",&test_para1);
}
void test_newprotocol::get_para()
{
Tcl& tcl=Tcl::instance();
tcl.evalf("puts /"argv is %d/"",test_para1);
}
void test_newprotocol::test_fun()
{
Tcl& tcl=Tcl::instance();
tcl.eval("puts /"message from test_fun()/n/"");
}
int test_newprotocol::command(int argc, const char*const* argv)
{
if( argc == 2 )
{
if( strcmp( argv[1], "test_fun" )==0 )
{
test_fun();
return (TCL_OK);
}
if( !strcmp( argv[1], "get_para" ) )
{
get_para();
return (TCL_OK);
}
}
return (Agent::command(argc, argv));
}
第5步:
tcl脚本测试文件 test_newprotocol.tcl
set ns [new Simulator]
set test [new Agent/test_newprotocol]
$test set para_in_tcl 7
$test get_para
$test test_fun
第6步:
修改/usr/local/ns2/ns-allinone-2.34/ns-2.34下的makefile文件
在OBJ_CC = /下面添加xdw/test_newprotocol.o /
第7步:
终端进入/usr/local/ns2/ns-allinone-2.34/ns-2.34/ 输入make重新编译
第8步:
终端进入/usr/local/ns2/ns-allinone-2.34/ns-2.34/xdw 输入ns new_protocol.tcl
得到结果:
warning: no class variable Agent/test_newprotocol::para_in_tcl
see tcl-object.tcl in tclcl for info about this warning.
argv is 7
message from test_fun()
备注
第9步:
在/usr/local/ns2/ns-allinone-2.34/ns-2.34/tcl/lib,有一个ns-default.tcl文件
在其中添加新协议参数的默认值。在最后添加
Agent/test_newprotocol set para_in_tcl 100 那个参数名字是otcl空间的。
主目录下重新make即可
成功!~
内容很简单,没做解释。只是备注一下步骤
之后写有新包头信息的协议
转帖http://hi.baidu.com/xdwa/blog/item/d6af9854eaaf5a123a29350b.html
PS:make时报错,1.在test_newprotocol.cc文件中的comand函数中strcmp参数的'test_fun'和'get_para'在此作用域中尚未声明;2.没有对象无法调用成员函数Virtual int Agent::command(int, const char* const*)
解决方法:makefile文件中OBJ_CC = /下面添加xdw/test_newprotocol.o /放到最后