Search in sources :

Example 1 with CancelQueryRequest

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());
}
Also used : CancelQueryRequest(org.bboxdb.network.packages.request.CancelQueryRequest) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with CancelQueryRequest

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;
}
Also used : SuccessResponse(org.bboxdb.network.packages.response.SuccessResponse) ClientQuery(org.bboxdb.network.server.ClientQuery) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) CancelQueryRequest(org.bboxdb.network.packages.request.CancelQueryRequest) ErrorResponse(org.bboxdb.network.packages.response.ErrorResponse)

Aggregations

CancelQueryRequest (org.bboxdb.network.packages.request.CancelQueryRequest)2 ByteBuffer (java.nio.ByteBuffer)1 PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)1 ErrorResponse (org.bboxdb.network.packages.response.ErrorResponse)1 SuccessResponse (org.bboxdb.network.packages.response.SuccessResponse)1 ClientQuery (org.bboxdb.network.server.ClientQuery)1 Test (org.junit.Test)1