Skip to content

Commit ae532aa

Browse files
author
Robert Winkler
committed
Initial WebSocketProtocolServer and Tests for Properties and Actions
1 parent c34b23b commit ae532aa

File tree

22 files changed

+247
-63
lines changed

22 files changed

+247
-63
lines changed

build.gradle.kts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
plugins {
22
kotlin("jvm") version "2.0.20"
3-
id("org.jlleitschuh.gradle.ktlint") version "12.1.0"
43
id("org.jetbrains.kotlinx.kover") version "0.8.3"
54
}
65

7-
8-
96
subprojects {
107
apply(plugin = "org.jetbrains.kotlin.jvm")
11-
apply(plugin = "org.jlleitschuh.gradle.ktlint")
128
apply(plugin = "org.jetbrains.kotlinx.kover")
139

1410
group = "ai.ancf.lmos"
@@ -36,4 +32,3 @@ allprojects {
3632
mavenCentral()
3733
}
3834
}
39-

kotlin-wot-binding-http/build.gradle.kts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ plugins {
44

55
dependencies {
66
api(project(":kotlin-wot"))
7-
//implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
87
implementation("org.slf4j:slf4j-api")
98
implementation("io.ktor:ktor-server-core")
109
implementation("io.ktor:ktor-server-netty")
@@ -17,8 +16,7 @@ dependencies {
1716
implementation("io.ktor:ktor-client-content-negotiation")
1817
implementation("io.ktor:ktor-serialization-jackson")
1918
implementation("io.ktor:ktor-server-auto-head-response")
20-
//implementation("io.insert-koin:koin-ktor:4.0.0")
2119
testImplementation("io.ktor:ktor-server-test-host")
2220
testImplementation("ch.qos.logback:logback-classic:1.5.12")
2321
testImplementation("com.marcinziolo:kotlin-wiremock:2.1.1")
24-
}
22+
}

kotlin-wot-binding-http/src/main/resources/application.conf

Lines changed: 0 additions & 8 deletions
This file was deleted.

kotlin-wot-binding-mqtt/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ dependencies {
55
testImplementation("ch.qos.logback:logback-classic:1.5.12")
66
testImplementation("app.cash.turbine:turbine:1.2.0")
77
testImplementation("org.testcontainers:testcontainers:1.20.3")
8-
}
8+
}

kotlin-wot-binding-websocket/build.gradle.kts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ plugins {
44

55
dependencies {
66
api(project(":kotlin-wot"))
7-
//implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
87
implementation("org.slf4j:slf4j-api")
98
implementation("io.ktor:ktor-server-netty")
109
implementation("io.ktor:ktor-server-websockets")
@@ -14,9 +13,8 @@ dependencies {
1413
implementation("io.ktor:ktor-client-logging")
1514
implementation("io.ktor:ktor-server-call-logging")
1615
implementation("io.ktor:ktor-serialization-jackson")
17-
//implementation("io.insert-koin:koin-ktor:4.0.0")
1816
testImplementation("io.ktor:ktor-server-test-host")
1917
testImplementation("ch.qos.logback:logback-classic:1.5.12")
2018
testImplementation("com.marcinziolo:kotlin-wiremock:2.1.1")
2119
testImplementation("io.ktor:ktor-server-test-host-jvm:3.0.0")
22-
}
20+
}

kotlin-wot-binding-websocket/src/main/kotlin/websocket/HttpClientConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ data class HttpClientConfig(
99
val serverKey: String,
1010
val serverCert: String,
1111
val security: SecurityScheme
12-
)
12+
)

kotlin-wot-binding-websocket/src/main/kotlin/websocket/Messages.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,4 @@ data class ErrorMessage(
214214
var instance: String // URI reference to the specific occurrence of the problem
215215
) : WoTMessage {
216216
override val messageType: String = MessageTypes.ERROR
217-
}
217+
}

kotlin-wot-binding-websocket/src/main/kotlin/websocket/SecureWebSocketProtocolClientFactory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ai.ancf.lmos.wot.binding.websocket
22

33
/**
4-
* Creates new [HttpProtocolClient] instances that allow consuming Things via HTTPS.
4+
* Creates new [WebSocketProtocolClient] instances that allow consuming Things via WSS.
55
*/
66
class SecureWebSocketProtocolClientFactory() : WebSocketProtocolClientFactory() {
77

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,72 @@
11
package ai.ancf.lmos.wot.binding.websocket
22

3+
4+
import ai.ancf.lmos.wot.content.Content
5+
import ai.ancf.lmos.wot.thing.form.Form
36
import ai.anfc.lmos.wot.binding.ProtocolClient
7+
import ai.anfc.lmos.wot.binding.ProtocolClientException
48
import io.ktor.client.*
59
import io.ktor.client.engine.cio.*
10+
import io.ktor.client.plugins.websocket.*
11+
import io.ktor.http.*
12+
import io.ktor.serialization.jackson.*
13+
import io.ktor.websocket.*
14+
import kotlinx.coroutines.CompletableDeferred
15+
import org.slf4j.LoggerFactory
616

717
class WebSocketProtocolClient(
818
private val httpClientConfig: HttpClientConfig? = null,
9-
private val client: HttpClient = HttpClient(CIO)
19+
private val client: HttpClient = HttpClient(CIO) {
20+
install(WebSockets){
21+
contentConverter = JacksonWebsocketContentConverter()
22+
}
23+
}
1024
) : ProtocolClient {
25+
companion object {
26+
private val log = LoggerFactory.getLogger(WebSocketProtocolClient::class.java)
27+
}
28+
29+
private var session: DefaultClientWebSocketSession? = null
30+
1131
override suspend fun start() {
12-
TODO("Not yet implemented")
32+
log.info("Starting WebSocketProtocolClient")
33+
client.webSocket(
34+
method = HttpMethod.Get,
35+
host = httpClientConfig?.address ?: "localhost",
36+
port = httpClientConfig?.port ?: 80,
37+
path = "/ws"
38+
) {
39+
session = this
40+
}
1341
}
1442

1543
override suspend fun stop() {
16-
TODO("Not yet implemented")
44+
log.info("Stopping WebSocketProtocolClient")
45+
session?.close()
46+
session = null
1747
}
1848

49+
override suspend fun readResource(form: Form): Content {
50+
return resolveRequestToContent(form)
51+
}
52+
53+
private suspend fun resolveRequestToContent(form: Form, content: Content? = null): Content {
54+
val response = CompletableDeferred<Content>()
55+
56+
try {
57+
session?.let {
58+
it.sendSerialized(ReadPropertyMessage("test", property = "test"))
59+
60+
val readingMessage = it.receiveDeserialized<PropertyReadingMessage>()
61+
62+
val responseContent = Content(
63+
body = readingMessage.data.binaryValue()
64+
)
65+
response.complete(responseContent)
66+
}
67+
} catch (e: Exception) {
68+
response.completeExceptionally(ProtocolClientException("Error during http request: ${e.message}", e))
69+
}
70+
return response.await()
71+
}
1972
}

kotlin-wot-binding-websocket/src/main/kotlin/websocket/WebSocketProtocolClientFactory.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ open class WebSocketProtocolClientFactory(private val httpClientConfig: HttpClie
1111
}
1212
override val scheme: String
1313
get() = "ws"
14+
1415
override val client: WebSocketProtocolClient
1516
get() = WebSocketProtocolClient(httpClientConfig)
1617

0 commit comments

Comments
 (0)