From f0ab2594b1fffee25c6f0ef2fdeb0283f38bc2f5 2011-07-21 03:42:34 From: Brian E. Granger Date: 2011-07-21 03:42:34 Subject: [PATCH] Work to adapt routers to new Session message protocol. --- diff --git a/IPython/frontend/html/notebook/handlers.py b/IPython/frontend/html/notebook/handlers.py index 2d722fd..f472197 100644 --- a/IPython/frontend/html/notebook/handlers.py +++ b/IPython/frontend/html/notebook/handlers.py @@ -1,17 +1,22 @@ +"""Tornado handlers for the notebook.""" + +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- + import datetime import json import logging import os import urllib -import uuid -from Queue import Queue from tornado import web from tornado import websocket +#----------------------------------------------------------------------------- +# Handlers +#----------------------------------------------------------------------------- -_kernel_id_regex = r"(?P\w+-\w+-\w+-\w+-\w+)" -_kernel_action_regex = r"(?Prestart|interrupt)" class MainHandler(web.RequestHandler): def get(self): @@ -39,58 +44,6 @@ class KernelActionHandler(web.RequestHandler): self.write(json.dumps(new_kernel_id)) -class ZMQStreamRouter(object): - - def __init__(self, zmq_stream): - self.zmq_stream = zmq_stream - self._clients = {} - self.zmq_stream.on_recv(self._on_zmq_reply) - - def register_client(self, client): - client_id = uuid.uuid4() - self._clients[client_id] = client - return client_id - - def unregister_client(self, client_id): - del self._clients[client_id] - - def copy_clients(self, router): - # Copy the clients of another router. - for client_id, client in router._clients.items(): - client.router = self - self._clients[client_id] = client - - -class IOPubStreamRouter(ZMQStreamRouter): - - def _on_zmq_reply(self, msg_list): - for client_id, client in self._clients.items(): - for msg in msg_list: - client.write_message(msg) - - def forward_unicode(self, client_id, msg): - # This is a SUB stream that we should never write to. - pass - - -class ShellStreamRouter(ZMQStreamRouter): - - def __init__(self, zmq_stream): - ZMQStreamRouter.__init__(self, zmq_stream) - self._request_queue = Queue() - - def _on_zmq_reply(self, msg_list): - client_id = self._request_queue.get(block=False) - client = self._clients.get(client_id) - if client is not None: - for msg in msg_list: - client.write_message(msg) - - def forward_unicode(self, client_id, msg): - self._request_queue.put(client_id) - self.zmq_stream.send_unicode(msg) - - class ZMQStreamHandler(websocket.WebSocketHandler): def initialize(self, stream_name): diff --git a/IPython/frontend/html/notebook/kernelmanager.py b/IPython/frontend/html/notebook/kernelmanager.py index 58b29e1..6afc6bd 100644 --- a/IPython/frontend/html/notebook/kernelmanager.py +++ b/IPython/frontend/html/notebook/kernelmanager.py @@ -4,7 +4,6 @@ # Imports #----------------------------------------------------------------------------- -import logging import signal import sys import uuid @@ -13,7 +12,7 @@ import zmq from IPython.config.configurable import LoggingConfigurable from IPython.zmq.ipkernel import launch_kernel -from IPython.utils.traitlets import Instance, Dict, Unicode +from IPython.utils.traitlets import Instance, Dict #----------------------------------------------------------------------------- # Classes diff --git a/IPython/frontend/html/notebook/notebookapp.py b/IPython/frontend/html/notebook/notebookapp.py index cf419fc..7bc7ed6 100644 --- a/IPython/frontend/html/notebook/notebookapp.py +++ b/IPython/frontend/html/notebook/notebookapp.py @@ -99,8 +99,8 @@ class NotebookWebApplication(web.Application): self._session_dict[kernel_id] = sm iopub_stream = sm.get_iopub_stream() shell_stream = sm.get_shell_stream() - iopub_router = IOPubStreamRouter(iopub_stream) - shell_router = ShellStreamRouter(shell_stream) + iopub_router = IOPubStreamRouter(iopub_stream, sm.session) + shell_router = ShellStreamRouter(shell_stream, sm.session) self._routers[(kernel_id, 'iopub')] = iopub_router self._routers[(kernel_id, 'shell')] = shell_router @@ -139,6 +139,8 @@ class NotebookWebApplication(web.Application): router = self._routers[(kernel_id, stream_name)] return router + + #----------------------------------------------------------------------------- # Aliases and Flags #----------------------------------------------------------------------------- diff --git a/IPython/frontend/html/notebook/routers.py b/IPython/frontend/html/notebook/routers.py index 2e5dd47..6bfaa89 100644 --- a/IPython/frontend/html/notebook/routers.py +++ b/IPython/frontend/html/notebook/routers.py @@ -1,11 +1,12 @@ import uuid from Queue import Queue - +import json class ZMQStreamRouter(object): - def __init__(self, zmq_stream): + def __init__(self, zmq_stream, session): self.zmq_stream = zmq_stream + self.session = session self._clients = {} self.zmq_stream.on_recv(self._on_zmq_reply) @@ -29,6 +30,7 @@ class IOPubStreamRouter(ZMQStreamRouter): def _on_zmq_reply(self, msg_list): for client_id, client in self._clients.items(): for msg in msg_list: + print "Got message: ", msg client.write_message(msg) def forward_unicode(self, client_id, msg): @@ -38,8 +40,8 @@ class IOPubStreamRouter(ZMQStreamRouter): class ShellStreamRouter(ZMQStreamRouter): - def __init__(self, zmq_stream): - ZMQStreamRouter.__init__(self, zmq_stream) + def __init__(self, zmq_stream, session): + ZMQStreamRouter.__init__(self, zmq_stream, session) self._request_queue = Queue() def _on_zmq_reply(self, msg_list): @@ -50,8 +52,7 @@ class ShellStreamRouter(ZMQStreamRouter): client.write_message(msg) def forward_unicode(self, client_id, msg): - self._request_queue.put(client_id) - self.zmq_stream.send_unicode(msg) - - + print "Inbound message: ", msg + self._request_queue.put(client_id) + self.session.send(self.zmq_stream, msg)