Hi All, Our website moved to a new URL. Please bookmark the new link: https://ns2bloggers.blogspot.com/

Sunday 17 July 2016

SETTING A NODE TO SLEEP IN NS2

Hi,
I just saw a lot of mails asking about "how to set a node as sleeping in ns2?" Now I am going to explain more about this in this post. Normally when using certain functions like Sleep"() Function from SMAC,we get the Segmentation fault or Floating point as result and it was hard to solve that. Normally, a node has four state: on,off,idle and sleep. We can consider sleep and idle as same state. In off condition, node becomes off but it still broadcasts based on protocol used. As from the tcl script we can simply on/off the nodes. But to make a node sleep/idle, we have to make changes in its back end files(.cc files). Also we can do the same by using the command "set idle_time_".

For Idle Condition

$ cd ns-2.3x/tcl/ex/
$ grep -Rin idle_time *
The simulation examples are ( grep will also list a lot of config files.tcl ) :
realaudio/ramodel.tcl
sat-aloha.tcl
srm.tcl
srm-session.tcl
test-rcvr.tcl
( Copy namfilter.tcl to ex/ from nam-1.1x/bin/.)
tcpsrm.tcl ( Edit line 130 to e.g.: exec tclsh8.6 ./namfilter.tcl out.nam )
test-tbf.tcl
tg.tcl
xcp/2statemarkovB.tcl 
(xcp-tcp.tcl must be run beforehand to create the required xcp0.tr) 
wpan/{ wpan_demo1.tcl, wpan_demo2.tcl ....... wpan_demo5.tcl }

By running this files, we can see the visualization.

For Sleep Condition

Sleep Condition in Energy Models
To achieve sleep condition in energy models: energymodel.cc, modify mac-802_11.cc in ns-2.35/mac/
Please use the code void Mac802_11::recv function 

if(index_ == nodeno){
EnergyModel *em = netif_->node()->energy_model();
if (em && em->sleep()) {
//em->set_node_state(EnergyModel::INROUTE);
em->set_node_sleep(1);
}
}

Replace "nodeno" with the number of the nodes you want to set to sleep.

Perform the following commands in order.
./configure
make clean
make depend // Optional
make
sudo make install
U shall get no errors. If you are getting any errors ask in the comments section.
After that run the tcl file to test.

Result: When the nodes get the first packet, the shape of them must change from black circle to black circle blue hexagon.From then on the nodes must not send or receive any data packet.

Sleep Condition in Wireless Models
To achieve sleep condition in energy models: select wireless-phy.cc in ns-2.35/mac/
We can simply use function at the end of file on the wireless-phy.cc to set a node to sleep or off by just calling functions.But we may need to use them in another layer like in mac.To use those functions in mac-802_11.cc simply use the below code anywhere you want in mac-802_11.cc and add wireless-phy.cc to your mac headers:

#include "wireless-phy.h" // at the header of mac-802_11.cc
/*
* Use the below code in any function you want in mac
*/
Phy *p;
p=netif_;
(
(WirelessPhy *)p)->node_sleep();
.

And to use another one of those 4 function just change the function's name like ->node_sleep(); to ->node_wakeup();.

No comments :

Post a Comment