LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 12-23-2017, 03:27 AM   #1
moondead
LQ Newbie
 
Registered: Dec 2017
Posts: 3

Rep: Reputation: Disabled
network simulator (NS2) programming


i am working on multi sink scenario in WSN. I want to implement the scenario using NS2.35.
in my scenario,
1.initially sink are selected randomly then their role is exchange to other nodes based on their remaining energy.
I made some change in aodv.cc file to get the energy of nodes at any time. i used static variable to store the remaining energies of nodes in aodv.cc.
2.Now i want to pass these variables in tcl file. i use binding method but in tcl file i am getting initial energy value instead of updated values.
so how to pass updated values in tcl file ?

#####changes in aodv.cc file is here ....###
static float time =0.0;
double AODV::a = 50.0;
double AODV::b = 50.0;
double AODV::c=50.0;
double AODV::d=50.0;

variables to store initial energy of nodes (initially 4 nodes )

function to get updated energy of nodes :
void AODV::info_print(nsaddr_t index) {
FILE *fp1,*fp2; //*fp3
static float time =0.0;

fp1 = fopen("node_information1.txt","a");
fp2 = fopen("node_information2.txt","a");
//fp3 = fopen("node_information2.txt","w+");
iNode=(MobileNode *)(Node::get_node_by_address(index));
xpos=iNode->X();
ypos=iNode->Y();
iEnergy=iNode->energy_model()->energy();

time = CURRENT_TIME;
if(index==0)
AODV::a=iNode->energy_model()->energy();
if(index==1)
b=iNode->energy_model()->energy();
//printf("%.4f \n", b);
if(index==2)
c=iNode->energy_model()->energy();
if(index==3)
d=iNode->energy_model()->energy();
//

if(time >= 2.00 && time <= 2.01)
getenergy(a,b,c,d);

if(time >= 3.00 && time <= 3.01)
getenergy(a,b,c,d);
if(time >= 4.00 && time <= 4.01)
getenergy(a,b,c,d);
//fprintf(fp1,"at Time (%.6f), Position of %d is X:%.4f and Y:%.4f \n",CURRENT_TIME,index,xpos,ypos);
//fprintf(fp2,"at Time(%.6f), Updated Energy for Node %d is Energy %.4f \n",CURRENT_TIME,index,iEnergy);

fclose(fp1);
fclose(fp2);

}
void AODV::getenergy(double a ,double b,double c,double d) {
printf("time is : %.2f \ta=%.4f \t b= %.4f \t c= %.4f \t d=%.4f \n ", CURRENT_TIME,a,b,c,d);
}

### code to pass value in tcl file is (in command method in aodv.cc) :
if (strcmp(argv[1], "returnval1") == 0) {
tcl.resultf("%1.1f ",AODV::a);
Tcl& tcl=Tcl::instance();
printf("value of a is %lf :",a);
return TCL_OK;
}



########## Tcl file is ##################333
# start simulation


# event scheduler
set ns [new Simulator]


# trace file
$ns use-newtrace
set tracefd [open en.tr w]
$ns trace-all $tracefd
set namtrace [open en.nam w]
$ns namtrace-all-wireless $namtrace 500 500


# set topology
set topo [new Topography]
$topo load_flatgrid 500 500


# create god
create-god 5


# create channel
set wchan [new Channel/WirelessChannel]


# node configuration
$ns node-config -adhocRouting AODV \
-llType LL \
-macType Mac/802_11 \
-ifqType CMUPriQueue \
-ifqLen 50 \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-phyType Phy/WirelessPhy \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON \
-channel $wchan \
-energyModel "EnergyModel" \
-txPower 5.0 \
-rxPower 5.0 \
-initialEnergy 50 \
#-sleepPower 5 \
#-transitionPower 2 \
#-transitionTime .01 \
#-idlePower 1
#$ns set WirelessNewTrace_ ON


# create node
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]


# node position
$n0 set X_ 20
$n0 set Y_ 20

$n1 set X_ 300
$n1 set Y_ 20

$n2 set X_ 170
$n2 set Y_ 170

$n3 set X_ 300
$n3 set Y_ 300



# size of nodes
$ns initial_node_pos $n0 20
$ns initial_node_pos $n1 20
$ns initial_node_pos $n2 20
$ns initial_node_pos $n3 20

#################setting biding parameter ########################
set en [new Agent/AODV]
$en set e0 50
$en set e1 50
$en set e2 50
$en set e3 50
#puts "values are :[$en set e0 50][$en set e1 50] [$en set e2 50] [$en set e3 50]"


#############################################################

# ftp traffic flow
set tcp1 [new Agent/TCP]
$tcp1 set class_ 2
set sink1 [new Agent/TCPSink]
$ns attach-agent $n1 $tcp1
$ns attach-agent $n3 $sink1
$ns connect $tcp1 $sink1
set ftp [new Application/Traffic/CBR]
$ftp attach-agent $tcp1

$ns at 0.5 "$ftp start"


################ here i declare obj to access value of a in val1#######################
set obj [new Agent/AODV]
set val1 [$obj returnval1 ]
puts "$val1 " # here i want to access remaining energy of node0 but i get inital value instead of ##updated value


##################################################################################################
# simulation end
$ns at 5.0 "$n0 reset";
$ns at 5.0 "$n1 reset";
$ns at 5.0 "$n2 reset";
$ns at 5.0 "$n3 reset";


$ns at 5.01 "stop"
$ns at 5.01 "puts "NS EXITING..."; $ns halt"
set obj [new Agent/AODV]



proc stop {} {
global ns tracefd namtrace
close $tracefd
close $namtrace
exec nam en.nam &
exit 0
}

puts "Starting Simulation..."
set val1 [$obj returnval1 ]
puts " updated energy of node 0 is : $val1"
$ns run

###############output of tcl file is ####################
num_nodes is set 5
INITIALIZE THE LIST xListHead
updated energy value of node 0 50.0
Starting Simulation...
updated energy value of a : 50.0
value of a is 50.000000 :value of a is 50.000000 :channel.cc:sendUp - Calc highestAntennaZ_ and distCST_
highestAntennaZ_ = 1.5, distCST_ = 550.0
SORTING LISTS ...DONE!
time is : 2.01 a=49.9915 b= 42.8532 c= 42.8044 d=42.8706
time is : 2.01 a=49.9915 b= 42.7957 c= 42.8044 d=42.8706
time is : 3.00 a=49.9915 b= 38.0299 c= 38.0473 d=38.0560
time is : 4.00 a=49.9915 b= 33.3319 c= 33.2832 d=33.2345
time is : 4.01 a=49.9915 b= 33.3319 c= 33.2258 d=33.2345
time is : 4.01 a=49.9915 b= 33.3319 c= 33.2171 d=33.2345
root@hg-HP-Compaq-8200-Elite-MT-PC:/home/hg/Desktop#
 
Old 12-23-2017, 03:27 AM   #2
moondead
LQ Newbie
 
Registered: Dec 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
its urgent for my project.
Thanks in advance.
 
Old 12-23-2017, 05:11 AM   #3
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2017 CentOS6.9 CentOS7.4 + 50+ other Linux OS, for test only.
Posts: 17,026

Rep: Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444
Welcome to LQ.

Please edit post #1 to include code TAGS around the two blocks of code
http://www.linuxquestions.org/questi....php?do=bbcode
I.e. type [code]code text[/code]
... Or use the # button in the 'Advanced Editor'.
Editing : That's the 'Edit' button in post #1.

Would be nice if you told which lines to add the changes in aodv.cc :
Code:
$ diff -dbBwaur aodv.cc-orig aodv.cc > wsn-moondead-aodv.cc.patch
 
Old 01-03-2018, 04:44 AM   #4
moondead
LQ Newbie
 
Registered: Dec 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
i create a print_info() function to print nodes remaining energy. i use 4 static variables to stores 4 nodes remaining energy.
suppose in tcl file i start simulation at 0.00 sec and stop cbr at 2.00 sec. now i want to access nodes remaining energy in tcl file to make new decision. how to access nodes remaining energy in tcl file dynamically?
 
Old 01-03-2018, 05:17 AM   #5
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2017 CentOS6.9 CentOS7.4 + 50+ other Linux OS, for test only.
Posts: 17,026

Rep: Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444Reputation: 3444
Edit post #1, please.

See post #3 for HOWTO.
 
  


Reply

Tags
ns2, wsn


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
network simulator (NS2) programming himanshu@12 Programming 1 12-16-2017 09:56 AM
Network simulator (NS2) rohitdaid Linux - Software 0 11-11-2013 09:24 PM
Any network simulator 2 (ns2) experts around? Titanas25 Programming 1 06-12-2012 06:24 AM
ns2- network simulator problem vivekr Linux - Networking 5 02-16-2011 12:25 PM
NS2 - network simulator liquid.o Linux - Networking 0 11-24-2006 10:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 01:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration