门超出范围。 OMNet++

问题描述 投票:0回答:2

我正在创建一个简单的网络,它将消息从一个节点转发到下一个节点,直到到达目的地,但我不断收到错误“访问矢量门'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++
2个回答
1
投票

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

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

那部分代码不正确。


0
投票
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
#include "project1_m.h"

using namespace omnetpp;

class node : public cSimpleModule
{
  protected:
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
    virtual Project1Message *generateReplyMessage(Project1Message *msg);
    virtual void forwardMessage(Project1Message *msg);
    virtual void replyMessage(Project1Message *msg);
};

Define_Module(node);

void node::initialize()
{
    if (getIndex() == 0) {
        Project1Message *msg = new Project1Message("project1Message");
        msg->setSource(getIndex());
        msg->setDestination(4); // Send to Node 4
        msg->setData("Message from Node 0 to Node 4");
        send(msg, "gate$o", 0);
    }
}


void node::handleMessage(cMessage *msg)
{
    Project1Message *project1Msg = check_and_cast<Project1Message *>(msg);
    EV << "Node index: " << getIndex() << endl;
    if (project1Msg->getDestination() == getIndex()) {  // if node 4
        // Message arrived at its destination.
        EV << "Message " << project1Msg << " arrived after " << project1Msg->getHopCount() << " hops.\n";
        EV << "Node " << getIndex() << " received message from Node " << project1Msg->getSource() << ": " << project1Msg->getData() << endl;

        // Generate reply message and send it back along the same path
        Project1Message *replyMsg = generateReplyMessage(project1Msg);
        replyMessage(replyMsg);
        delete project1Msg; // Delete the original message
    }
    else{
        // Forward the message to the next node
        forwardMessage(project1Msg);
    }
}

void node::forwardMessage(Project1Message *msg){
    // Increment hop count.
    msg->setHopCount(msg->getHopCount()+1);
    int n = gateSize("gate");
    EV << "current index" << getIndex() << "\n";
    int k = intuniform(1, n-1);
    EV << "Forwarding message " << msg << " on gate[" << k << "]\n";
    send(msg, "gate$o", k);

}

void node::replyMessage(Project1Message *msg){
    if (getIndex() == 0) {
        endSimulation();
    }
    msg->setHopCount(msg->getHopCount() + 1);

    EV << "Forwarding reply message " << msg << "\n";
    send(msg, "gate$o", 0);
}

Project1Message *node::generateReplyMessage(Project1Message *originalMsg)
{
    // Create a reply message with reversed source and destination
    Project1Message *replyMsg = new Project1Message("replyMsg");
    replyMsg->setSource(originalMsg->getDestination()); // Reverse source and destination
    replyMsg->setDestination(getIndex()-1);
    replyMsg->setHopCount(originalMsg->getHopCount()); // Copy hop count from original message
    std::string replyData = "Reply from Node " + std::to_string(getIndex());
    replyMsg->setData(replyData.c_str());
    return replyMsg;
}

这就是我将消息从一个节点转发到目标节点,并使用相同路径回复消息的方法。这里使用了5个节点。

© www.soinside.com 2019 - 2024. All rights reserved.