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);
}
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);
}
}
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);
}
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);
}
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);
}
}
Aggregations