BaseNetwLayer.cc

00001 /***************************************************************************
00002  * file:        BaseNetwLayer.cc
00003  *
00004  * author:      Daniel Willkomm
00005  *
00006  * copyright:   (C) 2004 Telecommunication Networks Group (TKN) at
00007  *              Technische Universitaet Berlin, Germany.
00008  *
00009  *              This program is free software; you can redistribute it
00010  *              and/or modify it under the terms of the GNU General Public
00011  *              License as published by the Free Software Foundation; either
00012  *              version 2 of the License, or (at your option) any later
00013  *              version.
00014  *              For further information see file COPYING
00015  *              in the top level directory
00016  ***************************************************************************
00017  * part of:     framework implementation developed by tkn
00018  * description: network layer: general class for the network layer
00019  *              subclass to create your own network layer
00020  ***************************************************************************/
00021 
00022 
00023 #include "BaseNetwLayer.h"
00024 #include "NetwControlInfo.h"
00025 #include "NetwToMacControlInfo.h"
00026 #include "BaseMacLayer.h"
00027 #include "AddressingInterface.h"
00028 
00029 #include <cassert>
00030 
00031 Define_Module(BaseNetwLayer);
00032 
00033 void BaseNetwLayer::initialize(int stage)
00034 {
00035     BaseLayer::initialize(stage);
00036 
00037     if(stage==0){
00038       coreDebug = par("coreDebug").boolValue();
00039         headerLength= par("headerLength");
00040         arp = FindModule<ArpInterface*>::findSubModule(findHost());
00041     }
00042     else if(stage == 1) {
00043       // see if there is an addressing module available
00044       // otherwise use module id as network address
00045         AddressingInterface* addrScheme = FindModule<AddressingInterface*>
00046 													::findSubModule(findHost());
00047         if(addrScheme) {
00048           myNetwAddr = addrScheme->myNetwAddr(this);
00049         } else {
00050           myNetwAddr = getId();
00051         }
00052         coreEV << " myNetwAddr " << myNetwAddr << endl;
00053     }
00054 }
00055 
00059 cMessage* BaseNetwLayer::decapsMsg(NetwPkt *msg)
00060 {
00061     cMessage *m = msg->decapsulate();
00062     m->setControlInfo(new NetwControlInfo(msg->getSrcAddr()));
00063     // delete the netw packet
00064     delete msg;
00065     return m;
00066 }
00067 
00068 
00073 NetwPkt* BaseNetwLayer::encapsMsg(cPacket *appPkt) {
00074     int macAddr;
00075     int netwAddr;
00076 
00077     coreEV <<"in encaps...\n";
00078 
00079     NetwPkt *pkt = new NetwPkt(appPkt->getName(), appPkt->getKind());
00080     pkt->setBitLength(headerLength);
00081 
00082     NetwControlInfo* cInfo = dynamic_cast<NetwControlInfo*>(appPkt->removeControlInfo());
00083 
00084     if(cInfo == 0){
00085   EV << "warning: Application layer did not specifiy a destination L3 address\n"
00086      << "\tusing broadcast address instead\n";
00087   netwAddr = L3BROADCAST;
00088     } else {
00089   coreEV <<"CInfo removed, netw addr="<< cInfo->getNetwAddr()<<endl;
00090         netwAddr = cInfo->getNetwAddr();
00091   delete cInfo;
00092     }
00093 
00094     pkt->setSrcAddr(myNetwAddr);
00095     pkt->setDestAddr(netwAddr);
00096     coreEV << " netw "<< myNetwAddr << " sending packet" <<endl;
00097     if(netwAddr == L3BROADCAST) {
00098         coreEV << "sendDown: nHop=L3BROADCAST -> message has to be broadcasted"
00099            << " -> set destMac=L2BROADCAST\n";
00100         macAddr = L2BROADCAST;
00101     }
00102     else{
00103         coreEV <<"sendDown: get the MAC address\n";
00104         macAddr = arp->getMacAddr(netwAddr);
00105     }
00106 
00107     pkt->setControlInfo(new NetwToMacControlInfo(macAddr));
00108 
00109     //encapsulate the application packet
00110     pkt->encapsulate(appPkt);
00111     coreEV <<" pkt encapsulated\n";
00112     return pkt;
00113 }
00114 
00123 void BaseNetwLayer::handleLowerMsg(cMessage* msg)
00124 {
00125     NetwPkt *m = static_cast<NetwPkt *>(msg);
00126     coreEV << " handling packet from " << m->getSrcAddr() << endl;
00127     sendUp(decapsMsg(m));
00128 }
00129 
00140 void BaseNetwLayer::handleUpperMsg(cMessage* msg)
00141 {
00142   assert(dynamic_cast<cPacket*>(msg));
00143     sendDown(encapsMsg(static_cast<cPacket*>(msg)));
00144 }
00145 
00157 void BaseNetwLayer::handleLowerControl(cMessage* msg)
00158 {
00159   switch (msg->getKind())
00160   {
00161   case BaseMacLayer::TX_OVER:
00162     delete msg;
00163     break;
00164   default:
00165     EV << "BaseNetwLayer does not handle control messages called "
00166        << msg->getName() << endl;
00167     delete msg;
00168   }
00169 }