ndn-js-contrib
Version:
Reusable 'Classes' for Named Data Networking: NameTree, PIT, FIB, ContentStore, Interfaces, and Transports
137 lines (102 loc) • 3.75 kB
JavaScript
var ElementReader = require("ndn-js/js/encoding/element-reader.js").ElementReader;
var Transport = require("ndn-js/js/transport/transport.js").Transport;
var debug = {};
debug.debug = require("debug")("DataChannelTransport");
/**Transport Class for HTML5 DataChannels
*@constructor
*@param {DataChannel_Port} port one end of an HTML DataChannel
*@returns {DataChannelTransport}
*/
function DataChannelTransport (channel) {
Transport.call(this);
this.connectionInfo = new DataChannelTransport.ConnectionInfo(channel);
return this;
}
DataChannelTransport.prototype = new Transport();
DataChannelTransport.prototype.name = "DataChannelTransport";
DataChannelTransport.ConnectionInfo = function DataChannelTransportConnectionInfo(channel){
Transport.ConnectionInfo.call(this);
channel.binaryType = "arraybuffer";
this.channel = channel;
};
DataChannelTransport.ConnectionInfo.prototype = new Transport.ConnectionInfo();
DataChannelTransport.ConnectionInfo.prototype.name = "DataChannelTransport.ConnectionInfo";
DataChannelTransport.ConnectionInfo.prototype.getChannel = function()
{
return this.channel;
};
DataChannelTransport.ConnectionInfo.prototype.equals = function(other)
{
if (other === null || other.port === undefined){
return false;
}
return (this.port === other.port);
};
/**Set the event listener for incoming elements
*@param {Object} face the ndn.Face object that this transport is attached to
*@param {function} onopenCallback a callback to be performed once the transport is open
*/
DataChannelTransport.prototype.connect = function(connectionInfo, elementListener, onopenCallback, onclosedCallback)
{
this.elementReader = new ElementReader(elementListener);
var self = this;
var openTriggered = false;
connectionInfo.getChannel().onmessage = function(ev) {
if (ev.data instanceof ArrayBuffer) {
var result = ev.data;
var bytearray = new Buffer(new Uint8Array(result));
try {
// Find the end of the binary XML element and call face.onReceivedElement.
self.elementReader.onReceivedData(bytearray);
} catch (ex) {
debug("onmessage exception: ", ex);
return;
}
}
};
connectionInfo.getChannel().onopen = function(ev) {
// Face.registerPrefix will fetch the ndndid when needed.
if (!openTriggered)
onopenCallback();
};
connectionInfo.getChannel().onerror = function(ev) {
};
connectionInfo.getChannel().onclose = function(ev) {
self.dc = null;
// Close Face when WebSocket is closed
onclosedCallback();
};
if (connectionInfo.getChannel().readyState === "open"){
openTriggered = true;
onopenCallback();
}
};
/**Send the Uint8Array data.
*@param {Buffer} element the data packet
*/
DataChannelTransport.prototype.send = function(element)
{
if(this.connectionInfo.getChannel().readyState === "open"){
this.connectionInfo.getChannel().send(element.toArrayBuffer());
} else {
}
};
/**Define a connection listener for the {@link Interfaces} module. This Class method must be called before installing the class into Interfaces (if you want a Listener)
*@param {Number=} - port the port for the listener to listen on, default 7575
*//**))
DataChannelTransport.defineListener = function(subject, namespace){
subject.contentStore.insert()
this.Listener = function (newFace) {
this.server = net.createServer(function(socket){
socket.on('end', function() {
console.log('server disconnected');
});
newFace("tcpServer", socket);
});
this.server.listen(port, function(){
//console.log('server awaiting connections');
});
};
};
*/
module.exports = DataChannelTransport;