Search in sources :

Example 1 with HelloResponse

use of org.bboxdb.network.packages.response.HelloResponse 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 2 with HelloResponse

use of org.bboxdb.network.packages.response.HelloResponse in project bboxdb by jnidzwetzki.

the class BBoxDBConnection method runHandshake.

/**
 * Run the handshake with the server
 * @throws ExecutionException
 * @throws InterruptedException
 */
private void runHandshake() throws Exception {
    if (!connectionState.isInStartingState()) {
        logger.error("Handshaking called in wrong state: {}", connectionState);
    }
    // Capabilities are reported to server; now freeze client capabilities.
    clientCapabilities.freeze();
    final NetworkOperationFuture operationFuture = new NetworkOperationFuture(this, () -> {
        return new HelloRequest(getNextSequenceNumber(), NetworkConst.PROTOCOL_VERSION, clientCapabilities);
    });
    final HelloFuture helloFuture = new HelloFuture(operationFuture);
    helloFuture.waitForAll();
    if (operationFuture.isFailed()) {
        throw new Exception("Got an error during handshake");
    }
    final HelloResponse helloResponse = helloFuture.get(0);
    connectionCapabilities = helloResponse.getPeerCapabilities();
    connectionState.dispatchToRunning();
    logger.debug("Handshaking with {} done", getConnectionName());
    flushHandler = new ConnectionFlushRunnable(this);
    flushThread = new Thread(flushHandler);
    flushThread.setName("Flush thread for: " + getConnectionName());
    flushThread.start();
    mainteinanceHandler = new ConnectionMainteinanceRunnable(this);
    mainteinanceThread = new Thread(mainteinanceHandler);
    mainteinanceThread.setName("Connection mainteinace thread for: " + getConnectionName());
    mainteinanceThread.start();
}
Also used : HelloFuture(org.bboxdb.network.client.future.HelloFuture) HelloResponse(org.bboxdb.network.packages.response.HelloResponse) NetworkOperationFuture(org.bboxdb.network.client.future.NetworkOperationFuture) HelloRequest(org.bboxdb.network.packages.request.HelloRequest) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) SocketException(java.net.SocketException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with HelloResponse

use of org.bboxdb.network.packages.response.HelloResponse in project bboxdb by jnidzwetzki.

the class HelloHandler method handleServerResult.

/**
 * Handle the hello result package
 * @return
 */
@Override
public boolean handleServerResult(final BBoxDBConnection bBoxDBConnection, final ByteBuffer encodedPackage, final NetworkOperationFuture future) throws PackageEncodeException {
    if (logger.isDebugEnabled()) {
        logger.debug("Handle helo package");
    }
    final HelloResponse helloResponse = HelloResponse.decodePackage(encodedPackage);
    if (future != null) {
        future.setOperationResult(helloResponse);
        future.fireCompleteEvent();
    }
    return true;
}
Also used : HelloResponse(org.bboxdb.network.packages.response.HelloResponse)

Example 4 with HelloResponse

use of org.bboxdb.network.packages.response.HelloResponse in project bboxdb by jnidzwetzki.

the class TestNetworkClasses method encodeAndDecodeHeloResponse1.

/**
 * The the encoding and decoding of the response helo
 * @throws IOException
 * @throws PackageEncodeException
 */
@Test(timeout = 60000)
public void encodeAndDecodeHeloResponse1() throws IOException, PackageEncodeException {
    final PeerCapabilities peerCapabilities = new PeerCapabilities();
    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.assertFalse(decodedPackage.getPeerCapabilities().hasGZipCompression());
    Assert.assertFalse(helloPackage.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 5 with HelloResponse

use of org.bboxdb.network.packages.response.HelloResponse in project bboxdb by jnidzwetzki.

the class TestNetworkClasses method testCompressionReponse2.

/**
 * Test the compression response
 * @throws IOException
 * @throws PackageEncodeException
 */
@Test(timeout = 60000)
public void testCompressionReponse2() throws IOException, PackageEncodeException {
    final PeerCapabilities peerCapabilities = new PeerCapabilities();
    peerCapabilities.setGZipCompression();
    final short sequenceNumber = sequenceNumberGenerator.getNextSequenceNummber();
    final HelloResponse helloPackage = new HelloResponse(sequenceNumber, 2, peerCapabilities);
    final CompressionEnvelopeResponse compressionEnvelopeResponse = new CompressionEnvelopeResponse(NetworkConst.COMPRESSION_TYPE_GZIP, Arrays.asList(helloPackage));
    final byte[] encodedPackage = networkPackageToByte(compressionEnvelopeResponse);
    Assert.assertNotNull(encodedPackage);
    final ByteBuffer bb = NetworkPackageDecoder.encapsulateBytes(encodedPackage);
    final InputStream uncompressedByteStream = CompressionEnvelopeResponse.decodePackage(bb);
    final byte[] uncompressedBytes = ByteStreams.toByteArray(uncompressedByteStream);
    final ByteBuffer uncompressedByteBuffer = NetworkPackageDecoder.encapsulateBytes(uncompressedBytes);
    final HelloResponse decodedPackage = HelloResponse.decodePackage(uncompressedByteBuffer);
    Assert.assertEquals(helloPackage, decodedPackage);
    Assert.assertTrue(helloPackage.getPeerCapabilities().hasGZipCompression());
    Assert.assertTrue(decodedPackage.getPeerCapabilities().hasGZipCompression());
    Assert.assertEquals(helloPackage.toString(), decodedPackage.toString());
}
Also used : CompressionEnvelopeResponse(org.bboxdb.network.packages.response.CompressionEnvelopeResponse) InputStream(java.io.InputStream) HelloResponse(org.bboxdb.network.packages.response.HelloResponse) PeerCapabilities(org.bboxdb.network.capabilities.PeerCapabilities) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

HelloResponse (org.bboxdb.network.packages.response.HelloResponse)6 ByteBuffer (java.nio.ByteBuffer)3 PeerCapabilities (org.bboxdb.network.capabilities.PeerCapabilities)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)2 HelloRequest (org.bboxdb.network.packages.request.HelloRequest)2 InputStream (java.io.InputStream)1 SocketException (java.net.SocketException)1 ExecutionException (java.util.concurrent.ExecutionException)1 HelloFuture (org.bboxdb.network.client.future.HelloFuture)1 NetworkOperationFuture (org.bboxdb.network.client.future.NetworkOperationFuture)1 CompressionEnvelopeResponse (org.bboxdb.network.packages.response.CompressionEnvelopeResponse)1 ErrorResponse (org.bboxdb.network.packages.response.ErrorResponse)1