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

📄 physic.cpp

📁 OMNET++仿真三色算法的源码,三色算法是无线传感器中一个典型的分簇算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	if( (a == -1) || (b == -1) || (c == -1) || (d == -1) )	{		ev << "Error in the connectModules function \n";		wait(2);	}	d(a<<"->"<<b<<"-->"<<c<<"->"<<d<<" OK!");	cBasicChannel *channel = new cSimpleChannel("channel");	channel->setDelay(delay->doubleValue());	channel->setDatarate(dataRate->doubleValue());	channel->setError(error->doubleValue());	parentModule()->gate(b)->connectTo(mod->gate(c), channel);	//draw the link
	//if(!blDraw)
	if(!blDraw)
	{		cGate *g = parentModule()->gate(b);
		cDisplayString *ds = new cDisplayString("m=a;o=gray,0");

		sprintf(tmpstr,"%s",g->displayString().getString());

		//g->dispstr = ds;

		g->setDisplayString(ds->getString(), true);

		//sprintf(tmpstr, "画虚线线:n  displaystring = [%s] |\n", g->displayString());
		//Log(tmpstr);
		d("b-->c connected --> end actual connect");
	}
	else
	{
		cGate *g = parentModule()->gate(b);
		cDisplayString *ds = new cDisplayString(NULL);

		//sprintf(tmpstr,"默认 画线:gate displaystring is NULL : [%s]\n",g->displayString().getString());
		//Log(tmpstr);

		g->setDisplayString(ds->getString(), true);

	}

	return a;}//kind = 'O' || 'I'void Physic::setUpConn(char kind,int& a,int& b){	char s[20],t[20];	cModule* parent = parentModule();	//a is the gate on the physic module,	//b is on the compound module	d("setUpConn");	//define the gate's name	if(kind == 'I')		strcpy(t,"Rx%d\0");	else		strcpy(t,"Tx%d\0");	//create the gates to this module and its parent	gatesNum++;	sprintf(s,t,gatesNum);	a = addNewGate(this,s,kind);	b = addNewGate(parent,s,kind);	d("added gate 'b' :"<<s);	if( (a == -1)||(b == -1))	{		d("Error in the addGateVector function ");		wait(2);	}	//connect the gates	if(kind == 'O')	{	    this->gate(a)->connectTo(parent->gate(b));	    //connect((cModule*)this,a,(cLinkType *)NULL,(cModule*)parent,b);	    d("ph->comp conneected!");	}	else	{	   //connect((cModule*)parent,b,(cLinkType *)NULL,(cModule*)this,a);	   parent->gate(b)->connectTo(this->gate(a));	   d("comp->ph connected!");	}}int Physic::addNewGate(cModule *mod, char* gname, char type){	cGate* g;	int index = 0,	    gateNum = 0;	bool found = false;	int i = 0;	d("gates number before connection:"<<mod->gates());	//look for a free entry	while((i< mod->gatev.items()) && (!found))	{		g = (cGate*) mod->gate(i);		d("gate: "<<i);		if(g == NULL)		{			d("found a null gate vector place. Use it !");			g = new cGate(gname,type);			mod->gatev.addAt(i,g);			g->setOwnerModule( mod, i);			index = i;			found = true;		}		else		if(!g->isConnected())		{			d("found a not connected gate. recycling :"<<g->name());			delete g;			g = new cGate(gname,type);			mod->gatev.addAt(i,g);			g->setOwnerModule( mod, i);			found = true;			index = i;		}		else			i++;	}	//all the entries are used, add a new gate	if(!found)	{		d("free place not found, adding a new one ");		mod->addGate(gname,type);		gateNum = mod->gates();		if(mod->hasGate(gname))		{			d("has gate ok !");			index = mod->findGate(gname);		}		else		{			d("--- ERROR new gate not found!");		}	}	//mod->setGateSize(gname,mod->gates());	g = (cGate*) mod->gate(index);	//g->setIndex(index,gateNum);	//g->setIndex(index,mod->gates());	g->setOwnerModule(mod,index);	//cGate* newGate = new cGate(gname, type);	return index;}bool Physic::disconnectFrom(int rPhysic, int rMob){	int parent =(int) parentModule()->id();	int me = id();	d("-----diconnect------");	//disconect the link between the two nodes	putDownConn(me,parent,rMob,rPhysic);	d("Diconnection successfull!");	return true;}//           Left -------> arrow -------> Rightbool Physic::putDownConn(int lph,int leftId, int rightId,int rph)
{
	cModule *left = (cModule*)simulation.module(leftId);
	cModule *right = (cModule*)simulation.module(rightId);

	cModule *lPhysic = (cModule*)simulation.module(lph);
	cModule *rPhysic = (cModule*)simulation.module(rph);

	//a->b-->c->d
	int a =-1,
	    b =-1,
	    c =-1,
	    d =-1;	cGate *g;	d("put down Connection between:"<<left->name()<<","<<left->id()<<" - "<<right->name()<<","<<right->id());	//look for the connection gate to delete	if(!getGateIndex(left,right,a,b,c,d)  )	{		d("PutDown: gateindex retured false! ******************************");		return false;	}	d("deleting  gates a:"<<a<<" b:"<<b<<" c"<<c<<" d"<<d);	//tryal....//	left->setDisplayString(left->displayString(),true);//	right->setDisplayString(right->displayString(),true);//	lPhysic->setDisplayString(lPhysic->displayString(),true);//	rPhysic->setDisplayString(rPhysic->displayString(),true);	g = (cGate*)lPhysic->gate(a);	g->setTo(NULL);	lPhysic->gatev.remove(a);	delete g;	//get rid of the old gate	g = (cGate*)left->gatev.get(b);	g->setTo(NULL);	g->setFrom(NULL);	left->gatev.remove(b);	d(g->name());	delete g;	g = (cGate*)right->gatev.get(c);	right->gatev.remove(c);	g->setTo(NULL);	g->setFrom(NULL);	d(g->name());	delete g;	g = (cGate*)rPhysic->gatev.get(d);	rPhysic->gatev.remove(d);	g->setFrom(NULL);	d(g->name());	delete g;	// this was suggested by Varga to avoid the errors that come out using TkEnv	// but it doesn't seem to work...	//left->setDisplayString(left->displayString());	return true;}bool Physic::getGateIndex(cModule* left,cModule *right,int& a,int&b, int& c, int& d){	int gateNum = left->gates();	cGate *g,*farGate;	bool found = false;	int i = 0;	d("In GetGateIndex from "<<left->name()<<" to "<<right->name());	while( (i< left->gatev.items()) &&( !found ))	{		d(i);		g = (cGate *)left->gatev.get(i);		if ((g != NULL) &&(g->type()== 'O') && (g->isConnected()))		{			d("maybe.... "<<g->name());			if(g->toGate()->ownerModule()->id() == right->id())			{				//it's the right gate				farGate = (cGate*) g->toGate();				d("gates to disconnect found");				a = g->fromGate()->id();				b = i;				c = g->toGate()->id();				d = farGate->toGate()->id();				found = true;			}		}		i++;	}	return found;}void Physic::finish(){	d("Physic  says bye");	 FILE* fout = fopen("collectedData.dat","a");	 d("Messages with errors..."<<msgWithErr);	 fprintf(fout,"Messages with errors........ %d\n",msgWithErr);	 fclose(fout);}/* modify the function to check if reachable , in which band 
   i_band 0..0.5 Range  or o_band 0.5--1
 */bool Physic::isReachable(int from, int to, int& band){	Physic *s = (Physic*) simulation.module(from);	Physic *d = (Physic*) simulation.module(to);	int x,y;	double dist,a;	bool r;

	char tmpstr[128];	d->getPos(x,y);	//Pitagora @ work...
		dist = sqrt( (double) ((x - s->posX ) * (x - s->posX )) +		    ((y - s->posY ) * (y - s->posY )) );	a = sqrt( (double) ((d->posX - s->posX ) * (d->posX - s->posX )) +		   ((d->posY - s->posY ) * (d->posY - s->posY )) );
	
	r = ( dist <= range)||( a <= range);

	//sprintf(tmpstr,"from[%d] to [%d], dist [%f],range [%f]", from, to, dist, range);
	//Log(tmpstr);

	if(r) 
	{
		if (dist <= range/2)  band=I_BAND;
		else band=O_BAND;
	}	return r;}void Physic::getVect(int i, int& ph, int& mh){	cNeighbour * n = NULL;	n = (cNeighbour*)hosts[i];	ph = n->ph;	mh = n->mobHost;}void Physic::setPos(int x, int y){	posX = x;	posY = y;}void Physic::getPos(int &x, int &y){	x = posX;	y = posY;}

⌨️ 快捷键说明

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