tictoc例子理解 16-18
创始人
2024-03-12 18:24:56
0

tictoc16-18

    • tictoc 16 全局信号signal
    • tictoc 17 在仿真界面幕布上显示总条数信息
    • tictoc 18

tictoc 16 全局信号signal

  1. 前一步的主要问题是,如果我们想要更改所收集的统计信息,就必须修改模型的代码。统计计算深入到模型代码中,很难修改和理解。
  2. omnet++ 4.1提供了一种叫做“信号”的不同机制,我们可以用它来收集统计数据。
  3. 首先,我们必须识别模型状态发生变化的事件。我们可以在这些点上发射带有所选状态变量值的信号。
  4. 通过这种方式,c++代码只发出信号,但是如何处理这些信号仅由附加到它们的侦听器决定。
  5. 模型发出的信号和处理它们的侦听器可以使用’signal’和’statistic’属性在NED文件中定义。
  6. 我们将收集与上一步相同的统计信息,但请注意,我们不需要任何私有成员变量来计算这些值。我们将只使用一个信号,该信号在消息到达时发出,并在消息中携带hopcount。
  7. arrivalSignal = registerSignal("arrival");返回给定信号名称的信号ID。信号名称和id是全局的。特定名称的信号ID在第一次registerSignal()调用时被分配;对相同名称的进一步registerSignal()调用将返回相同的ID。注意:自从omnet++ 4.3以来,信号注册表在运行之间不会被清除,所以可以使用静态初始化来分配全局simsignal_t变量:
    msg
message TicTocMsg16
{int source;int destination;int hopCount = 0;
}

ned

simple Txc16
{parameters:@signal[arrival](type="long");//long型变量@statistic[hopCount](title="hop count"; source="arrival"; record=vector,stats; interpolationmode=none);@display("i=block/routing");gates:inout gate[];
}network Tictoc16
{types:channel Channel extends ned.DelayChannel {delay = 100ms;}submodules:tic[6]: Txc16;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 
#include 
#include 
#include "tictoc16_m.h"using namespace omnetpp;/*** The main problem with the previous step is that we must modify the model's* code if we want to change what statistics are gathered. Statistic calculation* is woven deeply into the model code which is hard to modify and understand.** OMNeT++ 4.1 provides a different mechanism called 'signals' that we can use* to gather statistics. First we have to identify the events where the state* of the model changes. We can emit signals at these points that carry the value* of chosen state variables. This way the C++ code only emits signals, but how those* signals are processed are determined only by the listeners that are attached to them.** The signals the model emits and the listeners that process them can be defined in* the NED file using the 'signal' and 'statistic' property.** We will gather the same statistics as in the previous step, but notice that we will not need* any private member variables to calculate these values. We will use only a single signal that* is emitted when a message arrives and carries the hopcount in the message.* 前一步的主要问题是,如果我们想要更改所收集的统计信息,就必须修改模型的代码。统计计算深入到模型代码中,很难修改和理解。omnet++ 4.1提供了一种叫做“信号”的不同机制,我们可以用它来收集统计数据。首先,我们必须识别模型状态发生变化的事件。我们可以在这些点上发射带有所选状态变量值的信号。通过这种方式,c++代码只发出信号,但是如何处理这些信号仅由附加到它们的侦听器决定。模型发出的信号和处理它们的侦听器可以使用'signal'和'statistic'属性在NED文件中定义。我们将收集与上一步相同的统计信息,但请注意,我们不需要任何私有成员变量来计算这些值。我们将只使用一个信号,该信号在消息到达时发出,并在消息中携带hopcount。*/
class Txc16 : public cSimpleModule
{private:simsignal_t arrivalSignal;protected:virtual TicTocMsg16 *generateMessage();virtual void forwardMessage(TicTocMsg16 *msg);virtual void initialize() override;virtual void handleMessage(cMessage *msg) override;
};Define_Module(Txc16);void Txc16::initialize()
{arrivalSignal = registerSignal("arrival");//返回信号的名称和ID// Module 0 sends the first messageif (getIndex() == 0) {// Boot the process scheduling the initial message as a self-message.TicTocMsg16 *msg = generateMessage();scheduleAt(0.0, msg);}
}void Txc16::handleMessage(cMessage *msg)
{TicTocMsg16 *ttmsg = check_and_cast(msg);if (ttmsg->getDestination() == getIndex()) {// Message arrivedint hopcount = ttmsg->getHopCount();// send a signal 发出一个号emit(arrivalSignal, hopcount);EV << "Message " << ttmsg << " arrived after " << hopcount << " hops.\n";bubble("ARRIVED, starting new one!");delete ttmsg;// Generate another one.EV << "Generating another message: ";TicTocMsg16 *newmsg = generateMessage();EV << newmsg << endl;forwardMessage(newmsg);}else {// We need to forward the message.forwardMessage(ttmsg);}
}TicTocMsg16 *Txc16::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.TicTocMsg16 *msg = new TicTocMsg16(msgname);msg->setSource(src);msg->setDestination(dest);return msg;
}void Txc16::forwardMessage(TicTocMsg16 *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 17 在仿真界面幕布上显示总条数信息

  1. 仿真幕布显示内容:
@figure[description](type=text; pos=5,20; font=,,bold; text="Random routing example - displaying last hop count");@figure[lasthopcount](type=text; pos=5,35; text="last hopCount: N/A");

msg略
ned

simple Txc17
{parameters:@signal[arrival](type="long");@statistic[hopCount](title="hop count"; source="arrival"; record=vector,stats; interpolationmode=none);@display("i=block/routing");gates:inout gate[];
}network Tictoc17
{parameters:@figure[description](type=text; pos=5,20; font=,,bold; text="Random routing example - displaying last hop count");@figure[lasthopcount](type=text; pos=5,35; text="last hopCount: N/A");types:channel Channel extends ned.DelayChannel {delay = 100ms;}submodules:tic[6]: Txc17;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 
#include 
#include 
#include "tictoc17_m.h"using namespace omnetpp;class Txc17 : public cSimpleModule
{
private:simsignal_t arrivalSignal;protected:virtual TicTocMsg17 *generateMessage();virtual void forwardMessage(TicTocMsg17 *msg);virtual void initialize() override;virtual void handleMessage(cMessage *msg) override;
};Define_Module(Txc17);void Txc17::initialize()
{arrivalSignal = registerSignal("arrival");// Module 0 sends the first messageif (getIndex() == 0) {// Boot the process scheduling the initial message as a self-message.TicTocMsg17 *msg = generateMessage();scheduleAt(0.0, msg);}
}void Txc17::handleMessage(cMessage *msg)
{TicTocMsg17 *ttmsg = check_and_cast(msg);if (ttmsg->getDestination() == getIndex()) {// Message arrivedint hopcount = ttmsg->getHopCount();// send a signalemit(arrivalSignal, hopcount);if (hasGUI()) {char label[50];// Write last hop count to stringsprintf(label, "last hopCount = %d", hopcount);// Get pointer to figurecCanvas *canvas = getParentModule()->getCanvas();cTextFigure *textFigure = check_and_cast(canvas->getFigure("lasthopcount"));// Update figure texttextFigure->setText(label);}EV << "Message " << ttmsg << " arrived after " << hopcount << " hops.\n";bubble("ARRIVED, starting new one!");delete ttmsg;// Generate another one.EV << "Generating another message: ";TicTocMsg17 *newmsg = generateMessage();EV << newmsg << endl;forwardMessage(newmsg);}else {// We need to forward the message.forwardMessage(ttmsg);}
}TicTocMsg17 *Txc17::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.TicTocMsg17 *msg = new TicTocMsg17(msgname);msg->setSource(src);msg->setDestination(dest);return msg;
}void Txc17::forwardMessage(TicTocMsg17 *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 18

  1. 前一步的主要问题是,如果我们想要更改所收集的统计信息,就必须修改模型的代码。统计计算深入到模型代码中,很难修改和理解。
  2. omnet++ 4.1提供了一种叫做“信号”的不同机制,我们可以用它来收集统计数据。首先,我们必须识别模型状态发生变化的事件。我们可以在这些点上发射带有所选状态变量值的信号。通过这种方式,c++代码只发出信号,但是如何处理这些信号仅由附加到它们的侦听器决定。
  3. 模型发出的信号和处理它们的侦听器可以使用’signal’和’statistic’属性在NED文件中定义。
  4. 我们将收集与上一步相同的统计信息,但请注意,我们不需要任何私有成员变量来计算这些值。我们将只使用一个信号,该信号在消息到达时发出,并在消息中携带hopcount。
    msg略
    ned
simple Txc18 extends Txc16
{
}network TicToc18
{parameters:int numCentralNodes = default(2);types:channel Channel extends ned.DelayChannel {delay = 100ms;}submodules:tic[numCentralNodes+4]: Txc18;connections:// connect the 2 nodes in one side to the central nodestic[0].gate++ <--> Channel <--> tic[2].gate++;tic[1].gate++ <--> Channel <--> tic[2].gate++;// connect the central nodes together将中心节点连接在一起for i=2..numCentralNodes+1 {tic[i].gate++ <--> Channel <--> tic[i+1].gate++;}// connect the 2 nodes on the other side to the central nodestic[numCentralNodes+2].gate++ <--> Channel <--> tic[numCentralNodes+1].gate++;tic[numCentralNodes+3].gate++ <--> Channel <--> tic[numCentralNodes+1].gate++;
}

cc

#include 
#include 
#include 
#include "tictoc18_m.h"using namespace omnetpp;/*** The main problem with the previous step is that we must modify the model's* code if we want to change what statistics are gathered. Statistic calculation* is woven deeply into the model code which is hard to modify and understand.** OMNeT++ 4.1 provides a different mechanism called 'signals' that we can use* to gather statistics. First we have to identify the events where the state* of the model changes. We can emit signals at these points that carry the value* of chosen state variables. This way the C++ code only emits signals, but how those* signals are processed are determined only by the listeners that are attached to them.** The signals the model emits and the listeners that process them can be defined in* the NED file using the 'signal' and 'statistic' property.** We will gather the same statistics as in the previous step, but notice that we will not need* any private member variables to calculate these values. We will use only a single signal that* is emitted when a message arrives and carries the hopcount in the message.* 前一步的主要问题是,如果我们想要更改所收集的统计信息,就必须修改模型的代码。统计计算深入到模型代码中,很难修改和理解。omnet++ 4.1提供了一种叫做“信号”的不同机制,我们可以用它来收集统计数据。首先,我们必须识别模型状态发生变化的事件。我们可以在这些点上发射带有所选状态变量值的信号。通过这种方式,c++代码只发出信号,但是如何处理这些信号仅由附加到它们的侦听器决定。模型发出的信号和处理它们的侦听器可以使用'signal'和'statistic'属性在NED文件中定义。我们将收集与上一步相同的统计信息,但请注意,我们不需要任何私有成员变量来计算这些值。我们将只使用一个信号,该信号在消息到达时发出,并在消息中携带hopcount。*/
class Txc18 : public cSimpleModule
{private:simsignal_t arrivalSignal;protected:virtual TicTocMsg18 *generateMessage();virtual void forwardMessage(TicTocMsg18 *msg);virtual void initialize() override;virtual void handleMessage(cMessage *msg) override;
};Define_Module(Txc18);void Txc18::initialize()
{arrivalSignal = registerSignal("arrival");// Module 0 sends the first messageif (getIndex() == 0) {// Boot the process scheduling the initial message as a self-message.TicTocMsg18 *msg = generateMessage();scheduleAt(0.0, msg);}
}void Txc18::handleMessage(cMessage *msg)
{TicTocMsg18 *ttmsg = check_and_cast(msg);if (ttmsg->getDestination() == getIndex()) {// Message arrivedint hopcount = ttmsg->getHopCount();// send a signalemit(arrivalSignal, hopcount);EV << "Message " << ttmsg << " arrived after " << hopcount << " hops.\n";bubble("ARRIVED, starting new one!");delete ttmsg;// Generate another one.EV << "Generating another message: ";TicTocMsg18 *newmsg = generateMessage();EV << newmsg << endl;forwardMessage(newmsg);}else {// We need to forward the message.forwardMessage(ttmsg);}
}TicTocMsg18 *Txc18::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.TicTocMsg18 *msg = new TicTocMsg18(msgname);msg->setSource(src);msg->setDestination(dest);return msg;
}void Txc18::forwardMessage(TicTocMsg18 *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);
}

相关内容

热门资讯

不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...