Omnet++ 在我的 class 中为 handleMessage 添加功能

Omnet++ Adding functionalities to handleMessage in my class

我在尝试继承我的队列 class (GateDropTailQueue) 中的 handlemessage 时遇到此错误,它继承自 DropTailQueue,后者又继承自 PassiveQueueBase。

class INET_API GateDropTailQueue: public DropTailQueue {
private:
    cMessage *msg1;
    cMessage *msg2;
protected:
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
... 
};

 void GateDropTailQueue::initialize()
{
    DropTailQueue::initialize();
msg1 = new cMessage("msg1"); scheduleAt(2, msg1); msg1 = nullptr;
...
}   


void GateDropTailQueue::handleMessage(cMessage *msg)
{
    DropTailQueue::handleMessage(msg);
if (msg == msg1) {
msg1 = new cMessage("msg1"); scheduleAt(10, msg1); msg1 = nullptr;
msg2 = new cMessage("msg2"); scheduleAt(2, msg2); msg2 = nullptr;
...
}
else if (msg == msg2) {
msg2 = new cMessage("msg2"); scheduleAt(2, msg2); msg2 = nullptr;
...
}
}

我遇到以下错误

Error in module (inet::GateDropTailQueue) Mysimulation.switch.eth[0].queue.Queue1 (id=33) at event #12, t=2: Signal "rcvdPk" emitted with wrong class (omnetpp::cMessage does not subclass from omnetpp::cPacket as declared).

此错误与 PassiveQueueBase 中 handleMessage 的第一行有关

// PassiveQueueBase.cc
namespace inet { 
simsignal_t PassiveQueueBase::rcvdPkSignal = registerSignal("rcvdPk");

    void PassiveQueueBase::handleMessage(cMessage *msg)
    {  numQueueReceived++;
       emit(rcvdPkSignal, msg);
       ...
    }
}

可能在您的模型的 NED 文件中有这一行:

@signal[rcvdPk](type=cPacket);

这意味着 rcvdPk 信号可以用 cPacket 对象发出,而不是 cMessage。你应该做的是将 msg 转换为 cPacket,例如这样:

void PassiveQueueBase::handleMessage(cMessage *msg)
{  numQueueReceived++;
   cPacket * pkt = dynamic_cast<cPacket *> (msg);
   if (pkt) {
       emit(rcvdPkSignal, pkt);
   } else {
       // ... not a cPacket
   }

   ...
}

通过对父 handleMessage(cMessage * msg) 设置条件解决了这个问题

 if (!msg->isSelfMessage()) { ... }

// while it didn't work using the condition
if (msg != msg1 && msg != msg2) { ... }