将消息从节点发送到其邻居

Sending a message from a node to its neighboors

本文关键字:邻居 消息 节点      更新时间:2023-10-16

我有一个节点网络,我想从 ID 401 标识的节点向其邻居发送消息。以下是我使用的代码:

for(int i = 0; i < 8; i++)
{
    cMessage *copy = msg->dup();
    send(copy, "out", i);
}
delete msg;

以下是我收到的错误消息

<!> Error in module (Node) topo. nd[401] (id=404) during network initialization: send()/sendDelayed(): Gate index 0 out of range when accessing vector gate `out[]' with size 0.

此错误意味着:

  • out在文件中声明为NED向量(可能是这样:output out[];
  • out尚未连接到任何其他节点(即它的大小等于 0)

您应该做的是将节点的门out连接到每个其他节点的输入门。此外,我建议在 for 循环中检查out向量的大小,例如:

    for (int i = 0; i < gate("out", 0)->getVectorSize(); ++i) {
       // ...
    }

注意:仅当至少连接了一个out端口时,上述代码才能正常工作。