All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.malinskiy.adam.transport.vertx.VertxSocketFactory.kt Maven / Gradle / Ivy

There is a newer version: 0.5.8
Show newest version
/*
 * Copyright (C) 2021 Anton Malinskiy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.malinskiy.adam.transport.vertx

import com.malinskiy.adam.transport.Socket
import com.malinskiy.adam.transport.SocketFactory
import io.vertx.core.Vertx
import io.vertx.core.net.NetClientOptions
import io.vertx.core.net.SocketAddress
import io.vertx.kotlin.coroutines.await
import java.net.InetSocketAddress
import java.util.concurrent.atomic.AtomicBoolean

class VertxSocketFactory(
    private val connectTimeout: Long = 10_000,
    private val idleTimeout: Long = 30_000
) : SocketFactory {
    private val vertx by lazy {
        Vertx.vertx().also { initialized.set(true) }
    }
    private val initialized = AtomicBoolean(false)

    override suspend fun tcp(socketAddress: InetSocketAddress, connectTimeout: Long?, idleTimeout: Long?): Socket {
        val vertxSocket = VertxSocket(SocketAddress.inetSocketAddress(socketAddress), NetClientOptions().apply {
            setConnectTimeout((connectTimeout ?: [email protected]).toTimeoutInt())
            setIdleTimeout((idleTimeout ?: [email protected]).toTimeoutInt())
        })
        val id = vertx.deployVerticle(vertxSocket).await()
        vertxSocket.id = id
        return vertxSocket
    }

    override fun close() {
        if (initialized.get()) {
            vertx.close()
        }
    }

    private fun Long.toTimeoutInt(): Int {
        val toInt = toInt()
        return if (toInt < 0) Int.MAX_VALUE
        else toInt
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy