DummyRoute.cc

00001 /***************************************************************************
00002  * file:       DummyRoute.cc
00003  *
00004  * author:      Jerome Rousselot
00005  *
00006  * copyright:   (C) 2009 CSEM SA, Neuchatel, Switzerland.
00007  *
00008  * description: Adaptor module that simply "translates" netwControlInfo to macControlInfo
00009  *
00010  **************************************************************************/
00011 
00012 #include <limits>
00013 #include <algorithm>
00014 
00015 #include "DummyRoute.h"
00016 #include "DummyRoutePkt_m.h"
00017 #include <NetwToMacControlInfo.h>
00018 #include <cassert>
00019 
00020 Define_Module(DummyRoute);
00021 
00022 void DummyRoute::initialize(int stage) {
00023   BaseNetwLayer::initialize(stage);
00024   if (stage == 0) {
00025     trace = par("trace");
00026     networkID = par("networkID");
00027   }
00028 }
00029 
00030 
00031 void DummyRoute::handleLowerMsg(cMessage* msg) {
00032   DummyRoutePkt* pkt = check_and_cast<DummyRoutePkt*>(msg);
00033   if(pkt->getNetworkID()==networkID) {
00034     sendUp(decapsMsg(pkt));
00035   } else {
00036     delete pkt;
00037   }
00038 }
00039 
00040 void DummyRoute::handleLowerControl(cMessage *msg) {
00041   sendControlUp(msg);
00042 }
00043 
00044 void DummyRoute::handleUpperMsg(cMessage* msg) {
00045 //  NetwControlInfo* cInfo =
00046 //      dynamic_cast<NetwControlInfo*> (msg->removeControlInfo());
00047 //  int nextHopMacAddr;
00048 //  if (cInfo == 0) {
00049 //    EV<<"DummyRoute warning: Application layer did not specifiy a destination L3 address\n"
00050 //         << "\tusing broadcast address instead\n";
00051 //    nextHopMacAddr = 0;
00052 //  } else {
00053 //    nextHopMacAddr = cInfo->getNetwAddr();
00054 //  }
00055 //  nextHopMacAddr = cInfo->getNetwAddr();
00056 //  msg->setControlInfo(new NetwToMacControlInfo(nextHopMacAddr));
00057   sendDown(encapsMsg(check_and_cast<cPacket*>(msg)));
00058 }
00059 
00060 void DummyRoute::finish() {
00061 }
00062 
00063 NetwPkt* DummyRoute::encapsMsg(cPacket *appPkt) {
00064     int macAddr;
00065     int netwAddr;
00066 
00067     debugEV <<"in encaps...\n";
00068 
00069     DummyRoutePkt *pkt = new DummyRoutePkt(appPkt->getName(), appPkt->getKind());
00070     pkt->setBitLength(headerLength);
00071 
00072     NetwControlInfo* cInfo = dynamic_cast<NetwControlInfo*>(appPkt->removeControlInfo());
00073 
00074     if(cInfo == 0){
00075     EV << "warning: Application layer did not specifiy a destination L3 address\n"
00076      << "\tusing broadcast address instead\n";
00077     netwAddr = L3BROADCAST;
00078     } else {
00079     debugEV <<"CInfo removed, netw addr="<< cInfo->getNetwAddr()<<endl;
00080         netwAddr = cInfo->getNetwAddr();
00081     delete cInfo;
00082     }
00083 
00084     pkt->setNetworkID(networkID);
00085     pkt->setSrcAddr(myNetwAddr);
00086     pkt->setDestAddr(netwAddr);
00087     debugEV << " netw "<< myNetwAddr << " sending packet" <<endl;
00088     if(netwAddr == L3BROADCAST) {
00089         debugEV << "sendDown: nHop=L3BROADCAST -> message has to be broadcasted"
00090            << " -> set destMac=L2BROADCAST\n";
00091         macAddr = L2BROADCAST;
00092     }
00093     else{
00094         debugEV <<"sendDown: get the MAC address\n";
00095         macAddr = arp->getMacAddr(netwAddr);
00096     }
00097 
00098     pkt->setControlInfo(new NetwToMacControlInfo(macAddr));
00099 
00100     //encapsulate the application packet
00101     pkt->encapsulate(appPkt);
00102     debugEV <<" pkt encapsulated\n";
00103     return pkt;
00104 }
00105 
00106 cMessage* DummyRoute::decapsMsg(NetwPkt *msg) {
00107   cMessage *m = msg->decapsulate();
00108   m->setControlInfo(new NetwControlInfo(msg->getSrcAddr()));
00109     // delete the netw packet
00110   delete msg;
00111   return m;
00112 }