INET Framework for OMNeT++/OMNEST
UDPBasicApp Class Reference

#include <UDPBasicApp.h>

Inheritance diagram for UDPBasicApp:
UDPAppBase UDPEchoApp

List of all members.

Protected Member Functions

virtual IPvXAddress chooseDestAddr ()
virtual cPacket * createPacket ()
virtual void sendPacket ()
virtual void processPacket (cPacket *msg)
virtual int numInitStages () const
virtual void initialize (int stage)
virtual void handleMessage (cMessage *msg)

Protected Attributes

std::string nodeName
int localPort
int destPort
std::vector< IPvXAddressdestAddresses
int numSent
int numReceived

Static Protected Attributes

static int counter

Detailed Description

UDP application. See NED for more info.


Member Function Documentation

IPvXAddress UDPBasicApp::chooseDestAddr ( ) [protected, virtual]

Referenced by sendPacket().

{
    int k = intrand(destAddresses.size());
    return destAddresses[k];
}
cPacket * UDPBasicApp::createPacket ( ) [protected, virtual]

Reimplemented in UDPEchoApp.

Referenced by sendPacket().

{
    char msgName[32];
    sprintf(msgName,"UDPBasicAppData-%d", counter++);

    cPacket *payload = new cPacket(msgName);
    payload->setByteLength(par("messageLength").longValue());
    return payload;
}
void UDPBasicApp::handleMessage ( cMessage *  msg) [protected, virtual]
{
    if (msg->isSelfMessage())
    {
        // send, then reschedule next sending
        sendPacket();
        scheduleAt(simTime()+(double)par("messageFreq"), msg);
    }
    else
    {
        // process incoming packet
        processPacket(PK(msg));
    }

    if (ev.isGUI())
    {
        char buf[40];
        sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent);
        getDisplayString().setTagArg("t",0,buf);
    }
}
void UDPBasicApp::initialize ( int  stage) [protected, virtual]

Reimplemented in UDPEchoApp.

{
    // because of IPAddressResolver, we need to wait until interfaces are registered,
    // address auto-assignment takes place etc.
    if (stage!=3)
        return;

    counter = 0;
    numSent = 0;
    numReceived = 0;
    WATCH(numSent);
    WATCH(numReceived);

    localPort = par("localPort");
    destPort = par("destPort");

    const char *destAddrs = par("destAddresses");
    cStringTokenizer tokenizer(destAddrs);
    const char *token;
    while ((token = tokenizer.nextToken())!=NULL)
        destAddresses.push_back(IPAddressResolver().resolve(token));

    if (destAddresses.empty())
        return;

    bindToPort(localPort);

    cMessage *timer = new cMessage("sendTimer");
    scheduleAt((double)par("messageFreq"), timer);
}
virtual int UDPBasicApp::numInitStages ( ) const [inline, protected, virtual]
{return 4;}
void UDPBasicApp::processPacket ( cPacket *  msg) [protected, virtual]

Reimplemented in UDPEchoApp.

Referenced by handleMessage().

{
    EV << "Received packet: ";
    printPacket(msg);
    delete msg;

    numReceived++;
}
void UDPBasicApp::sendPacket ( ) [protected, virtual]

Referenced by handleMessage().

{
    cPacket *payload = createPacket();
    IPvXAddress destAddr = chooseDestAddr();
    sendToUDP(payload, localPort, destAddr, destPort);

    numSent++;
}

Member Data Documentation

int UDPBasicApp::counter [static, protected]
std::vector<IPvXAddress> UDPBasicApp::destAddresses [protected]

Referenced by chooseDestAddr(), and initialize().

int UDPBasicApp::destPort [protected]

Referenced by initialize(), and sendPacket().

int UDPBasicApp::localPort [protected]

Referenced by initialize(), and sendPacket().

std::string UDPBasicApp::nodeName [protected]
int UDPBasicApp::numSent [protected]

The documentation for this class was generated from the following files: