Show More
@@ -1,20 +1,7 b'' | |||||
1 | """Tornado handlers for WebSocket <-> ZMQ sockets. |
|
1 | """Tornado handlers for WebSocket <-> ZMQ sockets.""" | |
2 |
|
2 | |||
3 | Authors: |
|
3 | # Copyright (c) IPython Development Team. | |
4 |
|
4 | # Distributed under the terms of the Modified BSD License. | ||
5 | * Brian Granger |
|
|||
6 | """ |
|
|||
7 |
|
||||
8 | #----------------------------------------------------------------------------- |
|
|||
9 | # Copyright (C) 2008-2011 The IPython Development Team |
|
|||
10 | # |
|
|||
11 | # Distributed under the terms of the BSD License. The full license is in |
|
|||
12 | # the file COPYING, distributed as part of this software. |
|
|||
13 | #----------------------------------------------------------------------------- |
|
|||
14 |
|
||||
15 | #----------------------------------------------------------------------------- |
|
|||
16 | # Imports |
|
|||
17 | #----------------------------------------------------------------------------- |
|
|||
18 |
|
5 | |||
19 | try: |
|
6 | try: | |
20 | from urllib.parse import urlparse # Py 3 |
|
7 | from urllib.parse import urlparse # Py 3 | |
@@ -37,9 +24,6 b' from IPython.utils.py3compat import PY3, cast_unicode' | |||||
37 |
|
24 | |||
38 | from .handlers import IPythonHandler |
|
25 | from .handlers import IPythonHandler | |
39 |
|
26 | |||
40 | #----------------------------------------------------------------------------- |
|
|||
41 | # ZMQ handlers |
|
|||
42 | #----------------------------------------------------------------------------- |
|
|||
43 |
|
27 | |||
44 | class ZMQStreamHandler(websocket.WebSocketHandler): |
|
28 | class ZMQStreamHandler(websocket.WebSocketHandler): | |
45 |
|
29 |
@@ -1,20 +1,7 b'' | |||||
1 |
"""Tornado handlers for |
|
1 | """Tornado handlers for kernels.""" | |
2 |
|
2 | |||
3 | Authors: |
|
3 | # Copyright (c) IPython Development Team. | |
4 |
|
4 | # Distributed under the terms of the Modified BSD License. | ||
5 | * Brian Granger |
|
|||
6 | """ |
|
|||
7 |
|
||||
8 | #----------------------------------------------------------------------------- |
|
|||
9 | # Copyright (C) 2008-2011 The IPython Development Team |
|
|||
10 | # |
|
|||
11 | # Distributed under the terms of the BSD License. The full license is in |
|
|||
12 | # the file COPYING, distributed as part of this software. |
|
|||
13 | #----------------------------------------------------------------------------- |
|
|||
14 |
|
||||
15 | #----------------------------------------------------------------------------- |
|
|||
16 | # Imports |
|
|||
17 | #----------------------------------------------------------------------------- |
|
|||
18 |
|
5 | |||
19 | import logging |
|
6 | import logging | |
20 | from tornado import web |
|
7 | from tornado import web | |
@@ -22,15 +9,13 b' from tornado import web' | |||||
22 | from zmq.utils import jsonapi |
|
9 | from zmq.utils import jsonapi | |
23 |
|
10 | |||
24 | from IPython.utils.jsonutil import date_default |
|
11 | from IPython.utils.jsonutil import date_default | |
|
12 | from IPython.utils.py3compat import string_types | |||
25 | from IPython.html.utils import url_path_join, url_escape |
|
13 | from IPython.html.utils import url_path_join, url_escape | |
26 |
|
14 | |||
27 | from ...base.handlers import IPythonHandler, json_errors |
|
15 | from ...base.handlers import IPythonHandler, json_errors | |
28 | from ...base.zmqhandlers import AuthenticatedZMQStreamHandler |
|
16 | from ...base.zmqhandlers import AuthenticatedZMQStreamHandler | |
29 |
|
17 | |||
30 | #----------------------------------------------------------------------------- |
|
18 | from IPython.core.release import kernel_protocol_version | |
31 | # Kernel handlers |
|
|||
32 | #----------------------------------------------------------------------------- |
|
|||
33 |
|
||||
34 |
|
19 | |||
35 | class MainKernelHandler(IPythonHandler): |
|
20 | class MainKernelHandler(IPythonHandler): | |
36 |
|
21 | |||
@@ -96,6 +81,34 b' class ZMQChannelHandler(AuthenticatedZMQStreamHandler):' | |||||
96 | km = self.kernel_manager |
|
81 | km = self.kernel_manager | |
97 | meth = getattr(km, 'connect_%s' % self.channel) |
|
82 | meth = getattr(km, 'connect_%s' % self.channel) | |
98 | self.zmq_stream = meth(self.kernel_id, identity=self.session.bsession) |
|
83 | self.zmq_stream = meth(self.kernel_id, identity=self.session.bsession) | |
|
84 | self.kernel_info_channel = None | |||
|
85 | self.kernel_info_channel = km.connect_shell(self.kernel_id) | |||
|
86 | self.kernel_info_channel.on_recv(self._handle_kernel_info) | |||
|
87 | self._request_kernel_info() | |||
|
88 | ||||
|
89 | def _request_kernel_info(self): | |||
|
90 | self.log.debug("requesting kernel info") | |||
|
91 | self.session.send(self.kernel_info_channel, "kernel_info_request") | |||
|
92 | ||||
|
93 | def _handle_kernel_info(self, msg): | |||
|
94 | idents,msg = self.session.feed_identities(msg) | |||
|
95 | try: | |||
|
96 | msg = self.session.unserialize(msg) | |||
|
97 | except: | |||
|
98 | self.log.error("Bad kernel_info reply", exc_info=True) | |||
|
99 | self._request_kernel_info() | |||
|
100 | return | |||
|
101 | else: | |||
|
102 | if msg['msg_type'] != 'kernel_info_reply' or 'protocol_version' not in msg['content']: | |||
|
103 | self.log.error("Kernel info request failed, assuming current %s", msg['content']) | |||
|
104 | else: | |||
|
105 | protocol_version = msg['content']['protocol_version'] | |||
|
106 | if protocol_version != kernel_protocol_version: | |||
|
107 | self.session.adapt_version = int(protocol_version.split('.')[0]) | |||
|
108 | self.log.info("adapting kernel to %s" % protocol_version) | |||
|
109 | self.kernel_info_channel.close() | |||
|
110 | self.kernel_info_channel = None | |||
|
111 | ||||
99 |
|
112 | |||
100 | def initialize(self, *args, **kwargs): |
|
113 | def initialize(self, *args, **kwargs): | |
101 | self.zmq_stream = None |
|
114 | self.zmq_stream = None |
@@ -63,7 +63,10 b' class BlockingIOPubChannel(BlockingChannelMixin, IOPubChannel):' | |||||
63 |
|
63 | |||
64 |
|
64 | |||
65 | class BlockingShellChannel(BlockingChannelMixin, ShellChannel): |
|
65 | class BlockingShellChannel(BlockingChannelMixin, ShellChannel): | |
66 | pass |
|
66 | def call_handlers(self, msg): | |
|
67 | if msg['msg_type'] == 'kernel_info_reply': | |||
|
68 | self._handle_kernel_info_reply(msg) | |||
|
69 | return super(BlockingShellChannel, self).call_handlers(msg) | |||
67 |
|
70 | |||
68 |
|
71 | |||
69 | class BlockingStdInChannel(BlockingChannelMixin, StdInChannel): |
|
72 | class BlockingStdInChannel(BlockingChannelMixin, StdInChannel): |
@@ -16,7 +16,8 b' import zmq' | |||||
16 | from zmq import ZMQError |
|
16 | from zmq import ZMQError | |
17 | from zmq.eventloop import ioloop, zmqstream |
|
17 | from zmq.eventloop import ioloop, zmqstream | |
18 |
|
18 | |||
19 | # Local imports |
|
19 | from IPython.core.release import kernel_protocol_version_info | |
|
20 | ||||
20 | from .channelsabc import ( |
|
21 | from .channelsabc import ( | |
21 | ShellChannelABC, IOPubChannelABC, |
|
22 | ShellChannelABC, IOPubChannelABC, | |
22 | HBChannelABC, StdInChannelABC, |
|
23 | HBChannelABC, StdInChannelABC, | |
@@ -27,6 +28,8 b' from IPython.utils.py3compat import string_types, iteritems' | |||||
27 | # Constants and exceptions |
|
28 | # Constants and exceptions | |
28 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
29 |
|
30 | |||
|
31 | major_protocol_version = kernel_protocol_version_info[0] | |||
|
32 | ||||
30 | class InvalidPortNumber(Exception): |
|
33 | class InvalidPortNumber(Exception): | |
31 | pass |
|
34 | pass | |
32 |
|
35 | |||
@@ -173,7 +176,8 b' class ZMQSocketChannel(Thread):' | |||||
173 | Unpacks message, and calls handlers with it. |
|
176 | Unpacks message, and calls handlers with it. | |
174 | """ |
|
177 | """ | |
175 | ident,smsg = self.session.feed_identities(msg) |
|
178 | ident,smsg = self.session.feed_identities(msg) | |
176 |
|
|
179 | msg = self.session.unserialize(smsg) | |
|
180 | self.call_handlers(msg) | |||
177 |
|
181 | |||
178 |
|
182 | |||
179 |
|
183 | |||
@@ -366,6 +370,15 b' class ShellChannel(ZMQSocketChannel):' | |||||
366 | self._queue_send(msg) |
|
370 | self._queue_send(msg) | |
367 | return msg['header']['msg_id'] |
|
371 | return msg['header']['msg_id'] | |
368 |
|
372 | |||
|
373 | def _handle_kernel_info_reply(self, msg): | |||
|
374 | """handle kernel info reply | |||
|
375 | ||||
|
376 | sets protocol adaptation version | |||
|
377 | """ | |||
|
378 | adapt_version = int(msg['content']['protocol_version'].split('.')[0]) | |||
|
379 | if adapt_version != major_protocol_version: | |||
|
380 | self.session.adapt_version = adapt_version | |||
|
381 | ||||
369 | def shutdown(self, restart=False): |
|
382 | def shutdown(self, restart=False): | |
370 | """Request an immediate kernel shutdown. |
|
383 | """Request an immediate kernel shutdown. | |
371 |
|
384 |
@@ -59,6 +59,7 b' class QtShellChannelMixin(ChannelQObject):' | |||||
59 | complete_reply = QtCore.Signal(object) |
|
59 | complete_reply = QtCore.Signal(object) | |
60 | inspect_reply = QtCore.Signal(object) |
|
60 | inspect_reply = QtCore.Signal(object) | |
61 | history_reply = QtCore.Signal(object) |
|
61 | history_reply = QtCore.Signal(object) | |
|
62 | kernel_info_reply = QtCore.Signal(object) | |||
62 |
|
63 | |||
63 | #--------------------------------------------------------------------------- |
|
64 | #--------------------------------------------------------------------------- | |
64 | # 'ShellChannel' interface |
|
65 | # 'ShellChannel' interface | |
@@ -72,6 +73,9 b' class QtShellChannelMixin(ChannelQObject):' | |||||
72 |
|
73 | |||
73 | # Emit signals for specialized message types. |
|
74 | # Emit signals for specialized message types. | |
74 | msg_type = msg['header']['msg_type'] |
|
75 | msg_type = msg['header']['msg_type'] | |
|
76 | if msg_type == 'kernel_info_reply': | |||
|
77 | self._handle_kernel_info_reply(msg) | |||
|
78 | ||||
75 | signal = getattr(self, msg_type, None) |
|
79 | signal = getattr(self, msg_type, None) | |
76 | if signal: |
|
80 | if signal: | |
77 | signal.emit(msg) |
|
81 | signal.emit(msg) |
General Comments 0
You need to be logged in to leave comments.
Login now