Search in sources :

Example 1 with PeerCapabilities

use of org.bboxdb.network.capabilities.PeerCapabilities in project bboxdb by jnidzwetzki.

the class HelloResponse method decodePackage.

/**
 * Decode the encoded package into a object
 *
 * @param encodedPackage
 * @return
 * @throws PackageEncodeException
 */
public static HelloResponse decodePackage(final ByteBuffer encodedPackage) throws PackageEncodeException {
    final short requestId = NetworkPackageDecoder.getRequestIDFromResponsePackage(encodedPackage);
    final boolean decodeResult = NetworkPackageDecoder.validateResponsePackageHeader(encodedPackage, NetworkConst.RESPONSE_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);
    peerCapabilities.freeze();
    return new HelloResponse(requestId, protocolVersion, peerCapabilities);
}
Also used : PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) PeerCapabilities(org.bboxdb.network.capabilities.PeerCapabilities)

Example 2 with PeerCapabilities

use of org.bboxdb.network.capabilities.PeerCapabilities 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 3 with PeerCapabilities

use of org.bboxdb.network.capabilities.PeerCapabilities in project bboxdb by jnidzwetzki.

the class TestNetworkClasses method encodeAndDecodeHeloResponse2.

/**
 * The the encoding and decoding of the response helo
 * @throws IOException
 * @throws PackageEncodeException
 */
@Test(timeout = 60000)
public void encodeAndDecodeHeloResponse2() throws IOException, PackageEncodeException {
    final PeerCapabilities peerCapabilities = new PeerCapabilities();
    peerCapabilities.setGZipCompression();
    final short sequenceNumber = sequenceNumberGenerator.getNextSequenceNummber();
    final HelloResponse helloPackage = new HelloResponse(sequenceNumber, 2, peerCapabilities);
    byte[] encodedVersion = networkPackageToByte(helloPackage);
    Assert.assertNotNull(encodedVersion);
    final ByteBuffer bb = NetworkPackageDecoder.encapsulateBytes(encodedVersion);
    final HelloResponse decodedPackage = HelloResponse.decodePackage(bb);
    Assert.assertEquals(helloPackage, decodedPackage);
    Assert.assertTrue(helloPackage.getPeerCapabilities().hasGZipCompression());
    Assert.assertTrue(decodedPackage.getPeerCapabilities().hasGZipCompression());
    Assert.assertEquals(helloPackage.hashCode(), decodedPackage.hashCode());
    Assert.assertEquals(helloPackage.toString(), decodedPackage.toString());
}
Also used : HelloResponse(org.bboxdb.network.packages.response.HelloResponse) PeerCapabilities(org.bboxdb.network.capabilities.PeerCapabilities) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with PeerCapabilities

use of org.bboxdb.network.capabilities.PeerCapabilities in project bboxdb by jnidzwetzki.

the class TestPeerCapabilities method testPeerCapabilitiesRO2.

/**
 * Test the peer capabilities
 */
@Test(expected = IllegalStateException.class)
public void testPeerCapabilitiesRO2() {
    final PeerCapabilities peerCapabilities = new PeerCapabilities();
    peerCapabilities.freeze();
    peerCapabilities.clearGZipCompression();
}
Also used : PeerCapabilities(org.bboxdb.network.capabilities.PeerCapabilities) Test(org.junit.Test)

Example 5 with PeerCapabilities

use of org.bboxdb.network.capabilities.PeerCapabilities in project bboxdb by jnidzwetzki.

the class TestPeerCapabilities method testPeerCapabilities2.

/**
 * Test the peer capabilities
 */
@Test(timeout = 60000)
public void testPeerCapabilities2() {
    final PeerCapabilities peerCapabilities1 = new PeerCapabilities();
    final PeerCapabilities peerCapabilities2 = new PeerCapabilities();
    Assert.assertTrue(peerCapabilities1.equals(peerCapabilities2));
    Assert.assertEquals(peerCapabilities1.hashCode(), peerCapabilities2.hashCode());
}
Also used : PeerCapabilities(org.bboxdb.network.capabilities.PeerCapabilities) Test(org.junit.Test)

Aggregations

PeerCapabilities (org.bboxdb.network.capabilities.PeerCapabilities)11 Test (org.junit.Test)9 ByteBuffer (java.nio.ByteBuffer)5 HelloResponse (org.bboxdb.network.packages.response.HelloResponse)3 PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)2 HelloRequest (org.bboxdb.network.packages.request.HelloRequest)2 InputStream (java.io.InputStream)1 CompressionEnvelopeResponse (org.bboxdb.network.packages.response.CompressionEnvelopeResponse)1