Skip to content

Commit

Permalink
improve registeredSourceAddress handling
Browse files Browse the repository at this point in the history
  • Loading branch information
froks committed Oct 5, 2023
1 parent 1c26b34 commit cf4c382
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public open class DefaultDoipEntityTcpConnectionMessageHandler(
) : DoipTcpConnectionMessageHandler(maxPayloadLength) {
private val logger: Logger = LoggerFactory.getLogger(DefaultDoipEntityTcpConnectionMessageHandler::class.java)

private var _registeredSourceAddress: Short? = null

public fun getRegisteredSourceAddress(): Short? =
_registeredSourceAddress

override suspend fun handleTcpMessage(message: DoipTcpMessage, output: OutputStream) {
runBlocking {
MDC.put("ecu", doipEntity.name)
Expand Down Expand Up @@ -58,11 +53,11 @@ public open class DefaultDoipEntityTcpConnectionMessageHandler(
return
}

if (_registeredSourceAddress == null) {
_registeredSourceAddress = message.sourceAddress
if (registeredSourceAddress == null) {
registeredSourceAddress = message.sourceAddress
}

if (_registeredSourceAddress != message.sourceAddress) {
if (registeredSourceAddress != message.sourceAddress) {
logger.error("Routing activation for ${message.sourceAddress} denied (Different source address already registered)")
output.writeFully(
DoipTcpRoutingActivationResponse(
Expand All @@ -72,8 +67,8 @@ public open class DefaultDoipEntityTcpConnectionMessageHandler(
).asByteArray
)
} else if (
doipEntity.hasAlreadyActiveConnection(message.sourceAddress, this) &&
doipEntity.ecus.all { it.config.additionalVam == null }
doipEntity.ecus.all { it.config.additionalVam == null } &&
doipEntity.hasAlreadyActiveConnection(message.sourceAddress, this)
) {
logger.error("Routing activation for ${message.sourceAddress} denied (Has already an active connection)")
output.writeFully(
Expand Down Expand Up @@ -102,7 +97,7 @@ public open class DefaultDoipEntityTcpConnectionMessageHandler(
}

override suspend fun handleTcpDiagMessage(message: DoipTcpDiagMessage, output: OutputStream) {
if (_registeredSourceAddress != message.sourceAddress) {
if (registeredSourceAddress != message.sourceAddress) {
val reject = DoipTcpDiagMessageNegAck(
message.targetAddress,
message.sourceAddress,
Expand Down Expand Up @@ -151,7 +146,7 @@ public interface DiagnosticMessageHandler {

public fun DoipEntity.hasAlreadyActiveConnection(sourceAddress: Short, exclude: DoipTcpConnectionMessageHandler?): Boolean =
this.connectionHandlers.any {
(it as DefaultDoipEntityTcpConnectionMessageHandler).getRegisteredSourceAddress() == sourceAddress
it.registeredSourceAddress == sourceAddress
&& it != exclude
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/library/DoipTcpMessages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public abstract class DoipTcpMessage : DoipMessage()
public open class DoipTcpConnectionMessageHandler(
public val maxPayloadLength: Int = Int.MAX_VALUE
) {
private var _registeredSourceAddress: Short? = null

public var registeredSourceAddress: Short?
get() = _registeredSourceAddress
protected set(value) {
_registeredSourceAddress = value
}


public open suspend fun receiveTcpData(brc: ByteReadChannel): DoipTcpMessage {
logger.traceIf { "# receiveTcpData" }
Expand Down Expand Up @@ -122,6 +130,7 @@ public open class DoipTcpConnectionMessageHandler(
output: OutputStream
) {
logger.traceIf { "# handleTcpRoutingActivationRequest $message" }
registeredSourceAddress = message.sourceAddress
}

protected open suspend fun handleTcpRoutingActivationResponse(
Expand Down

0 comments on commit cf4c382

Please sign in to comment.