Skip to content

Commit 125e501

Browse files
committed
Added AsyncDtlsSessionLifecycleCallbacks
1 parent 5fc13c8 commit 125e501

File tree

3 files changed

+96
-5
lines changed

3 files changed

+96
-5
lines changed

kotlin-mbedtls/src/main/kotlin/org/opencoap/ssl/transport/DtlsServer.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ class DtlsServer(
202202
override fun close() = ctx.close()
203203

204204
private fun reportHandshakeStarted() {
205-
executor.supply { lifecycleCallbacks.handshakeStarted(peerAddress) }
205+
lifecycleCallbacks.handshakeStarted(peerAddress)
206206
}
207207

208208
private fun reportHandshakeFinished(reason: DtlsSessionLifecycleCallbacks.Reason, err: Throwable? = null) {
209-
executor.supply { lifecycleCallbacks.handshakeFinished(peerAddress, ctx.startTimestamp, ctx.finishTimestamp, reason, err) }
209+
lifecycleCallbacks.handshakeFinished(peerAddress, ctx.startTimestamp, ctx.finishTimestamp, reason, err)
210210
}
211211
}
212212

@@ -287,11 +287,11 @@ class DtlsServer(
287287
}
288288

289289
private fun reportSessionStarted() {
290-
executor.supply { lifecycleCallbacks.sessionStarted(peerAddress, ctx.cipherSuite, ctx.reloaded) }
290+
lifecycleCallbacks.sessionStarted(peerAddress, ctx.cipherSuite, ctx.reloaded)
291291
}
292292

293293
private fun reportSessionFinished(reason: DtlsSessionLifecycleCallbacks.Reason, err: Throwable? = null) {
294-
executor.supply { lifecycleCallbacks.sessionFinished(peerAddress, reason, err) }
294+
lifecycleCallbacks.sessionFinished(peerAddress, reason, err)
295295
}
296296
}
297297
}

kotlin-mbedtls/src/main/kotlin/org/opencoap/ssl/transport/DtlsSessionLifecycleCallbacks.kt

+25-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,37 @@
1717
package org.opencoap.ssl.transport
1818

1919
import java.net.InetSocketAddress
20+
import java.util.concurrent.Executor
2021

2122
interface DtlsSessionLifecycleCallbacks {
2223
enum class Reason {
2324
SUCCEEDED, FAILED, CLOSED, EXPIRED
2425
}
26+
2527
fun handshakeStarted(adr: InetSocketAddress) = Unit
26-
fun handshakeFinished(adr: InetSocketAddress, hanshakeStartTimestamp: Long, hanshakeFinishTimestamp: Long, reason: Reason, throwable: Throwable? = null) = Unit
28+
fun handshakeFinished(adr: InetSocketAddress, hanshakeStartTimestamp: Long, hanshakeFinishTimestamp: Long, reason: Reason, throwable: Throwable? = null) =
29+
Unit
30+
2731
fun sessionStarted(adr: InetSocketAddress, cipherSuite: String, reloaded: Boolean) = Unit
2832
fun sessionFinished(adr: InetSocketAddress, reason: Reason, throwable: Throwable? = null) = Unit
2933
}
34+
35+
class AsyncDtlsSessionLifecycleCallbacks(private val executor: Executor, private val callbacks: DtlsSessionLifecycleCallbacks) :
36+
DtlsSessionLifecycleCallbacks {
37+
38+
override fun handshakeStarted(adr: InetSocketAddress) {
39+
executor.supply { callbacks.handshakeStarted(adr) }
40+
}
41+
42+
override fun handshakeFinished(adr: InetSocketAddress, hanshakeStartTimestamp: Long, hanshakeFinishTimestamp: Long, reason: DtlsSessionLifecycleCallbacks.Reason, throwable: Throwable?) {
43+
executor.supply { callbacks.handshakeFinished(adr, hanshakeStartTimestamp, hanshakeFinishTimestamp, reason, throwable) }
44+
}
45+
46+
override fun sessionStarted(adr: InetSocketAddress, cipherSuite: String, reloaded: Boolean) {
47+
executor.supply { callbacks.sessionStarted(adr, cipherSuite, reloaded) }
48+
}
49+
50+
override fun sessionFinished(adr: InetSocketAddress, reason: DtlsSessionLifecycleCallbacks.Reason, throwable: Throwable?) {
51+
executor.supply { callbacks.sessionFinished(adr, reason, throwable) }
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2022-2023 kotlin-mbedtls contributors (https://github.com/open-coap/kotlin-mbedtls)
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.opencoap.ssl.transport
18+
19+
import io.mockk.Called
20+
import io.mockk.confirmVerified
21+
import io.mockk.mockk
22+
import io.mockk.verify
23+
import org.junit.jupiter.api.Test
24+
import org.opencoap.ssl.transport.DtlsSessionLifecycleCallbacks.Reason
25+
import org.opencoap.ssl.util.localAddress
26+
import java.util.concurrent.Executors
27+
28+
class AsyncDtlsSessionLifecycleCallbacksTest {
29+
30+
@Test
31+
fun `should invoke callbacks`() {
32+
val callbackMock = mockk<DtlsSessionLifecycleCallbacks>()
33+
val asyncCallbacks = AsyncDtlsSessionLifecycleCallbacks(Executors.newSingleThreadExecutor(), callbackMock)
34+
35+
// when
36+
asyncCallbacks.handshakeStarted(localAddress(5683))
37+
asyncCallbacks.handshakeFinished(localAddress(5683), 0, 1, Reason.SUCCEEDED)
38+
asyncCallbacks.sessionStarted(localAddress(5683), "A", false)
39+
asyncCallbacks.sessionFinished(localAddress(5683), Reason.CLOSED)
40+
41+
// then
42+
verify {
43+
callbackMock.handshakeStarted(any())
44+
callbackMock.handshakeFinished(any(), 0, 1, Reason.SUCCEEDED)
45+
callbackMock.sessionStarted(any(), "A", false)
46+
callbackMock.sessionFinished(any(), Reason.CLOSED)
47+
}
48+
confirmVerified(callbackMock)
49+
}
50+
51+
@Test
52+
fun `should not invoke callbacks with non operational executor`() {
53+
val callbackMock = mockk<DtlsSessionLifecycleCallbacks>()
54+
val asyncCallbacks = AsyncDtlsSessionLifecycleCallbacks({}, callbackMock)
55+
56+
// when
57+
asyncCallbacks.handshakeStarted(localAddress(5683))
58+
asyncCallbacks.handshakeFinished(localAddress(5683), 0, 1, Reason.SUCCEEDED)
59+
asyncCallbacks.sessionStarted(localAddress(5683), "A", false)
60+
asyncCallbacks.sessionFinished(localAddress(5683), Reason.CLOSED)
61+
62+
// then
63+
verify {
64+
callbackMock wasNot Called
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)