Unclear about Schedule(), and SetRecvCallBack()

69 views
Skip to first unread message

Rui Yang

unread,
Oct 31, 2017, 6:54:54 PM10/31/17
to ns-3-users
Hi all,

I'm a new user of NS-3. When coding for that, I have a quick question that:
1. whether Simulator::Schedule() does a real delay/sleep for that node, or it just triggers the function after 'delay' seconds.
    For example, I have two consecutive lines of code: 
        Simulator::Schedule(Seconds(5.0), &SWIM::TimeoutCheck, this);
        Simulator::Schedule(Seconds(2.0), &SWIM::SendPacket, this);
    Then, will the the node do Timeout first at t=5 and do SendPacket at t=2 or do SendPacket first?

2. Will the SetRecvCallback() blocks other operations when waiting for the packets?
    For example
          socket_recv->SetRecvCallback (MakeCallback (&SWIM::HandleRead, this));
          while (1) {
               Simulator::Schedule(Seconds(5.0), &SWIM::SendPacket, this);
          }

    In this case, could the SendPacket (using another socket socket_send) run periodically and the HandleRead() is triggered normally (for example, also 5 seconds once)?

I try to test this by myself about this, but I keep failing in compiling part, so I want to guarantees that my logic/understanding of the NS3 behaviors are correct. Thanks!

Thanks in advance.

pdbarnes

unread,
Oct 31, 2017, 7:22:27 PM10/31/17
to ns-3-users


On Tuesday, October 31, 2017 at 3:54:54 PM UTC-7, Rui Yang wrote:
1. whether Simulator::Schedule() does a real delay/sleep for that node, or it just triggers the function after 'delay' seconds.
    For example, I have two consecutive lines of code: 
        Simulator::Schedule(Seconds(5.0), &SWIM::TimeoutCheck, this);
        Simulator::Schedule(Seconds(2.0), &SWIM::SendPacket, this);
    Then, will the the node do Timeout first at t=5 and do SendPacket at t=2 or do SendPacket first?

These both schedule events to happen in the future, in simulation time.  The first argument is how far into the future:  The SendPacket will be called 2s into the future; TimeoutCheck 5s in the future, so SendPacket will be called first.  Any number of other events may occur before, between of after those two events.
 
2. Will the SetRecvCallback() blocks other operations when waiting for the packets?
    For example
          socket_recv->SetRecvCallback (MakeCallback (&SWIM::HandleRead, this));
          while (1) {
               Simulator::Schedule(Seconds(5.0), &SWIM::SendPacket, this);
          }

    In this case, could the SendPacket (using another socket socket_send) run periodically and the HandleRead() is triggered normally (for example, also 5 seconds once)?

I try to test this by myself about this, but I keep failing in compiling part, so I want to guarantees that my logic/understanding of the NS3 behaviors are correct. Thanks!

For your sake I'm glad it doesn't compile yet:  while (1) looks like an infinite loop.

SetRecvCallback just registers the callback, which some Receive-like event will call when necessary, as part of receiving a packet.
a
The Schedule line in your loop will cause SendPacket to be invoked in 5s.  By putting it in a loop you are asking to schedule an infinite number of calls to SendPacket all at the same time, 5s in the future.  Perhaps you mean to send a packet every 5s?  In that case, call SendPacket once, and have it schedule the next invocation, again for 5s further in the future:

void SWIM::SendPacket (Time delay)
{
     // Send a packet
     ...

     // And schedule the next packet
    Simulator::Schedule (delay, &SWIM::SendPacket, this, delay);
}



Rui Yang

unread,
Nov 1, 2017, 3:36:07 PM11/1/17
to ns-3-users
Hi Peter,

Thank you so much! You saved my computer :D.
For the second case, I indeed want to send packets every 5 seconds, and every time I received packets from another socket, the HandleRead is triggered (maybe also 5 seconds once).

Thanks,
-Rui
Reply all
Reply to author
Forward
0 new messages