¶ SocketFactoryCopyright(c) 2012 Stefano Balietti MIT Licensed
Contract: Socket prototypes must implement the following methods:
|
(function( exports, node ) { |
Storage for socket types |
var types = {};
function checkContract( proto ) {
var test = proto; |
|
test = new proto(); |
|
if (!test.send) {
console.log('no send')
return false;
}
if (!test.connect){
console.log('no connect')
return false;
}
return true;
}
function getTypes() {
return types;
}
function get( type, options ) {
var Socket = types[type];
return (Socket) ? new Socket(options) : null;
}
function register( type, proto ) {
if (!type || !proto) return;
|
only register classes that fulfill the contract |
if ( checkContract(proto) ) {
types[type] = proto;
}
else {
node.err('cannot register invalid Socket class: ' + type);
}
}
|
expose the socketFactory methods |
exports.SocketFactory = {
checkContract: checkContract,
getTypes: getTypes,
get: get,
register: register
};
|
¶ Closure |
})(
'undefined' != typeof node ? node : module.exports
, 'undefined' != typeof node ? node : module.parent.exports
);
|