1 | /* |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, |
---|
13 | * software distributed under the License is distributed on an |
---|
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
---|
15 | * KIND, either express or implied. See the License for the |
---|
16 | * specific language governing permissions and limitations |
---|
17 | * under the License. |
---|
18 | */ |
---|
19 | |
---|
20 | var Guacamole = Guacamole || {}; |
---|
21 | |
---|
22 | /** |
---|
23 | * An object used by the Guacamole client to house arbitrarily-many named |
---|
24 | * input and output streams. |
---|
25 | * |
---|
26 | * @constructor |
---|
27 | * @param {Guacamole.Client} client |
---|
28 | * The client owning this object. |
---|
29 | * |
---|
30 | * @param {Number} index |
---|
31 | * The index of this object. |
---|
32 | */ |
---|
33 | Guacamole.Object = function guacamoleObject(client, index) { |
---|
34 | |
---|
35 | /** |
---|
36 | * Reference to this Guacamole.Object. |
---|
37 | * |
---|
38 | * @private |
---|
39 | * @type {Guacamole.Object} |
---|
40 | */ |
---|
41 | var guacObject = this; |
---|
42 | |
---|
43 | /** |
---|
44 | * Map of stream name to corresponding queue of callbacks. The queue of |
---|
45 | * callbacks is guaranteed to be in order of request. |
---|
46 | * |
---|
47 | * @private |
---|
48 | * @type {Object.<String, Function[]>} |
---|
49 | */ |
---|
50 | var bodyCallbacks = {}; |
---|
51 | |
---|
52 | /** |
---|
53 | * Removes and returns the callback at the head of the callback queue for |
---|
54 | * the stream having the given name. If no such callbacks exist, null is |
---|
55 | * returned. |
---|
56 | * |
---|
57 | * @private |
---|
58 | * @param {String} name |
---|
59 | * The name of the stream to retrieve a callback for. |
---|
60 | * |
---|
61 | * @returns {Function} |
---|
62 | * The next callback associated with the stream having the given name, |
---|
63 | * or null if no such callback exists. |
---|
64 | */ |
---|
65 | var dequeueBodyCallback = function dequeueBodyCallback(name) { |
---|
66 | |
---|
67 | // If no callbacks defined, simply return null |
---|
68 | var callbacks = bodyCallbacks[name]; |
---|
69 | if (!callbacks) |
---|
70 | return null; |
---|
71 | |
---|
72 | // Otherwise, pull off first callback, deleting the queue if empty |
---|
73 | var callback = callbacks.shift(); |
---|
74 | if (callbacks.length === 0) |
---|
75 | delete bodyCallbacks[name]; |
---|
76 | |
---|
77 | // Return found callback |
---|
78 | return callback; |
---|
79 | |
---|
80 | }; |
---|
81 | |
---|
82 | /** |
---|
83 | * Adds the given callback to the tail of the callback queue for the stream |
---|
84 | * having the given name. |
---|
85 | * |
---|
86 | * @private |
---|
87 | * @param {String} name |
---|
88 | * The name of the stream to associate with the given callback. |
---|
89 | * |
---|
90 | * @param {Function} callback |
---|
91 | * The callback to add to the queue of the stream with the given name. |
---|
92 | */ |
---|
93 | var enqueueBodyCallback = function enqueueBodyCallback(name, callback) { |
---|
94 | |
---|
95 | // Get callback queue by name, creating first if necessary |
---|
96 | var callbacks = bodyCallbacks[name]; |
---|
97 | if (!callbacks) { |
---|
98 | callbacks = []; |
---|
99 | bodyCallbacks[name] = callbacks; |
---|
100 | } |
---|
101 | |
---|
102 | // Add callback to end of queue |
---|
103 | callbacks.push(callback); |
---|
104 | |
---|
105 | }; |
---|
106 | |
---|
107 | /** |
---|
108 | * The index of this object. |
---|
109 | * |
---|
110 | * @type {Number} |
---|
111 | */ |
---|
112 | this.index = index; |
---|
113 | |
---|
114 | /** |
---|
115 | * Called when this object receives the body of a requested input stream. |
---|
116 | * By default, all objects will invoke the callbacks provided to their |
---|
117 | * requestInputStream() functions based on the name of the stream |
---|
118 | * requested. This behavior can be overridden by specifying a different |
---|
119 | * handler here. |
---|
120 | * |
---|
121 | * @event |
---|
122 | * @param {Guacamole.InputStream} inputStream |
---|
123 | * The input stream of the received body. |
---|
124 | * |
---|
125 | * @param {String} mimetype |
---|
126 | * The mimetype of the data being received. |
---|
127 | * |
---|
128 | * @param {String} name |
---|
129 | * The name of the stream whose body has been received. |
---|
130 | */ |
---|
131 | this.onbody = function defaultBodyHandler(inputStream, mimetype, name) { |
---|
132 | |
---|
133 | // Call queued callback for the received body, if any |
---|
134 | var callback = dequeueBodyCallback(name); |
---|
135 | if (callback) |
---|
136 | callback(inputStream, mimetype); |
---|
137 | |
---|
138 | }; |
---|
139 | |
---|
140 | /** |
---|
141 | * Called when this object is being undefined. Once undefined, no further |
---|
142 | * communication involving this object may occur. |
---|
143 | * |
---|
144 | * @event |
---|
145 | */ |
---|
146 | this.onundefine = null; |
---|
147 | |
---|
148 | /** |
---|
149 | * Requests read access to the input stream having the given name. If |
---|
150 | * successful, a new input stream will be created. |
---|
151 | * |
---|
152 | * @param {String} name |
---|
153 | * The name of the input stream to request. |
---|
154 | * |
---|
155 | * @param {Function} [bodyCallback] |
---|
156 | * The callback to invoke when the body of the requested input stream |
---|
157 | * is received. This callback will be provided a Guacamole.InputStream |
---|
158 | * and its mimetype as its two only arguments. If the onbody handler of |
---|
159 | * this object is overridden, this callback will not be invoked. |
---|
160 | */ |
---|
161 | this.requestInputStream = function requestInputStream(name, bodyCallback) { |
---|
162 | |
---|
163 | // Queue body callback if provided |
---|
164 | if (bodyCallback) |
---|
165 | enqueueBodyCallback(name, bodyCallback); |
---|
166 | |
---|
167 | // Send request for input stream |
---|
168 | client.requestObjectInputStream(guacObject.index, name); |
---|
169 | |
---|
170 | }; |
---|
171 | |
---|
172 | /** |
---|
173 | * Creates a new output stream associated with this object and having the |
---|
174 | * given mimetype and name. The legality of a mimetype and name is dictated |
---|
175 | * by the object itself. |
---|
176 | * |
---|
177 | * @param {String} mimetype |
---|
178 | * The mimetype of the data which will be sent to the output stream. |
---|
179 | * |
---|
180 | * @param {String} name |
---|
181 | * The defined name of an output stream within this object. |
---|
182 | * |
---|
183 | * @returns {Guacamole.OutputStream} |
---|
184 | * An output stream which will write blobs to the named output stream |
---|
185 | * of this object. |
---|
186 | */ |
---|
187 | this.createOutputStream = function createOutputStream(mimetype, name) { |
---|
188 | return client.createObjectOutputStream(guacObject.index, mimetype, name); |
---|
189 | }; |
---|
190 | |
---|
191 | }; |
---|
192 | |
---|
193 | /** |
---|
194 | * The reserved name denoting the root stream of any object. The contents of |
---|
195 | * the root stream MUST be a JSON map of stream name to mimetype. |
---|
196 | * |
---|
197 | * @constant |
---|
198 | * @type {String} |
---|
199 | */ |
---|
200 | Guacamole.Object.ROOT_STREAM = '/'; |
---|
201 | |
---|
202 | /** |
---|
203 | * The mimetype of a stream containing JSON which maps available stream names |
---|
204 | * to their corresponding mimetype. The root stream of a Guacamole.Object MUST |
---|
205 | * have this mimetype. |
---|
206 | * |
---|
207 | * @constant |
---|
208 | * @type {String} |
---|
209 | */ |
---|
210 | Guacamole.Object.STREAM_INDEX_MIMETYPE = 'application/vnd.glyptodon.guacamole.stream-index+json'; |
---|