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

p-sim-ecu.doip-sim-ecu-dsl.0.15.1.source-code.SimNetworking.kt Maven / Gradle / Ivy

Go to download

This is a kotlin based domain specific language (dsl), to quickly and intuitively write custom DoIP ECU simulations.

The newest version!
import library.*
import org.slf4j.LoggerFactory

public enum class NetworkMode {
    AUTO,
    SINGLE_IP,
}

@Suppress("unused")
public open class NetworkingData {
    /**
     * The network interface that should be used to bind on, can be an IP, or name
     */
    public var networkInterface: String? = "0.0.0.0"

    /**
     * Mode for assigning ip addresses to doip entities
     */
    public var networkMode: NetworkMode = NetworkMode.AUTO

    /**
     * Should udp be bound additionally on any?
     * There's an issue when binding it to a network interface with not receiving 255.255.255.255 broadcasts
     */
    public var bindOnAnyForUdpAdditional: Boolean = true

    /**
     * Network port this gateway should bind on (default: 13400)
     */
    public var localPort: Int = 13400

    /**
     * Whether VAM broadcasts shall be sent on startup (default: true)
     */
    public var broadcastEnable: Boolean = true

    /**
     * Default broadcast address for VAM messages (default: 255.255.255.255)
     */
    public var broadcastAddress: String = "255.255.255.255"

    public val tlsOptions: TlsOptions = TlsOptions()

    internal val _doipEntities: MutableList = mutableListOf()

    public val doipEntities: List
        get() = _doipEntities

    /**
     * Defines a DoIP-Gateway and the ECUs behind it
     */
    public fun gateway(name: String, receiver: DoipEntityDataHandler) {
        val gatewayData = DoipEntityData(name, DoipNodeType.GATEWAY)
        receiver.invoke(gatewayData)
        _doipEntities.add(gatewayData)
    }

    /**
     * Defines a DoIP-Gateway and the ECUs behind it
     */
    public fun doipEntity(name: String, receiver: DoipEntityDataHandler) {
        val gatewayData = DoipEntityData(name, DoipNodeType.NODE)
        receiver.invoke(gatewayData)
        _doipEntities.add(gatewayData)
    }

}

public open class SimDoipNetworking(data: NetworkingData) : SimNetworking(data) {
    override fun createDoipEntity(data: DoipEntityData): SimDoipEntity =
        SimDoipEntity(data)
}

public abstract class SimNetworking>(public val data: NetworkingData) {
    private val log = LoggerFactory.getLogger(SimNetworking::class.java)

    public val doipEntities: List
        get() {
            if (data.doipEntities.isNotEmpty() && _doipEntities.isEmpty()) {
                start()
            }
            return _doipEntities
        }

    private val _doipEntities: MutableList = mutableListOf()
    protected val _vams: MutableList = mutableListOf()

    protected fun addEntity(doipEntity: @UnsafeVariance T) {
        if (_doipEntities.any { it.config.logicalAddress == doipEntity.config.logicalAddress }) {
            log.error("Can't add '${doipEntity.name}' - entity with same logical address already exists")
            return
        }
        _doipEntities.add(doipEntity)
    }

    protected abstract fun createDoipEntity(data: DoipEntityData): T

    public open fun start() {
        _doipEntities.clear()

        data._doipEntities.map { createDoipEntity(it) }.forEach {
            _doipEntities.add(it)
        }

        _doipEntities.forEach {
            it.start()
        }
    }

    public open fun reset() {
        _doipEntities.forEach { it.reset() }
    }

    public open fun findEcuByName(ecuName: String, ignoreCase: Boolean = true): E? =
        _doipEntities.flatMap { it.ecus }.firstOrNull { ecuName.equals(it.name, ignoreCase) }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy