Cannot cast mes to cPacket

928 views
Skip to first unread message

jas

unread,
Oct 26, 2011, 5:27:50 AM10/26/11
to omnetpp
Hi, i have defined a new message type whose name is "mes" and i have
redefined handleMessage function to work with my "mes" message type.
But when i want to send a "mes" message, the following error appears:
check_and_cast(): cannot cast (mes *)net.nod.udp. to type 'cPacket *'

"net" is my network and "nod" one module. I think it occurs because
all the sending functions are defined with cPacket. "Send" and
"SendToUDP" functions are defined with cPacket. How can I send my
messages "mes" with no errors? I can't redefine these functions,
because the files where are they, are protected.

I send my "mes" messages broadcasting like this:
mes *msg = new mes();
for (int j=0; j<5; j++) {
mes *copy = (mes *) msg->dup();
send(copy, "udpOut");

Can someone help me?
Thanks!!

Zarrar Yousaf

unread,
Oct 26, 2011, 7:47:18 AM10/26/11
to omn...@googlegroups.com
I am experiencing similar problem, where in the TCPSinkApp I enque the arriving packets in  a queue, and later on i want to deque and delete them.
I use the following line to deque and then delete the packet:

delete playBuffer.pop();

But i get the following error message:
 Error in module (TCPSinkAppQ) testTCP.client1.tcpApp[0] (id=10) at event #233687, t=2: check_and_cast(): cannot cast (cMessage *)testTCP.client1.tcpApp[0].playAdvance to type 'cPacket *'.

I even tried the follwoing two options but to no avail:

1. delete (cMessage*)playBuffer.pop();
2. delete (cPacket*) playBuffer.pop();

Pelase note that teh packets that i enque are of the cPacekt type.

Any hint would be appreciated

Regards

Zarrar



From: jas <jas...@gmail.com>
To: omnetpp <omn...@googlegroups.com>
Sent: Wednesday, October 26, 2011 11:27 AM
Subject: [Omnetpp-l] Cannot cast mes to cPacket
--
Sent from the OMNeT++ mailing list. To configure your membership,
visit http://groups.google.com/group/omnetpp


Zarrar Yousaf

unread,
Oct 27, 2011, 7:21:52 AM10/27/11
to omn...@googlegroups.com
Dear All,

I am using Omnet++ 4.1 under windows 7. I am modifying the TCPSinkApp such that instead of deleting the received packet right away, i store it in a queue. After a lapse of a specific time interval i need to delete each packet in teh queue periodically one by one. I do not have any issues queing the received packets, however i am having issues related to the timer that i create for initiating the dequeuing process.

As a routine, i first create a timer (self message)  and then schedule it to be triggered at a specific time using the routine:

cMessage *timer = new cMessage("playAdvance", MK_PERIODIC_DEQUEUE);
scheduleAt(simTime( )+startPlay, timer);

Correspondingly, in the handleMessage (cMessage *msg), i specify the condition

else if (msg->getKind()==MK_PERIODIC_DEQUEUE)
    {
        emptyPlayBuffer(); //go to the routine where teh logic for dequeuing the queue is specified
        scheduleAt (simTime()+trigger, msg);      //schedule the triggering of the timer (MK_PERIODIC_DEQUEUE)
    }


The timer gets created without any problem, however at the time when the timer msg is supposed to get triggered, i get the following error:

<!> Error in module (TCPSinkApp) testTCP.client1.tcpApp[0] (id=10) at event #832, t=1.102739842: check_and_cast(): cannot cast (cMessage *)testTCP.client1.tcpApp[0].playAdvance to type 'cPacket *'.

I dont understand why the nomral timer of the type cMessage needs to be casted into a cPacket?


i would very much appreciate any hints to my problem

thanks

Zarrar

Zarrar Yousaf

unread,
Oct 27, 2011, 7:25:53 AM10/27/11
to omn...@googlegroups.com
Dear All,

I have been using omnet 3.2 under linux and i used to use gdb for debugging purposes.

However recently i have switched to omnet 4.1 under Windows 7 and i am not sure how to debug in this environment.

I have checked the manual but it doesnt say much (or have i missed anything?)
Is there a manual or a how-to  that would specify how to debug your programs in the omnet 4.1 IDE. For example how-to set breakpoints, how-to step through the program, how-to check variables....the usual debugging process steps.

I would appreciate your feedback.

thanks

Zarrar

Leonardo Maccari

unread,
Oct 27, 2011, 7:30:31 AM10/27/11
to omn...@googlegroups.com
On 27/10/2011 13:21, Zarrar Yousaf wrote:
> Dear All,

>
> As a routine, i first create a timer (self message) and then schedule it
> to be triggered at a specific time using the routine:
>
> cMessage *timer = new cMessage("playAdvance", MK_PERIODIC_DEQUEUE);
> scheduleAt(simTime( )+startPlay, timer);

This sounds like your application is imagined to deal with cPackets, but
when you add a timer, the handleMessage will not handle a cPacket but a
cMessage, and cannot cast it.

You should find where the cast is done and check the message type before
casting, or do a different cast and check the return value.

ciao,
leonardo.
--
Leonardo Maccari, Post-doc researcher@University of Trento
Tel: +39 0461 285323, project website: www.pervacy.eu

Zarrar Yousaf

unread,
Oct 27, 2011, 8:15:32 AM10/27/11
to omn...@googlegroups.com
Dear Leonardo,

thanks for the quick reply.....
actually that is what i have also been thinking, but the handleMessage accepts cMessage * type i.e., handleMessage(cMessage *), and hence i am creatign the timer of teh type cMessage and passing it. 

I am pasting the code below and you can see that. I have highlighted the relevant segmetns.
I feel that i may be missing something very trivial... :-)
Zarrar


#include "TCPSinkAppQ.h"
#include "TCPSocket.h"
#include "TCPCommand_m.h"

#define MK_ADVANCE_SLIDE_TRACKER 1     //ZY 25.10.2011

Define_Module(TCPSinkAppQ);

void TCPSinkAppQ::initialize()
{
    const char *address = par("address");
    int port = par("port");
    startPlay = par("startPlay");

    playBuffer.setName("Playout_Buffer"); //ZY 25.10.2011

    bytesRcvd = 0;
    WATCH(bytesRcvd);

    playBufferSize = 0; //ZY 25.10.2011
    WATCH(playBufferSize); //ZY 25.10.2011

    TCPSocket socket;
    socket.setOutputGate(gate("tcpOut"));
    socket.bind(address[0] ? IPvXAddress(address) : IPvXAddress(), port);
    socket.listen();
}

void TCPSinkAppQ::handleMessage(cMessage *msg)
{
    if (msg->getKind()==TCP_I_PEER_CLOSED)
    {
        // we close too
        msg->setKind(TCP_C_CLOSE);
        send(msg, "tcpOut");
    }
    else if (msg->getKind()==TCP_I_DATA || msg->getKind()==TCP_I_URGENT_DATA)
    {
        //as soon as the play buffer gets the first video packet, the app should start playing it
        createPlayTimer(); //create the self-msg (timer) that will periodically trigger the app to pop out packets from teh playbuffer

        bytesRcvd += PK(msg)->getByteLength();
        playBuffer.insert(msg);
        playBufferSize++;
        //delete msg;

        if (ev.isGUI())
        {
            char buf[32];
            sprintf(buf, "rcvd: %ld bytes", bytesRcvd);
            getDisplayString().setTagArg("t",0,buf);
        }
    }

    else if(msg->getKind()==MK_ADVANCE_SLIDE_TRACKER)
    {
        EV<<"Timer Message Received !"<<endl;
        emptyPlayBuffer();
        scheduleAt(simTime()+startPlay, msg);
    }

    else
    {
        // must be data or some kind of indication -- can be dropped
        EV<<"Unknown Message, hence dropping without further processing";
        delete msg;
    }
}

void TCPSinkAppQ::createPlayTimer()
{
    if(playBuffer.isEmpty())
    {
        simtime_t nextPlay = simTime();
        cMessage *timer = new cMessage("playAdvance", MK_ADVANCE_SLIDE_TRACKER);
        EV<<"Timer "<<timer->getName()<<" of type "<<timer->getKind()<<" created at time "<<simTime()<<endl;
        EV<<"Scheduling the timer msg to trigger at"<<simTime()+startPlay<<endl;
        scheduleAt(simTime()+startPlay, timer);
    }
    else
        EV<<"Buffer not empty...hence timer not created"<<endl;

        return;
}


void TCPSinkAppQ::emptyPlayBuffer()
{
    EV<<"Entering the emptyPlayBuffer routine at time "<<simTime()<<" and creating the timer\n"<<endl;;
    if(!playBuffer.isEmpty())
            {
            EV <<"Playing the Content (Popping first video packet from the playBuffer)";
            delete PK(playBuffer.pop());
            }
            else
                EV <<"Play Buffer is Empty !!"<<endl;

return;
}


void TCPSinkAppQ::finish()
{
    recordScalar("bytesRcvd", bytesRcvd);
}



From: Leonardo Maccari <leonardo...@disi.unitn.it>
To: omn...@googlegroups.com
Sent: Thursday, October 27, 2011 1:30 PM
Subject: Re: [Omnetpp-l] check_and_cast related issue - how to debug in omnet4.1

Andras Varga

unread,
Oct 27, 2011, 8:24:22 AM10/27/11
to omn...@googlegroups.com
Hi Zarrar,

> Is there a manual or a how-to  that would specify how to debug your
programs in the omnet 4.1 IDE

The IDE C++ support is built on Eclipse CDT, so CDT documentation,
tutorials, etc apply.

Generally omnetpp/doc/UserGuide.pdf describes the IDE, but it doesn't say
much about debugging, because debugging is already covered by the
documentation written by the CDT team.

I'd suggest the normal CDT documentation bundled with the IDE: Help | Help
Contents, then in the tree open C/C++ Development User Guide -> Tasks->
Running and debugging projects -> Debugging. It describes launching,
breakpoints, stepping, watches, etc.

There's some more stuff at http://www.eclipse.org/cdt/documentation.php

Andras


___________________________________________________________
From: omn...@googlegroups.com [mailto:omn...@googlegroups.com] On Behalf
Of Zarrar Yousaf
Sent: Thursday, October 27, 2011 1:26 PM
To: omn...@googlegroups.com
Subject: [Omnetpp-l] How to debug in omnet 4.1

Dear All,

thanks

Zarrar

--

Zarrar Yousaf

unread,
Oct 27, 2011, 9:04:02 AM10/27/11
to omn...@googlegroups.com
Leonardo,

thanks...the problem is solved....there was a conflict with the message type

i had specified

#define MK_PERIODIC_DEQUEUE 1,

whereas this number was in conflict...

Thanks for the pointer...i knew its going to be trivial :-)

Cheers

Zarrar


From: Leonardo Maccari <leonardo...@disi.unitn.it>
To: omn...@googlegroups.com
Sent: Thursday, October 27, 2011 1:30 PM
Subject: Re: [Omnetpp-l] check_and_cast related issue - how to debug in omnet4.1

Awais Ahmad

unread,
Feb 12, 2014, 8:01:55 AM2/12/14
to omn...@googlegroups.com
Can you write steps on how to debug a mixim example in omnet++;.

regards,
Reply all
Reply to author
Forward
0 new messages