OMNET ++中的多个连接

问题描述

我是Omnet ++的新手。我试图同时从一个节点(一个出门)向多个节点(多个入门)发送消息。

但是,当我运行omnet.ini时,它显示错误:“访问大小为0的向量gate out []时超出范围”。你能帮我吗?此代码有什么问题?我猜想k在send(msg,“ out”,k)方法中是错误的。

Ned文件

   simple Test

   {
    gates:
    input in[];
    output out[];
  }

  network testNetwork
  {
   @display("bgb=764,369");
   submodules:
    computer0: Test {
        @display("p=55,22");
    }
    computer1: Test {
        @display("p=141,36");
    }
    computer2: Test {
        @display("p=233,22");
    }
    computer3: Test {
        @display("p=308,24");
    }
    

     connections:
    computer1.out++ --> computer0.in++;
    computer1.out++ --> computer2.in++;
    computer1.out++ --> computer3.in++;
  }

文件

    #include <stdio.h>
    #include <string.h>
    #include <omnetpp.h>
    #include "tictoc15_m.h"

    using namespace omnetpp;

  class Test : public cSimpleModule
  {
   private:
   long numSent;
   long numReceived;
   cLongHistogram hopCountStats;
   cOutVector hopCountVector;

   protected:
   virtual TicTocMsg15 *generateMessage();
virtual void forwardMessage(TicTocMsg15 *msg);
virtual void initialize() override;
virtual void handleMessage(cmessage *msg) override;

// The finish() function is called by OMNeT++ at the end of the simulation:
    virtual void finish() override;
    };

   Define_Module(Test);

    void Test::initialize()
     {
    // Initialize variables
     numSent = 0;
numReceived = 0;
WATCH(numSent);
WATCH(numReceived);

hopCountStats.setName("hopCountStats");
hopCountStats.setRangeAutoupper(0,10,1.5);
hopCountVector.setName("HopCount");

// Module 0 sends the first message
if (getIndex() == 0) {
    // Boot the process scheduling the initial message as a self-message.
    TicTocMsg15 *msg = generateMessage();
    scheduleAt(0.0,msg);
    //scheduleAt(simTime(),new cmessage);
     }
 }

       void Test::handleMessage(cmessage *msg)
      {
       TicTocMsg15 *ttmsg = check_and_cast<TicTocMsg15 *>(msg);

if (ttmsg->getDestination() == getIndex()) {
    // Message arrived
    int hopcount = ttmsg->getHopCount();
    EV << "Message " << ttmsg << " arrived after " << hopcount << " hops.\n";
    bubble("ARRIVED,starting new one!");

    // update statistics.
    numReceived++;
    hopCountVector.record(hopcount);
    hopCountStats.collect(hopcount);

    delete ttmsg;

    // Generate another one.
    EV << "Generating another message: ";
    TicTocMsg15 *newmsg = generateMessage();
    EV << newmsg << endl;
    forwardMessage(newmsg);
    numSent++;
   // scheduleAt();
}
else {
    // We need to forward the message.
    forwardMessage(ttmsg);
   }
   }

    TicTocMsg15 *Test::generateMessage()
   {
// Produce source and destination addresses.
int src = getIndex();
int n = getVectorSize();
 int dest = intuniform(0,n-2);

if (dest >= src)
    dest++;

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

// Create message object and set source and destination field.
TicTocMsg15 *msg = new TicTocMsg15(msgname);
msg->setSource(src);
msg->setDestination(dest);
return msg;


   }

       void Test::forwardMessage(TicTocMsg15 *msg)
 {
// Increment hop count.
msg->setHopCount(msg->getHopCount()+1);

// Same routing as before: random gate.
int n = gateSize("out");
int k = intuniform(0,n-1);

EV << "Forwarding message " << msg << " on gate[" << k << "]\n";
send(msg,"out",k);
 }

    void Test::finish()
  {
// This function is called by OMNeT++ at the end of the simulation.
EV << "Sent:     " << numSent << endl;
EV << "Received: " << numReceived << endl;
EV << "Hop count,min:    " << hopCountStats.getMin() << endl;
EV << "Hop count,max:    " << hopCountStats.getMax() << endl;
EV << "Hop count,mean:   " << hopCountStats.getMean() << endl;
EV << "Hop count,stddev: " << hopCountStats.getStddev() << endl;

recordScalar("#sent",numSent);
recordScalar("#received",numReceived);

hopCountStats.recordAs("hop count");
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)