calling a function using Simulator::Schedule

289 views
Skip to first unread message

shin...@gmail.com

unread,
Jan 22, 2015, 11:35:07 AM1/22/15
to ns-3-...@googlegroups.com
Hello all,

I have created a new model for a net device (ndev) and placed this inside src/models. It has some new functions like

void enable_pkt();l // prints some data


I created an example.cc script inside scratch and tried to call the above function (using Simulator::Schedule). But I am getting some error while passing the parameters for Simulator Schedule.
 
#include <ndev-module.h>
// added all the other modules

main
(){

// creating nodes, joining them, etc

//created an object of my new model
Ptr<ndev> my_device = Create<ndev>;

// I wish to call the enable_pkt method at a certain time. I tried below but got an error.
Simulator::Schedule(Time(1), &mydev->enable_pkt);


}


I appreciate your time and help.

Thanks,
Neels



Matt Anonyme

unread,
Jan 22, 2015, 11:45:48 AM1/22/15
to ns-3-...@googlegroups.com
A method is like a function with an instance object as first parameter (roughly). The 2nd parameter of Schedule is the function/method, and after the parameters.

So in your case, i believe sthg along those line should work:

Ptr<ndev> my_device = Create<ndev>;
Simulator::Schedule(Time(1), &ndev::enable_pkt, my_device);

shin...@gmail.com

unread,
Jan 22, 2015, 12:10:36 PM1/22/15
to ns-3-...@googlegroups.com

Thanks a lot Matt for the quick reply. That worked !!!

Now I tried to extend the same by adding a parameter to the method .
But sadly I don't see the pkt_num being printed. Everything else is printed. Am I missing something.


void enable_pkt(uint8_t pkt_num)// prints the packet number
{
std::cout<<"Packet number " << pkt_num << "is stored\n";  
}



#include <ndev-module.h>
// added all the other modules

main
(){

// creating nodes, joining them, etc

//created an object of my new model
Ptr<ndev> my_device = Create<ndev>;

// Is this .
Simulator::Schedule(Time(1), &ndev::enable_pkt, my_device, 5);
}

Matt

unread,
Jan 22, 2015, 12:20:03 PM1/22/15
to ns-3-...@googlegroups.com
well I don't know why std::cout does not print anything, I guess ns3
must fiddle with it (?).
Anyway you should use the NS_LOG* functions as described in
http://www.nsnam.org/docs/manual/html/logging.html.
So in your case to make sure it prints sthg use NS_LOG_UNCOND (quoting
the doc "NS_LOG_UNCOND (...); macro will always log its arguments")
so:
void enable_pkt(uint8_t pkt_num)// prints the packet number
{
NS_LOG_UNCOND("Packet number " << pkt_num << "is stored\n");
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "ns-3-users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ns-3-users/sdPHNBu2Puk/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> ns-3-users+...@googlegroups.com.
> To post to this group, send email to ns-3-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/ns-3-users.
> For more options, visit https://groups.google.com/d/optout.

Matt

unread,
Jan 22, 2015, 12:21:55 PM1/22/15
to ns-3-...@googlegroups.com
by the way how did you enable syntax highlighting in the mailing list
xD ? Your code is well formatted contrary to my messy posts :(

shin...@gmail.com

unread,
Jan 22, 2015, 12:29:09 PM1/22/15
to ns-3-...@googlegroups.com
Thanks Matt, Even with

NS_LOG_UNCOND("Packet number " << pkt_num << "is stored\n");

It doesn’t print the pkt_num. i use the

Regarding the syntax highlighting: when you reply, there is the "Highlight code syntax " option indicated with {}.


Thanks again,
Neels

Matt

unread,
Jan 22, 2015, 12:36:49 PM1/22/15
to ns-3-...@googlegroups.com
How do you call your script ?

shin...@gmail.com

unread,
Jan 22, 2015, 12:41:24 PM1/22/15
to ns-3-...@googlegroups.com
I call my example.cc (mentioned above) as script.
Sorry if that doesn't adhere to the definition of a script :-).


There is a one good news.

 I did a simple 'for loop' trick to check if the parameter is received

        void
        ndev
::enable_pkt(uint8_t pkt_num)
       
{
       
for (int i=0;i<pkt_num;i++)
       
{

        NS_LOG_UNCOND
("Packet number " << pkt_num << "is stored\n");
       
}
       
}




I get the following

Packet number is stored

Packet number is stored

Packet number is stored

Packet number is stored

Packi number is stored

 

So this means that the value is received but for some strange reason doesn't get printed.

Matt

unread,
Jan 22, 2015, 12:51:17 PM1/22/15
to ns-3-...@googlegroups.com
ha ok sorry, I read a bit fast and thought nothing was printed while
it's only the packet_num that doesn't get printed. what about:

NS_LOG_UNCOND("Packet number " << (uint8_t)pkt_num << "is stored\n");

shin...@gmail.com

unread,
Jan 22, 2015, 12:57:23 PM1/22/15
to ns-3-...@googlegroups.com
That still doesn't work. This is quite puzzling. Not sure if its a bug or some silly mistake.

Konstantinos

unread,
Jan 22, 2015, 1:11:24 PM1/22/15
to ns-3-...@googlegroups.com
Hi,

Instead of uint8_t print it as a integer

NS_LOG_UNCOND("Packet number " << (int)pkt_num << "is stored\n");

the uin8_t = 5 you are trying to print is an invisible symbol

Tommaso Pecorella

unread,
Jan 22, 2015, 1:11:43 PM1/22/15
to ns-3-...@googlegroups.com
NS_LOG_UNCOND("Packet number " << int(pkt_num) << " is stored\n");

uint8_t is mistakenly converted into a single char, and if the number doesn't represent a printable ASCII char, nothing will be printed (in the best case). For the records... in the worst case the output will be interpreted as a Unicode stream and all will be gibberish.

T.

shin...@gmail.com

unread,
Jan 22, 2015, 1:26:55 PM1/22/15
to ns-3-...@googlegroups.com
Thanks all, this is ideal for me to develop my next modules.
Reply all
Reply to author
Forward
0 new messages