INET Framework for OMNeT++/OMNEST
AnsaIPTrafGen.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 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 General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 
00020 
00021 #include <omnetpp.h>
00022 #include "AnsaIPTrafGen.h"
00023 #include "IPControlInfo.h"
00024 #include "IPv6ControlInfo.h"
00025 #include "IPAddressResolver.h"
00026 
00027 
00028 Define_Module(AnsaIPTrafSink);
00029 
00030 
00031 void AnsaIPTrafSink::initialize()
00032 {
00033     numReceived = 0;
00034     WATCH(numReceived);
00035 }
00036 
00037 void AnsaIPTrafSink::handleMessage(cMessage *msg)
00038 {
00039     processPacket(check_and_cast<cPacket *>(msg));
00040 
00041     if (ev.isGUI())
00042     {
00043         char buf[32];
00044         sprintf(buf, "rcvd: %d pks", numReceived);
00045         getDisplayString().setTagArg("t",0,buf);
00046     }
00047 
00048 }
00049 
00050 void AnsaIPTrafSink::printPacket(cPacket *msg)
00051 {
00052     IPvXAddress src, dest;
00053     int protocol = -1;
00054     if (dynamic_cast<IPControlInfo *>(msg->getControlInfo())!=NULL)
00055     {
00056         IPControlInfo *ctrl = (IPControlInfo *)msg->getControlInfo();
00057         src = ctrl->getSrcAddr();
00058         dest = ctrl->getDestAddr();
00059         protocol = ctrl->getProtocol();
00060     }
00061     else if (dynamic_cast<IPv6ControlInfo *>(msg->getControlInfo())!=NULL)
00062     {
00063         IPv6ControlInfo *ctrl = (IPv6ControlInfo *)msg->getControlInfo();
00064         src = ctrl->getSrcAddr();
00065         dest = ctrl->getDestAddr();
00066         protocol = ctrl->getProtocol();
00067     }
00068 
00069     ev  << msg << endl;
00070     ev  << "Payload length: " << msg->getByteLength() << " bytes" << endl;
00071     if (protocol!=-1)
00072         ev  << "src: " << src << "  dest: " << dest << "  protocol=" << protocol << "\n";
00073 }
00074 
00075 void AnsaIPTrafSink::processPacket(cPacket *msg)
00076 {
00077     EV << "Received packet: ";
00078     printPacket(msg);
00079     std::cout << "Arrive: " << msg->getFullName() << " at time = " << msg->getArrivalTime() << endl;
00080     delete msg;
00081 
00082     numReceived++;
00083 }
00084 
00085 
00086 
00087 //===============================================
00088 
00089 
00090 Define_Module(AnsaIPTrafGen);
00091 
00092 int AnsaIPTrafGen::counter;
00093 
00094 void AnsaIPTrafGen::initialize(int stage)
00095 {
00096     // because of IPAddressResolver, we need to wait until interfaces are registered,
00097     // address auto-assignment takes place etc.
00098     if (stage!=3)
00099         return;
00100 
00101     AnsaIPTrafSink::initialize();
00102 
00103     protocol = par("protocol");
00104     msgByteLength = par("packetLength");
00105     numPackets = par("numPackets");
00106     simtime_t startTime = par("startTime");
00107 
00108     const char *destAddrs = par("destAddresses");
00109     cStringTokenizer tokenizer(destAddrs);
00110     const char *token;
00111     while ((token = tokenizer.nextToken())!=NULL)
00112         destAddresses.push_back(IPAddressResolver().resolve(token));
00113 
00114     counter = 0;
00115 
00116     numSent = 0;
00117     WATCH(numSent);
00118 
00119     if (destAddresses.empty())
00120         return;
00121 
00122     cMessage *timer = new cMessage("sendTimer");
00123     scheduleAt(startTime, timer);
00124 }
00125 
00126 IPvXAddress AnsaIPTrafGen::chooseDestAddr()
00127 {
00128     int k = intrand(destAddresses.size());
00129     return destAddresses[k];
00130 }
00131 
00132 void AnsaIPTrafGen::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         std::cout << "Send: " << payload->getFullName() << " at time = " << simTime()  << endl;
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 AnsaIPTrafGen::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     }
00180     else
00181     {
00182         // process incoming packet
00183         processPacket(PK(msg));
00184     }
00185 
00186     if (ev.isGUI())
00187     {
00188         char buf[40];
00189         sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent);
00190         getDisplayString().setTagArg("t",0,buf);
00191     }
00192 }
00193 
00194