-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add networkmanager to be able to introduce automatic ip assignments i…
…n the future, cleans up additional vam handling
- Loading branch information
Showing
20 changed files
with
797 additions
and
524 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import library.DoipEntity | ||
import org.slf4j.LoggerFactory | ||
import java.net.Inet4Address | ||
import java.net.InetAddress | ||
import java.net.NetworkInterface | ||
|
||
public open class NetworkManager( | ||
public val config: NetworkingData, | ||
public val doipEntities: List<DoipEntity<*>>, | ||
) { | ||
private val log = LoggerFactory.getLogger(NetworkManager::class.java) | ||
|
||
protected fun findInterfaceByName(): NetworkInterface? { | ||
var foundInterface: NetworkInterface? = null | ||
NetworkInterface.getNetworkInterfaces()?.let { netIntf -> | ||
while (netIntf.hasMoreElements()) { | ||
val entry = netIntf.nextElement() | ||
if (entry.displayName != null && entry.displayName.equals(config.networkInterface, true)) { | ||
foundInterface = entry | ||
break | ||
} | ||
entry.subInterfaces?.let { subInterfaces -> | ||
while (subInterfaces.hasMoreElements()) { | ||
val subInterface = subInterfaces.nextElement() | ||
if (subInterface.displayName != null && subInterface.displayName.equals( | ||
config.networkInterface, | ||
true | ||
) | ||
) { | ||
foundInterface = entry; | ||
break | ||
} | ||
} | ||
} | ||
if (foundInterface != null) { | ||
break | ||
} | ||
} | ||
} | ||
|
||
return foundInterface | ||
} | ||
|
||
protected fun getAvailableIPAddresses(): List<InetAddress> { | ||
if (config.networkInterface.isNullOrBlank() || config.networkInterface == "0.0.0.0") { | ||
return listOf(InetAddress.getByName(config.networkInterface)) | ||
} | ||
val list = mutableListOf<InetAddress>() | ||
findInterfaceByName()?.let { intf -> | ||
intf.inetAddresses?.let { inetAddresses -> | ||
while (inetAddresses.hasMoreElements()) { | ||
val address = inetAddresses.nextElement() | ||
if (address is Inet4Address) { | ||
list.add(address) | ||
} | ||
} | ||
} | ||
} | ||
if (list.isEmpty()) { | ||
InetAddress.getByName(config.networkInterface)?.let { addr -> | ||
list.add(addr) | ||
} | ||
} | ||
return list | ||
} | ||
|
||
protected open fun buildStartupMap(): Map<String, List<DoipEntity<*>>> { | ||
val ipAddresses = getAvailableIPAddresses().toMutableList() | ||
if (ipAddresses.isEmpty()) { | ||
throw IllegalArgumentException("No network interface with the identifier ${config.networkInterface} could be found") | ||
} | ||
log.info("There are ${ipAddresses.size} ip address available, and we have ${doipEntities.size} doip entities") | ||
val entitiesByIP = mutableMapOf<String, MutableList<DoipEntity<*>>>() | ||
doipEntities.forEach { entity -> | ||
val ip = if (ipAddresses.size == 1) { | ||
ipAddresses.first() | ||
} else { | ||
ipAddresses.removeFirst() | ||
} | ||
log.info("Assigning entity ${entity.name} to $ip") | ||
var entityList = entitiesByIP[ip.hostAddress] | ||
if (entityList == null) { | ||
entityList = mutableListOf() | ||
entitiesByIP[ip.hostAddress] = entityList | ||
} | ||
entityList.add(entity) | ||
} | ||
return entitiesByIP | ||
} | ||
|
||
public fun start() { | ||
val map = buildStartupMap() | ||
|
||
// UDP | ||
map.forEach { (address, entities) -> | ||
val unb = createUdpNetworkBinding(address, entities) | ||
unb.start() | ||
} | ||
|
||
if (config.bindOnAnyForUdpAdditional && !map.containsKey("0.0.0.0")) { | ||
val unb = createUdpNetworkBindingAny() | ||
unb.start() | ||
} | ||
|
||
// TCP | ||
map.forEach { (address, entities) -> | ||
val tnb = createTcpNetworkBinding(address, entities) | ||
tnb.start() | ||
} | ||
} | ||
|
||
protected open fun createTcpNetworkBinding( | ||
address: String, | ||
entities: List<DoipEntity<*>> | ||
): TcpNetworkBinding = | ||
TcpNetworkBinding(this, address, config.localPort, config.tlsOptions, entities) | ||
|
||
protected open fun createUdpNetworkBinding( | ||
address: String, | ||
entities: List<DoipEntity<*>> | ||
): UdpNetworkBinding = | ||
UdpNetworkBinding(address, config.localPort, config.broadcastEnable, config.broadcastAddress, entities) | ||
|
||
protected open fun createUdpNetworkBindingAny(): UdpNetworkBinding = | ||
UdpNetworkBinding("0.0.0.0", config.localPort, config.broadcastEnable, config.broadcastAddress, doipEntities) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.