Skip to content

Commit 68d19f6

Browse files
Introduce commHandlers for kernel-provided functionality; support debugPortComm messages in kernel
1 parent 796a7e3 commit 68d19f6

File tree

8 files changed

+85
-3
lines changed

8 files changed

+85
-3
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.jetbrains.kotlinx.jupyter.messaging
2+
3+
import kotlinx.serialization.Serializable
4+
5+
object ProvidedCommMessages {
6+
const val OPEN_DEBUG_PORT_TARGET: String = "debug_port_request"
7+
}
8+
9+
10+
@Serializable
11+
class CommDebugPortReply(
12+
val port: Int
13+
) : OkReply()

src/main/kotlin/org/jetbrains/kotlinx/jupyter/connection.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ class JupyterConnectionImpl(
271271

272272
override val stdinIn = StdinInputStream()
273273

274+
override val debugPort: Int?
275+
get() = config.debugPort
276+
274277
var contextMessage: Message? = null
275278

276279
override val executor: JupyterExecutor = JupyterExecutorImpl()

src/main/kotlin/org/jetbrains/kotlinx/jupyter/messaging/JupyterConnectionInternal.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ interface JupyterConnectionInternal : JupyterConnection {
1919
val executor: JupyterExecutor
2020
val stdinIn: InputStream
2121

22+
val debugPort: Int?
23+
2224
fun sendStatus(status: KernelStatus, incomingMessage: Message? = null)
2325
fun doWrappedInBusyIdle(incomingMessage: Message? = null, action: () -> Unit)
2426
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.jetbrains.kotlinx.jupyter.messaging
2+
3+
import kotlinx.serialization.json.JsonObject
4+
import kotlinx.serialization.json.JsonPrimitive
5+
import org.jetbrains.kotlinx.jupyter.ReplForJupyterImpl
6+
import org.jetbrains.kotlinx.jupyter.api.libraries.Comm
7+
import org.jetbrains.kotlinx.jupyter.messaging.ProvidedCommMessages.OPEN_DEBUG_PORT_TARGET
8+
9+
typealias CommMessageProcessCallback = (Comm, JsonObject, ReplForJupyterImpl) -> Unit
10+
11+
abstract class DefaultCommHandler {
12+
abstract val targetId: String
13+
14+
abstract val onReceiveAction: CommMessageProcessCallback
15+
16+
abstract fun registerAction(repl: ReplForJupyterImpl)
17+
}
18+
19+
class DebugPortCommHandler : DefaultCommHandler() {
20+
override val targetId: String
21+
get() = OPEN_DEBUG_PORT_TARGET
22+
23+
override val onReceiveAction: CommMessageProcessCallback
24+
get() = { comm, _, repl ->
25+
comm.send(
26+
JsonObject(
27+
mapOf(
28+
"port" to JsonPrimitive(repl.debugPort)
29+
)
30+
)
31+
)
32+
}
33+
34+
override fun registerAction(repl: ReplForJupyterImpl) {
35+
repl.notebook.commManager.registerCommTarget(targetId) { comm, d ->
36+
onReceiveAction(comm, d, repl)
37+
38+
comm.onMessage {
39+
onReceiveAction(comm, it, repl)
40+
}
41+
}
42+
}
43+
}
44+
45+
object ProvidedCommHandlersUtility {
46+
private val providedComms = setOf<DefaultCommHandler>(DebugPortCommHandler())
47+
48+
fun registerKnownCommsHandlersOnStartUp(repl: ReplForJupyterImpl) {
49+
providedComms.forEach {
50+
it.registerAction(repl)
51+
}
52+
}
53+
}

src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import org.jetbrains.kotlinx.jupyter.magics.FullMagicsHandler
5353
import org.jetbrains.kotlinx.jupyter.magics.MagicsProcessor
5454
import org.jetbrains.kotlinx.jupyter.messaging.DisplayHandler
5555
import org.jetbrains.kotlinx.jupyter.messaging.NoOpDisplayHandler
56+
import org.jetbrains.kotlinx.jupyter.messaging.ProvidedCommHandlersUtility.registerKnownCommsHandlersOnStartUp
5657
import org.jetbrains.kotlinx.jupyter.repl.CellExecutor
5758
import org.jetbrains.kotlinx.jupyter.repl.CompletionResult
5859
import org.jetbrains.kotlinx.jupyter.repl.ContextUpdater
@@ -122,6 +123,7 @@ interface ReplOptions {
122123
var executedCodeLogging: ExecutedCodeLogging
123124
var writeCompiledClasses: Boolean
124125
var outputConfig: OutputConfig
126+
val debugPort: Int?
125127
}
126128

127129
interface ReplForJupyter {
@@ -186,7 +188,8 @@ class ReplForJupyterImpl(
186188
private val scriptReceivers: List<Any> = emptyList(),
187189
override val isEmbedded: Boolean = false,
188190
override val notebook: MutableNotebook,
189-
override val librariesScanner: LibrariesScanner
191+
override val librariesScanner: LibrariesScanner,
192+
override val debugPort: Int? = null
190193
) : ReplForJupyter, ReplOptions, BaseKernelHost, UserHandlesProvider {
191194

192195
override val currentBranch: String
@@ -205,6 +208,10 @@ class ReplForJupyterImpl(
205208

206209
private val resourcesProcessor = LibraryResourcesProcessorImpl()
207210

211+
init {
212+
registerKnownCommsHandlersOnStartUp(this)
213+
}
214+
208215
override val sessionOptions: SessionOptions = object : SessionOptions {
209216
override var resolveSources: Boolean
210217
get() = resolver.resolveSources
@@ -414,7 +421,7 @@ class ReplForJupyterImpl(
414421
@Suppress("unused")
415422
private fun printUsagesInfo(cellId: Int, usedVariables: Set<String>?) {
416423
log.debug(buildString {
417-
if (usedVariables == null || usedVariables.isEmpty()) {
424+
if (usedVariables.isNullOrEmpty()) {
418425
append("No usages for cell $cellId")
419426
return@buildString
420427
}

src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl/creating/MockJupyterConnection.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ object MockJupyterConnection : JupyterConnectionInternal {
3232
get() = throw NotImplementedError()
3333
override val stdinIn: InputStream
3434
get() = throw NotImplementedError()
35+
override val debugPort: Int?
36+
get() = null
3537

3638
override fun sendStatus(status: KernelStatus, incomingMessage: Message?) {
3739
throw NotImplementedError()

src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl/creating/ReplFactory.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ abstract class ReplFactory {
2626
scriptReceivers,
2727
isEmbedded,
2828
notebook,
29-
librariesScanner
29+
librariesScanner,
30+
connection.debugPort
3031
)
3132
}
3233

src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/parseMagicsTests.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class ParseMagicsTests {
131131
override var executedCodeLogging = ExecutedCodeLogging.OFF
132132
override var writeCompiledClasses = false
133133
override var outputConfig = OutputConfig()
134+
override val debugPort: Int? = null
134135
}
135136

136137
private val options = TestReplOptions()

0 commit comments

Comments
 (0)