下面代码说明了如何访问其他节点的Agent,以AODV协议为例
添加以下代码到aodv.cc. 用这种方法也可以访问其他对象,比如 mobilenode, 等等.
- AODV*
aodvagent; -
- //
set i to be the node id -
- char
command[256]; -
- //
[Agent/AODV info instances] 返回一个aodvagent的数组 - sprintf(command,
"foreach aodvagent [Agent/AODV info instances]\ - {\nif
{ [$aodvagent id] == %d} {\nset i $aodvagent}}\nset t $i\n", i); -
- Tcl&
tcl = Tcl::instance(); - tcl.eval_r(command);
- const
char* ref = tcl.result(); -
- aodvagent
= (AODV*)tcl.lookup(ref);
这种方法需要在Agent的成员函数command中含有返回agent节点地址的命令:
- if
(argc == 2) { -
Tcl& tcl = Tcl::instance(); -
if(strncasecmp(argv[1], "id", 2) == 0) { -
tcl.resultf("%d", ra_addr_); -
return TCL_OK; -
} -
//other command -
... - }