Skip to content

Commit

Permalink
rename interceptor function, add synchronization for interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
froks committed Jan 27, 2022
1 parent 5daca42 commit 6b8de2e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
6 changes: 3 additions & 3 deletions doip-sim-ecu-dsl/src/main/kotlin/SimDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ open class ResponseData<out T : DataStorage>(
}

/**
* See [SimEcu.addInterceptor]
* See [SimEcu.addOrReplaceEcuInterceptor]
*/
fun addEcuInterceptor(
fun addOrReplaceEcuInterceptor(
name: String = UUID.randomUUID().toString(),
duration: Duration = Duration.INFINITE,
alsoCallWhenEcuIsBusy: Boolean = false,
interceptor: InterceptorResponseHandler
) =
ecu.addInterceptor(name, duration, alsoCallWhenEcuIsBusy, interceptor)
ecu.addOrReplaceEcuInterceptor(name, duration, alsoCallWhenEcuIsBusy, interceptor)

/**
* See [SimEcu.removeInterceptor]
Expand Down
54 changes: 29 additions & 25 deletions doip-sim-ecu-dsl/src/main/kotlin/SimEcu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,31 @@ class SimEcu(private val data: EcuData) : StandardEcu(data.toEcuConfig()) {
if (this.interceptors.isEmpty()) {
return false
}
this.interceptors
.filterValues { it.isExpired() }
.forEach {
this.interceptors.remove(it.key)
}

this.interceptors.forEach {
if (!it.value.isExpired() && (!busy || it.value.alsoCallWhenEcuIsBusy)) {
val responseData = ResponseData<InterceptorData>(
caller = it.value,
request = request,
ecu = this
)
if (it.value.interceptor.invoke(responseData, RequestMessage(request, busy))) {
if (responseData.continueMatching) {
return false
} else if (responseData.response.isNotEmpty()) {
sendResponse(request, responseData.response)
synchronized (this.interceptors) {
this.interceptors
.filterValues { it.isExpired() }
.forEach {
this.interceptors.remove(it.key)
}

this.interceptors.forEach {
if (!it.value.isExpired() && (!busy || it.value.alsoCallWhenEcuIsBusy)) {
val responseData = ResponseData<InterceptorData>(
caller = it.value,
request = request,
ecu = this
)
if (it.value.interceptor.invoke(responseData, RequestMessage(request, busy))) {
if (responseData.continueMatching) {
return false
} else if (responseData.response.isNotEmpty()) {
sendResponse(request, responseData.response)
}
return true
}
return true
}
}

}

return false
Expand Down Expand Up @@ -169,7 +171,7 @@ class SimEcu(private val data: EcuData) : StandardEcu(data.toEcuConfig()) {
*
* Interceptors are executed before request matching
*/
fun addInterceptor(
fun addOrReplaceEcuInterceptor(
name: String = UUID.randomUUID().toString(),
duration: Duration = Duration.INFINITE,
alsoCallWhenEcuIsBusy: Boolean = false,
Expand All @@ -180,11 +182,13 @@ class SimEcu(private val data: EcuData) : StandardEcu(data.toEcuConfig()) {
// expires at expirationTime
val expirationTime = if (duration == Duration.INFINITE) Long.MAX_VALUE else System.nanoTime() + duration.inWholeNanoseconds

interceptors[name] = InterceptorData(
name = name,
interceptor = interceptor,
alsoCallWhenEcuIsBusy = alsoCallWhenEcuIsBusy,
isExpired = { System.nanoTime() >= expirationTime })
synchronized (interceptors) {
interceptors[name] = InterceptorData(
name = name,
interceptor = interceptor,
alsoCallWhenEcuIsBusy = alsoCallWhenEcuIsBusy,
isExpired = { System.nanoTime() >= expirationTime })
}

return name
}
Expand Down
2 changes: 1 addition & 1 deletion doip-sim-ecu-dsl/src/test/kotlin/SimDslTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SimDslTest {
request(byteArrayOf(0x10), "REQ1") { ack() }
request("10", "REQ2") { ack() }
request("10 []", "REQ3") { ack() }
request(Regex("10.*"), "REQ4") { nrc(); addEcuInterceptor(duration = 1.seconds) { false } }
request(Regex("10.*"), "REQ4") { nrc(); addOrReplaceEcuInterceptor(duration = 1.seconds) { false } }
}
}
assertThat(gateways.size).isEqualTo(1)
Expand Down
12 changes: 6 additions & 6 deletions doip-sim-ecu-dsl/src/test/kotlin/SimEcuTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ class SimEcuTest {
var intercepted = false
var afterInterceptor = false
var removeInterceptor = false
ecu.addInterceptor("TESTREMOVE", 500.milliseconds) { removeInterceptor = true; false; }
ecu.addInterceptor("TESTBEFORE", 500.milliseconds) { beforeInterceptor = true; false; }
ecu.addInterceptor("TEST", 200.milliseconds) { intercepted = true; true }
ecu.addInterceptor("TESTAFTER", 500.milliseconds) { afterInterceptor = true; false }
ecu.addOrReplaceEcuInterceptor("TESTREMOVE", 500.milliseconds) { removeInterceptor = true; false; }
ecu.addOrReplaceEcuInterceptor("TESTBEFORE", 500.milliseconds) { beforeInterceptor = true; false; }
ecu.addOrReplaceEcuInterceptor("TEST", 200.milliseconds) { intercepted = true; true }
ecu.addOrReplaceEcuInterceptor("TESTAFTER", 500.milliseconds) { afterInterceptor = true; false }

ecu.handleRequest(UdsMessage(0x0000, 0x0001, byteArrayOf(0x11, 0x03)))
// sendResponse didn't get called again, because there's one true interceptor, therefore no response was sent
Expand Down Expand Up @@ -199,8 +199,8 @@ class SimEcuTest {
var noBusyCalled = false
var busyCalled = false
ecu.requests.add(RequestMatcher("TEST", byteArrayOf(0x10, 0x03), null) { println("WAITING"); Thread.sleep(400); println("DONE") })
ecu.addInterceptor("NOBUSY", 1500.milliseconds) { println("NOTBUSY"); noBusyCalled = true; false; }
ecu.addInterceptor("BUSY", 1500.milliseconds, true) { println("BUSY ${it.isBusy}"); if (it.isBusy) busyCalled = true; false; }
ecu.addOrReplaceEcuInterceptor("NOBUSY", 1500.milliseconds) { println("NOTBUSY"); noBusyCalled = true; false; }
ecu.addOrReplaceEcuInterceptor("BUSY", 1500.milliseconds, true) { println("BUSY ${it.isBusy}"); if (it.isBusy) busyCalled = true; false; }

ecu.start()
try {
Expand Down

0 comments on commit 6b8de2e

Please sign in to comment.