📄 802_11_mac.cc
字号:
#include "802_11_MAC.h"#include "CoOrdinatorBase.h"#include "Gear_packet_m.h"//#include <fstream.h>// This 802_11_MAC implementation considers the DCF - Distributed Coordination Function mode of operation in which each station compete for the channel individually// For DCF mode, 802_11 uses CSMA/CA ( Collision Avoidance )protocol// Whenever a new msg comes from network layer it is stored in the queue and when its nav timer expires , checks the channel to be idle for DIFS and then goes into back off interval. and tehn transmits after back off timer expires.Define_Module_Like ( MAC_802_11 , MAC802LayerModule );MAC_802_11::MAC_802_11(const char *name, cModule *parentModule, unsigned stacksize) : MACLayerBase(name, parentModule, stacksize){ m_SIFS = m_DIFS = m_EIFS = m_SlotTime = m_NAVTimer = m_New_NAVTimer=tstamp_NAVTimer= 0.0; iBOffTime = tstamp_BOffTimer = remainingBOffTime=0.0; m_pMsgRTSExpired = m_pMsgNAVTimer = NULL; m_FragmentLength = 100; m_BitRate = 100; Bflag = 0; m_pMsgCollisionTimer = NULL; m_pMsgDIFSTimer = NULL; m_pMsgBOffTimer = NULL; RtsExpiredCount = 0;} void MAC_802_11::initialize(){ cPar ¶m = par("SIFS"); m_SIFS = param.doubleValue(); param = par("DIFS"); m_DIFS = param.doubleValue(); param = par("EIFS"); m_EIFS = param.doubleValue(); param = par("SlotTime"); m_SlotTime = param.doubleValue(); m_pMsgRTSExpired = new cMessage("RTS_Expired", RTS_EXPIRED); m_pMsgNAVTimer = new cMessage("CheckAndTransmit", NAVTIMER); m_pMsgDIFSTimer = new cMessage("DIFS CHECK",DIFS_TIMER ); m_pMsgCollisionTimer = new cMessage("Collison Check", COLLISION_TIMER ); m_pMsgBOffTimer = new cMessage("BackOffTiemr", BACK_OFF ); //printf("\n\t MACLayeriodule: Name of current module: %s", fullName()); pNode = parentModule(); pNetwork = pNode->parentModule() ; no_of_nodes = pNetwork->par("numOfNodes");pCoOrdinator = pNode->submodule("CoOrdinator"); MyNodeId = ((CoOrdinatorBase*)pCoOrdinator)->getNodeId(); originMsgSrc = 0; fWrite = false; strcpy(fileb,"mac_name"); SetState(INITIALIZED); resetCW(); ssrc=0;}void MAC_802_11::HandleMessageFromTopLayer(cMessage *msg){ char strMessageName[255] = "MAC_802_11::HandleMessageFromTopLayer"; char strMessage[255]; int numOfFragments = 1; //msg->length() / m_FragmentLength + 1; /*sprintf(strMessage, "Node: %d, Data received from top layer. DataLength: %d, Number Of Fragments: %d", ((CoOrdinatorBase*)pCoOrdinator)->getNodeId(), msg->length(), numOfFragments); recordScalar(strMessageName, strMessage);*/ c802_11_Packet *pMACMsg; Header &NetHeader = ((BeaconPkt*)msg)->getHdr(); if(msg->kind()!=3 ) { originMsgSrc=NetHeader.OriginSrc; //printf( "\n %f MSG FROM N/W LAYER, Msg Kind is %d , and My node id = %d \n",simTime(), msg->kind(), ((CoOrdinatorBase*)pCoOrdinator)->getNodeId()); if((originMsgSrc>=0)&&(originMsgSrc<no_of_nodes)) if(fWrite) { FILE *nw_pkt; sprintf(fName,"mac/%s%d.txt", fileb,originMsgSrc);//((CoOrdinatorBase*)pCoOrdinator)->getNodeId()); nw_pkt = fopen(fName,"ab"); char a[255]; sprintf(a, "\n %f MSG FROM N/W LAYER, Msg Kind is %d , and My node id = %d \n",simTime(), msg->kind(), ((CoOrdinatorBase*)pCoOrdinator)->getNodeId()); fputs(a,nw_pkt); fclose(nw_pkt); } } if ( NetHeader.NextHopDestId == 999999 ) { /*sprintf(strMessage, "Node: %d, Broadcast Data received from top layer ", ((CoOrdinatorBase*)pCoOrdinator)->getNodeId() ); recordScalar(strMessageName, strMessage);*/ Create_BRDCAST_Message( msg, &pMACMsg ); /* if( ((CoOrdinatorBase*)pCoOrdinator)->getNodeId() == 4) { sprintf (strMessage, " MY MSG KIND %d " , (int) pMACMsg->kind() ); recordScalar(strMessageName, strMessage);}*/ m_QueueOfMessages.insert((cObject*)pMACMsg ); } else { //if(msg->length > RTS_THRESHOLD ) Create_RTS_Message(msg, &pMACMsg); // Insert the RTS into the queue /* if( ((CoOrdinatorBase*)pCoOrdinator)->getNodeId() == 4) { sprintf (strMessage, " MY MSG KIND %d" ,(int) pMACMsg->kind() ); recordScalar(strMessageName, strMessage);}*/ m_QueueOfMessages.insert((cObject*)pMACMsg); if((originMsgSrc>=0)&&(originMsgSrc<no_of_nodes)) if(fWrite) { FILE *nw_pkt; sprintf(fName,"mac/%s%d.txt", fileb,originMsgSrc);//((CoOrdinatorBase*)pCoOrdinator)->getNodeId()); nw_pkt = fopen(fName,"ab"); char a[255]; s802_11_MAC_Header* MACHeader; MACHeader = &(pMACMsg)->getHdr(); sprintf(a, " %f Created RTS from MyNodeId %d to %d for Msg Kind is %d ,Position of RTS in the queue = %d \n", simTime(), MACHeader->SourceMACAddress, MACHeader->DestinationMACAddress, msg->kind(), m_QueueOfMessages.length() ); fputs(a,nw_pkt); fclose(nw_pkt); } // One for DATA and one for acknowledgement cMessage *dataMsg = (cMessage*)msg->dup(); seq_no = 0; m_QueueOfMessages.insert((cObject*)dataMsg ); if((originMsgSrc>=0)&&(originMsgSrc<no_of_nodes)) if(fWrite) { FILE *nw_pkt; sprintf(fName,"mac/%s%d.txt", fileb,originMsgSrc);//((CoOrdinatorBase*)pCoOrdinator)->getNodeId()); nw_pkt = fopen(fName,"ab"); char a[255]; sprintf(a, " %f Created DATA Message Msg Kind is %d ,Position in the queue = %d My node id= %d \n",simTime(), msg->kind(), m_QueueOfMessages.length(), ((CoOrdinatorBase*)pCoOrdinator)->getNodeId()); fputs(a,nw_pkt); fclose(nw_pkt); } // One for DATA and one for acknowledgement for (int iFragmentIndex = 0; iFragmentIndex < numOfFragments; iFragmentIndex++) { c802_11_Packet *pMACFragment; cMessage *fragMsg = (cMessage*)msg->dup(); Create_Data_Message(fragMsg, iFragmentIndex, numOfFragments,&pMACFragment); // Insert the real data into the Fragment Queue m_QueueOfFragments.insert(pMACFragment); if((originMsgSrc>=0)&&(originMsgSrc<no_of_nodes)) if(fWrite) { FILE *nw_pkt; sprintf(fName,"mac/%s%d.txt", fileb,originMsgSrc);//((CoOrdinatorBase*)pCoOrdinator)->getNodeId()); nw_pkt = fopen(fName,"ab"); char a[255]; sprintf(a, " %f Created FRAGMENT Msg Kind is %d ,Position of FRAGMENT in the queue = %d and My node id = %d \n",simTime(),msg->kind(), m_QueueOfFragments.length(), ((CoOrdinatorBase*)pCoOrdinator)->getNodeId()); fputs(a,nw_pkt); fclose(nw_pkt); } // One for DATA and one for acknowledgement } } if(!m_pMsgNAVTimer->isScheduled()) scheduleAt(simTime()+m_NAVTimer , m_pMsgNAVTimer); //delete msg; return; }void MAC_802_11::HandleMessageFromBottomLayer(cMessage *msg){ char strMessageName[255] = "MAC_802_11::HandleMessageFromBottomLayer"; char strMessage[512] = ""; //printf("\n\t In HandleMessageFromBottomLayer, message type: %d\n\n", msg->kind()); switch(msg->kind()) { case RTS_TYPE: { if (false == CheckIfMessageIsForMe(msg) ) { // Intermediate Node // printf(" Intermediate node's msg for %d, simTime = %.20f, from %d \n\n", MyNodeId, simTime(),((c802_11_Packet*)msg)->getHdr().SourceMACAddress ); m_New_NAVTimer= ((c802_11_Packet*)msg)->getHdr().timeForTransfer; if (0 != m_NAVTimer) { if( (tstamp_NAVTimer-simTime() ) < m_New_NAVTimer ) { /*sprintf(strMessage,"RTS_TYPE UPDATE NAVTIMER , tstamp nav=%f, nav time=%f, new nav=%f", tstamp_NAVTimer,m_NAVTimer, m_New_NAVTimer ); recordScalar("RTS_INTERMEDIATE",strMessage);*/ changeNAVTimer(); } } else changeNAVTimer(); delete msg; /*m_NAVTimer = ((c802_11_Packet*)msg)->getHdr().timeForTransfer; changeNAVTimer(); // If the NAV Timer is already scheduled, then update it accordingly... if (true == m_pMsgNAVTimer->isScheduled()) { cancelEvent(m_pMsgNAVTimer); } scheduleAt(simTime() + m_NAVTimer, m_pMsgNAVTimer); */ /*sprintf(strMessage, "RTS received for %d -- Intermediate node Setting NAVTimer to %f, Current simeTime: %f.", ((CoOrdinatorBase*)pCoOrdinator)->getNodeId(), m_NAVTimer, simTime()); recordScalar("RTS_INTERMEDIATE", strMessage);*/ break; } // If already set the NAVTimer for some other transmission in the // neighborhood, DO NOT send the CTS. if( 0 != m_NAVTimer) // { // recordScalar("RTS_MATCH_BUT_NAV_TIMER_NOT_ZERO", " RTS_MAtch but not sending back the CTS because NAVTimer is not zero"); break; } m_New_NAVTimer = ((c802_11_Packet*)msg)->getHdr().timeForTransfer; changeNAVTimer(); /* If the NAV Timer is already scheduled, then update it accordingly... if (true == m_pMsgNAVTimer->isScheduled()) { cancelEvent(m_pMsgNAVTimer); } scheduleAt(simTime() + m_NAVTimer, m_pMsgNAVTimer);*/ //recordScalar("-----------------", "----------------------------------------------------------------------"); /*sprintf(strMessage, "RTS received for %d from %d -- Setting NAVTimer to %f. Current simulation Time: %f.", ((CoOrdinatorBase*)pCoOrdinator)->getNodeId(), ((c802_11_Packet*)msg)->getHdr().SourceMACAddress, m_NAVTimer, simTime());*/ //recordScalar("RTS_MATCH", strMessage); //recordScalar("-----------------", "----------------------------------------------------------------------"); originMsgSrc=msg->par("QuerySrc"); //Create the CTS Message c802_11_Packet *pMACMsg; Create_CTS_Message(msg, &pMACMsg); sendDelayed(pMACMsg,0.000306, "toBottomLayer"); if((originMsgSrc>=0)&&(originMsgSrc<no_of_nodes)) if(fWrite) { FILE *nw_pkt; sprintf(fName,"mac/%s%d.txt", fileb,originMsgSrc);//((CoOrdinatorBase*)pCoOrdinator)->getNodeId()); nw_pkt = fopen(fName,"ab"); char a[255]; sprintf(a, " %f Got RTS from %d Created CTS Message sending My node id = %d \n",simTime(), ((c802_11_Packet*)msg)->getHdr().SourceMACAddress, ((CoOrdinatorBase*)pCoOrdinator)->getNodeId()); fputs(a,nw_pkt); fclose(nw_pkt); } // One for DATA and one for acknowledgement // delete msg; break; } case CTS_TYPE: { if (false == CheckIfMessageIsForMe(msg) ) { // Intermediate Node m_New_NAVTimer= ((c802_11_Packet*)msg)->getHdr().timeForTransfer; if (0 != m_NAVTimer) { if( (tstamp_NAVTimer-simTime() ) < m_New_NAVTimer ) { changeNAVTimer(); } } else changeNAVTimer(); delete msg; break; } //changed 11 lines m_New_NAVTimer= ((c802_11_Packet*)msg)->getHdr().timeForTransfer; if (0 != m_NAVTimer) { if( (tstamp_NAVTimer-simTime() ) < m_New_NAVTimer ) { changeNAVTimer(); } } else changeNAVTimer(); ssrc = 0; /*recordScalar("-----------------", "----------------------------------------------------------------------"); sprintf(strMessage, "CTS received for %d -- SENDER from %d, lenght of queue of messages :d", ((CoOrdinatorBase*)pCoOrdinator)->getNodeId(), ((c802_11_Packet*)msg)->getHdr().SourceMACAddress, m_QueueOfMessages.length() ); recordScalar("CTS_MATCH:", strMessage); recordScalar("-----------------", "----------------------------------------------------------------------");*/ // Send the Data to the receiver //m_NAVTimer = 0; if(m_pMsgRTSExpired->isScheduled()) cancelEvent(m_pMsgRTSExpired); cMessage* pMsg= (cMessage*)m_QueueOfMessages.pop(); // printf(" POP Msg kind = %d MyNodeId %d \n",pMsg->kind(),((CoOrdinatorBase*)pCoOrdinator)->getNodeId()); pMsg= (cMessage*)m_QueueOfMessages.tail(); // printf(" POP 2 Msg kind = %d\n",pMsg->kind(),((CoOrdinatorBase*)pCoOrdinator)->getNodeId()); m_QueueOfMessages.pop(); // One for DATA and one for acknowledgement if (0 < m_QueueOfFragments.length()){ c802_11_Packet *pFragment = (c802_11_Packet*)m_QueueOfFragments.tail(); c802_11_Packet *pDataFragment = (c802_11_Packet*)pFragment->dup(); cMessage *network_msg = (cMessage*)pDataFragment->getObject("Netw_Pkt"); Header &NetHeader = ((BeaconPkt*)network_msg)->getHdr(); if( NetHeader.OriginSrc >= 0 && NetHeader.OriginSrc<no_of_nodes ) if(fWrite) { FILE *nw_pkt; sprintf(fName,"mac/%s%d.txt", fileb, NetHeader.OriginSrc); nw_pkt = fopen(fName,"ab");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -