-
Notifications
You must be signed in to change notification settings - Fork 3
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
13 changed files
with
636 additions
and
437 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
rootProject.name = "doip-sim-ecu-dsl" | ||
|
||
//include("doip-sim-ecu-dsl-test") | ||
include("doip-sim-ecu-dsl-test") |
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,108 @@ | ||
import library.DoipEntity | ||
import java.net.Inet4Address | ||
import java.net.InetAddress | ||
import java.net.NetworkInterface | ||
|
||
public open class NetworkManager( | ||
private val config: NetworkingData, | ||
private val doipEntities: List<DoipEntity<*>>, | ||
) { | ||
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 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") | ||
} | ||
val entitiesByIP = mutableMapOf<String, MutableList<DoipEntity<*>>>() | ||
doipEntities.forEach { entity -> | ||
val ip = if (ipAddresses.size == 1) { | ||
ipAddresses.first() | ||
} else { | ||
ipAddresses.first().also { | ||
ipAddresses.removeFirst() | ||
} | ||
} | ||
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 = UdpNetworkBinding(address, config.localPort, config.broadcastEnable, config.broadcastAddress, entities) | ||
unb.start() | ||
} | ||
|
||
if (config.bindOnAnyForUdpAdditional && !map.containsKey("0.0.0.0")) { | ||
val unb = UdpNetworkBinding("0.0.0.0", config.localPort, config.broadcastEnable, config.broadcastAddress, doipEntities) | ||
unb.start() | ||
} | ||
|
||
// TCP | ||
map.forEach { (address, entities) -> | ||
val tnb = TcpNetworkBinding(this, address, config.localPort, config.tlsOptions, entities) | ||
tnb.start() | ||
} | ||
} | ||
} |
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
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.