📄 gear.cc
字号:
#include "gear.h"#include "CoOrdinatorBase.h"#include "Calculations.h"#include "config.h"#include "Attribute.h"#include <math.h>#include "test.h"#include <fstream.h>#include <stdio.h>Define_Module_Like ( Gear , NetLayerModule );Gear::Gear(): NetLayerBase("Gear",NULL,0){// printf("gear constructor"); }Gear::Gear(const char *name,cModule *parentModule,unsigned stacksize): NetLayerBase(name,parentModule,stacksize){ flag=0;// printf("gear constructor with arg"); }void Gear :: initialize(void){ //cModule *pNode = parentModule(); pNode = parentModule(); pCoOrdinator = pNode->submodule("CoOrdinator"); MyNodeId=((CoOrdinatorBase*)pCoOrdinator)->getNodeId(); cModule *pNetwork=pNode->parentModule(); RegionList=new cArray(); neigh_list=new cArray(); srcArray=new cArray(); count=0; fileWrite = false; sendingData = false; //cPar* tempPar = &(pNetwork->par("filea")); strcpy(filea, "name");//tempPar->stringValue()); //printf("FILEA VALUE ISSSSSSSSSSSSS+++%s", filea); //Fse = false; dataCount=0; dataGeneratedTotal=0; gateWay=new cArray(); char s[50]; seqNo=0; firstSeqNo=0;}Gear::~Gear(){ //printf("Gear Distructor 1/n"); delete pCoOrdinator; //printf("Gear Distructor 2/n"); delete RegionList; delete neigh_list; //printf("Gear Distructor 3/n"); delete srcArray; cMessage* cm; //printf("Gear Distructor 4/n"); //if(count==0) //{ //printf("The TOTAL MESSAGES ====%ld",cm->total_msgs); // printf("The TOTAL LIVE MEssage=%ld",cm->live_msgs); count=1; //}}void Gear::handleMessage(cMessage *Msg){ cMessage* reply=NULL; BeaconPkt* beaconPkt; GearPacket* gpr=(GearPacket*)Msg; Header* hdr=&gpr->getHdr(); if(Msg->arrivalGateId()==gate("fromTopLayer")->id()) {flag=1;qFlag=true;} else if(Msg->isSelfMessage()) flag=2; else flag=0; threshold=par("RefreshInterval"); region_threshold=par("RegionRefreshInterval"); if(((hdr->NextHopDestId==((CoOrdinatorBase*)pCoOrdinator)->getNodeId()||(hdr->NextHopDestId==999999))&&(flag==0))||(flag==1)|| (flag==2)) { int x,y,RegionX,RegionY,range; double DistToRegion; Heuristics* h; int NodeId; switch(Msg->kind()) { case BEACON: // printf(" BEACON %d", MyNodeId); if(Msg->isSelfMessage()) { reply=generateBeaconMessage(); } else { beaconPkt=(BeaconPkt *)Msg; Header* hdr=&beaconPkt->getHdr(); NodeId=hdr->srcNodeId; reply=generateReplyBeaconMessage(NodeId); //printf(" %f BEACON from %d to NodeId %d\n",simTime(),MyNodeId,NodeId); } sendDown(reply); delete Msg; break; case BEACON_REPLY: updateNeighborList((BeaconReplyPkt*)Msg); // printf("updateNeighborList %d", MyNodeId); delete Msg; break; case FLOOD: if(isInRegion(Msg)) { //printf("\n%f FLOOD %d \n",simTime(),MyNodeId); if(isAlreadyRecv((QueryPacket*)Msg)) { // printf("\n%f deleting FLOOD %d \n",simTime(),MyNodeId); delete Msg; } else { if(fileWrite) { FILE * abc; sprintf(fileName, "500/%s%d.txt",filea, ((QueryPacket*)Msg)->getIQuerySrc()); abc = fopen(fileName, "ab"); char a[100]; sprintf(a, "%.15f INSIDE FLOOD The Query Src = %d NextHopId = %d\n",simTime(),((QueryPacket*)Msg)->getIQuerySrc(), MyNodeId); fputs(a, abc); fclose(abc); } cPar param=Msg->par("VisitedNodeList"); cArray* VisitedNodes=(cArray*)param.objectValue(); IdWrapper* idw=new IdWrapper(MyNodeId); VisitedNodes->takeOwnership(false); VisitedNodes->add(idw); setSrcNode((QueryPacket*)Msg); //if (!((QueryPacket*)Msg)->getExp()) if(!sendingData) createDataMsg((QueryPacket*)Msg); Header *hdr=&((QueryPacket*)Msg)->getHdr(); hdr->srcNodeId=MyNodeId; floodRegionNeigh((QueryPacket*) Msg); } } else { // printf("DELETED FLOOD MEESAGE %d\n",MyNodeId); delete Msg; } break; case QUERY: { int SrcNode = hdr->srcNodeId; //printf(" %f %d I GOT QUERY FROM %d ORIGIN %d \n",simTime(), MyNodeId,SrcNode,((QueryPacket*)Msg)->getIQuerySrc()); x =((CoOrdinatorBase*)pCoOrdinator)->getX(); //printf("the x issssssss==%d\n", x); y =((CoOrdinatorBase*)pCoOrdinator)->getY(); //printf("the yyyyyyyyy is ===%d\n", y); int x1,x2,y1,y2; sprintf(s, "%s", "ATTRIBUTE"); typedef std::vector<Attribute> vecAttribute; vecAttribute newVecAttr; test* t; cPar* VecParam; try{ VecParam = &Msg->par(s); t=(test*)VecParam->objectValue(); } catch(cException* e) { printf("In exception\n"); } newVecAttr=t->t_vecAttr; Attribute atr; bool check = true; atr= Attribute::find_key_from(Attribute::LONGITUDE_L, &newVecAttr); check = atr.compareAttribute(x); x1=atr.getVal(); atr= Attribute::find_key_from(Attribute::LONGITUDE_R, &newVecAttr); if(check) check = atr.compareAttribute(x); x2=atr.getVal(); atr= Attribute::find_key_from(Attribute::LATITUDE_L, &newVecAttr); if(check) check = atr.compareAttribute(y); y1=atr.getVal(); atr= Attribute::find_key_from(Attribute::LATITUDE_R, &newVecAttr); if(check) check = atr.compareAttribute(y); y2=atr.getVal(); h = new Heuristics(); int radius=round((h->Distance(x1,y1,x2,y2))/2.0); delete h; RegionX=round((x1+x2)/2.0); RegionY=round((y1+y2)/2.0); ((QueryPacket*)Msg)->setRegionX(RegionX); ((QueryPacket*)Msg)->setRegionY(RegionY); ((QueryPacket*)Msg)->setRadius(radius); sprintf(fileName, "500/%s%d.txt", filea,((QueryPacket*)Msg)->getIQuerySrc()); if (check) { IdWrapper* idw=new IdWrapper(MyNodeId); sprintf(s,"%d",MyNodeId); if(gateWay->find(s)==-1) gateWay->set(idw); for(int i=0;i<gateWay->items();i++) { idw = (IdWrapper*)gateWay->get(i); //printf(" gateway NodeId =%d no: %d\n",idw->getNodeId(),gateWay->items()); } setSrcNode((QueryPacket*)Msg); //printf(" %f GOT query from QuerySrc %d \n",simTime(),((QueryPacket*)Msg)->getIQuerySrc()); if(((QueryPacket*)Msg)->getExp()) if(!sendingData) createDataMsg((QueryPacket*)Msg); Header *hdr=&((QueryPacket*)Msg)->getHdr(); hdr->srcNodeId=MyNodeId; cPar param=Msg->par("VisitedNodeList"); cArray* VisitedNodes=(cArray*)param.objectValue(); VisitedNodes->takeOwnership(false); VisitedNodes->add(idw); floodRegionNeigh((QueryPacket*)Msg); //isGateway = true; break; } //printf("AFTER CHECK\n"); if(Msg->isSelfMessage()) { if(neigh_list->items()==0) { if (NeighborUpdateCount <= 3) { scheduleAt(simTime()+WAIT_NEIGHBOR_UPDATE,Msg); NeighborUpdateCount+=1; } else { //printf("deleting message as no neighbor %d \n", MyNodeId); delete Msg; } } else { for(int i=0;i< neigh_list->items();i++) { NeighbhorInfo* neigh=(NeighbhorInfo*)neigh_list->get(i); // printf(" NEIGHINFO of %d is %d \n",MyNodeId,neigh->getNodeId()); } processQuery(RegionX,RegionY,radius,(QueryPacket*)Msg,false); } } else { //printf("IN ELSE\n"); if(MyNodeId==((QueryPacket*)Msg)->getIQuerySrc()&&(flag!=1)) { delete Msg; break; } else { if((neigh_list->items()==0)||((simTime()-NeighUpdatedTime)>threshold)) { cMessage *reply=generateBeaconMessage(); sendDown(reply); //printf(" WAIT_NEIGh for nodeid %d \n",MyNodeId); scheduleAt(simTime()+WAIT_NEIGHBOR_UPDATE,Msg); NeighborUpdateCount=1; break; } Header* hdr=&((QueryPacket*)Msg)->getHdr(); processQuery(RegionX,RegionY,radius,(QueryPacket*)Msg,false); } } break; } case QUERY_REPLY: getLearnedCost((QueryPacket*)Msg); break; case UPDATE_LEARNED_COST: sprintf(fileName, "500/%s%d.txt",filea, ((QueryPacket*)Msg)->getIQuerySrc()); UpdateLearnedCost(Msg); delete Msg; break; case DATA: // printf("\n Time %f is %d got data from %d ANKUR finalSrc is %d \n",simTime(),MyNodeId, hdr->srcNodeId,((DataMsg*)Msg)->getIFinalDest()); if((qFlag==true)&&(((DataMsg*)Msg)->getIFinalDest()!=MyNodeId)) { if(!((DataMsg*)Msg)->getExploratory()) { dataCount=dataCount+1; if(fileWrite) { char a[100]; if (Msg->hasPar("SeqNo")) seqNo=(int)Msg->par("SeqNo"); if(dataCount==1) { firstSeqNo=seqNo; //printf(" firstSeqNo=%d %d MyNodeId\n",firstSeqNo,MyNodeId); } sprintf(a, "%f seq %d GOT DATA %d dataCount %d \n",simTime(),seqNo,MyNodeId,dataCount); FILE * abc; abc = fopen("filea", "ab"); fputs(a, abc); fclose(abc); } } } if(((DataMsg*)Msg)->getIFinalDest()==MyNodeId) { //printf(" %f GOT DATA MyNodeId =%d \n",simTime(),MyNodeId); if(((DataMsg*)Msg)->getExploratory()) { sendUp((DataMsg*)Msg); } else { dataCount=dataCount+1; //printf("%f MSG RECEIVED FINAL DEST %d from NodeId=%d \n",simTime(),MyNodeId,hdr->srcNodeId); if(fileWrite) { char a[100]; if (Msg->hasPar("SeqNo")) seqNo=(int)Msg->par("SeqNo"); if(dataCount==1) { firstSeqNo=seqNo; //printf("firstSeqNo %d %d MyNodeId\n",firstSeqNo,MyNodeId); } sprintf(a, "%f seq %d GOT DATA %d dataCount %d \n",simTime(),seqNo,MyNodeId,dataCount); FILE * abc; abc = fopen("filea", "ab"); fputs(a, abc); fclose(abc); } /* if(MyNodeId==3) printf("3 GOT DATA DIWALI\n"); else if(MyNodeId==4) printf("4 GOT DATA CHOC\n"); */ //printf("MSG RECEIVED FINAL DEST from NodeId=%d \n",hdr->srcNodeId); sendData((DataMsg*)Msg); // delete Msg; } } else { //if(!isAlreadyRecv((QueryPacket*) Msg)) sendData((DataMsg*)Msg); } break; } } else { //printf("NOT FOR ME DELETED %d the msg kind is =%d\n", MyNodeId, Msg->kind()); delete Msg; }}void Gear::createDataMsg(QueryPacket* Msg){ sendingData = true; DataMsg* dMsg = new DataMsg("Data", DATA); Header *hdr=&Msg->getHdr(); unsigned int destNodeId=hdr->srcNodeId; Header *dataHdr = &dMsg->getHdr(); dataHdr->srcNodeId = MyNodeId; dataHdr->NextHopDestId=destNodeId; dataHdr->OriginSrc=hdr->OriginSrc; dMsg->setRegionX(Msg->getRegionX()); dMsg->setRegionY(Msg->getRegionY()); dMsg->setRadius(Msg->getRadius()); dMsg->setIFinalDest(Msg->getIQuerySrc()); cPar* cpar=&dMsg->addPar("ATTRIBUTE"); test* t; //printf("\n%f CREATEDATA for Source query %d from nodeid %d nexthop %d\n",simTime(),Msg->getIQuerySrc(),MyNodeId,destNodeId); cPar* VecParam; try { VecParam = &Msg->par("ATTRIBUTE"); t=(test*)VecParam->objectValue(); } catch(cException* e) { printf("In exception\n"); } cpar->setObjectValue(t); dataDuration = Msg->getIDuration(); dataRate = Msg->getIRate(); sprintf(s, "NID%dX%dY%dR%d", Msg->getIQuerySrc(), Msg->getRegionX(), Msg->getRegionY(), Msg->getRadius()); dMsg->addPar("QueryId"); dMsg->par("QueryId")=s; dMsg->setDuration(Msg->getIDuration()); dMsg->setRate(dataRate); //printf("The value of getExp ==%d\n", Msg->getExp()); if(!Msg->getExp()) { // dMsg->setTimeDataFirstSent(0.0); // dMsg->setLastEnforcedTime(0.0); } else { dMsg->setTimeDataFirstSent(simTime()); dMsg->setLastEnforcedTime(simTime()); } cPar *VisitedNodes=&dMsg->addPar("VisitedNodeList"); cArray *VisitedArrayList=new cArray("VisitedList",1,1); IdWrapper* idw=new IdWrapper(MyNodeId); VisitedArrayList->takeOwnership(false); VisitedArrayList->add(idw); VisitedNodes->setObjectValue(VisitedArrayList);/* if(simTime()<30) { int d=30; scheduleAt(d+.75, dMsg); } else */ scheduleAt(simTime()+.75,dMsg);}void Gear::sendData(DataMsg* dMsg){ Header *dataHdr = &dMsg->getHdr(); //if(isGateway) //remove this //char temp1[100]; //sprintf(temp1, "MyNodeId = %d the time at which dMsg->setLastEnforcedTime is %f \n", MyNodeId, dMsg->getLastEnforcedTime()); if(dMsg->isSelfMessage()) { simtime_t now = simTime(); bool isExp = false; //printf("\n MyNode %d Data for Query===%d now = %f TimeDataFirstSent====%f Duration====%f\n",MyNodeId, dMsg->getIFinalDest(), now, dMsg->getTimeDataFirstSent(), dMsg->getDuration()); if((now - dMsg->getTimeDataFirstSent())<dMsg->getDuration()) { //printf("\n MyNodeId==%d Time=%f I am sending data", MyNodeId, now); //printf("The timeDataFirstSent is==%f, lastEnforcedTime==%f", dMsg->getTimeDataFirstSent(), dMsg->getLastEnforcedTime()); if(((dMsg->getTimeDataFirstSent()==0.0)||(now - dMsg->getLastEnforcedTime())>EXPLORATORY_DATA_INTERVAL)) { //printf("I am sending Exp Data"); dMsg->setExploratory(true); isExp = true; dMsg->setLastEnforcedTime(now); if(dMsg->getTimeDataFirstSent()==0.0) dMsg->setTimeDataFirstSent(now); sendingData=false; //printf("The TimeDataFirstSent====%f", dMsg->getTimeDataFirstSent()); } else { dataGeneratedTotal=dataGeneratedTotal+1; sendingData=true; // printf(" %d sending non exploratory data %d \n",MyNodeId,dMsg->getIFinalDest()); dMsg->setExploratory(false); } dataRate=dMsg->getRate(); Header *dataHdr = &dMsg->getHdr(); if(fileWrite) { char a[100]; IdWrapper* idw =new IdWrapper(MyNodeId);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -