handleMessage(* msg) 在 OMNET++ 中不起作用

handleMessage(* msg) doesn't work in OMNET++

我有一个无线网络,由 7 个名为 node1 到 7 的传感器和一个网关组成。每个节点和网关都有一个由@directIo 标记的输入端口。数据流的方式是,网关选择其中一个节点作为簇头,并使用 sendDirect() 向节点发送包含所选簇头的 id 号的消息,问题在于,节点的 handleMessage () 事件根本没有被调用。

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


using namespace omnetpp;

class node : public cSimpleModule
{
private:
    float propagationDelay;
    float duration;
    double eLevel;
    float dist;
    int mCount;
    int clusterHead;
    cModule *target;
    cMessage *timerMessage;
    cMessage *pack;
    cMessage *weak;
    cMessage *msg;
    bool gReq;
  protected:
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
};

Define_Module(node);

void node::initialize()
{
    gReq=false;
    eLevel=par("energyLeve");
    mCount=0;
    dist=(rand()%3)+1;
    timerMessage = new cMessage("timer");
    /*if (par("sendMsgOnInit").boolValue() == true) {
       EV << getName()<<getIndex() << "------ distance: " << dist << "\n";
    }*/
}

void node::handleMessage(cMessage *msg)
{
    if (msg->getSenderModuleId()==2){
            clusterHead=atoi((char *) msg->getName());
            if (getId()!=clusterHead){
                pack= new cMessage("Hello");
                timerMessage =new cMessage("Timer");
                mCount=0;
                target = getSimulation()->getModule(clusterHead);
                sendDirect(pack, propagationDelay, duration, target, "radioIn");
                eLevel-=dist;
                if (eLevel<50 && gReq==false){
                    weak= new cMessage("w");
                    target = getSimulation()->getModule(2);
                    sendDirect(weak, propagationDelay, duration, target, "radioIn");
                    gReq=true;
                    EV << "node index: "<< getIndex() << "request sent!";
                }
                EV << getName()<<getIndex() << "------ eLevel: " << eLevel;
                scheduleAt(simTime()+0.1, timerMessage);
            }
        }

}

代码 2

#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
using namespace omnetpp;


class gateway : public cSimpleModule
{
  public:
    int mCount;
    cMessage *clusterNodeSelecMess;
    cMessage *timerMessage;
    cMessage *msg;
    int round;
    int clusterHeadId;
    bool weakNodes[7];
    int inx;
  protected:
    int clusterHeadSelection(bool weakNodes[7]);
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
};
Define_Module(gateway);

void gateway::initialize()
{
    msg= new cMessage("self");
    scheduleAt(0.0, msg);
    round =0;
    for (int i = 0; i < 7; ++i) {
            weakNodes[i]=false;
        }
}

void gateway::handleMessage(cMessage *msg)
{
    if(msg->getSenderModuleId()==2){
        round++;
        clusterHeadId=clusterHeadSelection(weakNodes);
        std::string s = std::to_string(clusterHeadId);
        EV << "Node number "<<clusterHeadId << " selected as CH\n";
        char const *pchar = s.c_str();
        clusterNodeSelecMess = new cMessage(pchar);
            for (cModule::SubmoduleIterator it(getModuleByPath("FTN")); !it.end(); ++it) {
                    cModule *temp = *it;
                    if (strcmp(temp->getName(),getName())!=0){
                        sendDirect(clusterNodeSelecMess->dup(), temp, "radioIn");
                    }
                }
    }
    else if(msg->getSenderModuleId()==clusterHeadId){
        clusterHeadId=clusterHeadSelection(weakNodes);
        std::string s = std::to_string(clusterHeadId);
        EV << "Node number "<<clusterHeadId << " selected as CH\n";
        char const *pchar = s.c_str();
        clusterNodeSelecMess = new cMessage(pchar);
        for (cModule::SubmoduleIterator it(getModuleByPath("FTN")); !it.end(); ++it) {
            cModule *temp = *it;
            sendDirect(clusterNodeSelecMess->dup(), temp, "radioIn");
         }
    }
    else{
        EV << "Received from node: "<< msg->getSenderGateId() - 1 << "\n";
    }
}

int clusterHeadSelection(bool weakNodes[7]){
        int i = 0;
        int clusterHeadId;
        while(weakNodes[clusterHeadId=(rand() % 12)-2]==true){
                    i++;
                    if (i>7){
                        return(NULL);
                    }
                }
        return(clusterHeadId);
}

模拟器从不调用 handleMessage() 方法,因为您从不向任何模块发送消息,因此没有任何内容可传递。请注意,sendDirect() 调用仅出现在您的 handleMessage() 方法中,因此在调用其中一个方法之前,不会有任何消息可传递。模拟应该工作的方式是某些代码必须在 initialize() 方法中创建消息或事件(例如计时器),因为这是在模拟开始时调用的方法。

node::initialize() 中,您创建了一个 timerMessage,但从未安排它,因此它不会传送到 handleMessage()。 (并不是说它会有帮助,因为 handleMessage() 代码会明确忽略它):

gateway 模块创建并安排了一条自消息,该消息被传送到 gateway::handleMessage() 但未被处理,很可能最后落入 else 案例然后什么都不做。