arrivalSignal = registerSignal("arrival");
返回给定信号名称的信号ID。信号名称和id是全局的。特定名称的信号ID在第一次registerSignal()
调用时被分配;对相同名称的进一步registerSignal()
调用将返回相同的ID。注意:自从omnet++ 4.3以来,信号注册表在运行之间不会被清除,所以可以使用静态初始化来分配全局simsignal_t变量: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);
}
@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);
}
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);
}
上一篇:面试半年,上个月成功拿到阿里offer,全靠我啃烂了学长给的这份笔记
下一篇:SpringSecurity(二十一)--OAuth2:实现资源服务器(中)实现带有JdbcTokenStore的黑板模式