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

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...