Search in sources :

Example 1 with HandshakeProperty

use of org.apache.nifi.remote.protocol.HandshakeProperty in project nifi by apache.

the class SocketClientProtocol method handshake.

public void handshake(final Peer peer, final String destinationId) throws IOException, HandshakeException {
    if (handshakeComplete) {
        throw new IllegalStateException("Handshake has already been completed");
    }
    commsIdentifier = UUID.randomUUID().toString();
    logger.debug("{} handshaking with {}", this, peer);
    final Map<HandshakeProperty, String> properties = new HashMap<>();
    properties.put(HandshakeProperty.GZIP, String.valueOf(useCompression));
    if (destinationId != null) {
        properties.put(HandshakeProperty.PORT_IDENTIFIER, destinationId);
    }
    properties.put(HandshakeProperty.REQUEST_EXPIRATION_MILLIS, String.valueOf(timeoutMillis));
    if (versionNegotiator.getVersion() >= 5) {
        if (batchCount > 0) {
            properties.put(HandshakeProperty.BATCH_COUNT, String.valueOf(batchCount));
        }
        if (batchSize > 0L) {
            properties.put(HandshakeProperty.BATCH_SIZE, String.valueOf(batchSize));
        }
        if (batchMillis > 0L) {
            properties.put(HandshakeProperty.BATCH_DURATION, String.valueOf(batchMillis));
        }
    }
    final CommunicationsSession commsSession = peer.getCommunicationsSession();
    commsSession.setTimeout(timeoutMillis);
    final DataInputStream dis = new DataInputStream(commsSession.getInput().getInputStream());
    final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream());
    dos.writeUTF(commsIdentifier);
    if (versionNegotiator.getVersion() >= 3) {
        dos.writeUTF(peer.getUrl());
        transitUriPrefix = peer.getUrl();
        if (!transitUriPrefix.endsWith("/")) {
            transitUriPrefix = transitUriPrefix + "/";
        }
    }
    logger.debug("Handshaking with properties {}", properties);
    dos.writeInt(properties.size());
    for (final Map.Entry<HandshakeProperty, String> entry : properties.entrySet()) {
        dos.writeUTF(entry.getKey().name());
        dos.writeUTF(entry.getValue());
    }
    dos.flush();
    try {
        handshakeResponse = Response.read(dis);
    } catch (final ProtocolException e) {
        throw new HandshakeException(e);
    }
    switch(handshakeResponse.getCode()) {
        case PORT_NOT_IN_VALID_STATE:
        case UNKNOWN_PORT:
        case PORTS_DESTINATION_FULL:
            break;
        case PROPERTIES_OK:
            readyForFileTransfer = true;
            break;
        default:
            logger.error("{} received unexpected response {} from {} when negotiating Codec", new Object[] { this, handshakeResponse, peer });
            peer.close();
            throw new HandshakeException("Received unexpected response " + handshakeResponse);
    }
    logger.debug("{} Finished handshake with {}", this, peer);
    handshakeComplete = true;
}
Also used : ProtocolException(org.apache.nifi.remote.exception.ProtocolException) HandshakeProperty(org.apache.nifi.remote.protocol.HandshakeProperty) HashMap(java.util.HashMap) DataOutputStream(java.io.DataOutputStream) CommunicationsSession(org.apache.nifi.remote.protocol.CommunicationsSession) DataInputStream(java.io.DataInputStream) HashMap(java.util.HashMap) Map(java.util.Map) HandshakeException(org.apache.nifi.remote.exception.HandshakeException)

Aggregations

DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HandshakeException (org.apache.nifi.remote.exception.HandshakeException)1 ProtocolException (org.apache.nifi.remote.exception.ProtocolException)1 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)1 HandshakeProperty (org.apache.nifi.remote.protocol.HandshakeProperty)1