##// END OF EJS Templates
add version key to js message headers
MinRK -
Show More
@@ -1,618 +1,619
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 //============================================================================
5 5 // Kernel
6 6 //============================================================================
7 7
8 8 /**
9 9 * @module IPython
10 10 * @namespace IPython
11 11 * @submodule Kernel
12 12 */
13 13
14 14 var IPython = (function (IPython) {
15 15 "use strict";
16 16
17 17 var utils = IPython.utils;
18 18
19 19 // Initialization and connection.
20 20 /**
21 21 * A Kernel Class to communicate with the Python kernel
22 22 * @Class Kernel
23 23 */
24 24 var Kernel = function (kernel_service_url) {
25 25 this.kernel_id = null;
26 26 this.shell_channel = null;
27 27 this.iopub_channel = null;
28 28 this.stdin_channel = null;
29 29 this.kernel_service_url = kernel_service_url;
30 30 this.running = false;
31 31 this.username = "username";
32 32 this.session_id = utils.uuid();
33 33 this._msg_callbacks = {};
34 34 this.post = $.post;
35 35
36 36 if (typeof(WebSocket) !== 'undefined') {
37 37 this.WebSocket = WebSocket;
38 38 } else if (typeof(MozWebSocket) !== 'undefined') {
39 39 this.WebSocket = MozWebSocket;
40 40 } else {
41 41 alert('Your browser does not have WebSocket support, please try Chrome, Safari or Firefox ≥ 6. Firefox 4 and 5 are also supported by you have to enable WebSockets in about:config.');
42 42 }
43 43
44 44 this.bind_events();
45 45 this.init_iopub_handlers();
46 46 this.comm_manager = new IPython.CommManager(this);
47 47 this.widget_manager = new IPython.WidgetManager(this.comm_manager);
48 48
49 49 this.last_msg_id = null;
50 50 this.last_msg_callbacks = {};
51 51 };
52 52
53 53
54 54 Kernel.prototype._get_msg = function (msg_type, content, metadata) {
55 55 var msg = {
56 56 header : {
57 57 msg_id : utils.uuid(),
58 58 username : this.username,
59 59 session : this.session_id,
60 msg_type : msg_type
60 msg_type : msg_type,
61 version : "5.0"
61 62 },
62 63 metadata : metadata || {},
63 64 content : content,
64 65 parent_header : {}
65 66 };
66 67 return msg;
67 68 };
68 69
69 70 Kernel.prototype.bind_events = function () {
70 71 var that = this;
71 72 $([IPython.events]).on('send_input_reply.Kernel', function(evt, data) {
72 73 that.send_input_reply(data);
73 74 });
74 75 };
75 76
76 77 // Initialize the iopub handlers
77 78
78 79 Kernel.prototype.init_iopub_handlers = function () {
79 80 var output_msg_types = ['stream', 'display_data', 'execute_result', 'error'];
80 81 this._iopub_handlers = {};
81 82 this.register_iopub_handler('status', $.proxy(this._handle_status_message, this));
82 83 this.register_iopub_handler('clear_output', $.proxy(this._handle_clear_output, this));
83 84
84 85 for (var i=0; i < output_msg_types.length; i++) {
85 86 this.register_iopub_handler(output_msg_types[i], $.proxy(this._handle_output_message, this));
86 87 }
87 88 };
88 89
89 90 /**
90 91 * Start the Python kernel
91 92 * @method start
92 93 */
93 94 Kernel.prototype.start = function (params) {
94 95 params = params || {};
95 96 if (!this.running) {
96 97 var qs = $.param(params);
97 98 this.post(utils.url_join_encode(this.kernel_service_url) + '?' + qs,
98 99 $.proxy(this._kernel_started, this),
99 100 'json'
100 101 );
101 102 }
102 103 };
103 104
104 105 /**
105 106 * Restart the python kernel.
106 107 *
107 108 * Emit a 'status_restarting.Kernel' event with
108 109 * the current object as parameter
109 110 *
110 111 * @method restart
111 112 */
112 113 Kernel.prototype.restart = function () {
113 114 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
114 115 if (this.running) {
115 116 this.stop_channels();
116 117 this.post(utils.url_join_encode(this.kernel_url, "restart"),
117 118 $.proxy(this._kernel_started, this),
118 119 'json'
119 120 );
120 121 }
121 122 };
122 123
123 124
124 125 Kernel.prototype._kernel_started = function (json) {
125 126 console.log("Kernel started: ", json.id);
126 127 this.running = true;
127 128 this.kernel_id = json.id;
128 129 // trailing 's' in https will become wss for secure web sockets
129 130 this.ws_host = location.protocol.replace('http', 'ws') + "//" + location.host;
130 131 this.kernel_url = utils.url_path_join(this.kernel_service_url, this.kernel_id);
131 132 this.start_channels();
132 133 };
133 134
134 135
135 136 Kernel.prototype._websocket_closed = function(ws_url, early) {
136 137 this.stop_channels();
137 138 $([IPython.events]).trigger('websocket_closed.Kernel',
138 139 {ws_url: ws_url, kernel: this, early: early}
139 140 );
140 141 };
141 142
142 143 /**
143 144 * Start the `shell`and `iopub` channels.
144 145 * Will stop and restart them if they already exist.
145 146 *
146 147 * @method start_channels
147 148 */
148 149 Kernel.prototype.start_channels = function () {
149 150 var that = this;
150 151 this.stop_channels();
151 152 var ws_host_url = this.ws_host + this.kernel_url;
152 153 console.log("Starting WebSockets:", ws_host_url);
153 154 this.shell_channel = new this.WebSocket(
154 155 this.ws_host + utils.url_join_encode(this.kernel_url, "shell")
155 156 );
156 157 this.stdin_channel = new this.WebSocket(
157 158 this.ws_host + utils.url_join_encode(this.kernel_url, "stdin")
158 159 );
159 160 this.iopub_channel = new this.WebSocket(
160 161 this.ws_host + utils.url_join_encode(this.kernel_url, "iopub")
161 162 );
162 163
163 164 var already_called_onclose = false; // only alert once
164 165 var ws_closed_early = function(evt){
165 166 if (already_called_onclose){
166 167 return;
167 168 }
168 169 already_called_onclose = true;
169 170 if ( ! evt.wasClean ){
170 171 that._websocket_closed(ws_host_url, true);
171 172 }
172 173 };
173 174 var ws_closed_late = function(evt){
174 175 if (already_called_onclose){
175 176 return;
176 177 }
177 178 already_called_onclose = true;
178 179 if ( ! evt.wasClean ){
179 180 that._websocket_closed(ws_host_url, false);
180 181 }
181 182 };
182 183 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
183 184 for (var i=0; i < channels.length; i++) {
184 185 channels[i].onopen = $.proxy(this._ws_opened, this);
185 186 channels[i].onclose = ws_closed_early;
186 187 }
187 188 // switch from early-close to late-close message after 1s
188 189 setTimeout(function() {
189 190 for (var i=0; i < channels.length; i++) {
190 191 if (channels[i] !== null) {
191 192 channels[i].onclose = ws_closed_late;
192 193 }
193 194 }
194 195 }, 1000);
195 196 this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this);
196 197 this.iopub_channel.onmessage = $.proxy(this._handle_iopub_message, this);
197 198 this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this);
198 199 };
199 200
200 201 /**
201 202 * Handle a websocket entering the open state
202 203 * sends session and cookie authentication info as first message.
203 204 * Once all sockets are open, signal the Kernel.status_started event.
204 205 * @method _ws_opened
205 206 */
206 207 Kernel.prototype._ws_opened = function (evt) {
207 208 // send the session id so the Session object Python-side
208 209 // has the same identity
209 210 evt.target.send(this.session_id + ':' + document.cookie);
210 211
211 212 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
212 213 for (var i=0; i < channels.length; i++) {
213 214 // if any channel is not ready, don't trigger event.
214 215 if ( !channels[i].readyState ) return;
215 216 }
216 217 // all events ready, trigger started event.
217 218 $([IPython.events]).trigger('status_started.Kernel', {kernel: this});
218 219 };
219 220
220 221 /**
221 222 * Stop the websocket channels.
222 223 * @method stop_channels
223 224 */
224 225 Kernel.prototype.stop_channels = function () {
225 226 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
226 227 for (var i=0; i < channels.length; i++) {
227 228 if ( channels[i] !== null ) {
228 229 channels[i].onclose = null;
229 230 channels[i].close();
230 231 }
231 232 }
232 233 this.shell_channel = this.iopub_channel = this.stdin_channel = null;
233 234 };
234 235
235 236 // Main public methods.
236 237
237 238 // send a message on the Kernel's shell channel
238 239 Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata) {
239 240 var msg = this._get_msg(msg_type, content, metadata);
240 241 this.shell_channel.send(JSON.stringify(msg));
241 242 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
242 243 return msg.header.msg_id;
243 244 };
244 245
245 246 /**
246 247 * Get kernel info
247 248 *
248 249 * @param callback {function}
249 250 * @method kernel_info
250 251 *
251 252 * When calling this method, pass a callback function that expects one argument.
252 253 * The callback will be passed the complete `kernel_info_reply` message documented
253 254 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#kernel-info)
254 255 */
255 256 Kernel.prototype.kernel_info = function (callback) {
256 257 var callbacks;
257 258 if (callback) {
258 259 callbacks = { shell : { reply : callback } };
259 260 }
260 261 return this.send_shell_message("kernel_info_request", {}, callbacks);
261 262 };
262 263
263 264 /**
264 265 * Get info on an object
265 266 *
266 267 * @param code {string}
267 268 * @param cursor_pos {integer}
268 269 * @param callback {function}
269 270 * @method inspect
270 271 *
271 272 * When calling this method, pass a callback function that expects one argument.
272 273 * The callback will be passed the complete `inspect_reply` message documented
273 274 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
274 275 */
275 276 Kernel.prototype.inspect = function (code, cursor_pos, callback) {
276 277 var callbacks;
277 278 if (callback) {
278 279 callbacks = { shell : { reply : callback } };
279 280 }
280 281
281 282 var content = {
282 283 code : code,
283 284 cursor_pos : cursor_pos,
284 285 detail_level : 0,
285 286 };
286 287 return this.send_shell_message("inspect_request", content, callbacks);
287 288 };
288 289
289 290 /**
290 291 * Execute given code into kernel, and pass result to callback.
291 292 *
292 293 * @async
293 294 * @method execute
294 295 * @param {string} code
295 296 * @param [callbacks] {Object} With the following keys (all optional)
296 297 * @param callbacks.shell.reply {function}
297 298 * @param callbacks.shell.payload.[payload_name] {function}
298 299 * @param callbacks.iopub.output {function}
299 300 * @param callbacks.iopub.clear_output {function}
300 301 * @param callbacks.input {function}
301 302 * @param {object} [options]
302 303 * @param [options.silent=false] {Boolean}
303 304 * @param [options.user_expressions=empty_dict] {Dict}
304 305 * @param [options.allow_stdin=false] {Boolean} true|false
305 306 *
306 307 * @example
307 308 *
308 309 * The options object should contain the options for the execute call. Its default
309 310 * values are:
310 311 *
311 312 * options = {
312 313 * silent : true,
313 314 * user_expressions : {},
314 315 * allow_stdin : false
315 316 * }
316 317 *
317 318 * When calling this method pass a callbacks structure of the form:
318 319 *
319 320 * callbacks = {
320 321 * shell : {
321 322 * reply : execute_reply_callback,
322 323 * payload : {
323 324 * set_next_input : set_next_input_callback,
324 325 * }
325 326 * },
326 327 * iopub : {
327 328 * output : output_callback,
328 329 * clear_output : clear_output_callback,
329 330 * },
330 331 * input : raw_input_callback
331 332 * }
332 333 *
333 334 * Each callback will be passed the entire message as a single arugment.
334 335 * Payload handlers will be passed the corresponding payload and the execute_reply message.
335 336 */
336 337 Kernel.prototype.execute = function (code, callbacks, options) {
337 338
338 339 var content = {
339 340 code : code,
340 341 silent : true,
341 342 store_history : false,
342 343 user_expressions : {},
343 344 allow_stdin : false
344 345 };
345 346 callbacks = callbacks || {};
346 347 if (callbacks.input !== undefined) {
347 348 content.allow_stdin = true;
348 349 }
349 350 $.extend(true, content, options);
350 351 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
351 352 return this.send_shell_message("execute_request", content, callbacks);
352 353 };
353 354
354 355 /**
355 356 * When calling this method, pass a function to be called with the `complete_reply` message
356 357 * as its only argument when it arrives.
357 358 *
358 359 * `complete_reply` is documented
359 360 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
360 361 *
361 362 * @method complete
362 363 * @param code {string}
363 364 * @param cursor_pos {integer}
364 365 * @param callback {function}
365 366 *
366 367 */
367 368 Kernel.prototype.complete = function (code, cursor_pos, callback) {
368 369 var callbacks;
369 370 if (callback) {
370 371 callbacks = { shell : { reply : callback } };
371 372 }
372 373 var content = {
373 374 code : code,
374 375 cursor_pos : cursor_pos,
375 376 };
376 377 return this.send_shell_message("complete_request", content, callbacks);
377 378 };
378 379
379 380
380 381 Kernel.prototype.interrupt = function () {
381 382 if (this.running) {
382 383 $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this});
383 384 this.post(utils.url_join_encode(this.kernel_url, "interrupt"));
384 385 }
385 386 };
386 387
387 388
388 389 Kernel.prototype.kill = function () {
389 390 if (this.running) {
390 391 this.running = false;
391 392 var settings = {
392 393 cache : false,
393 394 type : "DELETE",
394 395 error : utils.log_ajax_error,
395 396 };
396 397 $.ajax(utils.url_join_encode(this.kernel_url), settings);
397 398 }
398 399 };
399 400
400 401 Kernel.prototype.send_input_reply = function (input) {
401 402 var content = {
402 403 value : input,
403 404 };
404 405 $([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content});
405 406 var msg = this._get_msg("input_reply", content);
406 407 this.stdin_channel.send(JSON.stringify(msg));
407 408 return msg.header.msg_id;
408 409 };
409 410
410 411
411 412 // Reply handlers
412 413
413 414 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
414 415 this._iopub_handlers[msg_type] = callback;
415 416 };
416 417
417 418 Kernel.prototype.get_iopub_handler = function (msg_type) {
418 419 // get iopub handler for a specific message type
419 420 return this._iopub_handlers[msg_type];
420 421 };
421 422
422 423
423 424 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
424 425 // get callbacks for a specific message
425 426 if (msg_id == this.last_msg_id) {
426 427 return this.last_msg_callbacks;
427 428 } else {
428 429 return this._msg_callbacks[msg_id];
429 430 }
430 431 };
431 432
432 433
433 434 Kernel.prototype.clear_callbacks_for_msg = function (msg_id) {
434 435 if (this._msg_callbacks[msg_id] !== undefined ) {
435 436 delete this._msg_callbacks[msg_id];
436 437 }
437 438 };
438 439
439 440 Kernel.prototype._finish_shell = function (msg_id) {
440 441 var callbacks = this._msg_callbacks[msg_id];
441 442 if (callbacks !== undefined) {
442 443 callbacks.shell_done = true;
443 444 if (callbacks.iopub_done) {
444 445 this.clear_callbacks_for_msg(msg_id);
445 446 }
446 447 }
447 448 };
448 449
449 450 Kernel.prototype._finish_iopub = function (msg_id) {
450 451 var callbacks = this._msg_callbacks[msg_id];
451 452 if (callbacks !== undefined) {
452 453 callbacks.iopub_done = true;
453 454 if (!callbacks.shell_done) {
454 455 this.clear_callbacks_for_msg(msg_id);
455 456 }
456 457 }
457 458 };
458 459
459 460 /* Set callbacks for a particular message.
460 461 * Callbacks should be a struct of the following form:
461 462 * shell : {
462 463 *
463 464 * }
464 465
465 466 */
466 467 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
467 468 this.last_msg_id = msg_id;
468 469 if (callbacks) {
469 470 // shallow-copy mapping, because we will modify it at the top level
470 471 var cbcopy = this._msg_callbacks[msg_id] = this.last_msg_callbacks = {};
471 472 cbcopy.shell = callbacks.shell;
472 473 cbcopy.iopub = callbacks.iopub;
473 474 cbcopy.input = callbacks.input;
474 475 cbcopy.shell_done = (!callbacks.shell);
475 476 cbcopy.iopub_done = (!callbacks.iopub);
476 477 } else {
477 478 this.last_msg_callbacks = {};
478 479 }
479 480 };
480 481
481 482
482 483 Kernel.prototype._handle_shell_reply = function (e) {
483 484 var reply = $.parseJSON(e.data);
484 485 $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply});
485 486 var content = reply.content;
486 487 var metadata = reply.metadata;
487 488 var parent_id = reply.parent_header.msg_id;
488 489 var callbacks = this.get_callbacks_for_msg(parent_id);
489 490 if (!callbacks || !callbacks.shell) {
490 491 return;
491 492 }
492 493 var shell_callbacks = callbacks.shell;
493 494
494 495 // signal that shell callbacks are done
495 496 this._finish_shell(parent_id);
496 497
497 498 if (shell_callbacks.reply !== undefined) {
498 499 shell_callbacks.reply(reply);
499 500 }
500 501 if (content.payload && shell_callbacks.payload) {
501 502 this._handle_payloads(content.payload, shell_callbacks.payload, reply);
502 503 }
503 504 };
504 505
505 506
506 507 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
507 508 var l = payloads.length;
508 509 // Payloads are handled by triggering events because we don't want the Kernel
509 510 // to depend on the Notebook or Pager classes.
510 511 for (var i=0; i<l; i++) {
511 512 var payload = payloads[i];
512 513 var callback = payload_callbacks[payload.source];
513 514 if (callback) {
514 515 callback(payload, msg);
515 516 }
516 517 }
517 518 };
518 519
519 520 Kernel.prototype._handle_status_message = function (msg) {
520 521 var execution_state = msg.content.execution_state;
521 522 var parent_id = msg.parent_header.msg_id;
522 523
523 524 // dispatch status msg callbacks, if any
524 525 var callbacks = this.get_callbacks_for_msg(parent_id);
525 526 if (callbacks && callbacks.iopub && callbacks.iopub.status) {
526 527 try {
527 528 callbacks.iopub.status(msg);
528 529 } catch (e) {
529 530 console.log("Exception in status msg handler", e, e.stack);
530 531 }
531 532 }
532 533
533 534 if (execution_state === 'busy') {
534 535 $([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
535 536 } else if (execution_state === 'idle') {
536 537 // signal that iopub callbacks are (probably) done
537 538 // async output may still arrive,
538 539 // but only for the most recent request
539 540 this._finish_iopub(parent_id);
540 541
541 542 // trigger status_idle event
542 543 $([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
543 544 } else if (execution_state === 'restarting') {
544 545 // autorestarting is distinct from restarting,
545 546 // in that it means the kernel died and the server is restarting it.
546 547 // status_restarting sets the notification widget,
547 548 // autorestart shows the more prominent dialog.
548 549 $([IPython.events]).trigger('status_autorestarting.Kernel', {kernel: this});
549 550 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
550 551 } else if (execution_state === 'dead') {
551 552 this.stop_channels();
552 553 $([IPython.events]).trigger('status_dead.Kernel', {kernel: this});
553 554 }
554 555 };
555 556
556 557
557 558 // handle clear_output message
558 559 Kernel.prototype._handle_clear_output = function (msg) {
559 560 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
560 561 if (!callbacks || !callbacks.iopub) {
561 562 return;
562 563 }
563 564 var callback = callbacks.iopub.clear_output;
564 565 if (callback) {
565 566 callback(msg);
566 567 }
567 568 };
568 569
569 570
570 571 // handle an output message (execute_result, display_data, etc.)
571 572 Kernel.prototype._handle_output_message = function (msg) {
572 573 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
573 574 if (!callbacks || !callbacks.iopub) {
574 575 return;
575 576 }
576 577 var callback = callbacks.iopub.output;
577 578 if (callback) {
578 579 callback(msg);
579 580 }
580 581 };
581 582
582 583 // dispatch IOPub messages to respective handlers.
583 584 // each message type should have a handler.
584 585 Kernel.prototype._handle_iopub_message = function (e) {
585 586 var msg = $.parseJSON(e.data);
586 587
587 588 var handler = this.get_iopub_handler(msg.header.msg_type);
588 589 if (handler !== undefined) {
589 590 handler(msg);
590 591 }
591 592 };
592 593
593 594
594 595 Kernel.prototype._handle_input_request = function (e) {
595 596 var request = $.parseJSON(e.data);
596 597 var header = request.header;
597 598 var content = request.content;
598 599 var metadata = request.metadata;
599 600 var msg_type = header.msg_type;
600 601 if (msg_type !== 'input_request') {
601 602 console.log("Invalid input request!", request);
602 603 return;
603 604 }
604 605 var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
605 606 if (callbacks) {
606 607 if (callbacks.input) {
607 608 callbacks.input(request);
608 609 }
609 610 }
610 611 };
611 612
612 613
613 614 IPython.Kernel = Kernel;
614 615
615 616 return IPython;
616 617
617 618 }(IPython));
618 619
General Comments 0
You need to be logged in to leave comments. Login now