omnet++中tictoc实例(中文注释) 1-6

omnet++中tictoc实例(中文注释) 1-6文章目录具体效果请自行复制运行 tictoc1ticto 5 相差不大 tictoc6 具体效果请自行复制运行 tictoc1ticto nedsimpleTxc gates inputin outputout networkTicto display bgb 171 129 submodules tic Txc1

具体效果请自行复制运行

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

(0)
上一篇 2026年3月17日 下午9:05
下一篇 2026年3月17日 下午9:05


相关推荐

  • Oracle导入.DMP文件命令

    Oracle导入.DMP文件命令imp 导入 dmp 该命令需要在 cmd 的 dos 命令窗口直接执行 而不是 sqlplus exe full y 是导入文件中全部内容 ignore y 相当于 如果没有的表 创建并倒入数据 如果已经有的表 忽略创建的 但不忽略倒入 imp 用户 密码 file 文件路径 full yignore y

    2026年3月19日
    2
  • ajax跨域问题以及解决方案_js跨域请求的三种方法

    ajax跨域问题以及解决方案_js跨域请求的三种方法出于浏览器的同源策略限制。同源策略(Sameoriginpolicy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的,浏览器只是针对同源策略的一种实现。同源策略会阻止一个域的javascript脚本和另外一个域的内容进行交互。所谓同源(即指在同一个域)就是两个页面具有相同的协议(protocol),主机(host)和端口号(port)AJAX跨域请求下面简单模拟一个场景—–>>前端有.

    2022年8月24日
    8
  • Retrofit2 详解和使用(一)

    Retrofit2 详解和使用(一)前言 要么出击 要么出局 命运女神总会眷顾拼尽全力的一方 一 概述 1 什么是 retrofitretr 是现在比较流行的网络请求框架 可以理解为 okhttp 的加强版 底层封装了 Okhttp 准确来说 Retrofit 是一个 RESTful 的 http 网络请求框架的封装 因为网络请求工作本质上是由 okhttp 来完成 而 Retrofit 负责网络请求接口的封装 本质过

    2026年3月20日
    2
  • SplitContainer容器控件左右Panel大小调整「建议收藏」

    SplitContainer容器控件左右Panel大小调整「建议收藏」1、新建一个Winform窗体,从上图中选择SplitContainer空间,拖拽到Form到上,如下图:2、你会发现,随便点击Panel1或者Panel2,会显示出粗框,但怎么调整两个Panel的大小呢?两个Panel之间的那条线,是选不中的,哈哈,不信可以试试。那么如何才能调整两个Form的大小呢?==》随便单击一个Panel,再按一下Esc,会出现下图:这时,

    2022年7月18日
    98
  • java中jersey什么意思_java jersey介绍

    java中jersey什么意思_java jersey介绍简介Jersey是JAX-RS(JSR311)开源参考实现用于构建RESTfulWebservice,它包含三个部分:核心服务器(CoreServer):通过提供JSR311中标准化的注释和API标准化,可以用直观的方式开发RESTfulWeb服务。核心客户端(CoreClient):Jersey客户端API能够帮助开发者与RESTful服务轻松通信;集成(Integration):J…

    2022年7月12日
    47
  • 谷歌大神出手,免费发布《智能体设计模式》,AI Agent开发的终极秘籍

    谷歌大神出手,免费发布《智能体设计模式》,AI Agent开发的终极秘籍

    2026年3月15日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号