SimpleNetwLayer.h

00001 /*
00002  * SimpleNetwLayer.h
00003  *
00004  *  Created on: 29.08.2008
00005  *      Author: Karl Wessel
00006  */
00007 
00008 #ifndef SIMPLENETWLAYER_H_
00009 #define SIMPLENETWLAYER_H_
00010 
00011 #include <omnetpp.h>
00012 #include <cassert>
00013 #include <BaseModule.h>
00014 #include <NetwPkt_m.h>
00015 #include <SimpleAddress.h>
00016 #include <NetwToMacControlInfo.h>
00017 #include <MacToNetwControlInfo.h>
00018 #include <BaseMacLayer.h>
00019 
00041 class SimpleNetwLayer : public BaseModule{
00042 //--------members----------
00043 protected:
00044   bool isSwitch;
00046   simtime_t boredTime;
00047 
00048   int maxTtl;
00049 
00050   int ip;
00051 
00052   int dataIn;
00053   int dataOut;
00054 
00055   cMessage* startJabberTimer;
00056 
00057   unsigned long runningSeqNumber;
00058 
00059   typedef std::map<int, int> RoutingTable;
00060 
00061   RoutingTable routingTable;
00062 
00063   enum NetwPktKind{
00064     HELLO_WORLD = 4200,
00065     JABBER,
00066     START_TO_JABBER
00067   };
00068 
00069 //--------methods----------
00070 protected:
00071   void scheduleJabbering(){
00072     if(startJabberTimer->isScheduled()){
00073       cancelEvent(startJabberTimer);
00074     }
00075 
00076     scheduleAt(simTime() + boredTime, startJabberTimer);
00077   }
00078 
00079   void broadcastHelloWorld() {
00080     assert(!ev.isDisabled());
00081     ev << "Broadcasting hello world.\n";
00082     NetwPkt* helloWorld = new NetwPkt("helloWorld", HELLO_WORLD);
00083 
00084     helloWorld->setDestAddr(L3BROADCAST);
00085     helloWorld->setSrcAddr(ip);
00086     helloWorld->setSeqNum(runningSeqNumber++);
00087     helloWorld->setTtl(maxTtl);
00088 
00089     NetwToMacControlInfo* cInfo = new NetwToMacControlInfo(L2BROADCAST);
00090 
00091     helloWorld->setControlInfo(cInfo);
00092 
00093     getNode()->bubble("Hello World!");
00094 
00095     sendDown(helloWorld);
00096   }
00097 
00098   void sendDown(cPacket* pkt) {
00099     send(pkt, dataOut);
00100   }
00101 
00102   void forwardPacket(NetwPkt* pkt, int nextHop){
00103     NetwPkt* fwd = new NetwPkt(pkt->getName(), pkt->getKind());
00104 
00105     fwd->setDestAddr(pkt->getDestAddr());
00106     fwd->setSrcAddr(pkt->getSrcAddr());
00107     fwd->setSeqNum(pkt->getSeqNum());
00108     fwd->setTtl(pkt->getTtl() - 1);
00109 
00110     NetwToMacControlInfo* cInfo = new NetwToMacControlInfo(nextHop);
00111 
00112     fwd->setControlInfo(cInfo);
00113 
00114     sendDown(fwd);
00115   }
00116 
00117   void handleHelloWorld(NetwPkt* pkt){
00118 
00119     //who said hello?
00120     int srcIP = pkt->getSrcAddr();
00121 
00122     //we already know ourself...
00123     if (srcIP == ip){
00124       delete pkt;
00125       return;
00126     }
00127 
00128     //do we already know him?
00129     if(routingTable.count(srcIP) == 0){
00130       //if not add him with the mac address of the previous hop
00131       MacToNetwControlInfo* cInfo = static_cast<MacToNetwControlInfo*>(pkt->getControlInfo());
00132 
00133       int prevHop = cInfo->getLastHopMac();
00134 
00135       routingTable[srcIP] = prevHop;
00136 
00137       char buff[255];
00138       sprintf(buff, "Got hello from %d", srcIP);
00139       getNode()->bubble(buff);
00140       ev << buff << endl;
00141     }
00142 
00143     //if we are a switch and the time to live of the packet
00144     //hasn't exceeded yet forward it
00145     if(isSwitch){
00146       if(pkt->getTtl() > 0) {
00147         forwardPacket(pkt, L2BROADCAST);
00148         char buff[255];
00149         sprintf(buff, "%d said hello!", srcIP);
00150         getNode()->bubble(buff);
00151         ev << buff << endl;
00152       }
00153     } else {
00154       //otherwise reset the bored timer after when we will start jabbering
00155       scheduleJabbering();
00156     }
00157 
00158     delete pkt;
00159   }
00160 
00161   void jabberToSomeone(){
00162     assert(!isSwitch);
00163     assert(routingTable.size() > 0);
00164     int target = intrand(routingTable.size());
00165 
00166 
00167     RoutingTable::const_iterator it = routingTable.begin();
00168     for(int i = 0; i < target; ++i)
00169       ++it;
00170 
00171     ev << "Jabbering - Routingtablesize:" << routingTable.size() << "  target:" << target << "  dest:" << it->first << endl;
00172 
00173     NetwPkt* jabber = new NetwPkt("jabber", JABBER);
00174 
00175     jabber->setDestAddr(it->first);
00176     jabber->setSrcAddr(ip);
00177     jabber->setSeqNum(runningSeqNumber++);
00178     jabber->setTtl(maxTtl);
00179 
00180     NetwToMacControlInfo* cInfo = new NetwToMacControlInfo(it->second);
00181 
00182     jabber->setControlInfo(cInfo);
00183 
00184     char buff[255];
00185     sprintf(buff, "Babbling with %d", it->first);
00186     getNode()->bubble(buff);
00187     ev << buff << endl;
00188     sendDown(jabber);
00189 
00190     scheduleJabbering();
00191   }
00192 
00193   void handleIncomingJabber(NetwPkt* pkt){
00194     if(isSwitch) {
00195       if(pkt->getDestAddr() != ip){
00196         assert(pkt->getTtl() > 0);
00197         assert(routingTable.count(pkt->getDestAddr()) > 0);
00198 
00199         int nextHop = routingTable[pkt->getDestAddr()];
00200 
00201         char buff[255];
00202         sprintf(buff, "%d babbles with %d", pkt->getSrcAddr(), pkt->getDestAddr());
00203         getNode()->bubble(buff);
00204         ev << buff << endl;
00205 
00206         forwardPacket(pkt, nextHop);
00207       } else {
00208         char buff[255];
00209         sprintf(buff, "%d babbles with me. But I'm a serious switch, I do not babble...", pkt->getSrcAddr());
00210         getNode()->bubble(buff);
00211         ev << buff << endl;
00212       }
00213     } else {
00214       assert(pkt->getDestAddr() == ip);
00215 
00216       char buff[255];
00217       sprintf(buff, "Got babbling from %d", pkt->getSrcAddr());
00218       getNode()->bubble(buff);
00219       ev << buff << endl;
00220     }
00221 
00222     delete pkt;
00223   }
00224 
00225 public:
00226   virtual ~SimpleNetwLayer() {
00227     cancelAndDelete(startJabberTimer);
00228   }
00229 
00230   virtual void initialize(int stage){
00231 
00232     if(stage == 0){
00233       dataOut = findGate("lowerGateOut");
00234       dataIn = findGate("lowerGateIn");
00235 
00236       isSwitch = par("isSwitch").boolValue();
00237 
00238       if(isSwitch)
00239         getParentModule()->getParentModule()->getDisplayString().setTagArg("i",0,"device/accesspoint");
00240 
00241       ip = par("ip").longValue();
00242       maxTtl = par("maxTtl").longValue();
00243       boredTime = par("boredTime").doubleValue();
00244     } else if(stage == 1) {
00245       startJabberTimer = new cMessage("jabber!", START_TO_JABBER);
00246       broadcastHelloWorld();
00247     }
00248   }
00249 
00250   virtual void handleMessage(cMessage* msg){
00251 
00252     switch(msg->getKind()){
00253     case HELLO_WORLD:
00254       handleHelloWorld(static_cast<NetwPkt*>(msg));
00255       break;
00256     case START_TO_JABBER:
00257       jabberToSomeone();
00258       break;
00259     case JABBER:
00260       handleIncomingJabber(static_cast<NetwPkt*>(msg));
00261       break;
00262 
00263     case BaseMacLayer::PACKET_DROPPED:
00264       ev << "Packet dropped by MAC layer." << endl;
00265       delete msg;
00266       break;
00267 
00268     default:
00269       error("unknown packet type of packet %s", msg->getName());
00270       break;
00271     }
00272   }
00273 
00274 
00275 };
00276 
00277 #endif /* SIMPLENETWLAYER_H_ */