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