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.
|