Search in sources :

Example 41 with PackageEncodeException

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

the class HelloRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        final ByteBuffer bb = DataEncoderHelper.intToByteBuffer(protocolVersion);
        final byte[] peerCapabilitiesBytes = peerCapabilities.toByteArray();
        // Body length
        final long bodyLength = bb.capacity() + peerCapabilitiesBytes.length;
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        outputStream.write(bb.array());
        outputStream.write(peerCapabilitiesBytes);
        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) ByteBuffer(java.nio.ByteBuffer) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException)

Example 42 with PackageEncodeException

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

the class InsertTupleRequest method decodeTuple.

/**
 * Decode the encoded tuple into a object
 *
 * @param encodedPackage
 * @return
 * @throws IOException
 * @throws PackageEncodeException
 */
public static InsertTupleRequest decodeTuple(final ByteBuffer encodedPackage) throws IOException, PackageEncodeException {
    final short sequenceNumber = NetworkPackageDecoder.getRequestIDFromRequestPackage(encodedPackage);
    final boolean decodeResult = NetworkPackageDecoder.validateRequestPackageHeader(encodedPackage, NetworkConst.REQUEST_TYPE_INSERT_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 decoding: " + encodedPackage.remaining());
    }
    final RoutingHeader routingHeader = NetworkPackageDecoder.getRoutingHeaderFromRequestPackage(encodedPackage);
    final TupleStoreName ssTableName = new TupleStoreName(tupleAndTable.getTable());
    return new InsertTupleRequest(sequenceNumber, routingHeader, ssTableName, tupleAndTable.getTuple());
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) RoutingHeader(org.bboxdb.network.routing.RoutingHeader) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName) TupleAndTable(org.bboxdb.storage.entity.TupleAndTable)

Example 43 with PackageEncodeException

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

the class KeepAliveRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        final byte[] tableBytes = tablename.getBytes();
        final ByteBuffer bb = ByteBuffer.allocate(8);
        bb.order(Const.APPLICATION_BYTE_ORDER);
        bb.putShort((short) tableBytes.length);
        bb.put(NetworkConst.UNUSED_BYTE);
        bb.put(NetworkConst.UNUSED_BYTE);
        bb.putInt(tuples.size());
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write(bb.array());
        bos.write(tableBytes);
        for (final Tuple tuple : tuples) {
            final byte[] keyByteArray = tuple.getKey().getBytes();
            final byte[] boundingBoxBytes = tuple.getBoundingBoxBytes();
            final ByteBuffer keyLengthBytes = DataEncoderHelper.intToByteBuffer(keyByteArray.length);
            final ByteBuffer boundingBoxLength = DataEncoderHelper.intToByteBuffer(boundingBoxBytes.length);
            final ByteBuffer versionBytes = DataEncoderHelper.longToByteBuffer(tuple.getVersionTimestamp());
            bos.write(keyLengthBytes.array());
            bos.write(keyByteArray);
            bos.write(boundingBoxLength.array());
            bos.write(boundingBoxBytes);
            bos.write(versionBytes.array());
        }
        bos.flush();
        bos.close();
        final byte[] bodyBytes = bos.toByteArray();
        final int bodyLength = bodyBytes.length;
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        outputStream.write(bodyBytes);
        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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Tuple(org.bboxdb.storage.entity.Tuple) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException)

Example 44 with PackageEncodeException

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

the class LockTupleRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        final byte[] tablenameBytes = tablename.getBytes();
        final byte[] keyBytes = key.getBytes();
        final ByteBuffer bb = ByteBuffer.allocate(12);
        bb.order(Const.APPLICATION_BYTE_ORDER);
        bb.putShort((short) tablenameBytes.length);
        bb.putShort((short) keyBytes.length);
        bb.putLong(version);
        // Body length
        final long bodyLength = bb.capacity() + tablenameBytes.length + keyBytes.length;
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        // Write body
        outputStream.write(bb.array());
        outputStream.write(tablenameBytes);
        outputStream.write(keyBytes);
        return headerLength + bodyLength;
    } catch (IOException e) {
        throw new PackageEncodeException("Got exception while converting package into bytes", e);
    }
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 45 with PackageEncodeException

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

the class QueryBoundingBoxRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        final byte[] tableBytes = table.getFullnameBytes();
        final byte[] bboxBytes = box.toByteArray();
        final ByteBuffer bb = ByteBuffer.allocate(12);
        bb.order(Const.APPLICATION_BYTE_ORDER);
        bb.put(getQueryType());
        if (pagingEnabled) {
            bb.put((byte) 1);
        } else {
            bb.put((byte) 0);
        }
        bb.putShort(tuplesPerPage);
        bb.putShort((short) tableBytes.length);
        bb.put(NetworkConst.UNUSED_BYTE);
        bb.put(NetworkConst.UNUSED_BYTE);
        bb.putInt((int) bboxBytes.length);
        final long bodyLength = bb.capacity() + tableBytes.length + bboxBytes.length;
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        // Write body
        outputStream.write(bb.array());
        outputStream.write(tableBytes);
        outputStream.write(bboxBytes);
        return headerLength + bodyLength;
    } catch (IOException e) {
        throw new PackageEncodeException("Got exception while converting package into bytes", e);
    }
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

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