具体效果请自行复制运行
tictoc1
tictoc1.ned
simple Txc1 { gates: input in; output out; } network Tictoc1 { @display("bgb=171,129"); submodules: tic: Txc1; toc: Txc1 { @display("p=130,99"); } connections: tic.out --> { delay = 100ms; } --> toc.in; tic.in <-- { delay = 100ms; } <-- toc.out; }
txc1.cc
#include
#include
using namespace omnetpp; class Txc1 : public cSimpleModule { protected: virtual void initialize() override; virtual void handleMessage(cMessage *msg) override; }; Define_Module(Txc1); void Txc1::initialize() { if (strcmp("tic", getName()) == 0) { cMessage *msg = new cMessage("tictocMsg"); send(msg, "out"); } } void Txc1::handleMessage(cMessage *msg) { send(msg, "out"); }
tictoc2
tictoc2.ned
simple Txc2 { parameters: @display("i=block/routing"); // 添加一个图标(可以设置颜色) gates: input in; output out; } network Tictoc2 { @display("bgb=190,134"); submodules: tic: Txc2 { parameters: @display("i=,cyan"); // 基本格式为i=,+颜色,此处设置为cyan(青色) } toc: Txc2 { parameters: @display("i=,gold;p=152,92"); // 设颜色为gold(金色) } connections: tic.out --> { delay = 100ms; } --> toc.in; tic.in <-- { delay = 100ms; } <-- toc.out; }
ticto2.cc
#include
#include
using namespace omnetpp; class Txc2 : public cSimpleModule { protected: virtual void initialize() override; virtual void handleMessage(cMessage *msg) override; }; Define_Module(Txc2); void Txc2::initialize() { if (strcmp("tic", getName()) == 0) { // ev的效果等同于c++中的cout EV << "Sending initial message\n"; cMessage *msg = new cMessage("tictocMsg"); send(msg, "out"); } } void Txc2::handleMessage(cMessage *msg) { // msg->getName() 显示msg参数的名字,这里它将是"tictocMsg" EV << "Received message `" << msg->getName() << "', sending it out again\n"; send(msg, "out"); }
tictoc3
tictoc3.ned
simple Txc3 { parameters: @display("i=block/routing"); gates: input in; output out; } network Tictoc3 { submodules: tic: Txc3 { parameters: @display("i=,cyan"); } toc: Txc3 { parameters: @display("i=,gold"); } connections: tic.out --> { delay = 100ms; } --> toc.in; tic.in <-- { delay = 100ms; } <-- toc.out; }
tictoc3.cc
#include
#include
#include
using namespace omnetpp; class Txc3 : public cSimpleModule { private: int counter; // counter用来计数 protected: virtual void initialize() override; virtual void handleMessage(cMessage *msg) override; }; Define_Module(Txc3); void Txc3::initialize() { // 初始化counter为10.我们每次对他进行递减 // 当counter为0时,我们将消息删除 counter = 10; //watch()函数,可以让你检查括号中的变量,在仿真中,双击tic或者toc中的任意一个,在对话框 //中选择"counter"选项卡,你会发现"计数器"在列表中。(在仿真页面的左下部分) WATCH(counter); if (strcmp("tic", getName()) == 0) { EV << "Sending initial message\n"; cMessage *msg = new cMessage("tictocMsg"); send(msg, "out"); } } void Txc3::handleMessage(cMessage *msg) { //递减counter counter--; if (counter == 0) { // 如果counter为0,消息将被删除 // 仿真将会结束 EV << getName() << "'s counter reached zero, deleting message\n"; delete msg; } else { EV << getName() << "'s counter is " << counter << ", sending back message\n"; send(msg, "out"); } }
tictoc4
tictoc4.ned
simple Txc4 { parameters: bool sendMsgOnInit = default(false); //模块在初始化时是否发送信息 int limit = default(2); // 另一个限定,有一个默认值 @display("i=block/routing"); gates: input in; output out; } // // 添加模组限定条件 // network Tictoc4 { submodules: tic: Txc4 { parameters: sendMsgOnInit = true; @display("i=,cyan"); } toc: Txc4 { parameters: sendMsgOnInit = false; @display("i=,gold"); } connections: tic.out --> { delay = 100ms; } --> toc.in; tic.in <-- { delay = 100ms; } <-- toc.out; }
tictoc4.cc
#include
#include
#include
using namespace omnetpp; / * 在这一步,你要学会如何添加限定条件到仿真中 * 我们添加”magic number“10为限定条件 */ class Txc4 : public cSimpleModule { private: int counter; protected: virtual void initialize() override; virtual void handleMessage(cMessage *msg) override; }; Define_Module(Txc4); void Txc4::initialize() { // 用模组中的限定条件limit初始化counter // limit的初始化在"tictoc4.ned"中 counter = par("limit"); // 我们不再依靠名字判定它是否发送一个初始信息 // 利用限定条件bool型的sendMsgOnInit进行判断 if (par("sendMsgOnInit").boolValue() == true) { EV << "Sending initial message\n"; cMessage *msg = new cMessage("tictocMsg"); send(msg, "out"); } } void Txc4::handleMessage(cMessage *msg) { counter--; if (counter == 0) { EV << getName() << "'s counter reached zero, deleting message\n"; delete msg; } else { EV << getName() << "'s counter is " << counter << ", sending back message\n"; send(msg, "out"); } }
tictoc5 4、5相差不大
tictoc5.ned
simple Txc5 { parameters: bool sendMsgOnInit = default(false); int limit = default(2); @display("i=block/routing"); gates: input in; output out; } // 通过定义参数来专门化模块。我们本可以将整个正文留空,因为 sendMsgOnInit 参数的默认值无论如何都为 false。 // 请注意,限制参数在此仍未绑定。 // simple Tic5 extends Txc5 { parameters: @display("i=,cyan"); sendMsgOnInit = true; // Tic 将在初始化时发送信息 } simple Toc5 extends Txc5 { parameters: @display("i=,gold"); sendMsgOnInit = false; // Toc 在初始化时不发送信息 } network Tictoc5 { submodules: tic: Tic5; // 限制参数在此未绑定。我们将从 ini 文件得到它 toc: Toc5; connections: tic.out --> { delay = 100ms; } --> toc.in; tic.in <-- { delay = 100ms; } <-- toc.out; }
tictoc.cc
//同tictoc4.cc
tictoc6
tictoc6.ned
simple Txc6 { parameters: @display("i=block/routing"); gates: input in; output out; } network Tictoc6 { submodules: tic: Txc6 { parameters: @display("i=,cyan"); } toc: Txc6 { parameters: @display("i=,gold"); } connections: tic.out --> { delay = 100ms; } --> toc.in; tic.in <-- { delay = 100ms; } <-- toc.out; }
tictoc6.cc
#include
#include
#include
using namespace omnetpp; / * 在以前的模型中,"tic"和"toc"立即发送回收到的消息。在这里,我们将添加一些计时:tic 和 toc * 将保存消息 1 个模拟秒,然后再将其发送回来。在 omnet++ 中,这种计时是通过模块向自身发送消息 * 实现的。此类消息称为自我消息(但仅因它们使用,否则它们是完全普通的消息)或事件。可以使用 * scheduleAt() 函数发送自我消息,并可以指定它们何时应返回模块。 */ class Txc6 : public cSimpleModule { private: cMessage *event; // 指向事件对象的指针,我们将用于计时 cMessage *tictocMsg; // 变量记住消息,直到我们发送回来 public: Txc6(); virtual ~Txc6(); protected: virtual void initialize() override; virtual void handleMessage(cMessage *msg) override; }; Define_Module(Txc6); Txc6::Txc6() { // 将指针设置为空指针,以便析构函数不会崩溃 // 即使初始化由于启动过程中运行时错误或用户取消而未调用。 event = tictocMsg = nullptr; } Txc6::~Txc6() { // 释放动态分配的对象 cancelAndDelete(event); delete tictocMsg; } void Txc6::initialize() { // 创建我们将用于计时的事件对象 ——只是一些普通消息。 event = new cMessage("event"); // 尚未发送 tictoc 消息。 tictocMsg = nullptr; if (strcmp("tic", getName()) == 0) { // 我们不会马上开始,而是向自己发送信息)——我们将在 t=5.0s 模拟时间进行第一次发送。 EV << "Scheduling first send to t=5.0s\n"; tictocMsg = new cMessage("tictocMsg"); scheduleAt(5.0, event); } } void Txc6::handleMessage(cMessage *msg) { // 有几种方法可以区分消息,例如按消息类型(cMessage 的 int 属性)或使用 // dynamic_cast(前提是从 cMessage 的子类)。在此代码中,我们只需检查是否识别指针,该指 // 针(如果可行)是最简单和最快的方法。 if (msg == event) { //自消息到达,因此我们可以发送 tictocMsg 和 nullptr 出其指针, // 以便它不会混淆我们以后。 EV << "Wait period is over, sending back message\n"; send(tictocMsg, "out"); tictocMsg = nullptr; } else { // 如果我们收到的消息不是我们的自我信息, // 那么它必须是来自我们合作伙伴的 tic-toc 消息。 // 我们记住它在 tictocMsg 变量中的指针, // 然后安排我们的自消息在 1s 模拟时间返回给我们。 EV << "Message arrived, starting to wait 1 sec...\n"; tictocMsg = msg; scheduleAt(simTime()+1.0, event); } }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/220166.html原文链接:https://javaforall.net
