2

Let's say that we need to send this message Hellow World using UDP protocol between two PCs A and B . Computer A will send the message to B with some time delay (i.e. constant or time-varying). Now to simulate this scenario, my first attempt is to use sleep function but this solution will freezes the entire application. Another solution is to implement mutlithreads and use sleep() with the thread that is responsible for getting the data and store this in a global variable and access this variable from another thread. In this solution, there might be difficulties in the synchronization between the threads. To overcome this problem, I will write the received data in txt file and read it from another thread. My question is what is the proper way to carry out this trivial experiment? I will appreciate if the answer has some C++ pseudo.


Edit:

My attempt to solve it is as follows, for the Master side (client),

Master masterObj
int main()
{
    masterObj.initialize();
    masterObj.connect();

    while( masterObj.isConnected() == true ){
        get currentTime and data; // currentTime here is sendTime
        datagram = currentTime + data;
        masterObj.send( datagram );
    }
}

For the Slave side (server), the pseudo code is

Slave  slaveObj
int main()
{
    slaveObj.initialize();
    slaveObj.connect();
    slaveObj.slaveThreadInit();

    while( slaveObj.isConnected() == true ){
        slaveObj.getData();
    }
}

Slave::recieve()
{
    get currentTime and call it recievedTime
    get datagram from Master;
    this->slaveThread( recievedTime + datagram );   
}

Slave::slaveThread( info )
{
    sleep( 1 msec );
    info = recievedTime + datagram ;
    get time delay;
        time delay = sendTime - recievedTime;
    extract data from datagram; 
    insert data and time delay in txt file ( call it txtSlaveData);
}

Slave::getData()
{
    read from txtSlaveData;
}

As you can see, I'm using an independent thread which inside it, I'm using sleep(). I'm not sure if this approach is applicable.

pnuts
  • 58,317
  • 11
  • 87
  • 139
CroCo
  • 5,531
  • 9
  • 56
  • 88
  • 1
    Possible duplicate of [Network tools that simulate slow network connection](http://stackoverflow.com/questions/1094760/network-tools-that-simulate-slow-network-connection) – Ed Heal Oct 29 '15 at 12:54
  • @EdHeal, not exactly. Here I'm asking about simulating time delay between a client and a server but I'm not asking about any tools as the case in the link you've posted it. Basically I'm looking for a C++ pseudo. – CroCo Nov 10 '15 at 03:53

1 Answers1

0

A simple way to simulate sending UDP datagrams from one computer to another is to send the datagrams through the loopback interface to another - or the same - process on the same computer. That will function exactly like the real thing except for the delay.

You can simulate the delay either when sending or receiving. Once you've implemented it one way, the other should be trivial. I think delaying the sending side is more natural option. Here is an approach for the more general problem of simulating network delay. See the last paragraph for a trivial experiment of sending only one datagram.

In case you choose delaying on send, what you could do is, instead of sending, store the datagram in a queue, along with the time it should be sent (target = now + delay).

Then, in another thread, wait for a datagram to become available, then sleep for max(target - now, 0). After sleeping, send the datagram and move on to the next one. Wait if queue is empty.

To simulate jitter, randomize the delay. To let jitter simulation send the datagrams in non-sequential order, use a priority queue, sorted by the target send-time.

Remember to synchronize the access to the queue.

For a single datagram, you can do much simpler. Simply start a new thread, sleep for the delay, send and end thread. No need for synchronization. Here's c++ code for that:

std::thread([]{
    std::this_thread::sleep_for(delay);
    send("foo");
}).detach();
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Thank you so much for your answer. I'm still confused though from the practical perspective. I've added C++ pseudo code to illustrate my approach but I'm not sure if this is a sufficient way to handle this problem. – CroCo Nov 10 '15 at 03:47