在ns2中做了一个广播的组件,实现方式是:
1. 仿照cbr_traffic.cc,编写合适的Application
2. 编写Agent,实现广播功能
在编写Agent的过程中出现问题:怎样完成Application对该Agent的发包控制。
学习cbr_traffic.cc及UDP代码,将一些关键性的实现列举如下:
CBR部分:
(1) cbr_traffic.cc中,由double CBR_Traffic::next_interval(int &size)确定发包间隔,CBR中间隔时间符合uniform(x,y)
(2) void CBR_traffic::start()中实现了包的发送,调用了函数timeout():
void TrafficGenerator::timeout()
{
if (! running_)
return;
/* send a packet */
send(size_);
/* figure out when to send the next one */
nextPkttime_ = next_interval(size_);
/* schedule it */
if (nextPkttime_ > 0)
timer_.resched(nextPkttime_);
else
running_ = 0;
}
timeout()中调用了虚构函数next_interval()实现发包间隔控制,同时调用继承自Application类的函数send(size_)
(3) void Application::send(int nbytes)
{
agent_->sendmsg(nbytes);
}
agent_为该应用绑定到的代理,定义在Application中,通过otcl “$app attach-agent $agent”实现绑定。
函数中调用agent类的sendmsg(int nbytes)实现控制代理发送包