public DaemonConnection connect()

in client/src/main/java/org/mvndaemon/mvnd/client/DaemonConnector.java [570:619]


    public DaemonConnection connect(String str, byte[] token) throws DaemonException.ConnectException {
        SocketAddress address = SocketFamily.fromString(str);
        try {
            LOGGER.debug("Trying to connect to address {}.", address);
            SocketFamily family = SocketFamily.familyOf(address);
            SocketChannel socketChannel = family.openSocket();
            socketChannel.configureBlocking(false);
            boolean connected = socketChannel.connect(address);
            if (!connected) {
                long t0 = System.currentTimeMillis();
                long t1 = t0
                        + parameters
                                .property(Environment.MVND_SOCKET_CONNECT_TIMEOUT)
                                .asDuration()
                                .toMillis();
                while (!connected && t0 < t1) {
                    Thread.sleep(10);
                    connected = socketChannel.finishConnect();
                    if (!connected) {
                        t0 = System.currentTimeMillis();
                    }
                }
                if (!connected) {
                    throw new IOException("Timeout");
                }
            }
            socketChannel.configureBlocking(true);

            //            Socket socket = socketChannel.socket();
            //            socket.connect(address, CONNECT_TIMEOUT);
            //            if (socket.getLocalSocketAddress().equals(socket.getRemoteSocketAddress())) {
            //                socketChannel.close();
            //                throw new DaemonException.ConnectException(String.format("Socket connected to itself on
            // %s.", address));
            //            }
            LOGGER.debug("Connected to address {}.", socketChannel.getRemoteAddress());

            ByteBuffer tokenBuffer = ByteBuffer.wrap(token);
            do {
                socketChannel.write(tokenBuffer);
            } while (tokenBuffer.remaining() > 0);
            LOGGER.debug("Exchanged token successfully");

            return new DaemonConnection(socketChannel);
        } catch (DaemonException.ConnectException e) {
            throw e;
        } catch (Exception e) {
            throw new DaemonException.ConnectException(String.format("Could not connect to server %s.", address), e);
        }
    }