-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
934 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
ok-marketplace-be/ok-marketplace-app-ktor/src/commonMain/kotlin/base/KtorWsSessionRepo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ru.otus.otuskotlin.marketplace.app.ktor.base | ||
|
||
import ru.otus.otuskotlin.marketplace.common.ws.IMkplWsSession | ||
import ru.otus.otuskotlin.marketplace.common.ws.IMkplWsSessionRepo | ||
|
||
class KtorWsSessionRepo: IMkplWsSessionRepo { | ||
private val sessions: MutableSet<IMkplWsSession> = mutableSetOf() | ||
override fun add(session: IMkplWsSession) { | ||
sessions.add(session) | ||
} | ||
|
||
override fun clearAll() { | ||
sessions.clear() | ||
} | ||
|
||
override fun remove(session: IMkplWsSession) { | ||
sessions.remove(session) | ||
} | ||
|
||
override suspend fun <T> sendAll(obj: T) { | ||
sessions.forEach { it.send(obj) } | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
ok-marketplace-be/ok-marketplace-app-ktor/src/commonMain/kotlin/base/KtorWsSessionV2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ru.otus.otuskotlin.marketplace.app.ktor.base | ||
|
||
import io.ktor.websocket.* | ||
import ru.otus.otuskotlin.marketplace.api.v2.apiV2ResponseSerialize | ||
import ru.otus.otuskotlin.marketplace.api.v2.models.IResponse | ||
import ru.otus.otuskotlin.marketplace.common.ws.IMkplWsSession | ||
|
||
data class KtorWsSessionV2( | ||
private val session: WebSocketSession | ||
) : IMkplWsSession { | ||
override suspend fun <T> send(obj: T) { | ||
require(obj is IResponse) | ||
session.send(Frame.Text(apiV2ResponseSerialize(obj))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
ok-marketplace-be/ok-marketplace-app-ktor/src/commonMain/kotlin/v2/WsController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package ru.otus.otuskotlin.marketplace.app.ktor.v2 | ||
|
||
import io.ktor.websocket.* | ||
import kotlinx.coroutines.channels.ClosedReceiveChannelException | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.mapNotNull | ||
import kotlinx.coroutines.flow.onCompletion | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import ru.otus.otuskotlin.marketplace.api.v2.apiV2RequestDeserialize | ||
import ru.otus.otuskotlin.marketplace.api.v2.apiV2ResponseSerialize | ||
import ru.otus.otuskotlin.marketplace.api.v2.mappers.fromTransport | ||
import ru.otus.otuskotlin.marketplace.api.v2.mappers.toTransportAd | ||
import ru.otus.otuskotlin.marketplace.api.v2.mappers.toTransportInit | ||
import ru.otus.otuskotlin.marketplace.api.v2.models.IRequest | ||
import ru.otus.otuskotlin.marketplace.app.common.controllerHelper | ||
import ru.otus.otuskotlin.marketplace.app.ktor.MkplAppSettings | ||
import ru.otus.otuskotlin.marketplace.app.ktor.base.KtorWsSessionV2 | ||
import ru.otus.otuskotlin.marketplace.common.models.MkplCommand | ||
import kotlin.reflect.KClass | ||
|
||
private val clWsV2: KClass<*> = WebSocketSession::wsHandlerV2::class | ||
suspend fun WebSocketSession.wsHandlerV2(appSettings: MkplAppSettings) = with(KtorWsSessionV2(this)) { | ||
// Обновление реестра сессий | ||
val sessions = appSettings.corSettings.wsSessions | ||
sessions.add(this) | ||
|
||
// Handle init request | ||
appSettings.controllerHelper( | ||
{ | ||
command = MkplCommand.INIT | ||
wsSession = this@with | ||
}, | ||
{ outgoing.send(Frame.Text(apiV2ResponseSerialize(toTransportInit()))) }, | ||
clWsV2, | ||
"wsV2-init" | ||
) | ||
|
||
// Handle flow | ||
incoming.receiveAsFlow() | ||
.mapNotNull { it -> | ||
val frame = it as? Frame.Text ?: return@mapNotNull | ||
// Handle without flow destruction | ||
try { | ||
appSettings.controllerHelper( | ||
{ | ||
fromTransport(apiV2RequestDeserialize<IRequest>(frame.readText())) | ||
wsSession = this@with | ||
}, | ||
{ | ||
val result = apiV2ResponseSerialize(toTransportAd()) | ||
// If change request, response is sent to everyone | ||
outgoing.send(Frame.Text(result)) | ||
}, | ||
clWsV2, | ||
"wsV2-handle" | ||
) | ||
|
||
} catch (_: ClosedReceiveChannelException) { | ||
sessions.remove(this@with) | ||
} catch (e: Throwable) { | ||
println("FFF") | ||
} | ||
} | ||
.onCompletion { | ||
// Handle finish request | ||
appSettings.controllerHelper( | ||
{ | ||
command = MkplCommand.FINISH | ||
wsSession = this@with | ||
}, | ||
{ }, | ||
clWsV2, | ||
"wsV2-finish" | ||
) | ||
sessions.remove(this@with) | ||
} | ||
.collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
ok-marketplace-be/ok-marketplace-app-ktor/src/jvmMain/kotlin/base/KtorWsSessionV1.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ru.otus.otuskotlin.marketplace.app.ktor.base | ||
|
||
import io.ktor.websocket.* | ||
import ru.otus.otuskotlin.marketplace.api.v1.apiV1ResponseSerialize | ||
import ru.otus.otuskotlin.marketplace.api.v1.models.IResponse | ||
import ru.otus.otuskotlin.marketplace.common.ws.IMkplWsSession | ||
|
||
data class KtorWsSessionV1( | ||
private val session: WebSocketSession | ||
) : IMkplWsSession { | ||
override suspend fun <T> send(obj: T) { | ||
require(obj is IResponse) | ||
session.send(Frame.Text(apiV1ResponseSerialize(obj))) | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
ok-marketplace-be/ok-marketplace-app-ktor/src/jvmMain/kotlin/v1/WsController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package ru.otus.otuskotlin.marketplace.app.ktor.v1 | ||
|
||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import io.ktor.websocket.* | ||
import kotlinx.coroutines.channels.ClosedReceiveChannelException | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.mapNotNull | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import ru.otus.otuskotlin.marketplace.api.v1.apiV1Mapper | ||
import ru.otus.otuskotlin.marketplace.api.v1.models.IRequest | ||
import ru.otus.otuskotlin.marketplace.app.common.controllerHelper | ||
import ru.otus.otuskotlin.marketplace.app.ktor.MkplAppSettings | ||
import ru.otus.otuskotlin.marketplace.app.ktor.base.KtorWsSessionV1 | ||
import ru.otus.otuskotlin.marketplace.common.models.MkplCommand | ||
import ru.otus.otuskotlin.marketplace.mappers.v1.fromTransport | ||
import ru.otus.otuskotlin.marketplace.mappers.v1.toTransportAd | ||
import ru.otus.otuskotlin.marketplace.mappers.v1.toTransportInit | ||
import kotlin.reflect.KClass | ||
|
||
private val clWsV1: KClass<*> = WebSocketSession::wsHandlerV1::class | ||
suspend fun WebSocketSession.wsHandlerV1(appSettings: MkplAppSettings) = with(KtorWsSessionV1(this)) { | ||
val sessions = appSettings.corSettings.wsSessions | ||
sessions.add(this) | ||
|
||
// Handle init request | ||
appSettings.controllerHelper( | ||
{ | ||
command = MkplCommand.INIT | ||
wsSession = this@with | ||
}, | ||
{ outgoing.send(Frame.Text(apiV1Mapper.writeValueAsString(toTransportInit()))) }, | ||
clWsV1, | ||
"wsV1-init" | ||
) | ||
|
||
// Handle flow | ||
incoming.receiveAsFlow().mapNotNull { | ||
val frame = it as? Frame.Text ?: return@mapNotNull | ||
// Handle without flow destruction | ||
try { | ||
appSettings.controllerHelper( | ||
{ | ||
fromTransport(apiV1Mapper.readValue<IRequest>(frame.readText())) | ||
wsSession = this@with | ||
}, | ||
{ | ||
val result = apiV1Mapper.writeValueAsString(toTransportAd()) | ||
// If change request, response is sent to everyone | ||
outgoing.send(Frame.Text(result)) | ||
}, | ||
clWsV1, | ||
"wsV1-handle" | ||
) | ||
|
||
} catch (_: ClosedReceiveChannelException) { | ||
sessions.remove(this@with) | ||
} finally { | ||
// Handle finish request | ||
appSettings.controllerHelper( | ||
{ | ||
command = MkplCommand.FINISH | ||
wsSession = this@with | ||
}, | ||
{ }, | ||
clWsV1, | ||
"wsV1-finish" | ||
) | ||
sessions.remove(this@with) | ||
} | ||
}.collect() | ||
} |
Oops, something went wrong.