UDPEchoApp.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2005 Andras Babos
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 
00019 #include <omnetpp.h>
00020 #include "UDPEchoApp.h"
00021 #include "UDPEchoAppMsg_m.h"
00022 #include "UDPControlInfo_m.h"
00023 
00024 
00025 Define_Module(UDPEchoApp);
00026 
00027 void UDPEchoApp::initialize(int stage)
00028 {
00029     UDPBasicApp::initialize(stage);
00030 
00031     // because of IPAddressResolver, we need to wait until interfaces are registered,
00032     // address auto-assignment takes place etc.
00033     if (stage!=3)
00034         return;
00035 }
00036 
00037 void UDPEchoApp::finish()
00038 {
00039 }
00040 
00041 cPacket *UDPEchoApp::createPacket()
00042 {
00043     char msgName[32];
00044     sprintf(msgName,"UDPEcho-%d", counter++);
00045 
00046     UDPEchoAppMsg *message = new UDPEchoAppMsg(msgName);
00047     message->setByteLength(par("messageLength").longValue());
00048 
00049     return message;
00050 }
00051 
00052 void UDPEchoApp::processPacket(cPacket *msg)
00053 {
00054     if (msg->getKind() == UDP_I_ERROR)
00055     {
00056         delete msg;
00057         return;
00058     }
00059 
00060     UDPEchoAppMsg *packet = check_and_cast<UDPEchoAppMsg *>(msg);
00061 
00062     if (packet->getIsRequest())
00063     {
00064         UDPControlInfo *controlInfo = check_and_cast<UDPControlInfo *>(packet->getControlInfo());
00065 
00066         // swap src and dest
00067         IPvXAddress srcAddr = controlInfo->getSrcAddr();
00068         int srcPort = controlInfo->getSrcPort();
00069         controlInfo->setSrcAddr(controlInfo->getDestAddr());
00070         controlInfo->setSrcPort(controlInfo->getDestPort());
00071         controlInfo->setDestAddr(srcAddr);
00072         controlInfo->setDestPort(srcPort);
00073 
00074         packet->setIsRequest(false);
00075         send(packet, "udpOut");
00076     }
00077     else
00078     {
00079         simtime_t rtt = simTime() - packet->getCreationTime();
00080         EV << "RTT: " << rtt << "\n";
00081         delete msg;
00082     }
00083     numReceived++;
00084 }
00085 
00086 
00087