Skip to content

Commit

Permalink
#15 : make request timeout configurable on server-demo
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Feb 7, 2020
1 parent b175e87 commit 9d9a833
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@
public class ClientServlet extends HttpServlet {

private static final String FORMAT_PARAM = "format";
private static final String TIMEOUT_PARAM = "timeout";
private static final String REPLACE_PARAM = "replace";

private static final Logger LOG = LoggerFactory.getLogger(ClientServlet.class);

private static final long TIMEOUT = 5000; // ms
private static final long DEFAULT_TIMEOUT = 5000; // ms

private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -151,7 +152,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
if (registration != null) {
// create & process request
DiscoverRequest request = new DiscoverRequest(target);
DiscoverResponse cResponse = server.send(registration, request, TIMEOUT);
DiscoverResponse cResponse = server.send(registration, request, extractTimeout(req));
processDeviceResponse(req, resp, cResponse);
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
Expand All @@ -176,7 +177,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se

// create & process request
ReadRequest request = new ReadRequest(contentFormat, target);
ReadResponse cResponse = server.send(registration, request, TIMEOUT);
ReadResponse cResponse = server.send(registration, request, extractTimeout(req));
processDeviceResponse(req, resp, cResponse);
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
Expand Down Expand Up @@ -239,7 +240,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
target = StringUtils.removeEnd(target, path[path.length - 1]);
AttributeSet attributes = AttributeSet.parse(req.getQueryString());
WriteAttributesRequest request = new WriteAttributesRequest(target, attributes);
WriteAttributesResponse cResponse = server.send(registration, request, TIMEOUT);
WriteAttributesResponse cResponse = server.send(registration, request, extractTimeout(req));
processDeviceResponse(req, resp, cResponse);
} else {
// get content format
Expand All @@ -258,7 +259,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
LwM2mNode node = extractLwM2mNode(target, req);
WriteRequest request = new WriteRequest(replace ? Mode.REPLACE : Mode.UPDATE, contentFormat, target,
node);
WriteResponse cResponse = server.send(registration, request, TIMEOUT);
WriteResponse cResponse = server.send(registration, request, extractTimeout(req));
processDeviceResponse(req, resp, cResponse);
}
} else {
Expand Down Expand Up @@ -292,7 +293,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S

// create & process request
ObserveRequest request = new ObserveRequest(contentFormat, target);
ObserveResponse cResponse = server.send(registration, request, TIMEOUT);
ObserveResponse cResponse = server.send(registration, request, extractTimeout(req));
processDeviceResponse(req, resp, cResponse);
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
Expand All @@ -312,7 +313,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
if (registration != null) {
ExecuteRequest request = new ExecuteRequest(target, IOUtils.toString(req.getInputStream()));
ExecuteResponse cResponse = server.send(registration, request, TIMEOUT);
ExecuteResponse cResponse = server.send(registration, request, extractTimeout(req));
processDeviceResponse(req, resp, cResponse);
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
Expand Down Expand Up @@ -346,7 +347,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
request = new CreateRequest(contentFormat, target, (LwM2mObjectInstance) node);
}

CreateResponse cResponse = server.send(registration, request, TIMEOUT);
CreateResponse cResponse = server.send(registration, request, extractTimeout(req));
processDeviceResponse(req, resp, cResponse);
} else {
throw new IllegalArgumentException("payload must contain an object instance");
Expand Down Expand Up @@ -391,7 +392,7 @@ protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws
Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
if (registration != null) {
DeleteRequest request = new DeleteRequest(target);
DeleteResponse cResponse = server.send(registration, request, TIMEOUT);
DeleteResponse cResponse = server.send(registration, request, extractTimeout(req));
processDeviceResponse(req, resp, cResponse);
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
Expand All @@ -406,7 +407,7 @@ private void processDeviceResponse(HttpServletRequest req, HttpServletResponse r
throws IOException {
if (cResponse == null) {
LOG.warn(String.format("Request %s%s timed out.", req.getServletPath(), req.getPathInfo()));
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
resp.setStatus(HttpServletResponse.SC_GATEWAY_TIMEOUT);
resp.getWriter().append("Request timeout").flush();
} else {
String response = this.gson.toJson(cResponse);
Expand Down Expand Up @@ -434,4 +435,20 @@ private LwM2mNode extractLwM2mNode(String target, HttpServletRequest req) throws
}
throw new InvalidRequestException("content type %s not supported", req.getContentType());
}

private long extractTimeout(HttpServletRequest req) {
// get content format
String timeoutParam = req.getParameter(TIMEOUT_PARAM);
long timeout;
if (timeoutParam != null) {
try {
timeout = Long.parseLong(timeoutParam) * 1000;
} catch (NumberFormatException e) {
timeout = DEFAULT_TIMEOUT;
}
} else {
timeout = DEFAULT_TIMEOUT;
}
return timeout;
}
}
9 changes: 9 additions & 0 deletions leshan-server-demo/src/main/resources/webapp/css/lw-app.css
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ body.modal-open,
text-overflow: ellipsis;
}

.btn-info,
.btn-info:hover,
.btn-info:active,
.btn-info:visited,
.btn-info:focus {
background-color: #babdb6;
border-color: #babdb6;
}

/* Sticky footer styles
-------------------------------------------------- */
html {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ lwClientControllers.controller('ClientDetailCtrl', [

// default format
$scope.settings={};
$scope.settings.timeout = {format:"5s", value:5};
$scope.settings.multi = {format:"TLV"};
$scope.settings.single = {format:"TLV"};

Expand Down
39 changes: 22 additions & 17 deletions leshan-server-demo/src/main/resources/webapp/js/helper-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,29 @@ myModule.factory('helper', ["$filter", function($filter) {
serviceInstance.handleResponse = function (response, lwm2mNode, successCallback) {
lwm2mNode.date = new Date();
var formattedDate = $filter('date')(lwm2mNode.date, 'HH:mm:ss.sss');
if (!response.valid){
lwm2mNode.status = "INVALID";
}else if (response.success){
lwm2mNode.status = "SUCCESS";
}else {
lwm2mNode.status = "ERROR";
}

if (response.valid)
lwm2mNode.tooltip = formattedDate + "<br/>" + response.status ;
else
lwm2mNode.tooltip = formattedDate + "<br/> Not LWM2M Code <br/>" + response.status;

if (response.errormessage)
lwm2mNode.tooltip = lwm2mNode.tooltip + "<br/>" + response.errormessage;
if (response != null){
if (!response.valid){
lwm2mNode.status = "INVALID";
}else if (response.success){
lwm2mNode.status = "SUCCESS";
}else {
lwm2mNode.status = "ERROR";
}

if (response.valid)
lwm2mNode.tooltip = formattedDate + "<br/>" + response.status ;
else
lwm2mNode.tooltip = formattedDate + "<br/> Not LWM2M Code <br/>" + response.status;

if (response.errormessage)
lwm2mNode.tooltip = lwm2mNode.tooltip + "<br/>" + response.errormessage;

if (successCallback && response.success) {
successCallback(formattedDate);
if (successCallback && response.success) {
successCallback(formattedDate);
}
} else {
lwm2mNode.status = "TIMEOUT";
lwm2mNode.tooltip = formattedDate + "<br/>" + lwm2mNode.status ;
}
};
return serviceInstance;
Expand Down
Loading

0 comments on commit 9d9a833

Please sign in to comment.