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...
Access reports can be saved as HTTP documents
Indicate whether the statement is true or false
Which of the following strategies is based on analyzing the work to be done, including its associated current processes and the level of effectiveness and resources required?
a. Outsourcing governance process b. Smart sourcing c. Outsourcing contract d. Service-level agreement