www.pudn.com > 3.rar > manager.cc, change:2005-04-22,size:3914b


//-------------------------------------------------------------------
//  file name: manager.cpp
// 
//    - contains the implementation of manager class
//
//-------------------------------------------------------------------


#include "manager.h"
#include <string>

#include <fstream>


void manager::initialize()
{
	int i,j;

	// check out the parameters in the .ini file and in the .h file
	if ((int)this->parentModule()->par("NNODES")!=NNODES)
	{
		ev << "manager::initialize() error: parameter NNODES is different in .ini and in .h files\n";
		endSimulation();
	}

	if ((int)this->parentModule()->par("MAXCONN")!=MAXCONN)
	{
		ev << "manager::initialize() error: parameter MAXCONN is different in .ini and in .h files\n";
		endSimulation();
	}

	// just initialization code...
	for (i=0;i<NNODES;i++)
	{
		for (j=0;j<NNODES;j++)
		{
			cm[i][j] = false;
		}
	}

	// hide the manager from the display
	setDisplayString("p=-100,-100,exact");

	int dist = distance((int)this->parentModule()->par("SSTRENGTH"));

	// generate randomly the initial positions of the nodes
	nodepos_t pos[NNODES];
	for (i=0;i<NNODES;i++)
	{
		pos[i].x = intrand(SPACEX);
		pos[i].y = intrand(SPACEY);

		// display the node on the screen
		char tempstring[30];
		sprintf(tempstring,"p=%d,%d,exact;i=snode_%d_%d",pos[i].x+MX,pos[i].y+MY,1,2);
		this->parentModule()->submodule("snode",i)->setDisplayString(tempstring);

		// connect this module to whomever is in transmission range
		for (j=0;j<i;j++)
		{
			// if the two nodes can hear each other then connect them
			if (dist*dist>=((pos[i].x-pos[j].x)*(pos[i].x-pos[j].x) + (pos[i].y-pos[j].y)*(pos[i].y-pos[j].y)))
			{
				cModule *mod1,*mod2;
				mod1 = this->parentModule()->submodule("snode",i);
				mod2 = this->parentModule()->submodule("snode",j);

				cGate *gate1 = NULL;
				cGate *gate2 = NULL;

				int cnt1,cnt2;

				// find the first empty gate on source
				for (cnt1=0;cnt1<MAXCONN;cnt1++)
				{
					gate1 = mod1->gate("in",cnt1);
					if (!gate1->isConnected())
						break;
				}

				if (cnt1==MAXCONN)
				{
					ev << "error: maximum connectivity reached (1).\n";
					endSimulation();
				}

				// find the first empty gate on destination
				for (cnt2=0;cnt2<MAXCONN;cnt2++)
				{
					gate2 = mod2->gate("out",cnt2);
					if (!gate2->isConnected())
						break;
				}

				if (cnt2==MAXCONN)
				{
					ev << "error: maximum connectivity reached (2).\n";
					endSimulation();
				}


				// connect the gates
				gate1->setFrom(gate2);
				gate2->setTo(gate1);
				gate1->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
				gate2->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);

				// connect the reverse link
				gate1 = mod1->gate("out",cnt1);
				gate2 = mod2->gate("in",cnt2);
				gate1->setTo(gate2);
				gate2->setFrom(gate1);

				gate1->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);
				gate2->setDisplayString("m=m,50,50,50,50;o=#AAA,1;",false);

				// update the data structure
				cm[i][j] = true;
				cm[j][i] = true;
			}
		}
	}

	// update each node data structure
	for (i=0;i<NNODES;i++)
	{
		cModule *mod = this->parentModule()->submodule("snode",i);
		mod->par("PX") = pos[i].x;
		mod->par("PY") = pos[i].y;
	}
}


void manager::activity()
{

	// main loop
	while (1)
	{
		cMessage *msg;
		msg = receive();

		switch (msg->kind())
		{

		case M_UPDSSTRENGTH:						// signal strength update by a node
			updateSignalStrength(msg);
			delete msg;
			break;
		
		default:
			ev << "manager::handleMessage() error: unknown message received by manager\n";
			endSimulation();
		}
	}
}


void manager::updateSignalStrength(cMessage *msg)
{
}


int manager::distance(int sstrength)
{
	int res;

		// right now by signal strength i understand the range a node can communicate
		// any other interpretations of the signal strength and conversions to distance
		// can be performed here
		res = sstrength;

	return res;
}