【问题标题】:Gate out of range. OMNet++门超出范围。 OMNet++
【发布时间】:2021-03-05 09:44:01
【问题描述】:

我正在创建一个简单的网络,该网络将消息从一个节点转发到下一个节点,直到它到达目的地,但我不断收到错误消息“访问向量门 'portal$o[] 时门索引 2 超出范围”尺寸为 2 英寸。

.cc 文件:

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

using namespace omnetpp;

class Node : public cSimpleModule
{
protected:
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
    virtual Packet *generateMessage(opp_string message, int destination);
    virtual void forwardMessage(Packet *msg);
};

Define_Module(Node);

void Node::initialize()
{
    if (getIndex() == 0)
    {
        Packet *msg = generateMessage("hey there", 4);
        scheduleAt(0.0, msg);
    }

}

void Node::handleMessage(cMessage *msg)
{
    Packet *pmsg = check_and_cast<Packet *>(msg);

    if(pmsg->getDestination() == getIndex())
    {
        EV << "Message " << pmsg << " arrived after " << pmsg->getHopCount() << " hops.\n";
        EV << "Contents of" << pmsg << ": " << pmsg->getStrmessage() << endl;
        bubble("ARRIVED!");
        delete pmsg;

    }else{
        forwardMessage(pmsg);
    }
}

Packet *Node::generateMessage(opp_string message, int destination)
{
    int source = getIndex();
    int dest = destination;
    opp_string strmsg = message;

    char msgname[20];
    sprintf(msgname, "From-%d-to-%d", source, dest);

    Packet *msg = new Packet(msgname);
    msg->setSource(source);
    msg->setDestination(dest);
    msg->setStrmessage(strmsg.c_str());
    return msg;
}

void Node::forwardMessage(Packet *msg)
{
        msg->setHopCount(msg->getHopCount()+1);
        int k = getIndex();
        send(msg, "portal$o", k);
}

.ned 文件:

package revised;

simple Node
{
    parameters:
        @display("i=block/routing");
    gates:
        inout portal[];
}

network Hi
{
    @display("bgb=562.42,318.09");
    types:
        channel Channel extends ned.DelayChannel
        {
            delay = 100ms;
        }

    submodules:
        node[5]: Node;

    connections:
        node[0].portal++ <--> Channel <--> node[1].portal++;
        node[1].portal++ <--> Channel <--> node[2].portal++;
        node[2].portal++ <--> Channel <--> node[3].portal++;
        node[3].portal++ <--> Channel <--> node[4].portal++;
}

也许错误是因为节点没有正确连接到门?我不知道。

【问题讨论】:

    标签: networking omnet++


    【解决方案1】:

    向量索引是基于 0 的,所以如果你有一个大小为 2 的向量,你只能使用索引 0 和 1,这样错误是合理的。

    实际问题发生在 forwardMessage 函数中,您在该函数中获取当前模块的索引 (0-4) 并使用该值设置门索引,但门向量的大小仅为您创建的 1 或 2总线拓扑。

    那部分代码不正确。

    【讨论】:

    • node[0].portal++ 频道 node[1].portal++;节点[1].portal++ 频道 节点[2].portal++;节点[2].portal++ 频道 节点[3].portal++;节点[3].portal++ 频道 节点[4].portal++; //我以为这是在添加所需的门?
    • 是的,++ 运算符会这样做,但是您的错误不是来自 NED 文件,而是来自 c++ 代码。
    • if (getIndex() != 0 and getIndex() != 4) { send(msg, "portal$o", 1); }else{ 发送(msg, "portal$o", 0); } //这是我能想到的最好的东西。有没有更好的办法?
    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2012-05-04
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 2014-02-25
    相关资源
    最近更新 更多