Adding Metric Variables in NetSim

There are two ways for you to get new Metrics in NetSim.

Option 1 (Easy): To get a “derived metric” from the existing NetSim output variables, then enable the packet trace & event trace and save them as XL files. Then use the XL UI or write VB scripts to get your derived metrics.

Option 2 (Hard): To get a completely new metric code will need to added in NetSim. An example is given below where new metrics have been added for LEACH (a new modification on the original DSR and code is available in a earlier blog entry) code,

Data_Packet_Received_PL is a new variable that has been defined in 802.15.4.h

#else

_declspec(dllimport) POWER** pstruDevicePower;

//For LEACH/Data Metrics
// Array size is 101 since maximum number of sensors is 100 and 1 for sink

_declspec(dllimport) int Data_Packet_Sent_NWL[101] ;
_declspec(dllimport) int Data_Packet_Received_NWL[10] ;
_declspec(dllimport) int Data_Packet_Sent_PL[101] ;
_declspec(dllimport) int Data_Packet_Received_PL[101] ;
_declspec(dllimport) double Lifetime[101];
#endif

This is used in packetprocessing.c in the section

#ifdef _LEACH
fn_NetSim_LEACH_GetNextHop(pstruEventDetails);
if(pstruEventDetails->nDeviceId == pstruEventDetails->pPacket->nSourceId)
Data_Packet_Sent_NWL[pstruEventDetails->nDeviceId-1] += 1;
return 1;
#endif
#ifdef _LEACH

This variable after having been updated during the simulation in the section above, is finally printed in DSR.c as shown below

_declspec(dllexport) int fn_NetSim_DSR_Metrics(char* filename)
{
//This #ifdef…..#endif block defines the LEACH Metrics, which shows the packets
//sent and received by the Network Layer and the Physicaal Layer
//To enable LEACH, uncomment the line “#define _LEACH” in DSR.h
#ifdef _LEACH
FILE* fp;
int i;
fp=fopen(filename,”a+”);
fprintf(fp,”#LEACH Metrics\n”);
fprintf(fp,”DeviceID\tData Sent(Network Layer)\tData Sent(Physical Layer)\tData Received(Network Layer)\tData Received(Physical Layer)\tLifetime(sec)\n”);
for(i=0; i<NETWORK->nDeviceCount; i++)
{
fprintf(fp,”%d\t%d\t%d\t%d\t%d\t%f\n”,NETWORK->ppstruDeviceList[i]->nDeviceId,Data_Packet_Sent_NWL[NETWORK->ppstruDeviceList[i]->nDeviceId-1],Data_Packet_Sent_PL[NETWORK->ppstruDeviceList[i]->nDeviceId-1],Data_Packet_Received_NWL[NETWORK->ppstruDeviceList[i]->nDeviceId-1],Data_Packet_Received_PL[NETWORK->ppstruDeviceList[i]->nDeviceId-1],Lifetime[NETWORK->ppstruDeviceList[i]->nDeviceId-1]/1000000);
}
fclose(fp);
#endif
return fn_NetSim_DSR_Metrics_F(filename);
}

If you face difficulty debugging code, please read the section in NetSim Help “Debugging when running NetSim via CLI (Only for Internetworks, Cognitive Radio
networks, MANET, Wireless Sensor Networks and Personal Area Network)”. Also ensure you are running as Visual studio in the administrator mode

Comments are closed.