⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 applayersimple.cc

📁 基于Oment++的无线传感器网络仿真
💻 CC
字号:
#include <stdlib.h>#include "CoOrdinatorBase.h"#include "Gear_packet_m.h"#include "AppLayerSimple.h"#include "Calculations.h"#define MAX_HOP 255Define_Module_Like ( AppLayerSimple , AppLayerModule );AppLayerSimple :: AppLayerSimple(const char *name, cModule *parentModule, unsigned stacksize) :	AppLayerBase(name, parentModule, stacksize){//printf("\n\t In constructor for AppLayerSimple");}void AppLayerSimple::initialize(){	//ev << "\n\t In AppLayerSimple::initialize()";		//printf("\n\t AppLayerModule: Name of current module: %s", fullName());	cModule *pNode = parentModule();	pCoOrdinator = pNode->submodule("CoOrdinator");	int nodeId=	((CoOrdinatorBase*)pCoOrdinator)->getNodeId();			cModule *pNetwork=pNode->parentModule();	int noOfQuery=pNetwork->par("NoOfQuery");	int Region=pNetwork->par("RegionNo");	//if (nodeId<noOfQuery)//||nodeId==8)//Changes for the sample demo	/*if(nodeId==394)	{			cMessage* msg = new cMessage();			msg->setKind(QUERY);		scheduleAt(simTime()+5,msg);	}*/	if ((nodeId>Region)&& (nodeId<noOfQuery+Region+1 )) //nodeId ==  3 || nodeId == 4)//||nodeId==8)//	if ((nodeId ==  3 )) //|| (nodeId==5))	{				cMessage* msg = new cMessage();			msg->setKind(QUERY);		//	QueryPacket* msg=generateQuery();	//	srand(0);		int j=1+(int) (100.0*rand()/(RAND_MAX+1.0));		//recordScalar("Simulation Time   .............. \n\n\n" ,simTime());//		recordScalar("Random number generated is %d %d....................................... \n\n\n",j);     		//printf("NodeId==%d is scheduled at t%f\n", nodeId, simTime()+nodeId+j);		FILE * abc;		abc = fopen("qtime20.txt", "at");		char a[100];		sprintf(a, "nodeId %d  t%f\n",nodeId, simTime()+j);//		recordScalar("", a);		fputs(a, abc);		fclose(abc);				//scheduleAt(20.0,msg);		scheduleAt(simTime()+j,msg);		//scheduleAt(simTime()+nodeId+j,msg);		//checkIfSchedule=simTime()+nodeId+j;/*		if(nodeId==3)		{		scheduleAt(85.0,msg);		checkIfSchedule=simTime()+85.0;		}*//*		if(nodeId==4)		{		scheduleAt(41.0,msg);		checkIfSchedule=simTime()+41.0;		}		if(nodeId==5)		{		scheduleAt(21.0,msg);		checkIfSchedule=simTime()+21.0;		}*/	}	aryQueryEnforceTime = new cArray();	return;}void AppLayerSimple::handleMessage(cMessage *msg){//	printf("\n\t In AppLayerSimple::handleMessage");//	ev <<  "\n\t In AppLayerSimple::handleMessage";		cModule *pNode = parentModule();	pCoOrdinator = pNode->submodule("CoOrdinator");	int nodeId=	((CoOrdinatorBase*)pCoOrdinator)->getNodeId();				if (TARGET == msg->kind())	{		cModule *pNode = parentModule();		int iNodeId = pNode->par("NodeId");		//printf("\n\t AppLayerSimple::handleMessage for Node: %d, Message Received from TopAppLayer", iNodeId);		msg->par("xPos") = pNode->par("xPos");		msg->par("yPos") = pNode->par("yPos");		if (-1 == msg->findPar("RadioPower"))			msg->addPar("RadioPower") = 5;		send(msg, "toBottomLayer");	}	else if(DATA == msg->kind())	{		cModule *pNode = parentModule();		int iNodeId = pNode->par("NodeId");		//printf("\n\t AppLayerSimple::handleMessage for Node: %d, Message Received of type DATA from BottomAppLayer", iNodeId);	    //cGate *pTopGate = gate("toTopLayer");	    //if (true == pTopGate->isConnected())	    //send(msg, "toTopLayer");		simtime_t now=simTime();		//printf(" The Current Time is %f TimeDataFirstSent= %f Duration is %f \n",now,((DataMsg*) msg)->getTimeDataFirstSent(),((DataMsg*)msg)->getDuration());		if((now -((DataMsg*) msg)->getTimeDataFirstSent())<((DataMsg*)msg)->getDuration())		{			cPar* qIdPar = &msg->par("QueryId");			queryEnforceTime* oQET = (queryEnforceTime*)(aryQueryEnforceTime->get(qIdPar->stringValue()));		//	if((simTime()-oQET->getQueryEnforceTime())>60)		//	{				QueryPacket* qry=generateQuery(true, msg); 				//printf("\n %f Sending the exploratory query APP  by  nodeId %d \n",simTime(), nodeId);				send(qry,"toBottomLayer");		//	}		}	}	else if(QUERY == msg->kind())	{		//printf("Inside AppLayer handleMessage");		int j=1+(int) (10.0*rand()/(RAND_MAX+1.0));		QueryPacket* msg1=generateQuery(false, msg);		cPar *VisitedNodes=&(msg1->par("VisitedNodeList"));		cArray *VisitedArrayList=new cArray("VisitedList");		VisitedNodes->setObjectValue(VisitedArrayList);		//printf("APPLAYERSIMPLE THe Query from NodeId %d to Region X=%d Region Y=%d  simtime==%f ",((CoOrdinatorBase*)pCoOrdinator)->getNodeId(),msg1->getRegionX(),msg1->getRegionY(), simTime());	//	printf("Sent at %f \n\n", simTime()); 		send(msg1,"toBottomLayer");	}}QueryPacket* AppLayerSimple::generateQuery(bool flag, cMessage* Msg){		QueryPacket* qry=new QueryPacket("query",QUERY);	int MyX=((CoOrdinatorBase*)pCoOrdinator)->getX();	int MyY=((CoOrdinatorBase*)pCoOrdinator)->getY();	Header* hdr=&qry->getHdr();	hdr->srcNodeId=((CoOrdinatorBase*)pCoOrdinator)->getNodeId();	hdr->OriginSrc=hdr->srcNodeId;			hdr->ttl=MAX_HOP;	//hdr->destLoc=NULL;	int qId = qid.getNextQueryId();	//printf("QueryId= %d \n",qId);	cModule *pNode = parentModule();	cModule *pNetwork=pNode->parentModule();	int gridSizeX=pNetwork->par("gridSizeX");	int gridSizeY=pNetwork->par("gridSizeY");	int x1 = pNetwork->par("RegionX1");	int y1 = pNetwork->par("RegionY1");	int x2 = pNetwork->par("RegionX2");	int y2 = pNetwork->par("RegionY2");	Heuristics* h = new Heuristics();        int radius=round((h->Distance(x1,y1,x2,y2))/2.0);        delete h;	//int j=1+(int) (10.0*rand()/(RAND_MAX+1.0));	qry->setRegionX(round((x1+x2)/2.0));     //(gridSizeX-MyX)/j);	qry->setRegionY(round((y1+y2)/2.0));      //(gridSizeY-MyY)/j);	qry->setRadius(radius);	char s[50];	sprintf(s, "NID%dX%dY%dR%d", hdr->srcNodeId, qry->getRegionX(), qry->getRegionY(), qry->getRadius());	aryQueryEnforceTime->set(new queryEnforceTime(s, simTime()));	//qry->setQryId(qId);	cPar *QueryPar = &qry->addPar("QueryId");	QueryPar->setStringValue(s);	//adding the Attribute for query//	printf("-------------whatever---%d", Attribute::GE);	//Attribute a;	Attribute queryLogitude_L (Attribute::LONGITUDE_L, Attribute::GE, x1);	Attribute queryLogitude_R (Attribute::LONGITUDE_R, Attribute::LE, x2);	Attribute queryLattitude_L (Attribute::LATITUDE_L, Attribute::GE, y1);	Attribute queryLattitude_R (Attribute::LATITUDE_R, Attribute::LE, y2);    typedef std::vector<Attribute> vecAttri;	vecAttri vecAttr;	vecAttr.push_back(queryLogitude_L);	vecAttr.push_back(queryLogitude_R);	vecAttr.push_back(queryLattitude_L);	vecAttr.push_back(queryLattitude_R);	test *t = new test();	t->t_vecAttr = vecAttr;    cPar *AttrObj = &qry->addPar("ATTRIBUTE");		//AttrObj->setPointerValue(&vecAttr);		AttrObj->setObjectValue(t);	//char s[50];	sprintf(s, "%s", "ATTRIBUTE");				  	cPar VecParam = qry->par(s);//	printf(" The QUERY Pointer value is in applayer %p \n",qry);		/*vecAttri* newVecAttr;     newVecAttr=(vecAttri*)VecParam.pointerValue();	 //printf(" The Pointer value is in applayer %p \n",VecParam.pointerValue());	 Attribute* attr=Attribute::find_key_from(Attribute::LONGITUDE_L,newVecAttr);	 //printf(" the key is in attribute %d\n",attr->getKey());	  vecAttri::iterator i;	 for (i = newVecAttr->begin(); i != newVecAttr->end(); ++i)	 //printf(" key is in for %d\n",(*i)->getKey());	 */	if(flag)	{		DataMsg* dMsg = (DataMsg*)Msg;		qry->setIDuration(dMsg->getDuration()-(simTime()-dMsg->getTimeDataFirstSent()));		qry->setIRate(dMsg->getRate());		qry->setExp(true);			//	printf(" The new time duration  %f %d MyNodeId dMsg->getDuration() %f simTime() %f dMsg->getTimeDataFirstSent() %f  \n",qry->getIDuration(),((CoOrdinatorBase*)pCoOrdinator)->getNodeId(),dMsg->getDuration(),simTime(),dMsg->getTimeDataFirstSent());	}	else	{		qry->setIDuration(500);		qry->setIRate(5);		qry->setExp(false);	}		qry->setIQuerySrc(hdr->srcNodeId);					cPar *VisitedNodes=&qry->addPar("VisitedNodeList");	cArray *VisitedArrayList=new cArray("VisitedList");	VisitedNodes->setObjectValue(VisitedArrayList);	//printf("THe Query from NodeId %d to Region X=%d Region Y=%d   ",((CoOrdinatorBase*)pCoOrdinator)->getNodeId(),qry->getRegionX(),qry->getRegionY());	return qry;}AppLayerSimple::~AppLayerSimple(){//	printf("Cariappa Is our PM");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -