final GridNioSession ses,
final GridMemcachedMessage req,
final GridTuple3<GridRestCommand, Boolean, Boolean> cmd
) {
if (cmd.get1() == NOOP) {
GridMemcachedMessage res0 = new GridMemcachedMessage(req);
res0.status(SUCCESS);
sendResponse(ses, res0);
return null;
}
GridFuture<GridRestResponse> f = hnd.handleAsync(createRestRequest(req, cmd.get1()));
f.listenAsync(new CIX1<GridFuture<GridRestResponse>>() {
@Override public void applyx(GridFuture<GridRestResponse> f) throws GridException {
GridRestResponse restRes = f.get();
// Handle 'Stat' command (special case because several packets are included in response).
if (cmd.get1() == CACHE_METRICS) {
assert restRes.getResponse() instanceof GridCacheRestMetrics;
Map<String, Long> metrics = ((GridCacheRestMetrics)restRes.getResponse()).map();
for (Map.Entry<String, Long> e : metrics.entrySet()) {
GridMemcachedMessage res = new GridMemcachedMessage(req);
res.key(e.getKey());
res.value(String.valueOf(e.getValue()));
sendResponse(ses, res);
}
sendResponse(ses, new GridMemcachedMessage(req));
}
else {
GridMemcachedMessage res = new GridMemcachedMessage(req);
if (restRes.getSuccessStatus() == GridRestResponse.STATUS_SUCCESS) {
switch (cmd.get1()) {
case CACHE_GET: {
res.status(restRes.getResponse() == null ? KEY_NOT_FOUND : SUCCESS);
break;
}
case CACHE_PUT:
case CACHE_ADD:
case CACHE_REMOVE:
case CACHE_REPLACE:
case CACHE_CAS:
case CACHE_APPEND:
case CACHE_PREPEND: {
boolean res0 = restRes.getResponse().equals(Boolean.TRUE);
res.status(res0 ? SUCCESS : FAILURE);
break;
}
default: {
res.status(SUCCESS);
break;
}
}
}
else
res.status(FAILURE);
if (cmd.get3())
res.key(req.key());
if (restRes.getSuccessStatus() == GridRestResponse.STATUS_SUCCESS && res.addData() &&
restRes.getResponse() != null)
res.value(restRes.getResponse());
sendResponse(ses, res);
}
}
});