Search in sources :

Example 26 with PackageEncodeException

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

the class HelloRequest method decodeRequest.

/**
 * Decode the encoded package into a object
 *
 * @param encodedPackage
 * @return
 * @throws PackageEncodeException
 */
public static HelloRequest decodeRequest(final ByteBuffer encodedPackage) throws PackageEncodeException {
    final short sequenceNumber = NetworkPackageDecoder.getRequestIDFromRequestPackage(encodedPackage);
    final boolean decodeResult = NetworkPackageDecoder.validateRequestPackageHeader(encodedPackage, NetworkConst.REQUEST_TYPE_HELLO);
    if (decodeResult == false) {
        throw new PackageEncodeException("Unable to decode package");
    }
    final int protocolVersion = encodedPackage.getInt();
    final byte[] capabilityBytes = new byte[PeerCapabilities.CAPABILITY_BYTES];
    encodedPackage.get(capabilityBytes, 0, capabilityBytes.length);
    if (encodedPackage.remaining() != 0) {
        throw new PackageEncodeException("Some bytes are left after decoding: " + encodedPackage.remaining());
    }
    final PeerCapabilities peerCapabilities = new PeerCapabilities(capabilityBytes);
    return new HelloRequest(sequenceNumber, protocolVersion, peerCapabilities);
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) PeerCapabilities(org.bboxdb.network.capabilities.PeerCapabilities)

Example 27 with PackageEncodeException

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

the class InsertTupleRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        final byte[] tupleAsByte = NetworkTupleEncoderDecoder.encode(tuple, table.getFullname());
        // Body length
        final long bodyLength = tupleAsByte.length;
        // Unrouted package
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        // Write tuple
        outputStream.write(tupleAsByte);
        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)

Example 28 with PackageEncodeException

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

the class KeepAliveRequest method decodeTuple.

/**
 * Decode the encoded package into a object
 *
 * @param encodedPackage
 * @return
 * @throws PackageEncodeException
 */
public static KeepAliveRequest decodeTuple(final ByteBuffer encodedPackage) throws PackageEncodeException {
    final short sequenceNumber = NetworkPackageDecoder.getRequestIDFromRequestPackage(encodedPackage);
    final boolean decodeResult = NetworkPackageDecoder.validateRequestPackageHeader(encodedPackage, NetworkConst.REQUEST_TYPE_KEEP_ALIVE);
    if (decodeResult == false) {
        throw new PackageEncodeException("Unable to decode package");
    }
    final List<Tuple> tuples = new ArrayList<>();
    final short tableLength = encodedPackage.getShort();
    // Unused
    encodedPackage.get();
    // Unused
    encodedPackage.get();
    final int elements = encodedPackage.getInt();
    final byte[] tableNameBytes = new byte[tableLength];
    encodedPackage.get(tableNameBytes, 0, tableNameBytes.length);
    final String tableName = new String(tableNameBytes);
    for (int i = 0; i < elements; i++) {
        final int keyLength = encodedPackage.getInt();
        final byte[] keyBytes = new byte[keyLength];
        encodedPackage.get(keyBytes, 0, keyBytes.length);
        final String key = new String(keyBytes);
        final int boundingBoxLength = encodedPackage.getInt();
        final byte[] boundingBoxBytes = new byte[boundingBoxLength];
        encodedPackage.get(boundingBoxBytes, 0, boundingBoxBytes.length);
        final BoundingBox boundingBox = BoundingBox.fromByteArray(boundingBoxBytes);
        final long version = encodedPackage.getLong();
        final Tuple tuple = new Tuple(key, boundingBox, "".getBytes(), version);
        tuples.add(tuple);
    }
    if (encodedPackage.remaining() != 0) {
        throw new PackageEncodeException("Some bytes are left after decoding: " + encodedPackage.remaining());
    }
    return new KeepAliveRequest(sequenceNumber, tableName, tuples);
}
Also used : BoundingBox(org.bboxdb.commons.math.BoundingBox) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) ArrayList(java.util.ArrayList) Tuple(org.bboxdb.storage.entity.Tuple)

Example 29 with PackageEncodeException

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

the class LockTupleRequest method decodeTuple.

/**
 * Decode the encoded package into a object
 *
 * @param encodedPackage
 * @return
 * @throws PackageEncodeException
 * @throws IOException
 */
public static LockTupleRequest decodeTuple(final ByteBuffer encodedPackage) throws PackageEncodeException, IOException {
    final short sequenceNumber = NetworkPackageDecoder.getRequestIDFromRequestPackage(encodedPackage);
    final boolean decodeResult = NetworkPackageDecoder.validateRequestPackageHeader(encodedPackage, NetworkConst.REQUEST_TYPE_LOCK_TUPLE);
    if (decodeResult == false) {
        throw new PackageEncodeException("Unable to decode package");
    }
    final short tablenameLength = encodedPackage.getShort();
    final short keyLength = encodedPackage.getShort();
    final long version = encodedPackage.getLong();
    // Tablename
    final byte[] tableBytes = new byte[tablenameLength];
    encodedPackage.get(tableBytes, 0, tableBytes.length);
    final String tablename = new String(tableBytes);
    // Key
    final byte[] keyBytes = new byte[keyLength];
    encodedPackage.get(keyBytes, 0, keyBytes.length);
    final String key = new String(keyBytes);
    if (encodedPackage.remaining() != 0) {
        throw new PackageEncodeException("Some bytes are left after decoding: " + encodedPackage.remaining());
    }
    final RoutingHeader routingHeader = NetworkPackageDecoder.getRoutingHeaderFromRequestPackage(encodedPackage);
    return new LockTupleRequest(sequenceNumber, routingHeader, tablename, key, version);
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) RoutingHeader(org.bboxdb.network.routing.RoutingHeader)

Example 30 with PackageEncodeException

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

the class NextPageRequest method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        final ByteBuffer bb = ByteBuffer.allocate(2);
        bb.order(Const.APPLICATION_BYTE_ORDER);
        bb.putShort((short) querySequence);
        // Calculate body length
        final long bodyLength = bb.capacity();
        final long headerLength = appendRequestPackageHeader(bodyLength, outputStream);
        // Write body
        outputStream.write(bb.array());
        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)

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