use of org.bboxdb.network.packages.request.CancelQueryRequest in project bboxdb by jnidzwetzki.
the class TestNetworkClasses method encodeAndDecodeCancelQuery.
/**
* The encoding and decoding of an cancel query package
* @throws IOException
* @throws PackageEncodeException
*/
@Test(timeout = 60000)
public void encodeAndDecodeCancelQuery() throws IOException, PackageEncodeException {
final short sequenceNumber = sequenceNumberGenerator.getNextSequenceNummber();
final short querySequence = 12;
final CancelQueryRequest cancelQueryRequest = new CancelQueryRequest(sequenceNumber, querySequence);
byte[] encodedVersion = networkPackageToByte(cancelQueryRequest);
Assert.assertNotNull(encodedVersion);
final ByteBuffer bb = NetworkPackageDecoder.encapsulateBytes(encodedVersion);
final CancelQueryRequest decodedPackage = CancelQueryRequest.decodeTuple(bb);
Assert.assertEquals(decodedPackage.getQuerySequence(), querySequence);
Assert.assertEquals(cancelQueryRequest.hashCode(), decodedPackage.hashCode());
Assert.assertEquals(cancelQueryRequest.toString(), decodedPackage.toString());
}
use of org.bboxdb.network.packages.request.CancelQueryRequest in project bboxdb by jnidzwetzki.
the class CancelQueryHandler method handleRequest.
@Override
public /**
* Cancel the given query
*/
boolean handleRequest(final ByteBuffer encodedPackage, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
try {
final CancelQueryRequest nextPagePackage = CancelQueryRequest.decodeTuple(encodedPackage);
final short queryToCancel = nextPagePackage.getQuerySequence();
logger.debug("Cancel query {} requested", queryToCancel);
final Map<Short, ClientQuery> activeQueries = clientConnectionHandler.getActiveQueries();
if (!activeQueries.containsKey(queryToCancel)) {
logger.error("Unable to cancel query {} - not found", queryToCancel);
clientConnectionHandler.writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_QUERY_NOT_FOUND));
} else {
final ClientQuery clientQuery = activeQueries.remove(queryToCancel);
clientQuery.close();
clientConnectionHandler.writeResultPackage(new SuccessResponse(packageSequence));
logger.info("Sending success for canceling query {} (request package {})", queryToCancel, packageSequence);
}
} catch (PackageEncodeException e) {
logger.warn("Error getting next page for a query", e);
final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION);
clientConnectionHandler.writeResultPackage(responsePackage);
}
return true;
}
Aggregations