LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-13-2014, 10:10 AM   #1
rose24
LQ Newbie
 
Registered: Oct 2012
Posts: 14

Rep: Reputation: Disabled
Failing to transfer variable from AODV class to UDPAgent class in ns2


Hello all,
I'm working on ns-2 network simulator and I got stuck when things come to transfer or exchange variables between classes. I was able to transfer one variable from Mac_802.11() class to AODV routing class. However, when I tried to transfer the same variable from AODV class to UDPAgent() class, everything become complicated and each time I got a segmentation fault. Briefly, what I need is that to transfer any variable, for example "counter", from AODV class to UDP class. Thus what I did is the following:

In aodv.h file,and under AODV() class I declared my variable
class AODV: public Agent {

public:
//for testing
int counter;

....


Now in aodv.cc file, I initialize the "counter" variable with 33 in constructor as the following:

AODV::AODV(nsaddr_t id) : Agent(PT_AODV),
btimer(this), htimer(this), ntimer(this),
rtimer(this), lrtimer(this), rqueue() {

counter=33;
}

After updating this counter by incrementing it in recvRequest() function, I want to access those values in UDPAgent() class

AODV::recvRequest(Packet *p) {

counter++;
}

So what I did is that I define an AODV pointer in class UdpAgent (udp.h):

AODV* myaodv;

Then, I added the suitable lines in the definition of member function “command” in class UdpAgent (udp.cc)so that I can obtain my information from AODV and consume in udp.cc:

int UdpAgent::command(int argc, const char*const* argv)
{
if(argc == 3) {
if (strcmp(argv[1], "attached-routing") == 0) {
myaodv = (AODV*) TclObject::lookup(argv[2]);
if (!myaodv){
puts("Scheduler(myaodv): attempt to schedule an event with a NULL handler.");
return TCL_ERROR;
}
return (TCL_OK);
}
}
In order to send the AODV object from OTcl domain to C++ domain, I used following lines in my scenario.tcl script:

for {set i 0} {$i < $opt(nn) } {incr i} {
set agent_($i) [new Agent/UDP]
set routAgent [$node_($i) agent 255]
$agent_($i) attached-routing $routAgent

}

Now, inside my udpAgent()class, I'm trying to use aodv pointer to access the counter variable in sendmsg() function in this way:

void UdpAgent::sendmsg(int nbytes, AppData* data, const char* flags)
{
if(myaodv!=NULL){
printf("Not NULL :)) \n");
printf("The counter value:%d \n",myaodv->counter);

}else {
printf("not able to allocate memory :-----(((\n");
}
}
However, each time when I run the tcl script, I got a segmentation fault error and when I use insight "a debugger for ns2", I found that he is trying to read from another address and returned non reasonable values such as:
The counter value:1918989860
not able to allocate memory :-----(((
Not NULL :))
The counter value:50331648
Not NULL :))
The counter value:1918989860
not able to allocate memory :-----(((

So anyone has an idea why I got failed to access this variable!? Is there anything wrong with my code?
This is what I understand after searching on the internet and reading the chapter linkage between c++ and otcl in ns2 book. So is the way I'm transferring the variable is wrong? because I'm suffering for a long time and I didn't find such a clear example on transferring variables from lower layer to upper layers. Please help me.

Your help is really appreciated.

Thanks in advance.
 
  


Reply

Tags
errors, ns2, ns2-c++-tcl, ns234, segmentation fault ns2


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
Why can't we access a protected member in a derived class by a base class's object? Aquarius_Girl Programming 5 02-04-2012 10:35 PM
Does making a class a 'friend' make the class' member classes 'friends'? Jusctsch Programming 7 11-17-2011 07:58 PM
[SOLVED] Python n00b: promoting a derived class to base class? CoderMan Programming 2 03-11-2010 01:46 PM
C++ templated Node class: pointers to different instantated class types jhwilliams Programming 3 08-20-2007 06:20 PM
Which C++ editor in Linux has the class view/class browser feature imaginationworks Programming 7 05-21-2006 11:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:13 PM.

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
Open Source Consulting | Domain Registration