【问题标题】:How to make one node communicate with multiple nodes? Omnet++如何让一个节点与多个节点通信?网络++
【发布时间】:2022-10-03 05:42:28
【问题描述】:

我试图让我的节点在不更改消息中的任何数据的情况下相互通信。 就像节点 1 和 2 回显 tictocMsg 一样,节点 2 和 3 在这种情况下会回显不同的消息 rndMsg。 这对我怎么不起作用。

simple Txc1
{
    gates:
        input in1;
        input in2;
        output out1;
        output out2;
}

//
// Two instances (tic and toc) of Txc1 connected both ways.
// Tic and toc will pass messages to one another.
//
network Tictoc1
{
    @display(\"bgb=628,433\");
    submodules:
        tic: Txc1 {
            @display(\"p=264,321\");
        }
        toc: Txc1;
        rnd: Txc1 {
            @display(\"p=474,100\");
        }
    connections allowunconnected:
        toc.out1 --> tic.in1;
        tic.out1 --> toc.in1;
        toc.out2 --> rnd.in1;
        rnd.out1 --> toc.in2;
}

i have 3 nodes as so

我想让 toc 节点仅将 tictocMsg 发送到 tic 节点,将 rndMsg 发送到 rnd 节点

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

using namespace omnetpp;

/**
 * Derive the Txc1 class from cSimpleModule. In the Tictoc1 network,
 * both the `tic\' and `toc\' modules are Txc1 objects, created by OMNeT++
 * at the beginning of the simulation.
 */
class Txc1 : public cSimpleModule
{
  protected:
    // The following redefined virtual function holds the algorithm.
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
};

// The module class needs to be registered with OMNeT++
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, \"out1\");
    }
    if (strcmp(\"rnd\",getName())==0){
        cMessage *msg = new cMessage(\"rndMsg\");
                send(msg, \"out1\");
    }
}

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
    send(msg,\"out1\");
    // send out the message
}

我试图将其更改为

send(msg,\"in1\",\"out1\") ;
send(msg,\"in2\",\"out2\") ;

试过了

send(msg,out1)}
else{
send(msg,out2)}
}

到目前为止,两者都对我不起作用有什么办法可以实现吗?

    标签: omnet++ cc


    【解决方案1】:

    中间的节点(即toc)必须以某种方式识别收到的消息。例如,它可能会检查消息的名称。让我们假设:

    • toc 收到名为tictocMsg 的消息后,将其发送给tic
    • toc 收到名为rndMsg 的消息后,将其发送给rnd
    • ticrnd 收到消息后发送给toc

    以下代码执行上述规则:

    void Txc1::handleMessage(cMessage *msg) {
      if (isName("toc")) {
        if (msg->isName("tictocMsg")) {
            send(msg,"out1");
        } else if (msg->isName("rndMsg")) {
            send(msg,"out2");
        }
      } else {
        // other nodes just sends this message back
        send(msg,"out1");
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-05
      • 2012-11-25
      • 1970-01-01
      • 2020-07-13
      • 2016-10-13
      • 2017-09-14
      • 2022-01-25
      • 1970-01-01
      相关资源
      最近更新 更多