omn​​et++ 三个节点或三个以上之间的通信

问题描述

所以我想配置三个节点如何相互通信,但有两种方式。我尝试了不同的方法,但所有的错误都不同,它甚至没有运行模拟,但是这段代码运行了模拟 GUI 但无法运行模拟

first NO:
Node 1 --> Node 2 --> Node 3 --> Node 1 --> terminate & display round trip time
Second NO:
Node 1 --> Node 2 --> Node 3 --> Node 1
Node 1 <-- Node 2 <-- Node 3 <-- Node 1

代码如下

.NED 文件代码

network Network
{
    @display("bgb=437,321");

    types:
        simple Test
        {
            gates:
                input input_gate[];
                output output_gate[];
        }

    submodules:
        Node0: Test {
            @display("p=43,85");
        }
        Node1: Test {
            @display("p=173,54");
        }
        test: Test {
            @display("p=183,172");
        }

    connections:


        Node0.output_gate++ --> Node1.input_gate++;
        Node1.output_gate++ --> test.input_gate++;
        test.output_gate++ --> Node0.input_gate++;
        test.output_gate++ --> Node1.input_gate++;
        Node1.output_gate++ --> Node0.input_gate++;
        Node0.output_gate++ --> test.input_gate++;
}

.CC 代码

#include <stdio.h>
#include <string.h>
#include <omnetpp.h>  

using namespace omnetpp;
class Test : public cSimpleModule
{
  protected:
    virtual void initialize() override;
    virtual void handleMessage(cmessage *msg) override;

};

Define_Module(Test);


void Test::initialize()
{

    if (strcmp("Node0",getName()) == 0) {
        cmessage *msg = new cmessage("tictocMsg");
                send(msg,"output_gate");


    }
}

void Test::handleMessage(cmessage *msg){
        send(msg,"output_gate");
}

解决方法

在网络中,您配置节点之间的连接,而不是数据包必须经过的路由。所以那部分是不正确的。事实上,每个节点都必须连接到每个其他节点,然后你必须在 .cc 文件中实现你的路由逻辑。

第一个场景相对简单,因为您实际上只是接收消息并在输出门上发送出去。但您必须使用单个门(而不是矢量门)。这样你当前的 .cc 代码就可以工作了。

第二种情况有点复杂。在这种情况下,任何收到的消息都应该在 other 门上发送,因此您必须检查传入消息的门。