www.pudn.com > adHocSimtest.rar > inspector.cc, change:2003-03-07,size:1994b


#include "h/inspector.h"

#include "h/physic.h"

#include <stdio.h>

#define d(x) ev<<((parentModule()->id()-2)/5)<<","<<name()<<":"<<x<<"\n"


Define_Module(Inspector);

void Inspector::initialize()
{
	//store parameters for a faster access
	hostNum = (int)parentModule()-> par("dim");
	
	active = par("active").boolValue();
	
	snapshotInterval = (simtime_t) par("snapshotInterval");

	fileName = &par("fileName");
	
	physic = (Physic*)parentModule()->submodule("physic");
	
	//fulfill the node arrays
	collectNodes();

	snaps = 0;
	memset(matrix,0,sizeof(int)*CELLS * CELLS);

	cellW =(int) ( (int) parentModule()->par("width") / CELLS);
	cellH =(int) ( (int) parentModule()->par("height") / CELLS);

	//let's go
	if (active)
 	  scheduleAt(simTime() + snapshotInterval, new cMessage("nodes snapshot"));
}

void Inspector::collectNodes()
{

 	cModule* mod;
	
        d("detectNeigh");
	int j = 0;

        for(int i=1; i<= simulation.lastModuleIndex(); i++)
        {
                mod = (cModule*)simulation.module(i);
                
		//store all the physi layer pointers
		if( strcmp(mod->name(), "physic")  == 0)
		{
                   hosts[j] = i;// ((Physic*)mod);
		   d(mod->name()<<" "<<mod->id());
		   j++;
		}
        }
	d("---");
}


void Inspector::handleMessage(cMessage* msg)
{
	d("Snapshot");	
	Physic* node = NULL;
	int i;
	int x, y;

	for( i = 0; i<hostNum ; i++)
	{
		node = (Physic*) simulation.module(hosts[i]);

		node->getPos(x,y);
		d(node->name()<<" "<<node->id()<<" "<<hostNum);

		x = (int) (x / cellW);
		y = (int) (y / cellH);
		d(x<<" ------ "<<y);
		matrix[x][y]++;

	}
	node = NULL;
	snaps++;

	if (active)
		scheduleAt(simTime() + snapshotInterval, new cMessage("node snapshot"));

}


void Inspector::finish()
{
	FILE* outFile = fopen("matrix.dat","w");

	for(int i = 0; i < CELLS; i++)
	{
		for(int j = 0; j < CELLS; j++)
		{
			fprintf(outFile,"%d \t %d \t %4.3f\n",(int)(i*cellW/2), (int)(j*cellH/2), matrix[i][j]);
		}
	}
	d("Ins says bye");
}