Fault-tolerant UDP Client

Test of UDP Proxy

Using the UpperCase server ServerUpperCaseUDP.jar and your program NetcatUDP from the previous session, we are going to simulate the packet losses using a UDP proxy. These phenomena do occur in real life but are very rare on a local network.

The program UDPProxy.jar allows to simulate packet loss, reorderings and delays on your machine. ProxyUDP is a UDP proxy which will drop a percentage of the packets and which will randomly exchange the order of the packets.

In three different terminals, simultaneously displayed on screen, run the three programs below:

$ java -jar ServerUpperCaseUDP.jar 4545 UTF-8  
$ java -jar UDPProxy.jar 7777 localhost 4545 -no-swap
$ java fr.uge.net.udp.NetcatUDP localhost 7777 utf-8

Type several lines in NetcatUDP. What happens when a packet is lost ? What is the impact on your program after the losse.

First attempt to fault tolerance

In this exercise, we will modify NetcatUDP to make it fault tolerant.

Copy the code the NetcatUDP in a class ClientUpperCaseUDPTimeout and modify it to print "Server did not respond in time" if the server did not respond in less than one second.

Reminder: The problem is that method receive of DatagramChannel is blocking and that it does not accept a timeout.
The solution consists in starting a listener thread which will perform the call to receive on the DatagramChannel and will pass the received strings to the main thread using a BlockingQueue.
After starting your listener thread, you can access the answer of the server using the poll method of the BlockingQueue which can be used with a timeout.

Test your class ClientUpperCaseUDPTimeout with the same set up as in the previous exercise.

Copy your class in a class ClientUpperCaseUDPRetry and modify it so that it resend the line if the server has not replied after one second (until an answer from the server is received).

Test your class ClientUpperCaseUDPRetry with the same set up as in the previous exercise.

Thousands of lines in uppercase ?

In this exercise, we want to use the server ServerUpperCaseUDP to turn into uppercase all of the lines of a file.
The current protocol does not allow us to do that, in the presence of packet loss or mixing. The aim of the exercise is just to see for yourself that it cannot work. It is normal that you do not obtain a correct uppercase file.

We want to write a program ClientUpperCaseUDPFile which takes as parameters:

The main of ClientUpperCaseUDPFile should:
  1. retrieve all the lines in in-filename open in the UTF-8 charset and store them in a list;
  2. turn every line in uppercase using the ServerUpperCaseUDP whose address is given as a parameter;
    You will proceed exactly as for ClientUpperCaseUDPRetry: for each line, a request is sent to the server. If no answer is received within timeout milliseconds, the request for the line is sent again until an answer is received. All the strings corresponding to the answers are stored in a list.
  3. Finally, all of the lines of this list are written in UTF-8 to the file out-filename.

Starting from the template ClientUpperCaseUDPFile.java, write the program ClientUpperCaseUDPFile.

Test your program through the UDP proxy with the file in.txt as follows:

        $ java -jar ServerUpperCaseUDP.jar 4545 UTF-8
    
        $ java -jar UDPProxy.jar 7777 localhost 4545 -no-swap
    
        $ java fr.uge.net.udp.ClientUpperCaseUDPFile in.txt out.txt 300 localhost 7777
    

Look at the file out.txt. Explain the presence of duplicated lines knowning that by default a packet takes 300ms to go from the client to the server or from the server to the client.