##// END OF EJS Templates
Add CommManager.new_comm...
MinRK -
Show More
@@ -1,183 +1,192 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2013 The IPython Development Team
2 // Copyright (C) 2013 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // Comm and CommManager bases
9 // Comm and CommManager bases
10 //============================================================================
10 //============================================================================
11 /**
11 /**
12 * Base Comm classes
12 * Base Comm classes
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 * @submodule comm
15 * @submodule comm
16 */
16 */
17
17
18 var IPython = (function (IPython) {
18 var IPython = (function (IPython) {
19 "use strict";
19 "use strict";
20
20
21 //-----------------------------------------------------------------------
21 //-----------------------------------------------------------------------
22 // CommManager class
22 // CommManager class
23 //-----------------------------------------------------------------------
23 //-----------------------------------------------------------------------
24
24
25 var CommManager = function (kernel) {
25 var CommManager = function (kernel) {
26 this.comms = {};
26 this.comms = {};
27 this.targets = {};
27 this.targets = {};
28 if (kernel !== undefined) {
28 if (kernel !== undefined) {
29 this.init_kernel(kernel);
29 this.init_kernel(kernel);
30 }
30 }
31 };
31 };
32
32
33 CommManager.prototype.init_kernel = function (kernel) {
33 CommManager.prototype.init_kernel = function (kernel) {
34 // connect the kernel, and register message handlers
34 // connect the kernel, and register message handlers
35 this.kernel = kernel;
35 this.kernel = kernel;
36 var msg_types = ['comm_open', 'comm_msg', 'comm_close'];
36 var msg_types = ['comm_open', 'comm_msg', 'comm_close'];
37 for (var i = 0; i < msg_types.length; i++) {
37 for (var i = 0; i < msg_types.length; i++) {
38 var msg_type = msg_types[i];
38 var msg_type = msg_types[i];
39 kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this));
39 kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this));
40 }
40 }
41 };
41 };
42
42
43 CommManager.prototype.new_comm = function (target_name, data, callbacks, metadata, comm_id) {
44 // Create a new Comm, register it, and open its Kernel-side counterpart
45 // Mimics the auto-registration in `Comm.__init__` in the IPython Comm
46 var comm = new Comm(target_name, comm_id);
47 this.register_comm(comm);
48 comm.open(data, callbacks, metadata);
49 return comm;
50 };
51
43 CommManager.prototype.register_target = function (target_name, f) {
52 CommManager.prototype.register_target = function (target_name, f) {
44 // Register a target function for a given target name
53 // Register a target function for a given target name
45 this.targets[target_name] = f;
54 this.targets[target_name] = f;
46 };
55 };
47
56
48 CommManager.prototype.unregister_target = function (target_name, f) {
57 CommManager.prototype.unregister_target = function (target_name, f) {
49 // Unregister a target function for a given target name
58 // Unregister a target function for a given target name
50 delete this.targets[target_name];
59 delete this.targets[target_name];
51 };
60 };
52
61
53 CommManager.prototype.register_comm = function (comm) {
62 CommManager.prototype.register_comm = function (comm) {
54 // Register a comm in the mapping
63 // Register a comm in the mapping
55 this.comms[comm.comm_id] = comm;
64 this.comms[comm.comm_id] = comm;
56 comm.kernel = this.kernel;
65 comm.kernel = this.kernel;
57 return comm.comm_id;
66 return comm.comm_id;
58 };
67 };
59
68
60 CommManager.prototype.unregister_comm = function (comm_id) {
69 CommManager.prototype.unregister_comm = function (comm_id) {
61 // Remove a comm from the mapping
70 // Remove a comm from the mapping
62 delete this.comms[comm_id];
71 delete this.comms[comm_id];
63 };
72 };
64
73
65 // comm message handlers
74 // comm message handlers
66
75
67 CommManager.prototype.comm_open = function (msg) {
76 CommManager.prototype.comm_open = function (msg) {
68 var content = msg.content;
77 var content = msg.content;
69 var f = this.targets[content.target_name];
78 var f = this.targets[content.target_name];
70 if (f === undefined) {
79 if (f === undefined) {
71 console.log("No such target registered: ", content.target_name);
80 console.log("No such target registered: ", content.target_name);
72 console.log("Available targets are: ", this.targets);
81 console.log("Available targets are: ", this.targets);
73 return;
82 return;
74 }
83 }
75 var comm = new Comm(content.comm_id);
84 var comm = new Comm(content.target_name, content.comm_id);
76 this.register_comm(comm);
85 this.register_comm(comm);
77 try {
86 try {
78 f(comm, msg);
87 f(comm, msg);
79 } catch (e) {
88 } catch (e) {
80 console.log("Exception opening new comm:", e, msg);
89 console.log("Exception opening new comm:", e, msg);
81 comm.close();
90 comm.close();
82 this.unregister_comm(comm);
91 this.unregister_comm(comm);
83 }
92 }
84 };
93 };
85
94
86 CommManager.prototype.comm_close = function (msg) {
95 CommManager.prototype.comm_close = function (msg) {
87 var content = msg.content;
96 var content = msg.content;
88 var comm = this.comms[content.comm_id];
97 var comm = this.comms[content.comm_id];
89 if (comm === undefined) {
98 if (comm === undefined) {
90 return;
99 return;
91 }
100 }
92 delete this.comms[content.comm_id];
101 delete this.comms[content.comm_id];
93 try {
102 try {
94 comm.handle_close(msg);
103 comm.handle_close(msg);
95 } catch (e) {
104 } catch (e) {
96 console.log("Exception closing comm: ", e, msg);
105 console.log("Exception closing comm: ", e, msg);
97 }
106 }
98 };
107 };
99
108
100 CommManager.prototype.comm_msg = function (msg) {
109 CommManager.prototype.comm_msg = function (msg) {
101 var content = msg.content;
110 var content = msg.content;
102 var comm = this.comms[content.comm_id];
111 var comm = this.comms[content.comm_id];
103 if (comm === undefined) {
112 if (comm === undefined) {
104 return;
113 return;
105 }
114 }
106 try {
115 try {
107 comm.handle_msg(msg);
116 comm.handle_msg(msg);
108 } catch (e) {
117 } catch (e) {
109 console.log("Exception handling comm msg: ", e, msg);
118 console.log("Exception handling comm msg: ", e, msg);
110 }
119 }
111 };
120 };
112
121
113 //-----------------------------------------------------------------------
122 //-----------------------------------------------------------------------
114 // Comm base class
123 // Comm base class
115 //-----------------------------------------------------------------------
124 //-----------------------------------------------------------------------
116
125
117 var Comm = function (comm_id, target_name) {
126 var Comm = function (target_name, comm_id) {
118 this.comm_id = comm_id || new IPython.utils.uuid();
119 this.target_name = target_name;
127 this.target_name = target_name;
128 this.comm_id = comm_id || IPython.utils.uuid();
120 this._msg_callback = this._close_callback = null;
129 this._msg_callback = this._close_callback = null;
121 };
130 };
122
131
123 // methods for sending messages
132 // methods for sending messages
124 Comm.prototype.open = function (data, callbacks, metadata) {
133 Comm.prototype.open = function (data, callbacks, metadata) {
125 var content = {
134 var content = {
126 comm_id : this.comm_id,
135 comm_id : this.comm_id,
127 target_name : this.target_name,
136 target_name : this.target_name,
128 data : data || {},
137 data : data || {},
129 };
138 };
130 return this.kernel.send_shell_message("comm_open", content, callbacks, metadata);
139 return this.kernel.send_shell_message("comm_open", content, callbacks, metadata);
131 };
140 };
132
141
133 Comm.prototype.send = function (data, callbacks, metadata) {
142 Comm.prototype.send = function (data, callbacks, metadata) {
134 var content = {
143 var content = {
135 comm_id : this.comm_id,
144 comm_id : this.comm_id,
136 data : data || {},
145 data : data || {},
137 };
146 };
138 return this.kernel.send_shell_message("comm_msg", content, callbacks, metadata);
147 return this.kernel.send_shell_message("comm_msg", content, callbacks, metadata);
139 };
148 };
140
149
141 Comm.prototype.close = function (data, callbacks, metadata) {
150 Comm.prototype.close = function (data, callbacks, metadata) {
142 var content = {
151 var content = {
143 comm_id : this.comm_id,
152 comm_id : this.comm_id,
144 data : data || {},
153 data : data || {},
145 };
154 };
146 return this.kernel.send_shell_message("comm_close", content, callbacks, metadata);
155 return this.kernel.send_shell_message("comm_close", content, callbacks, metadata);
147 };
156 };
148
157
149 // methods for registering callbacks for incoming messages
158 // methods for registering callbacks for incoming messages
150 Comm.prototype._register_callback = function (key, callback) {
159 Comm.prototype._register_callback = function (key, callback) {
151 this['_' + key + '_callback'] = callback;
160 this['_' + key + '_callback'] = callback;
152 };
161 };
153
162
154 Comm.prototype.on_msg = function (callback) {
163 Comm.prototype.on_msg = function (callback) {
155 this._register_callback('msg', callback);
164 this._register_callback('msg', callback);
156 };
165 };
157
166
158 Comm.prototype.on_close = function (callback) {
167 Comm.prototype.on_close = function (callback) {
159 this._register_callback('close', callback);
168 this._register_callback('close', callback);
160 };
169 };
161
170
162 // methods for handling incoming messages
171 // methods for handling incoming messages
163
172
164 Comm.prototype._maybe_callback = function (key, msg) {
173 Comm.prototype._maybe_callback = function (key, msg) {
165 var callback = this['_' + key + '_callback'];
174 var callback = this['_' + key + '_callback'];
166 if (callback) callback(msg);
175 if (callback) callback(msg);
167 };
176 };
168
177
169 Comm.prototype.handle_msg = function (msg) {
178 Comm.prototype.handle_msg = function (msg) {
170 this._maybe_callback('msg', msg);
179 this._maybe_callback('msg', msg);
171 };
180 };
172
181
173 Comm.prototype.handle_close = function (msg) {
182 Comm.prototype.handle_close = function (msg) {
174 this._maybe_callback('close', msg);
183 this._maybe_callback('close', msg);
175 };
184 };
176
185
177 IPython.CommManager = CommManager;
186 IPython.CommManager = CommManager;
178 IPython.Comm = Comm;
187 IPython.Comm = Comm;
179
188
180 return IPython;
189 return IPython;
181
190
182 }(IPython));
191 }(IPython));
183
192
General Comments 0
You need to be logged in to leave comments. Login now