|
INET Framework for OMNeT++/OMNEST
|
00001 // 00002 // Copyright (C) 2000 Institut fuer Telematik, Universitaet Karlsruhe 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 "UDPBasicApp.h" 00021 #include "UDPControlInfo_m.h" 00022 #include "IPAddressResolver.h" 00023 00024 00025 00026 Define_Module(UDPBasicApp); 00027 00028 int UDPBasicApp::counter; 00029 00030 void UDPBasicApp::initialize(int stage) 00031 { 00032 // because of IPAddressResolver, we need to wait until interfaces are registered, 00033 // address auto-assignment takes place etc. 00034 if (stage!=3) 00035 return; 00036 00037 counter = 0; 00038 numSent = 0; 00039 numReceived = 0; 00040 WATCH(numSent); 00041 WATCH(numReceived); 00042 00043 localPort = par("localPort"); 00044 destPort = par("destPort"); 00045 00046 const char *destAddrs = par("destAddresses"); 00047 cStringTokenizer tokenizer(destAddrs); 00048 const char *token; 00049 while ((token = tokenizer.nextToken())!=NULL) 00050 destAddresses.push_back(IPAddressResolver().resolve(token)); 00051 00052 if (destAddresses.empty()) 00053 return; 00054 00055 bindToPort(localPort); 00056 00057 cMessage *timer = new cMessage("sendTimer"); 00058 scheduleAt((double)par("messageFreq"), timer); 00059 } 00060 00061 IPvXAddress UDPBasicApp::chooseDestAddr() 00062 { 00063 int k = intrand(destAddresses.size()); 00064 return destAddresses[k]; 00065 } 00066 00067 00068 cPacket *UDPBasicApp::createPacket() 00069 { 00070 char msgName[32]; 00071 sprintf(msgName,"UDPBasicAppData-%d", counter++); 00072 00073 cPacket *payload = new cPacket(msgName); 00074 payload->setByteLength(par("messageLength").longValue()); 00075 return payload; 00076 } 00077 00078 void UDPBasicApp::sendPacket() 00079 { 00080 cPacket *payload = createPacket(); 00081 IPvXAddress destAddr = chooseDestAddr(); 00082 sendToUDP(payload, localPort, destAddr, destPort); 00083 00084 numSent++; 00085 } 00086 00087 void UDPBasicApp::handleMessage(cMessage *msg) 00088 { 00089 if (msg->isSelfMessage()) 00090 { 00091 // send, then reschedule next sending 00092 sendPacket(); 00093 scheduleAt(simTime()+(double)par("messageFreq"), msg); 00094 } 00095 else 00096 { 00097 // process incoming packet 00098 processPacket(PK(msg)); 00099 } 00100 00101 if (ev.isGUI()) 00102 { 00103 char buf[40]; 00104 sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent); 00105 getDisplayString().setTagArg("t",0,buf); 00106 } 00107 } 00108 00109 00110 void UDPBasicApp::processPacket(cPacket *msg) 00111 { 00112 EV << "Received packet: "; 00113 printPacket(msg); 00114 delete msg; 00115 00116 numReceived++; 00117 } 00118