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

时间:2016-11-29 18:54:59

标签: c++ inheritance omnet++ inet

我在尝试继承我的队列类(GateDropTailQueue)中的handlemessage时遇到此错误,该队列类继承自DropTailQueue,而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;
...
}
}

我遇到以下错误

  

模块中的错误(inet :: GateDropTailQueue)   在事件#12,t = 2的Mysimulation.switch.eth [0] .queue.Queue1(id = 33):   发出错误类的信号“rcvdPk”(omnetpp :: cMessage没有   从omnetpp :: cPacket声明的子类。)

此错误与PassiveQueueBase

中handleMessage的第一行有关
// PassiveQueueBase.cc
namespace inet { 
simsignal_t PassiveQueueBase::rcvdPkSignal = registerSignal("rcvdPk");

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

2 个答案:

答案 0 :(得分:1)

可能在您的模型的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
   }

   ...
}

答案 1 :(得分:0)

通过在父句柄语句(cMessage * msg)上具有条件来解决它

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

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