Skip to content

Commit

Permalink
ref: Move Harvesters to kotlin. (#2081)
Browse files Browse the repository at this point in the history
* ref: Move Harvesters to kotlin.
  • Loading branch information
bgrozev authored Jan 23, 2024
1 parent b2d4229 commit 57abe58
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 153 deletions.
147 changes: 0 additions & 147 deletions jvb/src/main/java/org/jitsi/videobridge/ice/Harvesters.java

This file was deleted.

6 changes: 4 additions & 2 deletions jvb/src/main/kotlin/org/jitsi/videobridge/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ fun main() {
}

startIce4j()
Harvesters.initializeStaticConfiguration()

// Initialize, binding on the main ICE port.
Harvesters.init()

XmppStringPrepUtil.setMaxCacheSizes(XmppClientConnectionConfig.config.jidCacheSize)
PacketQueue.setEnableStatisticsDefault(true)
Expand Down Expand Up @@ -219,5 +221,5 @@ private fun startIce4j() {

private fun stopIce4j() {
// Shut down harvesters.
Harvesters.closeStaticConfiguration()
Harvesters.close()
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class JvbHealthChecker : HealthCheckService {
if (config.requireStun && MappingCandidateHarvesters.stunDiscoveryFailed) {
return Result(success = false, message = "Address discovery through STUN failed")
}
if (!Harvesters.isHealthy()) {
if (!Harvesters.INSTANCE.healthy) {
return Result(success = false, message = "Failed to bind single-port")
}

Expand All @@ -57,7 +57,7 @@ class JvbHealthChecker : HealthCheckService {
}

private fun hasValidAddress(): Boolean {
if (Harvesters.singlePortHarvesters?.any { it.localAddress.address.isValid() } == true) {
if (Harvesters.INSTANCE.singlePortHarvesters.any { it.localAddress.address.isValid() }) {
return true
}
if (MappingCandidateHarvesters.getHarvesters().any { it.mask?.address?.isValid() == true }) {
Expand Down
72 changes: 72 additions & 0 deletions jvb/src/main/kotlin/org/jitsi/videobridge/ice/Harvesters.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright @ 2018 - present 8x8, Inc.
*
* 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 org.jitsi.videobridge.ice

import org.ice4j.ice.harvest.SinglePortUdpHarvester
import org.ice4j.ice.harvest.TcpHarvester
import org.jitsi.utils.logging2.createLogger
import java.io.IOException

class Harvesters private constructor(
val tcpHarvester: TcpHarvester?,
val singlePortHarvesters: List<SinglePortUdpHarvester>
) {
/* We're unhealthy if there are no single port harvesters. */
val healthy: Boolean
get() = singlePortHarvesters.isNotEmpty()

private fun close() {
singlePortHarvesters.forEach { it.close() }
tcpHarvester?.close()
}

companion object {
private val logger = createLogger()

fun init() {
// Trigger the lazy init.
INSTANCE
}

fun close() = INSTANCE.close()

val INSTANCE: Harvesters by lazy {
val singlePortHarvesters = SinglePortUdpHarvester.createHarvesters(IceConfig.config.port)
if (singlePortHarvesters.isEmpty()) {
logger.warn("No single-port harvesters created.")
}
val tcpHarvester: TcpHarvester? = if (IceConfig.config.tcpEnabled) {
val port = IceConfig.config.tcpPort
try {
TcpHarvester(IceConfig.config.port, IceConfig.config.iceSslTcp).apply {
logger.info("Initialized TCP harvester on port $port, ssltcp=${IceConfig.config.iceSslTcp}")
IceConfig.config.tcpMappedPort?.let { mappedPort ->
logger.info("Adding mapped port $mappedPort")
addMappedPort(mappedPort)
}
}
} catch (ioe: IOException) {
logger.warn("Failed to initialize TCP harvester on port $port")
null
}
} else {
null
}

Harvesters(tcpHarvester, singlePortHarvesters)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,10 @@ class IceTransport @JvmOverloads constructor(

companion object {
fun appendHarvesters(iceAgent: Agent) {
Harvesters.tcpHarvester?.let {
Harvesters.INSTANCE.tcpHarvester?.let {
iceAgent.addCandidateHarvester(it)
}
Harvesters.singlePortHarvesters?.forEach(iceAgent::addCandidateHarvester)
Harvesters.INSTANCE.singlePortHarvesters.forEach(iceAgent::addCandidateHarvester)
}

/**
Expand Down

0 comments on commit 57abe58

Please sign in to comment.