-
Notifications
You must be signed in to change notification settings - Fork 88
/
transportSelector.js
101 lines (85 loc) · 3.7 KB
/
transportSelector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of iotagent-json
*
* iotagent-json is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* iotagent-json is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with iotagent-json.
* If not, seehttp://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::[contacto@tid.es]
*/
/* eslint-disable prefer-spread */
const path = require('path');
const fs = require('fs');
const config = require('./configService');
const async = require('async');
let transportBindings = [];
/**
* Start all the transport protocol bindings found in the bindings directory.
*
* @param {Object} newConfig Configuration object to start the bindings
*/
function startTransportBindings(newConfig, callback) {
function invokeBinding(binding, callback) {
binding.start(callback);
}
const bindings = fs.readdirSync(path.join(__dirname, './bindings'));
transportBindings = bindings.map(function (item) {
return require('./bindings/' + item);
});
/* eslint-disable no-unused-vars */
async.map(transportBindings, invokeBinding, function (error) {
callback();
});
}
function createExecutionsForBinding(argument, functionName, protocol) {
config.getLogger().debug('Creating execution for function %s and protocol %s', functionName, protocol);
function addHandler(current, binding) {
if (binding[functionName] && (!protocol || binding.protocol === protocol)) {
const args = [binding[functionName]].concat(argument);
const boundFunction = binding[functionName].bind.apply(binding[functionName], args);
config.getLogger().debug('Binding found for function %s and protocol %s', functionName, protocol);
current.push(boundFunction);
}
return current;
}
return transportBindings.reduce(addHandler, []);
}
/**
* Execute the function given by the 'functionName' parameter for all the transport bindings, with the arguments
* given in the 'argument' array. If the optional parameter protocol is not null, the function will only be
* executed in the plugin of the selected protocol.
*
* @param {Array} argument Array of arguments to call the function with.
* @param {String} functionName Name of the function to call in every transport plugin.
* @param {String} protocol Transport protocol where the function must be executed.
*/
function applyFunctionFromBinding(argument, functionName, protocol, callback) {
config.getLogger().debug('Looking for bindings for the function %s and protocol %s', functionName, protocol);
async.series(createExecutionsForBinding(argument, functionName, protocol), callback);
}
/**
* Stop all the transport protocol bindings of the agent.
*/
function stopTransportBindings(callback) {
function invokeBinding(binding, callback) {
binding.stop(callback);
}
async.map(transportBindings, invokeBinding, callback);
}
exports.stopTransportBindings = stopTransportBindings;
exports.applyFunctionFromBinding = applyFunctionFromBinding;
exports.startTransportBindings = startTransportBindings;
exports.createExecutionsForBinding = createExecutionsForBinding;