Utils.prettyPrint(request);
}
// execute the request
try {
Response response = null;
boolean success = true;
long time = 5000;
boolean timedOut = false;
request.send();
System.out.println();
System.out.println("**** TEST: " + testName + " ****");
System.out.println("**** BEGIN CHECK ****");
response = request.waitForResponse(time);
if (response != null) {
success &= checkType(Type.ACK, response.getType());
success &= checkInt(EXPECTED_RESPONSE_CODE.value, response.getCode().value, "code");
success &= checkToken(request.getToken(), response.getToken());
success &= hasContentType(response);
success &= hasNonEmptyPalyoad(response);
success &= hasObserve(response);
if (success) {
time = response.getOptions().getMaxAge() * 1000;
System.out.println("+++++ Max-Age: "+time+" +++++");
if (time==0) time = 5000;
for (int l = 0; success && (l < observeLoop); ++l) {
response = request.waitForResponse(time + 1000);
// checking the response
if (response != null) {
System.out.println("Received notification " + l);
// print response info
if (verbose) {
System.out.println("Response received");
System.out.println("Time elapsed (ms): " + response.getRTT());
Utils.prettyPrint(response);
}
success &= checkResponse(request, response);
// update timeout
time = response.getOptions().getMaxAge() * 1000;
if (!timedOut && l >= 2) {
System.out.println("+++++++++++++++++++++++");
System.out.println("++++ REBOOT SERVER ++++");
System.out.println("+++++++++++++++++++++++");
System.out.println("++++ obs-reset PUT ++++");
Request asyncRequest = new Request(Code.POST, Type.CON);
asyncRequest.setPayload("sesame");
asyncRequest.setURI(serverURI + "/obs-reset");
asyncRequest.addMessageObserver(new MessageObserverAdapter() {
public void onResponse(Response response) {
if (response != null) {
System.out.println("Received: " + response.getCode());
System.out.println("+++++++++++++++++++++++");
}
}
});
asyncRequest.send();
}
} else if (!timedOut) {
timedOut = true;
l = observeLoop / 2;
System.out.println("PASS: Max-Age timed out");
System.out.println("+++++ Re-registering +++++");
Request reregister = Request.newGet();
reregister.setURI(uri);
reregister.setToken(request.getToken());
reregister.setObserve();
request = reregister;
request.send();
response = request.waitForResponse(time);
if (response != null) {
success &= checkType(Type.ACK, response.getType());
success &= checkInt(EXPECTED_RESPONSE_CODE.value, response.getCode().value, "code");
success &= checkToken(request.getToken(), response.getToken());
success &= hasContentType(response);
success &= hasNonEmptyPalyoad(response);
success &= hasObserve(response);
} else {
System.out.println("FAIL: Re-registration failed");