Commit 60fa602b authored by Ludovic Courtès's avatar Ludovic Courtès
Browse files

servers: Take <connection> records.

* jupyter/servers.scm (connection-file->kernel): Rename to...
(connection->kernel): ... this, and take a CONNECTION instead of a
PORT.
(connection-file->kernel): Rewrite as a composition.
parent 3edceba2
......@@ -17,11 +17,13 @@
(define-module (jupyter servers)
#:use-module (jupyter kernels)
#:use-module (jupyter messages)
#:use-module (simple-zmq)
#:use-module (json)
#:use-module (rnrs bytevectors)
#:use-module (ice-9 match)
#:export (connection-file->kernel))
#:export (connection->kernel
connection-file->kernel))
;;; Commentary:
;;;
......@@ -31,34 +33,38 @@
;;;
;;; Code:
(define* (connection-file->kernel port #:key (name "guix"))
"Read the contents of a Jupyter \"connection file\" from PORT and return a
<kernel> record."
;; See <https://jupyter-client.readthedocs.io/en/stable/kernels.html#connection-files>.
(let* ((table (json->scm port))
(ip (assoc-ref table "ip"))
(scheme (assoc-ref table "transport"))
(key (assoc-ref table "key"))
(context (zmq-create-context)))
(define (port->uri port)
(string-append scheme "://" ip ":"
(number->string port)))
(define* (connection->kernel connection
#:key
(name "guix")
(context (zmq-create-context)))
"Return a <kernel> for a server connected to CONNECTION."
(define (port->uri port)
(string-append (connection-transport connection) "://"
(connection-ip connection) ":"
(number->string port)))
(define (make-socket type port)
(let* ((uri (port->uri port))
(socket (zmq-create-socket context type)))
(zmq-bind-socket socket uri)
socket))
(define (make-socket type port)
(let* ((uri (port->uri port))
(socket (zmq-create-socket context type)))
(zmq-bind-socket socket uri)
socket))
(kernel name (getpid)
#:key (string->utf8 (connection-key connection))
#:control
(make-socket ZMQ_ROUTER (connection-control-port connection))
#:shell
(make-socket ZMQ_ROUTER (connection-shell-port connection))
#:standard-input
(make-socket ZMQ_ROUTER (connection-stdin-port connection))
#:heartbeat
(make-socket ZMQ_REP (connection-heartbeat-port connection))
#:iosub
(make-socket ZMQ_PUB (connection-iopub-port connection))))
(define connection-file->kernel
;; Read the contents of a Jupyter \"connection file\" from PORT and return a
;; <kernel> record.
;; See <https://jupyter-client.readthedocs.io/en/stable/kernels.html#connection-files>.
(compose connection->kernel json->connection json->scm))
(kernel name (getpid)
#:key (string->utf8 key)
#:control
(make-socket ZMQ_ROUTER (assoc-ref table "control_port"))
#:shell
(make-socket ZMQ_ROUTER (assoc-ref table "shell_port"))
#:standard-input
(make-socket ZMQ_ROUTER (assoc-ref table "stdin_port"))
#:heartbeat
(make-socket ZMQ_REP (assoc-ref table "hb_port"))
#:iosub
(make-socket ZMQ_PUB (assoc-ref table "iopub_port")))))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment