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