一些OMNET使用心得

article/2025/7/20 21:31:34

一些菜鸡学习心得,如果有错的话希望大佬能帮忙指出,感激不尽!!

(底层组织结构是大佬帮忙写的,感谢大佬带入门)

  • 项目组织

\prj

\prjname

\simulation

\results

package.ned

omnet.ini

network.ned

\src

package.ned

host.ned

host.cc

host.h

其中ned主要负责简单模块、复杂模块、网络等的组件与拓扑的描述。h文件来声明,c文件来定义模块的行为。

如简单的tictoc项目:

  1. tictoc.ned

simple Txc1

{

    gates:

        input in;

        output out;

}

  1. tictoc.h

class Txc1 : public cSimpleModule

{

  protected:

    // The following redefined virtual function holds the algorithm.

    virtual void initialize() override;

    virtual void handleMessage(cMessage *msg) override;

};

  1. tictoc.cc

Define_Module(Txc1);

void Txc1::initialize()

{

    // Initialize is called at the beginning of the simulation.

    // To bootstrap the tic-toc-tic-toc process, one of the modules needs

    // to send the first message. Let this be `tic'.

    // Am I Tic or Toc?

    if (strcmp("tic", getName()) == 0) {

        // create and send first message on gate "out". "tictocMsg" is an

        // arbitrary string which will be the name of the message object.

        cMessage *msg = new cMessage("tictocMsg");

        send(msg, "out");

    }

}

void Txc1::handleMessage(cMessage *msg)

{

    // The handleMessage() method is called whenever a message arrives

    // at the module. Here, we just send it to the other module, through

    // gate `out'. Because both `tic' and `toc' does the same, the message

    // will bounce between the two.

    send(msg, "out"); // send out the message

}

而ini文件则是进行网络的配置,如:

[Tictoc1]

network = Tictoc1

以上是最简单的网络配置,后续还可以通过在ned中定义一些参数然后在ini中修改它来配置需要的网络。

复杂的网络则会在src文件夹中定义简单模块与复杂模块,然后在simulation文件夹中定义网络的ned及ini文件。

  • 新建项目

File->new->OMNET++ Project

 

填写项目名称:

 

->NEXT->选择空项目/带src和simulation文件夹的项目。网络上的简易教程多为空项目,但带俩文件夹的项目后续会更适合复杂network的组织。

 

一般情况下,simulation文件夹放网络模块文件及ini配置,src文件夹放组织网络的简单及复杂模块(如node、host等)

三、简单模块与复杂模块的介绍及拼装网络

这部分以简易自组织网络为例介绍简单模块与复杂模块的ned部分。

简单的maclayer.ned:

package tdma_mac_demo;

//此处定义文件位置,处于tdma_mac_demo/src/maclayer.ned

//若需要在tdma_mac_demo/文件夹下但/src以外的地方(如simulation)引用它

//则需要import tdma_mac_demo/maclayer.ned

simple MacLayer

{

    parameters:

        @display("i=block/routing");

        int numSlots = default(10);

        double slotDuration @unit(s) = default(500ms);

        string slotAllocate = default("");

    gates:

        input upperIn;

        output upperOut;

        input phyIn;

        output phyOut;

}

简单来说,简单模块都是这个样子:

package ……;

simple ……

{

    parameters:

        ……

    gates:

        input ……;

        output ……;

}

而用简单模块的复杂模块则长这个样子:

package ……;

simple Node

{

    parameters:

        ……

    gates:

        input ……;

        output ……;

}

submodules:

        trafficGen: TrafficGen {

            ……

        }

        macLayer: MacLayer {

            ……

        }

        phyLayer: PhyLayer {

            ……

        }

connections:

trafficGen.lowerOut --> macLayer.upperIn;

……

}

再把复杂模块node拼装成一个网络network:

package tdma_mac_demo.simulations;

import tdma_mac_demo.DemoNode;

network DemoNetwork

{

    

    parameters:

        @display("bgb=500,500");

        int nodenum = default(5);

    types:

        channel Channel extends ned.DatarateChannel

        {

            datarate = 100Mbps;

            delay = default(100ms);

        }

    submodules:

        node[nodenum]: DemoNode;

    connections:

        for i=0..sizeof(node[0].radioIn) , for j = 0..sizeof(node[0].radioIn){

        node[i].radioOut++ --> Channel --> node[j].radioIn++ if i!=j ;}

        

}

很显然,parameters指参数,default()为默认,默认后就可以在ini快乐的更改它了~

gates指门(用来连接);submodule为子模块;connections指连接,用-->或者<--来表示。Channel为信道,可以直接继承(extends)omnet库中的信道类型,直接在types中定义一些参数的值。

注:

在parameters中可以用@class属性明确指定C++类,若未指定则默认当前文件夹的下的同名文件。

在ned中定义的参数,可以直接赋值,也可以设为默认后在ini文件中配置它。而在ini文件中赋值并不能覆盖ned中的赋值。参数还有一些属性,如@mutable、@unit、@derectIn等。

更具体可以参照《OMNET++与网络仿真 赵永利 张杰 著》及其他教程。

四、简单模块行为描述

首先举一个例子,下述是Txc1.h文件。和普通的C++类一样的声明,但继承简单模块cSimpleModule,initialize()、handleMessage()也是必须的——须在.cc文件中进行初始化及处理信息的描述。

class Txc1 : public cSimpleModule

{

  protected:

    // The following redefined virtual function holds the algorithm.

    virtual void initialize() override;

    virtual void handleMessage(cMessage *msg) override;

};

然后看Txc1.cc文件:

Define_Module(Txc1);

void Txc1::initialize()

{

    // Initialize is called at the beginning of the simulation.

    // To bootstrap the tic-toc-tic-toc process, one of the modules needs

    // to send the first message. Let this be `tic'.

    // Am I Tic or Toc?

    if (strcmp("tic", getName()) == 0) {

        // create and send first message on gate "out". "tictocMsg" is an

        // arbitrary string which will be the name of the message object.

        cMessage *msg = new cMessage("tictocMsg");

        send(msg, "out");

    }

}

void Txc1::handleMessage(cMessage *msg)

{

    // The handleMessage() method is called whenever a message arrives

    // at the module. Here, we just send it to the other module, through

    // gate `out'. Because both `tic' and `toc' does the same, the message

    // will bounce between the two.

    send(msg, "out"); // send out the message

}

在仿真时会自动运行上述两个函数,也可以加入其他函数的声明及定义,但需要要initialize()和handleMessage()来call它们。

initialize()和handleMessage()都如其名,进行初始化和处理消息,初始化函数则是在仿真进行的开始对模块进行初始化,可以将ned中的参数导入,初始化变量等。处理消息函数则是在消息来到时自行调用该函数对msg进行处理,这个msg包括外来消息及自消息。

这个自消息则是一个新概念,比如modula A可以自己给自己发消息,用scheduleAfter ( delaytime, msg),在delaytime后发送msg给自己,就像定闹钟,到时间了告诉自己该起床、开会等等。

写不动了……


http://chatgpt.dhexx.cn/article/3y64W9An.shtml

相关文章

OMNET的安装说明

1.安装MingGW GCC编译环境的安装程序 1.下载安装包&#xff1a; http://www.mingw.org/download/installer 2.自动弹出一个窗口 3.选择下载  左栏选中basic Setup&#xff0c;右栏选中GCC和G&#xff0c;选择的方式右键 Mark for Installation 4.下载成功  点击菜单栏的In…

2022-04-21 Omnet学习笔记(一)

Omnet学习笔记&#xff08;一&#xff09; 前言 1、本机为Inspiron 5005&#xff0c;为64位&#xff0c;所用操作系统为Windos 10。所使用软件为Omnet-5.2.1。 2、推荐Omnet入门教程系列 实验现象 在网络Tictoc1中创建两个节点&#xff0c;分别命名为"tic"和"t…

OMNeT++学习(概述+框架)一

中文手册地址 之前一直学习的是mininet&#xff0c;但是所做的方向有一个开源的代码&#xff0c;和我需要的框架一样&#xff0c;它用的仿真器是Omnetpp-5.4.1。 概述 omnet是开源的基于组件的模块化的开放网络仿真平台。是离散事件仿真器&#xff0c;具备强大完善的图形界面…

OMNeT学习之OMNeT安装与运行

OMNeT学习之OMNeT安装与运行 前言 本文原创&#xff0c;创作不易&#xff0c;转载请注明&#xff01;&#xff01;&#xff01; 本人为OMNet初学者&#xff0c;如有错误&#xff0c;请不吝请教&#xff01; 最近学WSN(无线传感器网络)&#xff0c;做传感器实验&#xff0c;老…

omnet++ 快速入门 | 计算机网络仿真 | omnet++ 入门教程

文章目录 omnet1 网络仿真软件综述01 wsn仿真软件02 为什么使用omnet03 学习资料04 目标 2 omnet介绍1 omnet安装与启动2 使用omnet仿真的一般步骤3 新建项目 3 定义仿真网络的拓扑结构01 新建一个空的 network02 Module的概念01-Module的概念02-Module Type的定义步骤1. ned文…

无图无真相!设计模式概览

设计模式太多&#xff0c;最近没有更新&#xff0c;最后想想还是看一点更新一点吧。

Mac系统Safari浏览器启动无图模式

有的时候我们用热点上网&#xff0c;图片的出现会消耗大量的流量&#xff0c;这时候就需要启动无图模式不加载图片。 步骤&#xff1a;启动Safari浏览器->偏好设置->高级->勾选“在菜单栏中显示“开发”菜单”->点击开发菜单->勾选“停用图像” 转载于:https://…

无图无真相!工厂模式结构

抽象类工厂模式结构图&#xff1a; 接口类工厂模式结构图&#xff1a; 工厂模式示例代码 工厂静态方法说明

设计模式之代理模式(Proxy Pattern)

1.概念 为其他对象提供一种代理&#xff0c;以控制对这个对象的访问。代理对象起到中介作用&#xff0c;可去掉功能或者增加额外功能。 2.角色 抽象主题角色&#xff08;Subject&#xff09;&#xff1a;声明了目标对象和代理对象的共同接口&#xff0c;这样一来在任何可以使用…

树莓派4B配置Selenium库+设置无图无头属性

写一个树莓派4B配置selenium库&#xff0c;并成功调用浏览器进行自动化爬虫 本文会手把手教你配置环境&#xff0c;并带你写第一个selenium爬虫程序 使用版本说明 系统&#xff1a;官网2020-02-13-raspbian-buster-full Python&#xff1a;树莓派自带的3.7.3 以下所有的pip操…

屏蔽图片的chrome插件

有时候觉得CSDN博客的广告好烦啊&#xff0c;还有有些看小说的网站&#xff0c;于是找了个屏蔽图片的插件。 效果如图&#xff1a; 之前博客的广告 之后&#xff1a; CSDN的广告有字&#xff0c;所以不会完全不显示&#xff0c;但是有很多小说网站都可以直接全部屏蔽掉…

python selenium playwright库使用教程 破解网页防止开发者模式 截取数据请求

安装chromedriver 下载 chromedriver的版本一定要与Chrome的版本一致&#xff0c;不然就不起作用。 有两个下载地址&#xff1a; 1、http://chromedriver.storage.googleapis.com/index.html 2、CNPM Binaries Mirror 当然&#xff0c;你首先需要查看你的Chrome版本&…

360浏览器,怎么开启无图片模式,在开热点的时候,省流量

如果某些网站&#xff0c;我需要加载图片&#xff0c;如csdn上传图片的时候。 这时候我们需要进行放权&#xff1a;

【Playwright】关于无痕模式与无头模式

Playwright的无头模式和无痕模式 无头模式 无头模式指的是自动测试框架在执行过程中不打开浏览器窗口的功能。在Playwright中&#xff0c;关闭无头模式会在测试代码执行过程中显示浏览器窗口&#xff0c;执行的过程会显示在浏览器的上。 # 以下代码以Playwright的同步API为例…

爬虫之selenium开启无界面模式

绝大多数服务器是没有界面的&#xff0c;selenium控制谷歌浏览器也是存在无界面模式的&#xff08;又称之为无头模式&#xff09; 开启无界面模式的方法 实例化配置对象 options webdriver.ChromeOptions()配置对象添加开启无界面模式的命令 options.add_argument("--he…

locust入门 -6 无图模式和分布式执行

无图模式 无图模式即不通过UI界面进行执行locust测试用例。locust提供了命令参数&#xff0c;我们只需要在执行时添加一个--headless标签即可 “Disable the web interface, and start the test immediately. Use -u and -t to control user count and run time ” 以下内容为…

Python爬虫配置Selenium库+设置无图无头属性

出一个最新windows下配置selenium全环境的教程~ 本文会手把手教你配置环境&#xff0c;并带你写第一个selenium爬虫程序 环境配置 1.安装Selenium库 Python和Anaconda-Python中并不包含Selenium包&#xff0c;这里我们要安装一下Selenium这个包 以下三种方式认选一种即可&am…

chrome如何进入无图模式?

chrom只显示文字不显示图片 隐私和安全-图片 可以设置所有网站都不显示图片 自定义指定网站不允许显示图片

locust入门 —— 无图模式和分布式执行

无图模式 无图模式即不通过UI界面进行执行locust测试用例。locust提供了命令参数&#xff0c;我们只需要在执行时添加一个--headless标签即可 “ Disable the web interface, and start the test immediately. Use -u and -t to control user count and run time ” 以下内容为…

PC端浏览器如何设置无图模式

以谷歌浏览器为例&#xff0c;注意有些浏览器并不支持该功能。 1&#xff09;打开自定义与控制 2&#xff09;选择设置 3&#xff09;查看左边状态栏&#xff0c;选择高级设置--》隐私设置和安全性 4&#xff09;选择内容设置 5&#xff09;图片 6&#xff09;选择不显示任何图…