Commit 53445295 authored by legoc's avatar legoc
Browse files

Implemented timeout in Java Request

parent 5512fb68
......@@ -189,6 +189,14 @@ public class Server {
return requestSocket;
}
RequestSocket createRequestSocket(String endpoint, int timeout) throws SocketException {
RequestSocket requestSocket = new RequestSocket(contextImpl, timeout, parser);
requestSocket.connect(endpoint);
return requestSocket;
}
/**
* Connects to the server. Returns false if there is no connection.
* It must be called to initialize the receiving status.
......
......@@ -112,6 +112,10 @@ public class This {
return server.createRequestSocket(endpoint);
}
public RequestSocket createRequestSocket(String endpoint, int timeout) {
return server.createRequestSocket(endpoint, timeout);
}
/**
* Method provided by convenience to simplify the parsing of JSON messages.
* @param message
......
......@@ -4,6 +4,7 @@ import java.util.List;
import org.json.simple.JSONObject;
import fr.ill.ics.cameo.base.ConnectionTimeout;
import fr.ill.ics.cameo.base.Instance;
import fr.ill.ics.cameo.base.RequestSocket;
import fr.ill.ics.cameo.base.Server;
......@@ -23,6 +24,7 @@ public class Request {
private String requesterApplicationName;
private int requesterApplicationId;
private String requesterServerEndpoint;
private int timeout = 0;
public Request(String requesterApplicationName, int requesterApplicationId, String serverUrl, int serverPort, int requesterPort, byte[] messagePart1, byte[] messagePart2) {
......@@ -53,20 +55,34 @@ public class Request {
return result;
}
public void reply(byte[] response) {
public void setTimeout(int value) {
timeout = value;
}
public boolean reply(byte[] response) {
JSONObject request = new JSONObject();
request.put(Messages.TYPE, Messages.RESPONSE);
// Create a new socket.
// Notice that trying to reuse a socket by calling connect() does not work (it is worse with jeromq)
RequestSocket requestSocket = This.getCom().createRequestSocket(requesterEndpoint);
requestSocket.request(Messages.serialize(request), response);
requestSocket.terminate();
RequestSocket requestSocket = This.getCom().createRequestSocket(requesterEndpoint, timeout);
try {
requestSocket.request(Messages.serialize(request), response);
}
catch (ConnectionTimeout e) {
return false;
}
finally {
requestSocket.terminate();
}
return true;
}
public void reply(String response) {
reply(Messages.serialize(response));
public boolean reply(String response) {
return reply(Messages.serialize(response));
}
public Instance connectToRequester() {
......
......@@ -54,7 +54,10 @@ int main(int argc, char *argv[]) {
parse(request->getSecondBinaryPart(), data2);
cout << "Received request " << data1 << " " << data2 << endl;
request->reply("2nd response");
bool res = request->reply("2nd response");
if (!res) {
cout << "Error, no timeout expected" << endl;
}
// Receive third request without receive on the requester side.
request = responder->receive();
......@@ -62,7 +65,7 @@ int main(int argc, char *argv[]) {
// Reply with timeout
request->setTimeout(100);
bool res = request->reply("3rd response");
res = request->reply("3rd response");
if (!res) {
cout << "Timeout with " << request->getObjectId() << endl;
......
......@@ -53,16 +53,23 @@ public class Responder {
System.out.println("Received request " + Messages.parseString(data[0]) + " " + Messages.parseString(data[1]));
// Reply.
request.reply("2nd response");
boolean res = request.reply("2nd response");
if (!res) {
System.out.println("Error, no timeout expected");
}
// Receive the third request.
request = responder.receive();
System.out.println("Received request " + request.get());
// Reply.
//request.setTimeout(100);
request.reply("3rd response");
request.setTimeout(100);
res = request.reply("3rd response");
if (!res) {
System.out.println("Timeout with " + request.toString());
}
request.terminate();
......
......@@ -78,6 +78,7 @@ public class TestResponder {
requester.send("request");
// Wait 1s.
System.out.println("Wait so that the responder has timed out");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment