1
0
mirror of https://github.com/nttgin/BGPalerter.git synced 2024-05-19 06:50:08 +00:00
Files
nttgin-BGPalerter/connectorFactory.js

69 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-07-03 17:56:07 +02:00
import env from "./env";
export default class ConnectorFactory {
constructor() {
2019-07-05 00:09:59 +02:00
this.connectors = {};
2019-07-03 17:56:07 +02:00
}
2019-07-05 00:09:59 +02:00
getConnector = (name) => {
return this.connectors[name];
};
getConnectors = () => {
return Object.keys(this.connectors).map(name => this.connectors[name]);
};
2019-07-03 17:56:07 +02:00
loadConnectors = () => {
2019-07-05 00:09:59 +02:00
const connectors = Object.keys(this.connectors);
if (connectors.length === 0) {
for (let connector of env.config.connectors) {
this.connectors[connector.name] = new connector.class(connector.name, connector.params, env);
2019-07-05 00:09:59 +02:00
}
2019-07-03 17:56:07 +02:00
}
};
connectConnectors = () =>
new Promise((resolve, reject) => {
2019-07-05 00:09:59 +02:00
const connectors = this.getConnectors();
if (connectors.length === 0) {
reject(new Error("No connections available"));
2019-07-03 17:56:07 +02:00
} else {
2019-07-05 00:09:59 +02:00
resolve(Promise.all(connectors
.map(connector =>
new Promise((resolve, reject) => {
connector.connect()
.then(() => {
connector.connected = true;
resolve(true);
})
.catch((error) => {
env.logger.log({
level: 'error',
message: error
});
resolve(false);
})
}))));
}
});
2019-07-03 17:56:07 +02:00
2019-07-05 00:09:59 +02:00
subscribeConnectors = (params, callback) =>
new Promise((resolve, reject) => {
const connectors = this.getConnectors();
if (connectors.length === 0) {
reject(new Error("No connections available"));
} else {
2019-07-09 02:46:08 +02:00
const connectorList = connectors
.map(connector => connector.subscribe(params));
resolve(Promise.all(connectorList));
2019-07-03 17:56:07 +02:00
}
2019-07-09 02:46:08 +02:00
})
2019-07-03 17:56:07 +02:00
}