2013-10-25 05:25:42 +08:00
|
|
|
/*
|
|
|
|
* connection
|
|
|
|
* cylonjs.com
|
|
|
|
*
|
2014-02-28 02:33:43 +08:00
|
|
|
* Copyright (c) 2013-2014 The Hybrid Group
|
2013-10-25 05:25:42 +08:00
|
|
|
* Licensed under the Apache 2.0 license.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 02:33:43 +08:00
|
|
|
'use strict';
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-11-01 05:14:02 +08:00
|
|
|
var Registry = require('./registry'),
|
2014-11-07 01:48:21 +08:00
|
|
|
Config = require('./config'),
|
2014-11-11 07:56:25 +08:00
|
|
|
Logger = require('./logger');
|
2014-05-14 10:44:40 +08:00
|
|
|
|
2014-11-07 01:48:21 +08:00
|
|
|
var testMode = function() {
|
|
|
|
return process.env.NODE_ENV === 'test' && Config.testMode;
|
|
|
|
};
|
2014-10-31 04:15:40 +08:00
|
|
|
|
2014-11-11 07:56:25 +08:00
|
|
|
// Public: Creates a new Adaptor and returns it.
|
2014-05-07 23:38:06 +08:00
|
|
|
//
|
|
|
|
// opts - hash of acceptable params:
|
|
|
|
// robot - Robot the Connection belongs to
|
|
|
|
// name - name for the connection
|
|
|
|
// adaptor - string module name of the adaptor to be set up
|
|
|
|
// port - string port to use for the Connection
|
|
|
|
//
|
|
|
|
// Returns the newly set-up connection
|
2014-11-11 07:56:25 +08:00
|
|
|
module.exports = function Connection(opts) {
|
2014-11-06 02:04:45 +08:00
|
|
|
var module;
|
|
|
|
|
2014-11-11 07:56:25 +08:00
|
|
|
opts = opts || {};
|
|
|
|
|
2014-11-06 02:04:45 +08:00
|
|
|
if (opts.module) {
|
|
|
|
module = Registry.register(opts.module);
|
|
|
|
} else {
|
|
|
|
module = Registry.findByAdaptor(opts.adaptor);
|
|
|
|
}
|
2014-10-31 04:15:40 +08:00
|
|
|
|
|
|
|
if (!module) {
|
2014-11-01 05:14:02 +08:00
|
|
|
Registry.register('cylon-' + opts.adaptor);
|
|
|
|
module = Registry.findByAdaptor(opts.adaptor);
|
2014-10-31 04:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var adaptor = module.adaptor(opts);
|
|
|
|
|
2014-11-12 03:41:18 +08:00
|
|
|
for (var prop in adaptor) {
|
2014-11-12 05:41:09 +08:00
|
|
|
if (~['constructor'].indexOf(prop)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-12 03:41:18 +08:00
|
|
|
if (typeof adaptor[prop] === 'function') {
|
|
|
|
adaptor[prop] = adaptor[prop].bind(adaptor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-07 01:48:21 +08:00
|
|
|
if (testMode()) {
|
2014-11-01 05:14:02 +08:00
|
|
|
var testAdaptor = Registry.findByAdaptor('test').adaptor(opts);
|
2014-10-31 04:15:40 +08:00
|
|
|
|
|
|
|
for (var prop in adaptor) {
|
|
|
|
if (typeof adaptor[prop] === 'function' && !testAdaptor[prop]) {
|
|
|
|
testAdaptor[prop] = function() { return true; };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return testAdaptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return adaptor;
|
2014-05-07 23:38:06 +08:00
|
|
|
};
|