同时从所有节点发送消息

时间:2015-10-09 22:31:35

标签: omnet++

以下是.ned文件

simple computer
{
    parameters:

    gates:
        input in1;
        output out1;
        input in2;
        output out2;

}

//
// TODO documentation
//
network Network
{
    @display("bgb=538,302");
    submodules:
        A: computer {
            @display("p=30,88");
        }
        B: computer {
            @display("p=344,96");
        }
        C: computer {
            @display("p=209,199");
        }
    connections:

        A.out1 --> B.in1;
        B.out1 --> A.in1;

        A.out2 --> C.in1;
        C.out1 --> A.in2;

        C.out2 --> B.in2;
        B.out2 --> C.in2;
}

以下是.cc源文件

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

class computer: public cSimpleModule
{
  public:


    virtual void initialize();
    virtual void handleMessage(cMessage *msg);
};
//----------------------------------------------------------------------

Define_Module(computer);
void computer::initialize()
{

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

       send(msg, "out1");
       cMessage *copy = (cMessage *) msg->dup();
       send(copy, "out2");
    }
}


void computer::handleMessage(cMessage *msg)
{


}

现在我的问题是在初始化函数中,节点A向B和C发送消息。之后我想从节点B发送消息,FROM NODE B到节点A和C.同样在那之后我想发送消息来自节点C到A和B(如距离矢量协议)我该怎么做?

此外,第二个问题是如何将来自所有节点的消息同时发送到邻居节点i-e A向B,C发送消息; B向A,c发送消息; C发送到A,B。 All,A B C同时发送消息? 让我更简洁明了。 A向B发送消息如下

以下是.cc源文件

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

class computer: public cSimpleModule
{
  public:


    virtual void initialize();
    virtual void handleMessage(cMessage *msg);
};
//----------------------------------------------------------------------

Define_Module(computer);
void computer::initialize()
{

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

       send(msg, "out1");

    }
}

handleMessage(cMessage * msg) { 现在如何从C到A发送消息 }

A到B. C到A?

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题(第一个),你需要做的就是这么简单 在“handleMessage”函数中执行以下操作:

如果i节点“B”且msg来自“A”: 像在初始化函数中那样将msgs发送到A和C.

如果i节点“C”且msg来自“B”: 像初始化函数中那样将msgs发送到A和B.

您可以通过msg-&gt; getSource检查发送msg的人(类似于..类型的 “MSG-&gt;” 中你会看到选项。

希望它有所帮助。

答案 1 :(得分:0)

在这样做之前,你必须回答这个问题,C怎么知道B收到了这样的消息?你有两个解决方案:

  1. 从网络角度来看逻辑解决方案:在此之前 你必须向C发送一条消息,阻止他们接收 这样的消息
  2. 逻辑解决方案较少:通过检查B处的bool值 来自C模块的模​​块,并在此时决定发送消息, 或者更好地使用信号,在B处创建一个信号,然后向它招募信号 C模块(查看omnet ++ doc)。
  3. 祝你好运