omnet++inet

How can I access and use the packet contents in the received node using INET in OMNeT++?


In omnet++/INET I have two nodes(Standardhost) and by extending UDPBasicApp application, I have to send sensor values to the other node. I made my own .msg definition like below:

class PlantPacket extends FieldsChunk
{
    double sensorValue;
}

I have no problem with sending this value and I used this code:

void MyUdp::sendPacket()
{
    std::ostringstream str;
    str << packetName << "-" << numSent;
    Packet *packet = new Packet(str.str().c_str());

    const auto& payload = makeShared<PlantPacket>();
    payload->setChunkLength(B(par("messageLength")));
    payload->setSensorValue(sensorValue);
    payload->addTag<CreationTimeTag>()->setCreationTime(simTime());

    packet->insertAtBack(payload);

    L3Address destAddr = chooseDestAddr();
    socket.sendTo(packet, destAddr, destPort);
   
}

My problem is to retrieve the sensorValue in the received node. I used this code but I got an error:

void MyUdp::processPacket(Packet *pk)
{
    EV_INFO << "Received packet: " << UdpSocket::getReceivedPacketInfo(pk) << endl;
    PlantPacket* payload = check_and_cast<PlantPacket*>(pk);
    
    EV_INFO << "packet content1: " << payload->getSensorValue() << endl;
    
    delete pk;
}

Error: Cannot convert between integer units -- in module (inet::Udp)

How can I solve this problem?


Solution

  • The payload has been added using insertAtBack therefore it must be obtained using peekAtBack or popAtBack. The code should look like:

    void MyUdp::processPacket(Packet *pk)
    {
        EV_INFO << "Received packet: " << UdpSocket::getReceivedPacketInfo(pk) << endl;
        const auto& payload = pk->peekAtBack<PlantPacket>();
                
        EV_INFO << "packet content1: " << payload->getSensorValue() << endl;
        
        delete pk;
    }