首页 > 技术文章 > omnet++:用到的方法和语句

ShineLeBlog 2021-05-12 21:45 原文

1、方法

方法 说明
msg->getName() 获取发送的消息名
uniform(a,b) 生成[a,b]间的随机实数
intuniform(a,b) 生成[a,b]间的随机整数
exponential(n) 指数分布的随机数
truncnormal(a,b) [a,b]间的离散正态分布随机数 
scheduleAt(simTime()+timeout,event) 经过时间timeout之后,发送self-message event
cancelEvent(event) 与scheduleAt相反,是取消自发送的消息event
bubble("xxx") 以弹窗的形式弹出消息xxx 
cancelAndDelete(timeoutEvent) 释放指针指向的空间
(cMessage *)msg->dup() 获取消息msg的副本
gateSize("out") out端口的数量
getIndex() 获取当前module在module vector中的索引
getVectorSize() 获取当前module所在module vector的Size

send(msg,"out")

send(msg,"out",k)

从out端口发送消息msg;

从k号out端口发送消息msg

2、语句

1)创建不同名字的新消息

char msgname[20];
sprintf(msgname,"tic-%d-to-%d",src,dest);
xxxMsg * msg = new xxxMsg(msgname);

2)转发时消息跳数+1

msg->setHopCount(msg->getHopCount()+1);

3)选择端口进行转发

int n = gateSize("gate");
int k = intuniform(0,n-1);
EV << "Forwarding message " << msg << " on gate[" << k << "]\n";
send(msg,"gate$o",k);

 4)在每个节点上显示收发包的数量

void Txc14 :: refreashDisplay() const
{
    char buf[40];
    sprintf(buf,"rcvd: %ld sent: %ld",numReceived,numSent);
    getDisplayString().setTagArg("t",0,buf);
}

//在类中声明该函数为
virtual void refreshDisplay() const override;

 

推荐阅读