##// END OF EJS Templates
Move channels into their own object
Jessica B. Hamrick -
Show More
@@ -21,9 +21,11 define([
21 21 this.id = id;
22 22 this.name = name;
23 23
24 this.shell_channel = null;
25 this.iopub_channel = null;
26 this.stdin_channel = null;
24 this.channels = {
25 'shell': null,
26 'iopub': null,
27 'stdin': null
28 };
27 29
28 30 this.kernel_service_url = kernel_service_url;
29 31 this.kernel_url = utils.url_join_encode(this.kernel_service_url, this.id);
@@ -241,6 +243,7 define([
241 243
242 244 Kernel.prototype._kernel_connected = function () {
243 245 var that = this;
246 console.log('Connected to kernel: ', this.id);
244 247 this.events.trigger('status_connected.Kernel');
245 248 this.kernel_info(function () {
246 249 that.events.trigger('status_idle.Kernel');
@@ -264,13 +267,13 define([
264 267 this.stop_channels();
265 268 var ws_host_url = this.ws_url + this.kernel_url;
266 269 console.log("Starting WebSockets:", ws_host_url);
267 this.shell_channel = new this.WebSocket(
270 this.channels.shell = new this.WebSocket(
268 271 this.ws_url + utils.url_join_encode(this.kernel_url, "shell")
269 272 );
270 this.stdin_channel = new this.WebSocket(
273 this.channels.stdin = new this.WebSocket(
271 274 this.ws_url + utils.url_join_encode(this.kernel_url, "stdin")
272 275 );
273 this.iopub_channel = new this.WebSocket(
276 this.channels.iopub = new this.WebSocket(
274 277 this.ws_url + utils.url_join_encode(this.kernel_url, "iopub")
275 278 );
276 279
@@ -300,23 +303,23 define([
300 303 already_called_onclose = true;
301 304 that._ws_closed(ws_host_url, false);
302 305 };
303 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
304 for (var i=0; i < channels.length; i++) {
305 channels[i].onopen = $.proxy(this._ws_opened, this);
306 channels[i].onclose = ws_closed_early;
307 channels[i].onerror = ws_error;
306
307 for (var c in this.channels) {
308 this.channels[c].onopen = $.proxy(this._ws_opened, this);
309 this.channels[c].onclose = ws_closed_early;
310 this.channels[c].onerror = ws_error;
308 311 }
309 312 // switch from early-close to late-close message after 1s
310 313 setTimeout(function() {
311 for (var i=0; i < channels.length; i++) {
312 if (channels[i] !== null) {
313 channels[i].onclose = ws_closed_late;
314 for (var c in that.channels) {
315 if (that.channels[c] !== null) {
316 that.channels[c].onclose = ws_closed_late;
314 317 }
315 318 }
316 319 }, 1000);
317 this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this);
318 this.iopub_channel.onmessage = $.proxy(this._handle_iopub_message, this);
319 this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this);
320 this.channels.shell.onmessage = $.proxy(this._handle_shell_reply, this);
321 this.channels.iopub.onmessage = $.proxy(this._handle_iopub_message, this);
322 this.channels.stdin.onmessage = $.proxy(this._handle_input_request, this);
320 323 };
321 324
322 325 /**
@@ -332,8 +335,7 define([
332 335
333 336 if (this.is_connected()) {
334 337 // all events ready, trigger started event.
335 console.log('Connected to kernel: ', this.id);
336 this.events.trigger('status_connected.Kernel', {kernel: this});
338 this._kernel_connected();
337 339 }
338 340 };
339 341
@@ -353,26 +355,24 define([
353 355 * @method stop_channels
354 356 */
355 357 Kernel.prototype.stop_channels = function () {
356 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
357 for (var i=0; i < channels.length; i++) {
358 if ( channels[i] !== null ) {
359 channels[i].onclose = null;
360 channels[i].close();
358 for (var c in this.channels) {
359 if ( this.channels[c] !== null ) {
360 this.channels[c].onclose = null;
361 this.channels[c].close();
362 this.channels[c] = null;
361 363 }
362 364 }
363 this.shell_channel = this.iopub_channel = this.stdin_channel = null;
364 365 };
365 366
366 367 // Main public methods.
367 368
368 369 Kernel.prototype.is_connected = function () {
369 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
370 for (var i=0; i < channels.length; i++) {
370 for (var c in this.channels) {
371 371 // if any channel is not ready, then we're not connected
372 if (channels[i] === null) {
372 if (this.channels[c] === null) {
373 373 return false;
374 374 }
375 if (channels[i].readyState !== WebSocket.OPEN) {
375 if (this.channels[c].readyState !== WebSocket.OPEN) {
376 376 return false;
377 377 }
378 378 }
@@ -385,7 +385,7 define([
385 385 throw new Error("kernel is not connected");
386 386 }
387 387 var msg = this._get_msg(msg_type, content, metadata);
388 this.shell_channel.send(JSON.stringify(msg));
388 this.channels.shell.send(JSON.stringify(msg));
389 389 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
390 390 return msg.header.msg_id;
391 391 };
@@ -532,7 +532,7 define([
532 532 };
533 533 this.events.trigger('input_reply.Kernel', {kernel: this, content:content});
534 534 var msg = this._get_msg("input_reply", content);
535 this.stdin_channel.send(JSON.stringify(msg));
535 this.channels.stdin.send(JSON.stringify(msg));
536 536 return msg.header.msg_id;
537 537 };
538 538
General Comments 0
You need to be logged in to leave comments. Login now