From e332f01ad50c192602be4382660822c970fd6467 2010-09-13 20:51:38 From: Brian Granger Date: 2010-09-13 20:51:38 Subject: [PATCH] New connect_request message type added. --- diff --git a/IPython/zmq/entry_point.py b/IPython/zmq/entry_point.py index 411166c..b20c6b1 100644 --- a/IPython/zmq/entry_point.py +++ b/IPython/zmq/entry_point.py @@ -97,7 +97,8 @@ def make_kernel(namespace, kernel_factory, hb = Heartbeat(context, (namespace.ip, namespace.hb)) hb.start() - io.raw_print("Heartbeat REP Channel on port", hb.port) + hb_port = hb.port + io.raw_print("Heartbeat REP Channel on port", hb_port) # Redirect input streams and set a display hook. if out_stream_factory: @@ -107,8 +108,11 @@ def make_kernel(namespace, kernel_factory, sys.displayhook = display_hook_factory(session, pub_socket) # Create the kernel. - return kernel_factory(session=session, reply_socket=reply_socket, - pub_socket=pub_socket, req_socket=req_socket) + kernel = kernel_factory(session=session, reply_socket=reply_socket, + pub_socket=pub_socket, req_socket=req_socket) + kernel.record_ports(xrep_port=xrep_port, pub_port=pub_port, + req_port=req_port, hb_port=hb_port) + return kernel def start_kernel(namespace, kernel): diff --git a/IPython/zmq/ipkernel.py b/IPython/zmq/ipkernel.py index b2e0f5c..0f13e94 100755 --- a/IPython/zmq/ipkernel.py +++ b/IPython/zmq/ipkernel.py @@ -75,6 +75,10 @@ class Kernel(Configurable): # the end of our shutdown process (which happens after the underlying # IPython shell's own shutdown). _shutdown_message = None + + # This is a dict of port number that the kernel is listening on. It is set + # by record_ports and used by connect_request. + _recorded_ports = None def __init__(self, **kwargs): super(Kernel, self).__init__(**kwargs) @@ -143,7 +147,20 @@ class Kernel(Configurable): while True: time.sleep(self._poll_interval) self.do_one_iteration() - + + def record_ports(self, xrep_port, pub_port, req_port, hb_port): + """Record the ports that this kernel is using. + + The creator of the Kernel instance must call this methods if they + want the :meth:`connect_request` method to return the port numbers. + """ + self._recorded_ports = { + 'xrep_port' : xreq_port, + 'pub_port' : pub_port, + 'req_port' : req_port, + 'hb_port' : hb_port + } + #--------------------------------------------------------------------------- # Kernel request handlers #--------------------------------------------------------------------------- @@ -287,12 +304,21 @@ class Kernel(Configurable): msg = self.session.send(self.reply_socket, 'history_reply', content, parent, ident) io.raw_print(msg) - + + def connect_request(self, ident, parent): + if self._recorded_ports is not None: + content = self._recorded_ports.copy() + else: + content = {} + msg = self.session.send(self.reply_socket, 'connect_reply', + content, parent, ident) + io.raw_print(msg) + def shutdown_request(self, ident, parent): self.shell.exit_now = True self._shutdown_message = self.session.msg(u'shutdown_reply', {}, parent) sys.exit(0) - + #--------------------------------------------------------------------------- # Protected interface #--------------------------------------------------------------------------- diff --git a/docs/source/development/messaging.txt b/docs/source/development/messaging.txt index 910cc58..731f9e9 100644 --- a/docs/source/development/messaging.txt +++ b/docs/source/development/messaging.txt @@ -536,6 +536,30 @@ Message type: ``history_reply``:: } +Connect +------- + +When a client connects to the request/reply socket of the kernel, it can issue +a connect request to get basic information about the kernel, such as the ports +the other ZeroMQ sockets are listening on. This allows clients to only have +to know about a single port (the XREQ/XREP channel) to connect to a kernel. + +Message type: ``connect_request``:: + + content = { + } + +Message type: ``connect_reply``:: + + content = { + 'xrep_port' : int # The port the XREP socket is listening on. + 'pub_port' : int # The port the PUB socket is listening on. + 'req_port' : int # The port the REQ socket is listening on. + 'hb_port' : int # The port the heartbeat socket is listening on. + } + + + Kernel shutdown ---------------