Search in sources :

Example 1 with VersionNegotiator

use of org.apache.nifi.remote.VersionNegotiator in project nifi by apache.

the class DistributedSetCacheClientService method leaseCommsSession.

private CommsSession leaseCommsSession() throws IOException {
    CommsSession session = queue.poll();
    if (session != null && !session.isClosed()) {
        return session;
    }
    session = createCommsSession(configContext);
    final VersionNegotiator versionNegotiator = new StandardVersionNegotiator(1);
    try {
        ProtocolHandshake.initiateHandshake(session.getInputStream(), session.getOutputStream(), versionNegotiator);
        session.setProtocolVersion(versionNegotiator.getVersion());
    } catch (final HandshakeException e) {
        IOUtils.closeQuietly(session);
        throw new IOException(e);
    }
    return session;
}
Also used : VersionNegotiator(org.apache.nifi.remote.VersionNegotiator) StandardVersionNegotiator(org.apache.nifi.remote.StandardVersionNegotiator) StandardVersionNegotiator(org.apache.nifi.remote.StandardVersionNegotiator) IOException(java.io.IOException) HandshakeException(org.apache.nifi.distributed.cache.protocol.exception.HandshakeException)

Example 2 with VersionNegotiator

use of org.apache.nifi.remote.VersionNegotiator in project nifi by apache.

the class DistributedMapCacheClientService method leaseCommsSession.

private CommsSession leaseCommsSession() throws IOException {
    CommsSession session = queue.poll();
    if (session != null && !session.isClosed()) {
        return session;
    }
    session = createCommsSession(configContext);
    final VersionNegotiator versionNegotiator = new StandardVersionNegotiator(3, 2, 1);
    try {
        ProtocolHandshake.initiateHandshake(session.getInputStream(), session.getOutputStream(), versionNegotiator);
        session.setProtocolVersion(versionNegotiator.getVersion());
    } catch (final HandshakeException e) {
        try {
            session.close();
        } catch (final IOException ioe) {
        }
        throw new IOException(e);
    }
    return session;
}
Also used : VersionNegotiator(org.apache.nifi.remote.VersionNegotiator) StandardVersionNegotiator(org.apache.nifi.remote.StandardVersionNegotiator) StandardVersionNegotiator(org.apache.nifi.remote.StandardVersionNegotiator) IOException(java.io.IOException) HandshakeException(org.apache.nifi.distributed.cache.protocol.exception.HandshakeException)

Example 3 with VersionNegotiator

use of org.apache.nifi.remote.VersionNegotiator in project nifi by apache.

the class AbstractCacheServer method start.

@Override
public void start() throws IOException {
    serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.configureBlocking(true);
    serverSocketChannel.bind(new InetSocketAddress(port));
    final Runnable runnable = new Runnable() {

        @Override
        public void run() {
            while (true) {
                final SocketChannel socketChannel;
                try {
                    socketChannel = serverSocketChannel.accept();
                    logger.debug("Connected to {}", new Object[] { socketChannel });
                } catch (final IOException e) {
                    if (!stopped) {
                        logger.error("{} unable to accept connection from remote peer due to {}", this, e.toString());
                        if (logger.isDebugEnabled()) {
                            logger.error("", e);
                        }
                    }
                    return;
                }
                final Runnable processInputRunnable = new Runnable() {

                    @Override
                    public void run() {
                        final InputStream rawInputStream;
                        final OutputStream rawOutputStream;
                        final String peer = socketChannel.socket().getInetAddress().getHostName();
                        try {
                            if (sslContext == null) {
                                rawInputStream = new SocketChannelInputStream(socketChannel);
                                rawOutputStream = new SocketChannelOutputStream(socketChannel);
                            } else {
                                final SSLSocketChannel sslSocketChannel = new SSLSocketChannel(sslContext, socketChannel, false);
                                sslSocketChannel.connect();
                                rawInputStream = new SSLSocketChannelInputStream(sslSocketChannel);
                                rawOutputStream = new SSLSocketChannelOutputStream(sslSocketChannel);
                            }
                        } catch (IOException e) {
                            logger.error("Cannot create input and/or output streams for {}", new Object[] { identifier }, e);
                            if (logger.isDebugEnabled()) {
                                logger.error("", e);
                            }
                            try {
                                socketChannel.close();
                            } catch (IOException swallow) {
                            }
                            return;
                        }
                        try (final InputStream in = new BufferedInputStream(rawInputStream);
                            final OutputStream out = new BufferedOutputStream(rawOutputStream)) {
                            final VersionNegotiator versionNegotiator = getVersionNegotiator();
                            ProtocolHandshake.receiveHandshake(in, out, versionNegotiator);
                            boolean continueComms = true;
                            while (continueComms) {
                                continueComms = listen(in, out, versionNegotiator.getVersion());
                            }
                            // client has issued 'close'
                            logger.debug("Client issued close on {}", new Object[] { socketChannel });
                        } catch (final SocketTimeoutException e) {
                            logger.debug("30 sec timeout reached", e);
                        } catch (final IOException | HandshakeException e) {
                            if (!stopped) {
                                logger.error("{} unable to communicate with remote peer {} due to {}", new Object[] { this, peer, e.toString() });
                                if (logger.isDebugEnabled()) {
                                    logger.error("", e);
                                }
                            }
                        } finally {
                            processInputThreads.remove(Thread.currentThread());
                        }
                    }
                };
                final Thread processInputThread = new Thread(processInputRunnable);
                processInputThread.setName("Distributed Cache Server Communications Thread: " + identifier);
                processInputThread.setDaemon(true);
                processInputThread.start();
                processInputThreads.add(processInputThread);
            }
        }
    };
    final Thread thread = new Thread(runnable);
    thread.setDaemon(true);
    thread.setName("Distributed Cache Server: " + identifier);
    thread.start();
}
Also used : SSLSocketChannel(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SSLSocketChannelOutputStream(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelOutputStream) SocketChannelOutputStream(org.apache.nifi.remote.io.socket.SocketChannelOutputStream) SSLSocketChannelOutputStream(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelOutputStream) SSLSocketChannelInputStream(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelInputStream) InetSocketAddress(java.net.InetSocketAddress) BufferedInputStream(java.io.BufferedInputStream) SocketChannelInputStream(org.apache.nifi.remote.io.socket.SocketChannelInputStream) SSLSocketChannelInputStream(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) SocketChannelOutputStream(org.apache.nifi.remote.io.socket.SocketChannelOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) SSLSocketChannelOutputStream(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelOutputStream) SocketChannelInputStream(org.apache.nifi.remote.io.socket.SocketChannelInputStream) SSLSocketChannelInputStream(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelInputStream) IOException(java.io.IOException) SSLSocketChannel(org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel) StandardVersionNegotiator(org.apache.nifi.remote.StandardVersionNegotiator) VersionNegotiator(org.apache.nifi.remote.VersionNegotiator) SocketTimeoutException(java.net.SocketTimeoutException) BufferedInputStream(java.io.BufferedInputStream) BufferedOutputStream(java.io.BufferedOutputStream) HandshakeException(org.apache.nifi.distributed.cache.protocol.exception.HandshakeException)

Example 4 with VersionNegotiator

use of org.apache.nifi.remote.VersionNegotiator in project nifi by apache.

the class DataTransferResource method initiateServerProtocol.

private HttpFlowFileServerProtocol initiateServerProtocol(final HttpServletRequest req, final Peer peer, final Integer transportProtocolVersion) throws IOException {
    // Switch transaction protocol version based on transport protocol version.
    TransportProtocolVersionNegotiator negotiatedTransportProtocolVersion = new TransportProtocolVersionNegotiator(transportProtocolVersion);
    VersionNegotiator versionNegotiator = new StandardVersionNegotiator(negotiatedTransportProtocolVersion.getTransactionProtocolVersion());
    final String dataTransferUrl = req.getRequestURL().toString();
    ((HttpCommunicationsSession) peer.getCommunicationsSession()).setDataTransferUrl(dataTransferUrl);
    HttpFlowFileServerProtocol serverProtocol = getHttpFlowFileServerProtocol(versionNegotiator);
    HttpRemoteSiteListener.getInstance(nifiProperties).setupServerProtocol(serverProtocol);
    serverProtocol.handshake(peer);
    return serverProtocol;
}
Also used : TransportProtocolVersionNegotiator(org.apache.nifi.remote.client.http.TransportProtocolVersionNegotiator) VersionNegotiator(org.apache.nifi.remote.VersionNegotiator) TransportProtocolVersionNegotiator(org.apache.nifi.remote.client.http.TransportProtocolVersionNegotiator) StandardVersionNegotiator(org.apache.nifi.remote.StandardVersionNegotiator) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) StandardHttpFlowFileServerProtocol(org.apache.nifi.remote.protocol.http.StandardHttpFlowFileServerProtocol) HttpFlowFileServerProtocol(org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol) StandardVersionNegotiator(org.apache.nifi.remote.StandardVersionNegotiator)

Example 5 with VersionNegotiator

use of org.apache.nifi.remote.VersionNegotiator in project nifi by apache.

the class TestDataTransferResource method getDataTransferResource.

private DataTransferResource getDataTransferResource() {
    final NiFiServiceFacade serviceFacade = mock(NiFiServiceFacade.class);
    final HttpFlowFileServerProtocol serverProtocol = mock(HttpFlowFileServerProtocol.class);
    final DataTransferResource resource = new DataTransferResource(NiFiProperties.createBasicNiFiProperties(null, null)) {

        @Override
        protected void authorizeDataTransfer(AuthorizableLookup lookup, ResourceType resourceType, String identifier) {
        }

        @Override
        HttpFlowFileServerProtocol getHttpFlowFileServerProtocol(VersionNegotiator versionNegotiator) {
            return serverProtocol;
        }
    };
    resource.setProperties(NiFiProperties.createBasicNiFiProperties(null, null));
    resource.setServiceFacade(serviceFacade);
    return resource;
}
Also used : AuthorizableLookup(org.apache.nifi.authorization.AuthorizableLookup) NiFiServiceFacade(org.apache.nifi.web.NiFiServiceFacade) VersionNegotiator(org.apache.nifi.remote.VersionNegotiator) HttpFlowFileServerProtocol(org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol) ResourceType(org.apache.nifi.authorization.resource.ResourceType)

Aggregations

VersionNegotiator (org.apache.nifi.remote.VersionNegotiator)5 StandardVersionNegotiator (org.apache.nifi.remote.StandardVersionNegotiator)4 IOException (java.io.IOException)3 HandshakeException (org.apache.nifi.distributed.cache.protocol.exception.HandshakeException)3 HttpFlowFileServerProtocol (org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 AuthorizableLookup (org.apache.nifi.authorization.AuthorizableLookup)1 ResourceType (org.apache.nifi.authorization.resource.ResourceType)1 TransportProtocolVersionNegotiator (org.apache.nifi.remote.client.http.TransportProtocolVersionNegotiator)1 HttpCommunicationsSession (org.apache.nifi.remote.io.http.HttpCommunicationsSession)1 SocketChannelInputStream (org.apache.nifi.remote.io.socket.SocketChannelInputStream)1 SocketChannelOutputStream (org.apache.nifi.remote.io.socket.SocketChannelOutputStream)1 SSLSocketChannel (org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel)1