Search in sources :

Example 21 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class ListTablesResponse method decodePackage.

/**
 * Decode the encoded package into a object
 *
 * @param encodedPackage
 * @return
 * @throws PackageEncodeException
 */
public static ListTablesResponse decodePackage(final ByteBuffer encodedPackage) throws PackageEncodeException {
    final short requestId = NetworkPackageDecoder.getRequestIDFromResponsePackage(encodedPackage);
    final boolean decodeResult = NetworkPackageDecoder.validateResponsePackageHeader(encodedPackage, NetworkConst.RESPONSE_TYPE_LIST_TABLES);
    if (decodeResult == false) {
        throw new PackageEncodeException("Unable to decode package");
    }
    // Read the total amount of tables
    final int totalTables = encodedPackage.getInt();
    final List<TupleStoreName> tables = new ArrayList<TupleStoreName>(totalTables);
    // Read and decode tables
    for (short readTables = 0; readTables < totalTables; readTables++) {
        // Read table name length
        final short tableNameLength = encodedPackage.getShort();
        final byte[] tablenameBytes = new byte[tableNameLength];
        // Read table name and decode
        encodedPackage.get(tablenameBytes, 0, tablenameBytes.length);
        final String tablename = new String(tablenameBytes);
        final TupleStoreName sstableName = new TupleStoreName(tablename);
        tables.add(sstableName);
    }
    if (encodedPackage.remaining() != 0) {
        throw new PackageEncodeException("Some bytes are left after decoding: " + encodedPackage.remaining());
    }
    return new ListTablesResponse(requestId, tables);
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) ArrayList(java.util.ArrayList) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName)

Example 22 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class TupleResponse method decodePackage.

/**
 * Decode the encoded package into a object
 *
 * @param encodedPackage
 * @return
 * @throws PackageEncodeException
 */
public static TupleResponse decodePackage(final ByteBuffer encodedPackage) throws PackageEncodeException {
    final short requestId = NetworkPackageDecoder.getRequestIDFromResponsePackage(encodedPackage);
    final boolean decodeResult = NetworkPackageDecoder.validateResponsePackageHeader(encodedPackage, NetworkConst.RESPONSE_TYPE_TUPLE);
    if (decodeResult == false) {
        throw new PackageEncodeException("Unable to decode package");
    }
    final TupleAndTable tupleAndTable = NetworkTupleEncoderDecoder.decode(encodedPackage);
    if (encodedPackage.remaining() != 0) {
        throw new PackageEncodeException("Some bytes are left after encoding: " + encodedPackage.remaining());
    }
    return new TupleResponse(requestId, tupleAndTable.getTable(), tupleAndTable.getTuple());
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) TupleAndTable(org.bboxdb.storage.entity.TupleAndTable)

Example 23 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class PackageRouter method performInsertPackageRoutingAsync.

/**
 * Perform the routing task async
 * @param packageSequence
 * @param insertTupleRequest
 * @param boundingBox
 */
public void performInsertPackageRoutingAsync(final short packageSequence, final InsertTupleRequest insertTupleRequest) {
    final Runnable routeRunable = new ExceptionSafeRunnable() {

        @Override
        protected void runThread() {
            boolean operationSuccess = true;
            try {
                final RoutingHeader routingHeader = insertTupleRequest.getRoutingHeader();
                assert (routingHeader.isRoutedPackage()) : "Tuple is not a routed package";
                if (!routingHeader.reachedFinalInstance()) {
                    routingHeader.dispatchToNextHop();
                    operationSuccess = sendInsertPackage(insertTupleRequest);
                }
            } catch (InterruptedException e) {
                logger.error("Exception while routing package", e);
                Thread.currentThread().interrupt();
                operationSuccess = false;
            } catch (PackageEncodeException e) {
                logger.error("Exception while routing package", e);
                operationSuccess = false;
            }
            if (operationSuccess) {
                final SuccessResponse responsePackage = new SuccessResponse(packageSequence);
                clientConnectionHandler.writeResultPackageNE(responsePackage);
            } else {
                final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_ROUTING_FAILED);
                clientConnectionHandler.writeResultPackageNE(responsePackage);
            }
        }
    };
    // Submit the runnable to our pool
    if (threadPool.isShutdown()) {
        logger.warn("Thread pool is shutting down, don't route package: {}", packageSequence);
        final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_QUERY_SHUTDOWN);
        clientConnectionHandler.writeResultPackageNE(responsePackage);
    } else {
        threadPool.submit(routeRunable);
    }
}
Also used : SuccessResponse(org.bboxdb.network.packages.response.SuccessResponse) ExceptionSafeRunnable(org.bboxdb.commons.concurrent.ExceptionSafeRunnable) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) ExceptionSafeRunnable(org.bboxdb.commons.concurrent.ExceptionSafeRunnable) ErrorResponse(org.bboxdb.network.packages.response.ErrorResponse)

Example 24 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class CompressionEnvelopeRequest method writeToOutputStream.

public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        if (compressionType != NetworkConst.COMPRESSION_TYPE_GZIP) {
            throw new PackageEncodeException("Unknown compression method: " + compressionType);
        }
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final OutputStream os = new GZIPOutputStream(baos);
        // Write packages
        for (final NetworkRequestPackage networkRequestPackage : networkRequestPackages) {
            networkRequestPackage.writeToOutputStream(os);
        }
        os.close();
        final byte[] compressedBytes = baos.toByteArray();
        // Header
        final ByteBuffer bb = ByteBuffer.allocate(4);
        bb.order(Const.APPLICATION_BYTE_ORDER);
        bb.put(compressionType);
        bb.putShort((short) networkRequestPackages.size());
        // Body length
        final long bodyLength = bb.capacity() + compressedBytes.length;
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        // Write body
        outputStream.write(bb.array());
        outputStream.write(compressedBytes);
        return headerLength + bodyLength;
    } catch (IOException e) {
        throw new PackageEncodeException("Got an IO Exception while writing compressed data");
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) NetworkRequestPackage(org.bboxdb.network.packages.NetworkRequestPackage) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 25 with PackageEncodeException

use of org.bboxdb.network.packages.PackageEncodeException in project bboxdb by jnidzwetzki.

the class DisconnectRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        // Write body length
        final long bodyLength = 0;
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        return headerLength + bodyLength;
    } catch (Exception e) {
        throw new PackageEncodeException("Got exception while converting package into bytes", e);
    }
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException)

Aggregations

PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)68 IOException (java.io.IOException)31 ByteBuffer (java.nio.ByteBuffer)21 ErrorResponse (org.bboxdb.network.packages.response.ErrorResponse)16 TupleStoreName (org.bboxdb.storage.entity.TupleStoreName)14 RoutingHeader (org.bboxdb.network.routing.RoutingHeader)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ArrayList (java.util.ArrayList)9 BoundingBox (org.bboxdb.commons.math.BoundingBox)9 List (java.util.List)8 SuccessResponse (org.bboxdb.network.packages.response.SuccessResponse)6 Tuple (org.bboxdb.storage.entity.Tuple)6 StreamClientQuery (org.bboxdb.network.server.StreamClientQuery)5 OperatorTreeBuilder (org.bboxdb.storage.queryprocessor.OperatorTreeBuilder)5 NetworkRequestPackage (org.bboxdb.network.packages.NetworkRequestPackage)4 Operator (org.bboxdb.storage.queryprocessor.operator.Operator)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ExceptionSafeRunnable (org.bboxdb.commons.concurrent.ExceptionSafeRunnable)3 CompressionEnvelopeRequest (org.bboxdb.network.packages.request.CompressionEnvelopeRequest)3 ClientQuery (org.bboxdb.network.server.ClientQuery)3