Skip to content

Commit b7ff093

Browse files
committed
Store info about ZMQ socket types in one place
1 parent 0b43fb1 commit b7ff093

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

src/main/kotlin/org/jetbrains/kotlin/jupyter/config.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import kotlinx.coroutines.async
99
import org.apache.commons.io.FileUtils
1010
import org.json.JSONObject
1111
import org.slf4j.LoggerFactory
12+
import org.zeromq.ZMQ
1213
import java.io.File
1314
import java.nio.file.Files
1415
import java.nio.file.Paths
@@ -30,12 +31,12 @@ val LibraryPropertiesFile = ".properties"
3031

3132
internal val log by lazy { LoggerFactory.getLogger("ikotlin") }
3233

33-
enum class JupyterSockets {
34-
hb,
35-
shell,
36-
control,
37-
stdin,
38-
iopub
34+
enum class JupyterSockets(val zmqKernelType: Int, val zmqClientType: Int) {
35+
hb(ZMQ.REP, ZMQ.REP),
36+
shell(ZMQ.ROUTER, ZMQ.REQ),
37+
control(ZMQ.ROUTER, ZMQ.REQ),
38+
stdin(ZMQ.ROUTER, ZMQ.DEALER),
39+
iopub(ZMQ.PUB, ZMQ.SUB)
3940
}
4041

4142
data class OutputConfig(

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import javax.crypto.spec.SecretKeySpec
1313

1414
class JupyterConnection(val config: KernelConfig): Closeable {
1515

16-
inner class Socket(private val socket: JupyterSockets, type: Int) : ZMQ.Socket(context, type) {
16+
inner class Socket(private val socket: JupyterSockets, type: Int = socket.zmqKernelType) : ZMQ.Socket(context, type) {
1717
val name: String get() = socket.name
1818
init {
1919
val port = config.ports[socket.ordinal]
@@ -121,11 +121,11 @@ class JupyterConnection(val config: KernelConfig): Closeable {
121121

122122
private val disposable = Disposable { }
123123

124-
val heartbeat = Socket(JupyterSockets.hb, ZMQ.REP)
125-
val shell = Socket(JupyterSockets.shell, ZMQ.ROUTER)
126-
val control = Socket(JupyterSockets.control, ZMQ.ROUTER)
127-
val stdin = Socket(JupyterSockets.stdin, ZMQ.ROUTER)
128-
val iopub = Socket(JupyterSockets.iopub, ZMQ.PUB)
124+
val heartbeat = Socket(JupyterSockets.hb)
125+
val shell = Socket(JupyterSockets.shell)
126+
val control = Socket(JupyterSockets.control)
127+
val stdin = Socket(JupyterSockets.stdin)
128+
val iopub = Socket(JupyterSockets.iopub)
129129

130130
val stdinIn = StdinInputStream()
131131

src/test/kotlin/org/jetbrains/kotlin/jupyter/test/kernelServerTestsBase.kt

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ open class KernelServerTestsBase {
3737
server?.interrupt()
3838
}
3939

40+
inner class ClientSocket(context: ZMQ.Context, private val socket: JupyterSockets) : ZMQ.Socket(context, socket.zmqClientType) {
41+
fun connect() = connect("${config.transport}://*:${config.ports[socket.ordinal]}")
42+
}
43+
4044
fun ZMQ.Socket.sendMessage(msgType: String, content : JsonObject) {
4145
sendMessage(Message(id = messageId, header = makeHeader(msgType), content = content), hmac)
4246
}

0 commit comments

Comments
 (0)