Search in sources :

Example 1 with KeepAliveRequest

use of org.bboxdb.network.packages.request.KeepAliveRequest in project bboxdb by jnidzwetzki.

the class KeepAliveHandler method handleRequest.

/**
 * Handle the keep alive package. Simply send a success response package back
 */
@Override
public boolean handleRequest(final ByteBuffer encodedPackage, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
    this.clientConnectionHandler = clientConnectionHandler;
    final KeepAliveRequest keepAliveRequst = KeepAliveRequest.decodeTuple(encodedPackage);
    boolean gossipResult = true;
    if (!keepAliveRequst.getTuples().isEmpty()) {
        gossipResult = handleGossip(keepAliveRequst, clientConnectionHandler);
    }
    if (gossipResult) {
        final SuccessResponse responsePackage = new SuccessResponse(packageSequence);
        clientConnectionHandler.writeResultPackage(responsePackage);
    } else {
        final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_OUTDATED_TUPLES);
        clientConnectionHandler.writeResultPackage(responsePackage);
    }
    return true;
}
Also used : SuccessResponse(org.bboxdb.network.packages.response.SuccessResponse) KeepAliveRequest(org.bboxdb.network.packages.request.KeepAliveRequest) ErrorResponse(org.bboxdb.network.packages.response.ErrorResponse)

Example 2 with KeepAliveRequest

use of org.bboxdb.network.packages.request.KeepAliveRequest in project bboxdb by jnidzwetzki.

the class TestNetworkClasses method testRequestPackageHeader.

/**
 * Test the encoding of the request package header
 * @throws IOException
 * @throws PackageEncodeException
 */
@Test(timeout = 60000)
public void testRequestPackageHeader() throws IOException, PackageEncodeException {
    final short currentSequenceNumber = sequenceNumberGenerator.getSequeneNumberWithoutIncrement();
    final short sequenceNumber = sequenceNumberGenerator.getNextSequenceNummber();
    final KeepAliveRequest listTablesRequest = new KeepAliveRequest(sequenceNumber);
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    listTablesRequest.writeToOutputStream(bos);
    bos.flush();
    bos.close();
    final byte[] encodedPackage = bos.toByteArray();
    Assert.assertEquals(26, encodedPackage.length);
    final ByteBuffer bb = ByteBuffer.wrap(encodedPackage);
    bb.order(Const.APPLICATION_BYTE_ORDER);
    // Check fields
    Assert.assertEquals(currentSequenceNumber, bb.getShort());
    Assert.assertEquals(NetworkConst.REQUEST_TYPE_KEEP_ALIVE, bb.getShort());
}
Also used : KeepAliveRequest(org.bboxdb.network.packages.request.KeepAliveRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with KeepAliveRequest

use of org.bboxdb.network.packages.request.KeepAliveRequest in project bboxdb by jnidzwetzki.

the class TestNetworkClasses method encodeAndDecodeKeepAlive2.

/**
 * The the encoding and decoding of a keep alive package
 * @throws IOException
 * @throws PackageEncodeException
 */
@Test(timeout = 60000)
public void encodeAndDecodeKeepAlive2() throws IOException, PackageEncodeException {
    final short sequenceNumber = sequenceNumberGenerator.getNextSequenceNummber();
    final String tablename = "testgroup_abc";
    final List<Tuple> tuples = new ArrayList<>();
    tuples.add(new Tuple("abc", new BoundingBox(1d, 2d, 1d, 2d), "".getBytes()));
    tuples.add(new Tuple("def", new BoundingBox(2d, 2d, 4d, 20d), "".getBytes()));
    tuples.add(new Tuple("xyz", new BoundingBox(10d, 20d, 10d, 20d), "".getBytes()));
    final KeepAliveRequest keepAlivePackage = new KeepAliveRequest(sequenceNumber, tablename, tuples);
    byte[] encodedVersion = networkPackageToByte(keepAlivePackage);
    Assert.assertNotNull(encodedVersion);
    final ByteBuffer bb = NetworkPackageDecoder.encapsulateBytes(encodedVersion);
    final KeepAliveRequest decodedPackage = KeepAliveRequest.decodeTuple(bb);
    Assert.assertEquals(keepAlivePackage.getTablename(), decodedPackage.getTablename());
    Assert.assertEquals(keepAlivePackage.getTuples().size(), decodedPackage.getTuples().size());
}
Also used : BoundingBox(org.bboxdb.commons.math.BoundingBox) ArrayList(java.util.ArrayList) KeepAliveRequest(org.bboxdb.network.packages.request.KeepAliveRequest) ByteBuffer(java.nio.ByteBuffer) JoinedTuple(org.bboxdb.storage.entity.JoinedTuple) Tuple(org.bboxdb.storage.entity.Tuple) DeletedTuple(org.bboxdb.storage.entity.DeletedTuple) Test(org.junit.Test)

Example 4 with KeepAliveRequest

use of org.bboxdb.network.packages.request.KeepAliveRequest in project bboxdb by jnidzwetzki.

the class TestNetworkClasses method encodeAndDecodeKeepAlive1.

/**
 * The the encoding and decoding of a keep alive package
 * @throws IOException
 * @throws PackageEncodeException
 */
@Test(timeout = 60000)
public void encodeAndDecodeKeepAlive1() throws IOException, PackageEncodeException {
    final short sequenceNumber = sequenceNumberGenerator.getNextSequenceNummber();
    final KeepAliveRequest keepAlivePackage = new KeepAliveRequest(sequenceNumber);
    byte[] encodedVersion = networkPackageToByte(keepAlivePackage);
    Assert.assertNotNull(encodedVersion);
    final ByteBuffer bb = NetworkPackageDecoder.encapsulateBytes(encodedVersion);
    final KeepAliveRequest decodedPackage = KeepAliveRequest.decodeTuple(bb);
    Assert.assertEquals(keepAlivePackage, decodedPackage);
    Assert.assertEquals(keepAlivePackage.hashCode(), decodedPackage.hashCode());
    Assert.assertEquals(keepAlivePackage.toString(), decodedPackage.toString());
}
Also used : KeepAliveRequest(org.bboxdb.network.packages.request.KeepAliveRequest) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

KeepAliveRequest (org.bboxdb.network.packages.request.KeepAliveRequest)4 ByteBuffer (java.nio.ByteBuffer)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 BoundingBox (org.bboxdb.commons.math.BoundingBox)1 ErrorResponse (org.bboxdb.network.packages.response.ErrorResponse)1 SuccessResponse (org.bboxdb.network.packages.response.SuccessResponse)1 DeletedTuple (org.bboxdb.storage.entity.DeletedTuple)1 JoinedTuple (org.bboxdb.storage.entity.JoinedTuple)1 Tuple (org.bboxdb.storage.entity.Tuple)1