Detecting missing messages
Before you can deal with a problem, you have to spot it. Error detection is therefore an important part of programming.
At the end of the previous step you were asked how you could determine whether a pixel had been lost, and what you could do about it.
There are countless possible options, but some that you may have considered are:
- Counting pixels received and making assumptions about how many should have been received
- Identifying gaps in the image that has been received
- Filling in empty pixels with the colour of the pixels surrounding it
The answers to these questions are techniques for error detection and correction.
Many of the approaches to detecting errors in unreliable communication are hampered by the fact that you may not even know when a message was not received or never sent. Due to this, you may have to make assumptions and best guesses. This is another reason why many real-world applications use a mix of reliable and unreliable communication.
You will now implement a simple algorithm to detect whether a message has been lost. By simulating data loss, you can investigate ways of improving error detection.
For the moment, assume that the pixels are received in the order in which they are sent. Therefore, if a pixel is received out of sequence, we can assume that a message has been lost.
This simple error detection algorithm will work like this:
- Receive a pixel.
- Store the position of the pixel received.
- Receive a new pixel.
- Ask whether the position of the new pixel is more than 1 greater in x or y than the last pixel.
- If so, a pixel has been lost.
get_pixel_data function to implement this algorithm:
At the top of your program, create two new variables to hold the number of lost pixels and the last pixel that was updated.
lost_pixels = 0 last_pixel_updated = (-1, -1)
Output how many pixels have been lost to the image viewer.
viewer.text = lost_pixels
Check to see whether the pixel received is out of sequence. Compare the position of the pixel received to the position of the last one received, and update the
if (pos - last_pixel_updated > 1) or (pos - last_pixel_updated > 1): lost_pixels += 1 viewer.text = lost_pixels last_pixel_updated = pos
Run your program again. You will see that a count of lost pixels has appeared at the top of the image viewer.
It could be that, like mine, your lost pixel count is showing as zero! This could be because no pixels were lost, and is particularly likely if you are running your sending and receiving programs on the same computer.
Simulating lost packets
To test and ensure that your error detection is working, you can simulate losing pixels by randomly choosing not to send some of them.
randintfunction, which you can use to generate random numbers to use in deciding whether to send a packet.
from random import randint
To understand how well the error detection is working, you should keep a count of the pixels “lost”.
pixels_lost = 0
randintto decide randomly whether to send a pixel or not.
if randint(0,9) > 0: udp_client.sendto(data, ("127.0.0.1", 20001)) else: pixels_lost += 1
randint(0,9)will return a random number between
9. The condition
randint(0,10) > 0will return
Trueand send the pixel if the random number returned by
1or higher. Therefore, anytime a
0is returned by
randinta pixel won’t be sent, so you should expect to lose about 10% of all the pixels. You can change the error rate by modifying this formula.
Print the number of pixels “lost” when the complete image has been sent.
Run your two programs and compare the number of pixels you simulated as being lost and the number you detected as being lost. In my example, I lost 1,322 pixels, but detected only 1,162 being lost.
Improving the error detection
The algorithm I created to detect errors is finding about 88% of all errors. That’s quite good, but it could be better.
Try changing the error detection algorithm in the receiving program to improve the detection rate, you can also edit the sending program to send more information to help you.
Here are some pointers to get you started improving the existing error detection algorithm:
- When more than one pixel in sequence is lost, only one is counted. For example, if three pixels between the positions
(0,3)were lost, the count is only incremented by one.
- When pixels are received out of order, subsequent pixels will be detected as lost even though they may later be received. For example, if four pixels were received in the order
(0,4) (0,6) (0,5), (0,7), pixels
(0,7)would be marked as lost because they didn’t arrive in order.
Share your new programs in the comments section using a Pastebin link. Remember that you can ask for help if you need it!