This is needed to avoid an invalid state of the socket when un-pausing the
channel and attempting to send a message when there is a message waiting to be
received.
A `jupyter-kernel-client' defers most of its message sending, receiving, and
message processing to a subprocess which sends its messages to the kernel
through zmq sockets and polls for any messages to received from the kernel.
This commit generalizes this by introducing a new `jupyter-ioloop` type which
encapsulates the subprocess and the code necessary to build up the function
that does the work of communicating with a kernel in the subprocess
environment.
* jupyter-channels.el: Remove `jupyter-async-channel`
* jupyter-ioloop.el: New file.
(jupyter-ioloop-poller):
(jupyter-ioloop-nsockets):
(jupyter-ioloop-pre-hook):
(jupyter-ioloop-post-hook):
(jupyter-ioloop--argument-types): New variables.
(jupyter-ioloop): New function to create `jupyter-ioloop` objects.
(jupyter-ioloop-add-setup):
(jupyter-ioloop-add-teardown):
(jupyter-ioloop-add-arg-type):
(jupyter-ioloop-add-event): New macros.
(jupyter-ioloop-handler):
(jupyter-ioloop-add-callback):
(jupyter-ioloop-start):
(jupyter-ioloop-stop): New methods.
(jupyter-send): Method addition for `jupyter-ioloop` objects.
(jupyter-ioloop-wait-until): Moved function from `jupyter-client.el`, changed
argument order.
(jupyter-ioloop--replace-args):
(jupyter-ioloop--event-dispatcher):
(jupyter-ioloop--function):
(jupyter-ioloop--filter):
(jupyter-ioloop--sentinel): Helper functions.
* jupyter-client.el: Add `jupyter-ioloop` require.
Remove `jupyter-shell-channel`, `jupyter-iopub-channel`,
`jupyter-stdin-channel` classes.
(jupyter-kernel-client): Change type of `ioloop` slot to `jupyter-ioloop`,
remove `shell-channel`, iopub-channel`, `stdin-channel`, and `hb-channel`
slots. Add `channels` slot.
(jupyter-initialize-connection): Replace channel initialization with
initialization of new `channels` slot.
(jupyter-send): Add better assertion. Call an ioloop's `jupyter-send` method.
Expect a Jupyter channel keyword as the `channel` argument.
(jupyter--ioloop-do-command):
(jupyter--ioloop-with-lock-file):
(jupyter--ioloop-unlock):
(jupyter--ioloop-lock):
(jupyter--ioloop):
(jupyter--ioloop-sentinel):
(jupyter--get-channel):
(jupyter--ioloop-filter):
(jupyter--start-ioloop): Remove functions/macros.
(jupyter-ioloop-wait-until): Move to `jupyter-ioloop.el`
(jupyter-hb-pause):
(jupyter-hb-unpause):
(jupyter-hb-beating-p): Use `channels` slot.
(jupyter-client--ioloop-setup-form): New constant.
(jupyter-ioloop-add-arg-type): New jupyter-channel argument type.
(jupyter-ioloop-add-start-channel-event):
(jupyter-ioloop-add-stop-channel-event):
(jupyter-ioloop-add-send-event): New `jupyter-ioloop` event types for clients.
(jupyter-ioloop-start):
(jupyter-ioloop-handler): Method additions specializing to
`jupyter-kernel-client`s.
(jupyter-start-channel):
(jupyter-stop-channel):
(jupyter-channel-alive-p): Method additions for a client utilizing `channels` slot.
(jupyter-start-channels):
(jupyter-stop-channels):
(jupyter-channels-running-p): Use new `jupyter-ioloop` interface and channel
methods.
(jupyter-handle-message): (`jupyter-kernel-client` method) Add new `msg`
argument. Remove call to `jupyter-get-message`.
(jupyter-handle-message): (All channel methods) Replace
`jupyter-stdin-channel`, `jupyter-iopub-channel`, and `jupyter-shell-channel`
with `(eql :stdin)`, `(eql :iopub)`, and `(eql :shell)`
(jupyter-handle-input-request):
(jupyter-send-execute-request):
(jupyter-send-inspect-request):
(jupyter-send-complete-request):
(jupyter-send-history-request):
(jupyter-send-is-complete-request):
(jupyter-send-comm-info-request):
(jupyter-send-comm-open):
(jupyter-send-comm-msg):
(jupyter-send-comm-close):
(jupyter-sned-kernel-info-request):
(jupyter-send-shutdown-request): Remove uses of `shell-channel` and
`stdin-channel` slot. Pass in expected channel keyword as the `channel`
argument in the call to `jupyter-send`
`jupyter-channels.el` depends on the `jupyter-send` method defined in
`jupyter-messages.el` whereas `jupyter-messages.el` does not depend on any
functions in `jupyter-channels.el`.
* jupyter-channel.el: Require `jupyter-messages`
* jupyter-messages.el: Remove `jupyter-channels` require
* Add `jupyter-channel--reset-socket` which does the work of connecting the
heartbeat channel to its endpoint and takes care of possibly reseting the
socket after sending a ping.
* Actually set the paused slot to nil when un-pausing the
heartbeat channel
* Ensure the heartbeat socket is actually a socket when
receiving. This is to handle edge cases when the
heartbeat channel is stopped, but there still is a timer
left in the `timer-list`.
- Allow specifying a msg-id before a call to `jupyter-send`
- This avoid sending a message to the browser displaying the widgets on every
message send to the kernel. The previous implementation generated a new ID
without allowing the caller to pass one in.
- Simplify message polling by sending received messages from the kernel to the
parent Emacs process at the moment of arrival.
To make up for this:
- Move the functions defined in `jupyter-connection.el` to `jupyter-base.el`
- Add a `session` field to the `jupyter-kernel-manager` and `jupyter-channel`
classes
- Add a `kernel-info` field to the `jupyter-repl-client`
- Add a `conn-info` field to a `jupyter-session`
- Add a `session` field to `jupyter-kernel-client`
- Distinguish between an asynchronous channel and a synchronous one
- `jupyter-async-channel` :: Messages are sent and received through an ioloop
subprocess that the channel has access to. The
subprocess uses the `jupyter-sync-channel` for
sending and receiving messages.
- `jupyter-sync-channel` :: Messages are sent and received through a
`zmq-socket` in the current Emacs session.
- Define `jupyter-send` and `jupyter-recv` methods for channels.
- Update `jupyter-kernel-client` and ioloop interfaces to take into account
changes with channels.
This way when creating kernel clients with `jupyter-kernel-manager`, we can set
the `parent-instance` of the newly created client to the manager thereby
inheriting the `jupyter-session` from the kernel manager.
Also have `jupyter-channel` objects inherit from a `jupyter-connection` so that
we can define `jupyter-send` and `jupyter-recv` methods for channels in the future.
- `jupyter-client.el` only contains client related code
- `jupyter-kernel-manager` related code is placed in `jupyter-kernel-manager.el`
- Socket creating functions and generating connection info plist function are
placed in `jupyter-connection.el`. This also contains the
`jupyter-connection` class.
- Kernelspec related functions are placed in `jupyter-kernelspec.el`
- Move general utility functions and variables requires necessary for `jupyter`
into `jupyter-base.el`. This also contains the `jupyter-session` and
`jupyter-request` struct definitions.