Refactor InputStream API.

This commit is contained in:
Michael Jumper
2014-02-28 10:03:01 -08:00
parent 4478e91c8a
commit beb9e5c684
3 changed files with 220 additions and 47 deletions

View File

@@ -438,9 +438,10 @@ Guacamole.Client = function(tunnel) {
* close event.
*
* @event
* @param {String} mimetype The mimetype of the file received.
* @param {String} filename The name of the file received.
* @param {Guacamole.InputStream} stream A stream that will receive
* data from the server.
* @return {Guacamole.InputStream} The stream that will receive data from
* the server.
*/
this.onfile = null;
@@ -450,9 +451,10 @@ Guacamole.Client = function(tunnel) {
* close event.
*
* @event
* @param {String} mimetype The mimetype of the data which will be received.
* @param {String} name The name of the pipe.
* @param {Guacamole.InputStream} stream A stream that will receive
* data from the server.
* @return {Guacamole.InputStream} The stream that will receive data from
* the server.
*/
this.onpipe = null;
@@ -637,16 +639,8 @@ Guacamole.Client = function(tunnel) {
var data = parameters[1];
var stream = streams[stream_index];
// Convert to ArrayBuffer
var binary = window.atob(data);
var arrayBuffer = new ArrayBuffer(binary.length);
var bufferView = new Uint8Array(arrayBuffer);
for (var i=0; i<binary.length; i++)
bufferView[i] = binary.charCodeAt(i);
// Write data
stream.receive(arrayBuffer);
stream.receive(data);
// Send success response
tunnel.sendMessage("ack", stream_index, "OK", 0x0000);
@@ -851,15 +845,24 @@ Guacamole.Client = function(tunnel) {
var filename = parameters[2];
// Create stream
var stream = streams[stream_index] =
new Guacamole.InputStream(mimetype);
if (guac_client.onfile) {
// Call handler now that file stream is created
if (guac_client.onfile)
guac_client.onfile(filename, stream);
// Attempt to create stream
var stream = guac_client.onfile(mimetype, filename);
if (stream) {
streams[stream_index] = stream;
tunnel.sendMessage("ack", stream_index, "OK", 0x0000);
}
// Send success response
tunnel.sendMessage("ack", stream_index, "OK", 0x0000);
// Notify if creation failed
else
tunnel.sendMessage("ack", stream_index, "Unable to receive file", 0x0201);
}
// Otherwise, unsupported
else
tunnel.sendMessage("ack", stream_index, "File transfer unsupported", 0x0100);
},
@@ -943,15 +946,24 @@ Guacamole.Client = function(tunnel) {
var name = parameters[2];
// Create stream
var stream = streams[stream_index] =
new Guacamole.InputStream(mimetype);
if (guac_client.onpipe) {
// Call handler now that pipe stream is created
if (guac_client.onpipe)
guac_client.onpipe(name, stream);
// Attempt to create stream
var stream = guac_client.onpipe(mimetype, name);
if (stream) {
streams[stream_index] = stream;
tunnel.sendMessage("ack", stream_index, "OK", 0x0000);
}
// Send success response
tunnel.sendMessage("ack", stream_index, "OK", 0x0000);
// Notify if creation failed
else
tunnel.sendMessage("ack", stream_index, "Unable to create pipe", 0x0201);
}
// Otherwise, unsupported
else
tunnel.sendMessage("ack", stream_index, "Named pipes unsupported", 0x0100);
},

View File

@@ -31,6 +31,102 @@ var Guacamole = Guacamole || {};
*/
Guacamole.InputStream = function(mimetype) {
/**
* The mimetype of the data contained within this blob.
*/
this.mimetype = mimetype;
/**
* Receives the given base64-encoded data.
*
* @param {String} data The received base64 data.
*/
this.receive = function(data) {};
/**
* Closes this Guacamole.InputStream such that no further data will be
* written.
*/
this.close = function() {};
};
/**
* An input stream which receives all data packets as individual ArrayBuffer
* objects.
*
* @constructor
* @param {String} mimetype The mimetype of the data this stream will receive.
*/
Guacamole.ArrayBufferInputStream = function(mimetype) {
/**
* Reference to this Guacamole.InputStream.
* @private
*/
var guac_stream = this;
/*
* This is an input stream.
*/
Guacamole.InputStream.apply(this, [mimetype]);
// Receive implementation
this.receive = function(data) {
// Convert to ArrayBuffer
var binary = window.atob(data);
var arrayBuffer = new ArrayBuffer(binary.length);
var bufferView = new Uint8Array(arrayBuffer);
for (var i=0; i<binary.length; i++)
bufferView[i] = binary.charCodeAt(i);
// Call handler, if present
if (guac_stream.onreceive)
guac_stream.onreceive(arrayBuffer);
};
// Close implementation
this.close = function() {
// Call handler, if present
if (guac_stream.onclose)
guac_stream.onclose();
// NOTE: Currently not enforced.
};
/**
* Fired once for every blob of data received.
*
* @event
* @param {ArrayBuffer} buffer The data packet received.
*/
this.onreceive = null;
/**
* Fired once this stream is finished and no further data will be written.
* @event
*/
this.onclose = null;
};
Guacamole.ArrayBufferInputStream.prototype = new Guacamole.InputStream();
/**
* An input stream which continuously builds a single blob by appending each
* individual blob received. Only the size of each blob received is exposed.
*
* @constructor
* @augments Guacamole.InputStream
* @param {String} mimetype The mimetype of the data this stream will receive.
*/
Guacamole.BlobInputStream = function(mimetype) {
/**
* Reference to this Guacamole.InputStream.
* @private
@@ -43,10 +139,10 @@ Guacamole.InputStream = function(mimetype) {
*/
var length = 0;
/**
* The mimetype of the data contained within this blob.
/*
* This is an input stream.
*/
this.mimetype = mimetype;
Guacamole.InputStream.apply(this, [mimetype]);
// Get blob builder
var blob_builder;
@@ -70,28 +166,27 @@ Guacamole.InputStream = function(mimetype) {
})();
/**
* Receives the given ArrayBuffer, storing its data within this
* Guacamole.InputStream.
*
* @param {ArrayBuffer} buffer An ArrayBuffer containing the data to be
* received.
*/
this.receive = function(buffer) {
// Receive implementation
this.receive = function(data) {
blob_builder.append(buffer);
length += buffer.byteLength;
// Convert to ArrayBuffer
var binary = window.atob(data);
var arrayBuffer = new ArrayBuffer(binary.length);
var bufferView = new Uint8Array(arrayBuffer);
for (var i=0; i<binary.length; i++)
bufferView[i] = binary.charCodeAt(i);
blob_builder.append(arrayBuffer);
length += arrayBuffer.byteLength;
// Call handler, if present
if (guac_stream.onreceive)
guac_stream.onreceive(buffer.byteLength);
if (guac_stream.onprogress)
guac_stream.onprogress(arrayBuffer.byteLength);
};
/**
* Closes this Guacamole.InputStream such that no further data will be
* written.
*/
// Close implementation
this.close = function() {
// Call handler, if present
@@ -124,6 +219,66 @@ Guacamole.InputStream = function(mimetype) {
* @event
* @param {Number} length The number of bytes received.
*/
this.onprogress = null;
/**
* Fired once this stream is finished and no further data will be written.
* @event
*/
this.onclose = null;
};
Guacamole.BlobInputStream.prototype = new Guacamole.InputStream();
/**
* An input stream which receives strictly text data.
*
* @constructor
* @param {String} mimetype The mimetype of the data this stream will receive.
*/
Guacamole.StringInputStream = function(mimetype) {
/**
* Reference to this Guacamole.InputStream.
* @private
*/
var guac_stream = this;
/*
* This is an input stream.
*/
Guacamole.InputStream.apply(this, [mimetype]);
// Receive implementation
this.receive = function(data) {
// Convert to string
var text = window.atob(data);
// Call handler, if present
if (guac_stream.onreceive)
guac_stream.onreceive(text);
};
// Close implementation
this.close = function() {
// Call handler, if present
if (guac_stream.onclose)
guac_stream.onclose();
// NOTE: Currently not enforced.
};
/**
* Fired once for every blob of data received.
*
* @event
* @param {String} text The data packet received.
*/
this.onreceive = null;
/**
@@ -133,3 +288,5 @@ Guacamole.InputStream = function(mimetype) {
this.onclose = null;
};
Guacamole.StringInputStream.prototype = new Guacamole.InputStream();