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