Define a new version of the doOperation method that sets a timeout on waiting for the reply message. After a timeout, it retransmits the request message n times. If there is still no reply, it informs the caller.
What will be an ideal response?
With a timeout set on a socket, a receive operation will block for the given amount of time and then an InterruptedIOException will be raised.
In the constructor of Client, set a timeout of say, 3 seconds
```
Client(){
aSocket = new DatagramSocket();
aSocket.setSoTimeout(3000);// in milliseconds
}
```
In doOperation, catch InterruptedIOException. Repeatedly send the Request message and try to receive a reply, e.g. 3 times. If there is no reply, return a special value to indicate a failure.
```
public byte [] doOperation(RemoteObjectRef o, int methodId, byte [] arguments){
InetAddress serverIp = o.getIPaddress();
int serverPort = o.getPort();
RequestMessage rm = new RequestMessage(0, o, methodId, arguments );
byte [] message = rm.marshall(); DatagramPacket request =
new DatagramPacket(message,message.length(0, serverIp, serverPort);
for(int i=0; i<3;i++){
try{
aSocket.send(request);
byte buffer = new byte[messageLength];
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
aSocket.receive(reply);
return reply;
}catch (SocketException e){);
}catch (InterruptedIOException e){}
}
return null;
}
```
You might also like to view...
All exception classes inherit, either directly or indirectly, from ________.
a. class Error. b. class RuntimeException. c. class Throwable. d. None of the above.
The following HTML code is most likely in which tier of the Model-View-Controller design pattern?
Program Calculates Area and Perimeter of Circles
Enter the radius:
A. Model B. View C. Controller D. Unable to determine
A predefined formula that performs calculations by using specific values in a particular order
a. Function b. Set formula c. Average function
A screening firewall works in the application layer of the OSI model
Indicate whether the statement is true or false.