IPTrafGen.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2000 Institut fuer Telematik, Universitaet Karlsruhe
00003 // Copyright (C) 2004-2005 Andras Varga
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00017 //
00018 
00019 
00020 #include <omnetpp.h>
00021 #include "IPTrafGen.h"
00022 #include "IPControlInfo.h"
00023 #include "IPv6ControlInfo.h"
00024 #include "IPAddressResolver.h"
00025 
00026 
00027 Define_Module(IPTrafSink);
00028 
00029 
00030 void IPTrafSink::initialize()
00031 {
00032     numReceived = 0;
00033     WATCH(numReceived);
00034 }
00035 
00036 void IPTrafSink::handleMessage(cMessage *msg)
00037 {
00038     processPacket(check_and_cast<cPacket *>(msg));
00039 
00040     if (ev.isGUI())
00041     {
00042         char buf[32];
00043         sprintf(buf, "rcvd: %d pks", numReceived);
00044         getDisplayString().setTagArg("t",0,buf);
00045     }
00046 
00047 }
00048 
00049 void IPTrafSink::printPacket(cPacket *msg)
00050 {
00051     IPvXAddress src, dest;
00052     int protocol = -1;
00053     if (dynamic_cast<IPControlInfo *>(msg->getControlInfo())!=NULL)
00054     {
00055         IPControlInfo *ctrl = (IPControlInfo *)msg->getControlInfo();
00056         src = ctrl->getSrcAddr();
00057         dest = ctrl->getDestAddr();
00058         protocol = ctrl->getProtocol();
00059     }
00060     else if (dynamic_cast<IPv6ControlInfo *>(msg->getControlInfo())!=NULL)
00061     {
00062         IPv6ControlInfo *ctrl = (IPv6ControlInfo *)msg->getControlInfo();
00063         src = ctrl->getSrcAddr();
00064         dest = ctrl->getDestAddr();
00065         protocol = ctrl->getProtocol();
00066     }
00067 
00068     ev  << msg << endl;
00069     ev  << "Payload length: " << msg->getByteLength() << " bytes" << endl;
00070     if (protocol!=-1)
00071         ev  << "src: " << src << "  dest: " << dest << "  protocol=" << protocol << "\n";
00072 }
00073 
00074 void IPTrafSink::processPacket(cPacket *msg)
00075 {
00076     EV << "Received packet: ";
00077     printPacket(msg);
00078     delete msg;
00079 
00080     numReceived++;
00081 }
00082 
00083 
00084 
00085 //===============================================
00086 
00087 
00088 Define_Module(IPTrafGen);
00089 
00090 int IPTrafGen::counter;
00091 
00092 void IPTrafGen::initialize(int stage)
00093 {
00094     // because of IPAddressResolver, we need to wait until interfaces are registered,
00095     // address auto-assignment takes place etc.
00096     if (stage!=3)
00097         return;
00098 
00099     IPTrafSink::initialize();
00100 
00101     protocol = par("protocol");
00102     msgByteLength = par("packetLength");
00103     numPackets = par("numPackets");
00104     simtime_t startTime = par("startTime");
00105 
00106     const char *destAddrs = par("destAddresses");
00107     cStringTokenizer tokenizer(destAddrs);
00108     const char *token;
00109     while ((token = tokenizer.nextToken())!=NULL)
00110         destAddresses.push_back(IPAddressResolver().resolve(token));
00111 
00112     counter = 0;
00113 
00114     numSent = 0;
00115     WATCH(numSent);
00116 
00117     if (destAddresses.empty())
00118         return;
00119 
00120     if (numPackets > 0) {
00121         cMessage *timer = new cMessage("sendTimer");
00122         scheduleAt(startTime, timer);
00123     }
00124 }
00125 
00126 IPvXAddress IPTrafGen::chooseDestAddr()
00127 {
00128     int k = intrand(destAddresses.size());
00129     return destAddresses[k];
00130 }
00131 
00132 void IPTrafGen::sendPacket()
00133 {
00134     char msgName[32];
00135     sprintf(msgName,"appData-%d", counter++);
00136 
00137     cPacket *payload = new cPacket(msgName);
00138     payload->setByteLength(msgByteLength);
00139 
00140     IPvXAddress destAddr = chooseDestAddr();
00141     if (!destAddr.isIPv6())
00142     {
00143         // send to IPv4
00144         IPControlInfo *controlInfo = new IPControlInfo();
00145         controlInfo->setDestAddr(destAddr.get4());
00146         controlInfo->setProtocol(protocol);
00147         payload->setControlInfo(controlInfo);
00148 
00149         EV << "Sending packet: ";
00150         printPacket(payload);
00151 
00152         send(payload, "ipOut");
00153     }
00154     else
00155     {
00156         // send to IPv6
00157         IPv6ControlInfo *controlInfo = new IPv6ControlInfo();
00158         controlInfo->setDestAddr(destAddr.get6());
00159         controlInfo->setProtocol(protocol);
00160         payload->setControlInfo(controlInfo);
00161 
00162         EV << "Sending packet: ";
00163         printPacket(payload);
00164 
00165         send(payload, "ipv6Out");
00166     }
00167     numSent++;
00168 }
00169 
00170 void IPTrafGen::handleMessage(cMessage *msg)
00171 {
00172     if (msg->isSelfMessage())
00173     {
00174         // send, then reschedule next sending
00175         sendPacket();
00176 
00177         if (!numPackets || numSent<numPackets)
00178             scheduleAt(simTime()+(double)par("packetInterval"), msg);
00179         else
00180             delete msg;
00181     }
00182     else
00183     {
00184         // process incoming packet
00185         processPacket(PK(msg));
00186     }
00187 
00188     if (ev.isGUI())
00189     {
00190         char buf[40];
00191         sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent);
00192         getDisplayString().setTagArg("t",0,buf);
00193     }
00194 }
00195 
00196