tictoc 例子理解 13-15

news/2024/11/30 3:25:14/

tictoc13-

    • tictoc13 子类化cMessage生成消息,随机目标地址
    • tictoc 14 在13的基础上增加两变量显示于仿真界面
    • tictoc 15 模型数据输出为直方图

tictoc13 子类化cMessage生成消息,随机目标地址

  1. 在这一步中,目标地址不再是节点2——我们绘制了一个随机的目的地,并将目标地址添加到消息中。
  2. 最好的方法是子类化cMessage并添加destination作为数据成员。
    手工编写消息类通常是令人厌烦的,因为它包含很多样板代码,所以我们让omnet++为我们生成该类。
  3. 消息类规范在tictoc13.msg——tictoc13_m.h和.cc将从这个文件自动生成。
  4. 为了延长模型的执行时间,在消息到达目的地后,目标节点将生成另一个带有随机目的地地址的消息,以此类推。
    tictoc13.msg
message TicTocMsg13
{int source;int destination;int hopCount = 0;
}

ned

simple Txc13
{parameters:@display("i=block/routing");gates:inout gate[];
}network Tictoc13
{types:channel Channel extends ned.DelayChannel {delay = 100ms;}submodules:tic[6]: Txc13;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++;
}

cc

#include <stdio.h>
#include <string.h>
#include <omnetpp.h>using namespace omnetpp;// Include a generated file: the header file created from tictoc13.msg.
// It contains the definition of the TictocMsg10 class, derived from
// cMessage.包含一个生成的文件:从tictoc13.msg创建的头文件。它包含了派生自cMessage的TictocMsg10类的定义。
#include "tictoc13_m.h"/*** In this step the destination address is no longer node 2 -- we draw a* random destination, and we'll add the destination address to the message.** The best way is to subclass cMessage and add destination as a data member.* Hand-coding the message class is usually tiresome because it contains* a lot of boilerplate code, so we let OMNeT++ generate the class for us.* The message class specification is in tictoc13.msg -- tictoc13_m.h* and .cc will be generated from this file automatically.** To make the model execute longer, after a message arrives to its destination* the destination node will generate another message with a random destination* address, and so forth.* 1. 在这一步中,目标地址不再是节点2——我们绘制了一个随机的目的地,并将目标地址添加到消息中。
2. 最好的方法是子类化cMessage并添加destination作为数据成员。
手工编写消息类通常是令人厌烦的,因为它包含很多样板代码,所以我们让omnet++为我们生成该类。
3. 消息类规范在tictoc13.msg——tictoc13_m.h和.cc将从这个文件自动生成。
4. 为了延长模型的执行时间,在消息到达目的地后,目标节点将生成另一个带有随机目的地地址的消息,以此类推。*/
class Txc13 : public cSimpleModule
{protected:virtual TicTocMsg13 *generateMessage();virtual void forwardMessage(TicTocMsg13 *msg);virtual void initialize() override;virtual void handleMessage(cMessage *msg) override;
};Define_Module(Txc13);void Txc13::initialize()
{// Module 0 sends the first message ,模块0发送第一个消息if (getIndex() == 0) {// Boot the process scheduling the initial message as a self-message.TicTocMsg13 *msg = generateMessage();scheduleAt(0.0, msg);}
}void Txc13::handleMessage(cMessage *msg)
{TicTocMsg13 *ttmsg = check_and_cast<TicTocMsg13 *>(msg); //接收到的消息进行类型转换//如果消息到达目的地址if (ttmsg->getDestination() == getIndex()) {// Message arrived.EV << "Message " << ttmsg << " arrived after " << ttmsg->getHopCount() << " hops.\n";bubble("ARRIVED, starting new one!");delete ttmsg;// Generate another one.EV << "Generating another message: ";TicTocMsg13 *newmsg = generateMessage();EV << newmsg << endl;forwardMessage(newmsg);}else {// We need to forward the message.forwardMessage(ttmsg);}
}TicTocMsg13 *Txc13::generateMessage()
{// Produce source and destination addresses.生成源地址和目的地址。int src = getIndex();  // our module index我们的模块索引 0 int n = getVectorSize();  // module vector size模块矢量尺寸 ,共有6个节点,即模块int dest = intuniform(0, n-2); //0-4 均匀分布if (dest >= src) dest++;char msgname[20];sprintf(msgname, "tic-%d-to-%d", src, dest);// Create message object and set source and destination field.TicTocMsg13 *msg = new TicTocMsg13(msgname);msg->setSource(src);msg->setDestination(dest);return msg;
}void Txc13::forwardMessage(TicTocMsg13 *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);
}

在这里插入图片描述

tictoc 14 在13的基础上增加两变量显示于仿真界面

  1. 在此步骤中,我们跟踪发送和接收的消息数量,并将其显示在图标上方。
  2. WATCH(numSent); WATCH(numReceived);//收发次数的变量显示在仿真界面的模块上

msg

message TicTocMsg14
{int source;int destination;int hopCount = 0;
}

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++;
}

cc

#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;protected:virtual TicTocMsg14 *generateMessage();virtual void forwardMessage(TicTocMsg14 *msg);virtual void refreshDisplay() const override;virtual void initialize() override;virtual void handleMessage(cMessage *msg) override;
};Define_Module(Txc14);void Txc14::initialize()
{// Initialize variablesnumSent = 0;numReceived = 0;WATCH(numSent);//收发次数的变量显示在仿真界面的模块上WATCH(numReceived);// Module 0 sends the first messageif (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 arrivedint 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.forwardMessage(ttmsg);}
}TicTocMsg14 *Txc14::generateMessage()
{// Produce source and destination addresses.int src = getIndex();  // our module indexint n = getVectorSize();  // module vector sizeint 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];sprintf(buf, "rcvd: %ld sent: %ld", numReceived, numSent);getDisplayString().setTagArg("t", 0, buf);
}

在这里插入图片描述

tictoc 15 模型数据输出为直方图

  1. 这个模型足够令人兴奋,因此我们可以收集一些统计数据。我们将在输出向量中记录每条消息到达时的跳数。输出向量被写入omnetpp。vec文件,可以用Plove程序可视化。
  2. 我们还收集基本统计数据(最小值、最大值、平均值、std.dev.)和关于跳数的直方图,我们将在模拟结束时打印出这些数据。
  3. 仿真结束调用finish()
  4. recordScalar("#sent", numSent);//将一个double变量记录到标量结果文件中。
    msg
message TicTocMsg15
{int source;int destination;int hopCount = 0;
}

ned

simple Txc15
{parameters:@display("i=block/routing");gates:inout gate[];
}network Tictoc15
{types:channel Channel extends ned.DelayChannel {delay = 100ms;}submodules:tic[6]: Txc15;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++;
}

cc

#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
#include "tictoc15_m.h"using namespace omnetpp;/*** This model is exciting enough so that we can collect some statistics.* We'll record in output vectors the hop count of every message upon arrival.* Output vectors are written into the omnetpp.vec file and can be visualized* with the Plove program.** We also collect basic statistics (min, max, mean, std.dev.) and histogram* about the hop count which we'll print out at the end of the simulation.* 这个模型足够令人兴奋,因此我们可以收集一些统计数据。我们将在输出向量中记录每条消息到达时的跳数。输出向量被写入omnetpp。vec文件,可以用Plove程序可视化。我们还收集基本统计数据(最小值、最大值、平均值、std.dev.)和关于跳数的直方图,我们将在模拟结束时打印出这些数据。*/
class Txc15 : public cSimpleModule
{private:long numSent;long numReceived;cHistogram hopCountStats;//直方图类对象cOutVector hopCountVector;//输出向量protected:virtual TicTocMsg15 *generateMessage();//msg作消息virtual void forwardMessage(TicTocMsg15 *msg);virtual void initialize() override;virtual void handleMessage(cMessage *msg) override;// The finish() function is called by OMNeT++ at the end of the simulation://仿真结束之后会调用virtual void finish() override;
};Define_Module(Txc15);void Txc15::initialize()
{// Initialize variablesnumSent = 0;numReceived = 0;WATCH(numSent);WATCH(numReceived);hopCountStats.setName("hopCountStats");hopCountVector.setName("HopCount");// Module 0 sends the first messageif (getIndex() == 0) {// Boot the process scheduling the initial message as a self-message.TicTocMsg15 *msg = generateMessage();scheduleAt(0.0, msg);}
}void Txc15::handleMessage(cMessage *msg)
{TicTocMsg15 *ttmsg = check_and_cast<TicTocMsg15 *>(msg);if (ttmsg->getDestination() == getIndex()) {// Message arrivedint hopcount = ttmsg->getHopCount();EV << "Message " << ttmsg << " arrived after " << hopcount << " hops.\n";bubble("ARRIVED, starting new one!");// update statistics.更新统计数据numReceived++;hopCountVector.record(hopcount);hopCountStats.collect(hopcount);delete ttmsg;// Generate another one.EV << "Generating another message: ";TicTocMsg15 *newmsg = generateMessage();EV << newmsg << endl;forwardMessage(newmsg);numSent++;}else {// We need to forward the message.forwardMessage(ttmsg);}
}TicTocMsg15 *Txc15::generateMessage()
{// Produce source and destination addresses.int src = getIndex();int n = getVectorSize();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.TicTocMsg15 *msg = new TicTocMsg15(msgname);msg->setSource(src);msg->setDestination(dest);return msg;
}void Txc15::forwardMessage(TicTocMsg15 *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 Txc15::finish()
{// This function is called by OMNeT++ at the end of the simulation.EV << "Sent:     " << numSent << endl;EV << "Received: " << numReceived << endl;EV << "Hop count, min:    " << hopCountStats.getMin() << endl;EV << "Hop count, max:    " << hopCountStats.getMax() << endl;EV << "Hop count, mean:   " << hopCountStats.getMean() << endl;EV << "Hop count, stddev: " << hopCountStats.getStddev() << endl;recordScalar("#sent", numSent);//将一个double变量记录到标量结果文件中。recordScalar("#received", numReceived);hopCountStats.recordAs("hop count");
}

在这里插入图片描述


http://www.ppmy.cn/news/873.html

相关文章

【uvm】参数化Class中的静态属性

https://mp.weixin.qq.com/s/ISbgEao0NbAjAfWk9NAyXw static属性一般是在编译的时候就已经分配了内存,并被这个类的所有实例共享, 也就是在仿真时刻0之前就已经完成了静态属性的内存分配。 但是,参数化类中的静态属性可能有所区别。参数化类中的静态属性(参数化)是在参…

网站都变成灰色了,它是怎么实现的?

大家好&#xff0c;我是二哥呀。 想必大家都感受到了&#xff0c;很多网站、APP 在昨天都变灰了。 先来感受一下变灰后的效果。 这种灰色的效果怎么实现的呢&#xff1f;如何做到图片、文字、按钮都变灰的效果呢&#xff1f; 方案 1&#xff0c;换一套灰色的 UI&#xff0c;…

分布式搜索引擎 ElasticSearch(ES)

一、初识elasticsearch 1.了解ES 1&#xff09;elasticsearch的作用 elasticsearch是一款非常强大的开源搜索引擎&#xff0c;具备非常多强大功能&#xff0c;可以帮助我们从海量数据中快速找到需要的内容。elasticsearch结合kibana、Logstash、Beats&#xff0c;也就是elast…

【Spring框架】爆gan一万七千字,超详细的AOP技术详解,你真的不来看看吗?

✅作者简介&#xff1a;热爱Java后端开发的一名学习者&#xff0c;大家可以跟我一起讨论各种问题喔。 &#x1f34e;个人主页&#xff1a;Hhzzy99 &#x1f34a;个人信条&#xff1a;坚持就是胜利&#xff01; &#x1f49e;当前专栏&#xff1a;【Spring】 &#x1f96d;本文内…

Python数据分析实战-实现一维列表(数组)和多维列表(数组)的相互转化(附源码和实现效果)

前面我介绍了可视化的一些方法以及机器学习在预测方面的应用&#xff0c;分为分类问题&#xff08;预测值是离散型&#xff09;和回归问题&#xff08;预测值是连续型&#xff09;&#xff08;具体见之前的文章&#xff09;。 从本期开始&#xff0c;我将做一个数据分析类实战…

python零基础入门(完整版)

1python软件下载 我们需要下载python语言翻译器&#xff08;也就是运行环境&#xff09;&#xff0c;和一个用来写python的软件&#xff0c;用它写python语言比较方便 首先打开百度&#xff0c;下载org 然后点击下载 然后选择一个稳定版本下载 然后会跳转到一个页面&#xff0c…

第一天:Python元学习——通用人工智能的实现

文章目录0 封面1 第一章&#xff1a;元学习简介1.1 元学习与少样本学习1.2 元学习的类型——学习度量空间1.3 学习初始化1.4 学习优化器1.5 通过梯度下降来学习如何通过梯度下降来学习2 第二章&#xff1a;使用孪生网络进行人脸识别与音频视频2.1 什么是孪生神经网络孪生神经网…

云小课|基于华为云WAF的日志运维分析,构筑设备安全的城墙

阅识风云是华为云信息大咖&#xff0c;擅长将复杂信息多元化呈现&#xff0c;其出品的一张图(云图说)、深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云。更多精彩内容请单击此处。 摘要&#xff1a;云日志服务用于收集来自主机和云服务的日志数据&#x…