<commit_before>package com.fireflysource.net.tcp.aio

import com.fireflysource.common.sys.SystemLogger
import com.fireflysource.net.tcp.TcpClient
import com.fireflysource.net.tcp.TcpConnection
import com.fireflysource.net.tcp.secure.SecureEngineFactory
import com.fireflysource.net.tcp.secure.conscrypt.NoCheckConscryptSSLContextFactory
import java.net.SocketAddress
import java.net.StandardSocketOptions
import java.nio.channels.AsynchronousSocketChannel
import java.nio.channels.CompletionHandler
import java.util.concurrent.CompletableFuture


/**
 * @author <NAME>
 */
class AioTcpClient(val config: TcpConfig = TcpConfig()) : AbstractAioTcpChannelGroup(), TcpClient {

    companion object {
        private val log = SystemLogger.create(AioTcpClient::class.java)
    }

    private var secureEngineFactory: SecureEngineFactory = NoCheckConscryptSSLContextFactory()
    private var supportedProtocols: List<String> = emptyList()

    init {
        start()
    }

    override fun secureEngineFactory(secureEngineFactory: SecureEngineFactory): TcpClient {
        this.secureEngineFactory = secureEngineFactory
        return this
    }

    override fun enableSecureConnection(): TcpClient {
        config.enableSecureConnection = true
        return this
    }

    override fun connect(address: SocketAddress): CompletableFuture<TcpConnection> {
        val future = CompletableFuture<TcpConnection>()

        val socketChannel = AsynchronousSocketChannel.open(group)
        socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true)
        socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true)
        socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, false)
        socketChannel.connect(address, id.getAndIncrement(), object : CompletionHandler<Void?, Int> {

            override fun completed(result: Void?, connectionId: Int) {
                try {
                    val tcpConnection = if (config.enableSecureConnection) {
                        val tcpConnection = AioTcpConnection(
                            connectionId, socketChannel,
                            config.timeout,
                            getMessageThread(connectionId)
                                                            )
                        AioSecureTcpConnection(
                            tcpConnection,
                            secureEngineFactory.create(tcpConnection, true, supportedProtocols),
                            getMessageThread(connectionId)
                                              )
                    } else {
                        AioTcpConnection(connectionId, socketChannel, config.timeout, getMessageThread(connectionId))
                    }
                    future.complete(tcpConnection)
                } catch (e: Exception) {
                    log.warn(e) {"connecting exception. $connectionId"}
                }
            }

            override fun failed(t: Throwable?, connectionId: Int) {
                log.warn(t) { "connecting exception. $connectionId" }
                future.completeExceptionally(t)
            }
        })
        return future
    }

    override fun getThreadName() = "aio-tcp-client"
}<commit_msg>[fix]: fix the server only accepts one connection<commit_after>package com.fireflysource.net.tcp.aio

import com.fireflysource.common.sys.SystemLogger
import com.fireflysource.net.tcp.TcpClient
import com.fireflysource.net.tcp.TcpConnection
import com.fireflysource.net.tcp.secure.SecureEngineFactory
import com.fireflysource.net.tcp.secure.conscrypt.NoCheckConscryptSSLContextFactory
import java.net.SocketAddress
import java.net.StandardSocketOptions
import java.nio.channels.AsynchronousSocketChannel
import java.nio.channels.CompletionHandler
import java.util.concurrent.CompletableFuture


/**
 * @author <NAME>
 */
class AioTcpClient(val config: TcpConfig = TcpConfig()) : AbstractAioTcpChannelGroup(), TcpClient {

    companion object {
        private val log = SystemLogger.create(AioTcpClient::class.java)
    }

    private var secureEngineFactory: SecureEngineFactory = NoCheckConscryptSSLContextFactory()
    private var supportedProtocols: List<String> = emptyList()

    init {
        start()
    }

    override fun secureEngineFactory(secureEngineFactory: SecureEngineFactory): TcpClient {
        this.secureEngineFactory = secureEngineFactory
        return this
    }

    override fun enableSecureConnection(): TcpClient {
        config.enableSecureConnection = true
        return this
    }

    override fun connect(address: SocketAddress): CompletableFuture<TcpConnection> {
        val future = CompletableFuture<TcpConnection>()

        val socketChannel = AsynchronousSocketChannel.open(group)
        socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true)
        socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true)
        socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, false)
        socketChannel.connect(address, id.getAndIncrement(), object : CompletionHandler<Void?, Int> {

            override fun completed(result: Void?, connectionId: Int) {
                try {
                    val tcpConnection = if (config.enableSecureConnection) {
                        val tcpConnection = AioTcpConnection(
                            connectionId, socketChannel,
                            config.timeout,
                            getMessageThread(connectionId)
                                                            )
                        AioSecureTcpConnection(
                            tcpConnection,
                            secureEngineFactory.create(tcpConnection, true, supportedProtocols),
                            getMessageThread(connectionId)
                                              )
                    } else {
                        AioTcpConnection(connectionId, socketChannel, config.timeout, getMessageThread(connectionId))
                    }
                    future.complete(tcpConnection)
                } catch (e: Exception) {
                    log.warn(e) { "connecting exception. $connectionId" }
                }
            }

            override fun failed(t: Throwable?, connectionId: Int) {
                log.warn(t) { "connecting exception. $connectionId" }
                future.completeExceptionally(t)
            }
        })
        return future
    }

    override fun getThreadName() = "aio-tcp-client"
}
{'fraction_non_alphanumeric': 0.07418260333127699, 'fraction_numerical': 0.0, 'mean_word_length': 2.2040513833992095, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6542998', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1539, 'n_words': 291}