程序没有响应并已完成并显示错误消息

Program Not Responding and Finished With An Error Message

我正在制作一个随机生成的网络,其中节点将随机向两个节点发送消息。如果接收节点之前不知道该消息(在 ned 文件中用更新的参数表示),则接收节点会将消息转发给其他两个节点。已经知道该消息的节点只会删除该消息。构建网络没有问题,但是 运行 网络使程序在关闭不响应的程序后没有响应,只有“完成错误”消息。

这是我的代码:

内德档案:

simple Sg3
{
    parameters:
        @display("i=block/routing");
        bool updated;
    gates:
        input in[];  // declare in[] and out[] to be vector gates
        output out[];
}

network Simplegossip3
{
    parameters:
        int count;
        double connectedness; // 0.0<x<1.0
        @display("bgb=640,444");
        
    submodules:
        node[count]: Sg3 {
            gates:
                in[]; 
                out[];
        }
    connections allowunconnected:
        for i=0..count-2, for j=i+1..count-1, if uniform(0,1)<connectedness {
            node[i].out++ --> node[j].in++;
            node[i].in++ <-- node[j].out++;
        }
}

这是 cc 文件:

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

using namespace omnetpp;

/**
 * First attempt for gossip protocol
 */
class Sg3 : public cSimpleModule
{
  protected:
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;

};
Define_Module(Sg3);

void Sg3::initialize()
{
    bool status = par("updated");
    if (status == true) {
        cMessage *update = new cMessage("1st message");
        int j = gateSize("out");
        int k = intuniform(0, j-1);
        send(update, "out",k);
    }
}

void Sg3::handleMessage(cMessage *msg)
{
    bool updatestatus = par("updated");
    int sid = msg->getArrivalGate()->getIndex(); //variable containing the message source
    int n = gateSize("out");

    if (strncmp (msg->getName(),"1st message",2) == 0) {
        if (updatestatus == true){
            delete msg;
        }
        else {
            par("updated").setBoolValue(true);
            delete msg;
            cMessage *update = new cMessage("1st message");
            int l = intuniform(0, n-1);
            do {
                int l = intuniform(0, n-1); //preventing a node asking updated status to the same node it just sent an update to
            }
            while (l == sid);
            send(update, "out", l);

            int m = intuniform(0, n-1);
            do {
                do {
                    int m = intuniform(0, n-1); //choosing other gate to send two message at the same time
                }
                while (m==l);
            }
            while (m == sid);
            send(update, "out", m);
        }
    }
}

我没有找到这个问题的原因,很抱歉这个含糊的问题。非常感谢您的帮助。

首先,在每个 do-while 循环中,您创建了一个新变量(内部变量)。但是,然后比较外部。循环应如下所示:

int l = intuniform(0, n-1);
do {
  l = intuniform(0, n-1); // not "int l = ..."
} while (l == sid);

第二个 do-while 循环也必须这样做。

第二个问题是与这些循环相关的逻辑问题:

int m = intuniform(0, n-1);
do {
   do {
     m = intuniform(0, n-1); // not "int m = ..."
   } while (m==l);
} while (m == sid);

让我们假设:

**.count = 3

然后 n=2,并且 sid 可能等于 0 或 1 - 让我们假设 sid=1。在第一个 do-while 循环 l=0 之后,因为它必须不同于 sid。循环将在 m != 0m != 1 时结束。但是,m可能等于0或1,因为intuniform (0,1)可能return只有这些数字。因此,这些循环是不定式的。