Search in sources :

Example 1 with HelloRequest

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

use of org.bboxdb.network.packages.request.HelloRequest in project bboxdb by jnidzwetzki.

the class TestNetworkClasses method encodeAndDecodeHeloRequest2.

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

Example 3 with HelloRequest

use of org.bboxdb.network.packages.request.HelloRequest in project bboxdb by jnidzwetzki.

the class TestNetworkClasses method encodeAndDecodeHeloRequest1.

/**
 * The the encoding and decoding of the request helo
 * @throws IOException
 * @throws PackageEncodeException
 */
@Test(timeout = 60000)
public void encodeAndDecodeHeloRequest1() throws IOException, PackageEncodeException {
    final PeerCapabilities peerCapabilities = new PeerCapabilities();
    final short sequenceNumber = sequenceNumberGenerator.getNextSequenceNummber();
    final HelloRequest helloPackage = new HelloRequest(sequenceNumber, 2, peerCapabilities);
    byte[] encodedVersion = networkPackageToByte(helloPackage);
    Assert.assertNotNull(encodedVersion);
    final ByteBuffer bb = NetworkPackageDecoder.encapsulateBytes(encodedVersion);
    final HelloRequest decodedPackage = HelloRequest.decodeRequest(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 : PeerCapabilities(org.bboxdb.network.capabilities.PeerCapabilities) HelloRequest(org.bboxdb.network.packages.request.HelloRequest) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with HelloRequest

use of org.bboxdb.network.packages.request.HelloRequest in project bboxdb by jnidzwetzki.

the class HandshakeHandler method handleRequest.

/**
 * Handle the handshake request
 */
@Override
public boolean handleRequest(final ByteBuffer encodedPackage, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
    logger.info("Handshaking with: {}", clientConnectionHandler.clientSocket.getInetAddress());
    try {
        final HelloRequest heloRequest = HelloRequest.decodeRequest(encodedPackage);
        clientConnectionHandler.setConnectionCapabilities(heloRequest.getPeerCapabilities());
        final HelloResponse responsePackage = new HelloResponse(packageSequence, NetworkConst.PROTOCOL_VERSION, clientConnectionHandler.getConnectionCapabilities());
        clientConnectionHandler.writeResultPackage(responsePackage);
        clientConnectionHandler.setConnectionStateToOpen();
        return true;
    } catch (Exception e) {
        logger.warn("Error while reading network package", e);
        final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION);
        clientConnectionHandler.writeResultPackage(responsePackage);
        return false;
    }
}
Also used : HelloResponse(org.bboxdb.network.packages.response.HelloResponse) HelloRequest(org.bboxdb.network.packages.request.HelloRequest) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) IOException(java.io.IOException) ErrorResponse(org.bboxdb.network.packages.response.ErrorResponse)

Aggregations

HelloRequest (org.bboxdb.network.packages.request.HelloRequest)4 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 PeerCapabilities (org.bboxdb.network.capabilities.PeerCapabilities)2 PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)2 HelloResponse (org.bboxdb.network.packages.response.HelloResponse)2 Test (org.junit.Test)2 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 ErrorResponse (org.bboxdb.network.packages.response.ErrorResponse)1