OMNet++ 中的 NED 语言学习(4)
本节主要是关于信道(channel)的相关知识。
信道封装了与连接关联的参数和行为,信道与简单模块类似,在它们背后就是C++类,在默认情况下,类名与NED的类型名相同,除非有一个@class属性(@namespace也需要注意),例如,下面的信道类型需要一个名为CustomChannel 的C++ 类。
channel CustomChannel // needs a CustomChannel C++ class
{
}
跟简单模块不同的是,一般情况下不需要自己来定义信道类型,只需要从系统中已经预定义好的信道类型进行特殊化即可,内建的类型有: ned.IdealChannel, ned.DelayChannel and ned.Datarate-Channel。 (“ned” 是包名,和Java类似的是可以用import ned.* 来导入所有这三种类型,这样就不用写前缀ned)。
IdealChannel
- 没有参数,可以让所有数据没有任何延迟地传送。
DelayChannel
- 有两个参数。
- delay是一个double类型的参数,代表传播时延,可以用s,ms,us来指定。
- Disabled是一个布尔值,默认为false,当设为true时,信道对象会丢弃所有的信息。
DatarateChannel
- 与DelayChannel相比,它有更多的参数。
- datarate是一个double型的参数,代表信道的数据率,可以用bps,Kbps,Mbps,Gbps来指定,默认值是0,代表无限带宽。
- ber和per分别代表比特错误率和包错误率,并且允许基本的错误建模。它们是随机生成的范围在0到1之间的数值。通过在包对象中设置一个错误的标记(error flag),接收的模块进行检查并丢弃标记为冲突的数据包。它们的默认值都是0。
注意: There is no channel parameter that would decide whether the channel deliversthe message object to the destination module at the end or at the start of the reception; that is decided by the C++ code of the target simple module. See the setDeliverOn-ReceptionStart() method of cGate.
下面的例子基于内建信道类型定义了参数的值:
channel C extends ned.DatarateChannel
{
datarate = 100Mbps;
delay = 100us;
ber = 1e-10;
}
也可以修改或添加参数或属性,下面引入了一个基于长度的对传播迟延的计算:
channel DatarateChannel2 extends ned.DatarateChannel
{
double length @unit(m);
delay = this.length / 200000km 1s;
}
参数作为C++ 的实现类的输入尤为有用,但就算用内建的信道类型来重用C++的实现类,它们也能被模型的其它部分读取或使用。例如,添加一个cost参数(或@cost属性)能被路由算法观察到并用来进行路由选择,下面的例子展示了一个cost参数,以及@backbone属性注解。
channel Backbone extends ned.DatarateChannel
{
@backbone;
double cost = default(1);
}
Related posts:
评论