Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for IPC transport protocol #313

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/main/kotlin/org/jetbrains/kotlinx/jupyter/config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ data class KernelJupyterParams(
val sigScheme: String?,
val key: String?,
val ports: List<Int>,
val transport: String?
val transport: String?,
val ip: String?
) {
companion object {
fun fromFile(cfgFile: File): KernelJupyterParams {
Expand All @@ -102,15 +103,17 @@ object KernelJupyterParamsSerializer : KSerializer<KernelJupyterParams> {
val fieldName = "${socket.nameForUser}_port"
map[fieldName]?.let { Json.decodeFromJsonElement<Int>(it) } ?: throw RuntimeException("Cannot find $fieldName in config")
},
map["transport"]?.content ?: "tcp"
map["transport"]?.content ?: "tcp",
map["ip"]?.content
)
}

override fun serialize(encoder: Encoder, value: KernelJupyterParams) {
val map = mutableMapOf(
"signature_scheme" to JsonPrimitive(value.sigScheme),
"key" to JsonPrimitive(value.key),
"transport" to JsonPrimitive(value.transport)
"transport" to JsonPrimitive(value.transport),
"ip" to JsonPrimitive(value.ip)
)
JupyterSockets.values().forEach {
map["${it.nameForUser}_port"] = JsonPrimitive(value.ports[it.ordinal])
Expand All @@ -122,6 +125,7 @@ object KernelJupyterParamsSerializer : KSerializer<KernelJupyterParams> {
data class KernelConfig(
val ports: List<Int>,
val transport: String,
val ip: String,
val signatureScheme: String,
val signatureKey: String,
val scriptClasspath: List<File> = emptyList(),
Expand All @@ -131,7 +135,7 @@ data class KernelConfig(
val embedded: Boolean = false,
) {
fun toArgs(prefix: String = ""): KernelArgs {
val params = KernelJupyterParams(signatureScheme, signatureKey, ports, transport)
val params = KernelJupyterParams(signatureScheme, signatureKey, ports, transport, ip)

val cfgFile = File.createTempFile("kotlin-kernel-config-$prefix", ".json")
cfgFile.deleteOnExit()
Expand All @@ -152,6 +156,7 @@ data class KernelConfig(
return KernelConfig(
ports = cfg.ports,
transport = cfg.transport ?: "tcp",
ip = cfg.ip ?: "*",
signatureScheme = cfg.sigScheme ?: "hmac1-sha256",
signatureKey = if (cfg.sigScheme == null || cfg.key == null) "" else cfg.key,
scriptClasspath = scriptClasspath,
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/org/jetbrains/kotlinx/jupyter/connection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class JupyterConnection(val config: KernelConfig) : Closeable {
val name: String get() = socket.name
init {
val port = config.ports[socket.ordinal]
bind("${config.transport}://*:$port")
val address = "${config.transport}://${config.ip}${if (config.transport == "ipc") "-" else ":"}$port"
bind(address)
if (type == SocketType.PUB) {
// Workaround to prevent losing few first messages on kernel startup
// For more information on losing messages see this scheme:
Expand All @@ -35,7 +36,7 @@ class JupyterConnection(val config: KernelConfig) : Closeable {
// doesn't support this. Value of 500 ms was chosen experimentally.
Thread.sleep(500)
}
log.debug("[$name] listen: ${config.transport}://*:$port")
log.debug("[$name] listen: $address")
}

inline fun onData(body: Socket.(ByteArray) -> Unit) = recv()?.let { body(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ open class KernelServerTestsBase {
private val config = KernelConfig(
ports = JupyterSockets.values().map { randomPort() },
transport = "tcp",
ip = "127.0.0.1",
signatureScheme = "hmac1-sha256",
signatureKey = "",
scriptClasspath = classpath,
Expand Down Expand Up @@ -114,7 +115,7 @@ open class KernelServerTestsBase {
}

inner class ClientSocket(context: ZMQ.Context, private val socket: JupyterSockets) : ZMQ.Socket(context, socket.zmqClientType) {
fun connect() = connect("${config.transport}://*:${config.ports[socket.ordinal]}")
fun connect() = connect("${config.transport}://${config.ip}${if (config.transport == "ipc") "-" else ":"}${config.ports[socket.ordinal]}")
}

fun ZMQ.Socket.sendMessage(msgType: MessageType, content: MessageContent) {
Expand Down