- Home Page of ProjectGuideline.com ›
- Forums ›
- Get Help to do your B.E., and M.E., M.Tech., & M.S., Project in Ns2 ›
- Asking about number of neighbors
Hi there,
I added this piece of code to the file god.cc in order to count the number
of neighbors each node in a wireless ad hoc network has. However, I can not figure out how to write the array which contains this information to a log file any time inside the simulation period. Could you please guide me how to do this? In this case, I need to write the array “neighbors” returned by function countNeighbors() to a file.
Thank you very much.
/////////////////////////////////////////////////
// Count the number of neighbors for all nodes //
/////////////////////////////////////////////////
int *God::countNeighbors()
{
int curNode, otherNode;
int *neighbors = (int *)calloc(num_nodes, sizeof(int)); //this array
contains the number of neighbors for each node
for (curNode = 0; curNode < num_nodes; curNode++)
{
if (mb_node[curNode] -> X() < 0 || mb_node[curNode] -> Y() < 0) //if
this node lies out of simulation range
{
*(neighbors + curNode) = 0;
continue; //proceed to the next node
}
for (otherNode = 0; otherNode < num_nodes; otherNode++)
{
if (otherNode == curNode)
continue;
if (mb_node[otherNode] -> X() < 0 || mb_node[otherNode] -> Y() <
0)
{
continue; //proceed to the next node
}
if (IsNeighbor(curNode, otherNode))
{
*(neighbors + curNode) += 1;
}
}
}
return neighbors;
}
You need not do everything in god.cc itself.
You may handle the values inside your new agent code (if any) or pass them to a tcl variable as follows. Here AddNeighborToList will add the neighbors to a tcl list. After that You can use that variable from the simulation script itself.
The c++ part
void MyAgent::FillNeighborList()
{
Tcl& tcl = Tcl::instance();
int TotalNodes=God::instance()->nodes();
for(int i=0;i
{
if(God::instance()->IsNeighbor(i , here_.addr_))
{
tcl.evalf("%s AddNeighborToList %d ",name(), i);
}
}
}
the tcl part
Agent/MyAgent instproc AddNeighborToList {nn} {
$self instvar NeighborList_
lappend NeighborList_ $nn
}
I just want to thank you as soon as possible!!!!! I will try your approach now.