Public Member Functions | Private Member Functions | Private Attributes

MyApplication Class Reference

#include <MyApplication.h>

Inheritance diagram for MyApplication:
BaseApp BaseRpc BaseTcpSupport RpcListener

List of all members.

Public Member Functions

 MyApplication ()
 ~MyApplication ()

Private Member Functions

void initializeApp (int stage)
 initializes derived class-attributes
void finishApp ()
 collects statistical data of derived app
void handleTimerEvent (cMessage *msg)
void deliver (OverlayKey &key, cMessage *msg)
 Common API function: handles delivered messages from overlay.
void handleUDPMessage (cMessage *msg)
 method to handle messages that come directly from the UDP gate

Private Attributes

simtime_t sendPeriod
int numToSend
int largestKey
int numSent
int numReceived
cMessage * timerMsg

Detailed Description

Author:
Antonio Zea

Definition at line 31 of file MyApplication.h.


Constructor & Destructor Documentation

MyApplication::MyApplication (  )  [inline]

Definition at line 53 of file MyApplication.h.

{ timerMsg = NULL; };

MyApplication::~MyApplication (  )  [inline]

Definition at line 54 of file MyApplication.h.

{ cancelAndDelete(timerMsg); };


Member Function Documentation

void MyApplication::deliver ( OverlayKey key,
cMessage *  msg 
) [private, virtual]

Common API function: handles delivered messages from overlay.

method to handle decapsulated KBRdeliver messages from overlay module, should be overwritten in derived application

Parameters:
key destination key
msg delivered message

Reimplemented from BaseApp.

Definition at line 117 of file MyApplication.cc.

{
    // we are only expecting messages of type MyMessage, throw away any other
    MyMessage *myMsg = dynamic_cast<MyMessage*>(msg);
    if (myMsg == NULL) {
        delete msg; // type unknown!
        return;
    }

    // are we a PING? send a PONG!
    if (myMsg->getType() == MYMSG_PING) {
        myMsg->setType(MYMSG_PONG); // change type

        EV << thisNode.getIp() << ": Got packet from "
           << myMsg->getSenderAddress() << ", sending back!"
           << std::endl;

        // send it back to its owner
        sendMessageToUDP(myMsg->getSenderAddress(), myMsg);
    } else {
        // only handle PING messages
        delete msg;
    }
}

void MyApplication::finishApp (  )  [private, virtual]

collects statistical data of derived app

Reimplemented from BaseApp.

Definition at line 67 of file MyApplication.cc.

{
    // finish() is usually used to save the module's statistics.
    // We'll use globalStatistics->addStdDev(), which will calculate min, max, mean and deviation values.
    // The first parameter is a name for the value, you can use any name you like (use a name you can find quickly!).
    // In the end, the simulator will mix together all values, from all nodes, with the same name.

    globalStatistics->addStdDev("MyApplication: Sent packets", numSent);
    globalStatistics->addStdDev("MyApplication: Received packets", numReceived);
}

void MyApplication::handleTimerEvent ( cMessage *  msg  )  [private]

Definition at line 80 of file MyApplication.cc.

{
    // is this our timer?
    if (msg == timerMsg) {
        // reschedule our message
        scheduleAt(simTime() + sendPeriod, timerMsg);

        // if the simulator is still busy creating the network,
        // let's wait a bit longer
        if (underlayConfigurator->isInInitPhase()) return;

        for (int i = 0; i < numToSend; i++) {

            // let's create a random key
            OverlayKey randomKey(intuniform(1, largestKey));

            MyMessage *myMessage; // the message we'll send
            myMessage = new MyMessage();
            myMessage->setType(MYMSG_PING); // set the message type to PING
            myMessage->setSenderAddress(thisNode); // set the sender address to our own
            myMessage->setByteLength(100); // set the message length to 100 bytes

            numSent++; // update statistics

            EV << thisNode.getIp() << ": Sending packet to "
               << randomKey << "!" << std::endl;

            callRoute(randomKey, myMessage); // send it to the overlay
        }
    } else {
        // unknown message types are discarded
        delete msg;
    }
}

void MyApplication::handleUDPMessage ( cMessage *  msg  )  [private, virtual]

method to handle messages that come directly from the UDP gate

Parameters:
msg message to handle

Reimplemented from BaseApp.

Definition at line 144 of file MyApplication.cc.

{
    // we are only expecting messages of type MyMessage
    MyMessage *myMsg = dynamic_cast<MyMessage*>(msg);

    if (myMsg && myMsg->getType() == MYMSG_PONG) {
        EV << thisNode.getIp() << ": Got reply!" << std::endl;
        numReceived++;
    }

    // Message isn't needed any more -> delete it
    delete msg;
}

void MyApplication::initializeApp ( int  stage  )  [private, virtual]

initializes derived class-attributes

Parameters:
stage the init stage

Reimplemented from BaseApp.

Definition at line 36 of file MyApplication.cc.

{
    // initializeApp will be called twice, each with a different stage.
    // stage can be either MIN_STAGE_APP (this module is being created),
    // or MAX_STAGE_APP (all modules were created).
    // We only care about MIN_STAGE_APP here.

    if (stage != MIN_STAGE_APP) return;

    // copy the module parameter values to our own variables
    sendPeriod = par("sendPeriod");
    numToSend = par("numToSend");
    largestKey = par("largestKey");

    // initialize our statistics variables
    numSent = 0;
    numReceived = 0;

    // tell the GUI to display our variables
    WATCH(numSent);
    WATCH(numReceived);

    // start our timer!
    timerMsg = new cMessage("MyApplication Timer");
    scheduleAt(simTime() + sendPeriod, timerMsg);

    bindToPort(2000);
}


Member Data Documentation

Definition at line 36 of file MyApplication.h.

Referenced by handleTimerEvent(), and initializeApp().

Definition at line 40 of file MyApplication.h.

Referenced by finishApp(), handleUDPMessage(), and initializeApp().

int MyApplication::numSent [private]

Definition at line 39 of file MyApplication.h.

Referenced by finishApp(), handleTimerEvent(), and initializeApp().

int MyApplication::numToSend [private]

Definition at line 35 of file MyApplication.h.

Referenced by handleTimerEvent(), and initializeApp().

simtime_t MyApplication::sendPeriod [private]

Definition at line 34 of file MyApplication.h.

Referenced by handleTimerEvent(), and initializeApp().

cMessage* MyApplication::timerMsg [private]

Definition at line 43 of file MyApplication.h.

Referenced by handleTimerEvent(), initializeApp(), MyApplication(), and ~MyApplication().


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