##// END OF EJS Templates
Implemented module and namespace pattern in js notebook.
Implemented module and namespace pattern in js notebook.

File last commit:

r4351:27afdf32
r4352:c5b0c06e
Show More
routers.py
117 lines | 4.1 KiB | text/x-python | PythonLexer
Brian E. Granger
Updating the notebook to work with the latex master....
r4348 """Routers that connect WebSockets to ZMQ sockets."""
#-----------------------------------------------------------------------------
# Copyright (C) 2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING.txt, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Brian E. Granger
Initial reorg of files complete....
r4341 import uuid
from Queue import Queue
Brian E. Granger
Work to adapt routers to new Session message protocol.
r4346 import json
Brian E. Granger
Initial reorg of files complete....
r4341
Brian E. Granger
More work on updating the notebook zmq forwarding.
r4347 from IPython.config.configurable import Configurable
from IPython.utils.traitlets import Instance, Int, Dict
Brian E. Granger
Initial reorg of files complete....
r4341
Brian E. Granger
Updating the notebook to work with the latex master....
r4348 #-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
Brian E. Granger
More work on updating the notebook zmq forwarding.
r4347 class ZMQStreamRouter(Configurable):
zmq_stream = Instance('zmq.eventloop.zmqstream.ZMQStream')
session = Instance('IPython.zmq.session.Session')
max_msg_size = Int(2048, config=True, help="""
The max raw message size accepted from the browser
over a WebSocket connection.
""")
_clients = Dict()
def __init__(self, **kwargs):
super(ZMQStreamRouter,self).__init__(**kwargs)
Brian E. Granger
Initial reorg of files complete....
r4341 self.zmq_stream.on_recv(self._on_zmq_reply)
def register_client(self, client):
Brian E. Granger
More work on updating the notebook zmq forwarding.
r4347 """Register a client, returning a client uuid."""
Brian E. Granger
Initial reorg of files complete....
r4341 client_id = uuid.uuid4()
self._clients[client_id] = client
return client_id
def unregister_client(self, client_id):
Brian E. Granger
More work on updating the notebook zmq forwarding.
r4347 """Unregister a client by its client uuid."""
Brian E. Granger
Initial reorg of files complete....
r4341 del self._clients[client_id]
def copy_clients(self, router):
Brian E. Granger
More work on updating the notebook zmq forwarding.
r4347 """Copy the clients of another router to this one.
This is used to enable the backend zeromq stream to disconnect
and reconnect while the WebSocket connections to browsers
remain, such as when a kernel is restarted.
"""
Brian E. Granger
Initial reorg of files complete....
r4341 for client_id, client in router._clients.items():
client.router = self
self._clients[client_id] = client
Brian E. Granger
More work on updating the notebook zmq forwarding.
r4347 def forward_msg(self, client_id, msg):
"""Forward a msg to a client by its id.
The default implementation of this will fail silently if a message
arrives on a socket that doesn't support it. This method should
use max_msg_size to check and silently discard message that are too
long."""
pass
def _on_zmq_reply(self, msg_list):
"""Handle a message the ZMQ stream sends to the router.
Usually, this is where the return message will be written to
clients that need it using client.write_message().
"""
pass
Brian E. Granger
Updating the notebook to work with the latex master....
r4348 def _reserialize_reply(self, msg_list):
"""Reserialize a reply message using JSON.
This takes the msg list from the ZMQ socket, unserializes it using
self.session and then serializes the result using JSON. This method
should be used by self._on_zmq_reply to build messages that can
be sent back to the browser.
"""
idents, msg_list = self.session.feed_identities(msg_list)
Brian E. Granger
Change unpack_message to unserialize in routers.py.
r4351 msg = self.session.unserialize(msg_list)
Brian E. Granger
Updating the notebook to work with the latex master....
r4348 msg['header'].pop('date')
return json.dumps(msg)
Brian E. Granger
Initial reorg of files complete....
r4341
class IOPubStreamRouter(ZMQStreamRouter):
def _on_zmq_reply(self, msg_list):
Brian E. Granger
Updating the notebook to work with the latex master....
r4348 msg = self._reserialize_reply(msg_list)
Brian E. Granger
Initial reorg of files complete....
r4341 for client_id, client in self._clients.items():
Brian E. Granger
Updating the notebook to work with the latex master....
r4348 client.write_message(msg)
Brian E. Granger
Initial reorg of files complete....
r4341
class ShellStreamRouter(ZMQStreamRouter):
Brian E. Granger
More work on updating the notebook zmq forwarding.
r4347 _request_queue = Instance(Queue,(),{})
Brian E. Granger
Initial reorg of files complete....
r4341
def _on_zmq_reply(self, msg_list):
Brian E. Granger
Updating the notebook to work with the latex master....
r4348 msg = self._reserialize_reply(msg_list)
Brian E. Granger
Initial reorg of files complete....
r4341 client_id = self._request_queue.get(block=False)
client = self._clients.get(client_id)
if client is not None:
Brian E. Granger
Updating the notebook to work with the latex master....
r4348 client.write_message(msg)
Brian E. Granger
Initial reorg of files complete....
r4341
Brian E. Granger
More work on updating the notebook zmq forwarding.
r4347 def forward_msg(self, client_id, msg):
if len(msg) < self.max_msg_size:
msg = json.loads(msg)
to_send = self.session.serialize(msg)
self._request_queue.put(client_id)
Brian E. Granger
Updating the notebook to work with the latex master....
r4348 self.session.send(self.zmq_stream, msg)
Brian E. Granger
Initial reorg of files complete....
r4341