Search code examples
c++inheritanceomnet++inet

Omnet++ Adding functionalities to handleMessage in my class


I have this error trying to inherit handlemessage in my queue class (GateDropTailQueue) which is inheriting from DropTailQueue which in turn is inheriting from 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;
...
}
}

I am having the below error

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).

This error is related to first line in handleMessage in PassiveQueueBase

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

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

Solution

  • Probably in the NED file of your model you have this line:

    @signal[rcvdPk](type=cPacket);
    

    It means that rcvdPk signal may be emitted with a cPacket object, not with cMessage. What you should to do is to cast msg to cPacket, for example this way:

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