概述
tictoc14.ned文件
simple Txc14
{
parameters:
@display("i=block/routing");
gates:
inout gate[];
}
network Tictoc14
{
types:
channel Channel extends ned.DelayChannel {
delay = 100ms;
}
submodules:
tic[6]: Txc14;
connections:
tic[0].gate++ <--> Channel <--> tic[1].gate++;
tic[1].gate++ <--> Channel <--> tic[2].gate++;
tic[1].gate++ <--> Channel <--> tic[4].gate++;
tic[3].gate++ <--> Channel <--> tic[4].gate++;
tic[4].gate++ <--> Channel <--> tic[5].gate++;
}
tictoc14.msg文件
message TicTocMsg14
{
int source;
int destination;
int hopCount = 0;
}
Txc14文件
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
#include "tictoc14_m.h"//不要忘了
using namespace omnetpp;
/**
* In this step we keep track of how many messages we send and received,
* and display it above the icon.
*/
class Txc14 : public cSimpleModule
{
private:
long numSent;//发送数量
long numReceived;//接收数量
//refreshDisplay()是omnet自带的一个刷新当前的显示界面(时刻刷新)
//override放在子类虚函数后说明覆盖重写
protected:
virtual TicTocMsg14 *generateMessage();
virtual void forwardMessage(TicTocMsg14 *msg);
格式要牢记
virtual void refreshDisplay() const override;//coast修饰常量函数
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
};
Define_Module(Txc14);
void Txc14::initialize()
{
// Initialize variables初始化变量
numSent = 0;
numReceived = 0;
//此处WATCH全部大写
WATCH(numSent);
WATCH(numReceived);
// Module 0 sends the first message
if (getIndex() == 0) {
// Boot the process scheduling the initial message as a self-message.
TicTocMsg14 *msg = generateMessage();
numSent++;
scheduleAt(0.0, msg);
}
}
void Txc14::handleMessage(cMessage *msg)
{
TicTocMsg14 *ttmsg = check_and_cast<TicTocMsg14 *>(msg);
if (ttmsg->getDestination() == getIndex()) {
// Message arrived
此处要加int
int hopcount = ttmsg->getHopCount();
EV << "Message " << ttmsg << " arrived after " << hopcount << " hops.n";
numReceived++;
delete ttmsg;
bubble("ARRIVED, starting new one!");
// Generate another one.
EV << "Generating another message: ";
TicTocMsg14 *newmsg = generateMessage();
EV << newmsg << endl;
forwardMessage(newmsg);
numSent++;
}
else {
// We need to forward the message.
此处是ttmsg,因为开头已经强制转换
forwardMessage(ttmsg);
}
}
TicTocMsg14 *Txc14::generateMessage()
{
// Produce source and destination addresses.
int src = getIndex();
// our module index
int n = getVectorSize();
// module vector size
int dest = intuniform(0, n-2);
if (dest >= src)
dest++;
char msgname[20];
sprintf(msgname, "tic-%d-to-%d", src, dest);
// Create message object and set source and destination field.
TicTocMsg14 *msg = new TicTocMsg14(msgname);
msg->setSource(src);
msg->setDestination(dest);
return msg;
}
void Txc14::forwardMessage(TicTocMsg14 *msg)
{
// Increment hop count.
msg->setHopCount(msg->getHopCount()+1);
// Same routing as before: random gate.
int n = gateSize("gate");
int k = intuniform(0, n-1);
EV << "Forwarding message " << msg << " on gate[" << k << "]n";
send(msg, "gate$o", k);
}
void Txc14::refreshDisplay() const
{
char buf[40];
//此处是%ld,因为前面定义的是long格式
sprintf(buf, "rcvd: %ld sent: %ld", numReceived, numSent);
//p位置,b形状大小,i图形,is图形大小,i2小图标,r显示半径,t显示文本,tt提示
//背住显示的这个式子
getDisplayString().setTagArg("t", 0, buf);//setTagArg(标签名称,索引,值) 更新文本
}
运行结果
最后
以上就是辛勤咖啡为你收集整理的omnet++tictoc14案例解析的全部内容,希望文章能够帮你解决omnet++tictoc14案例解析所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复