You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.2 KiB
JavaScript

const events = require('events')
const md5 = require('md5')
4 years ago
class ConnectorRegistry {
constructor() {
this.Connectors = {}
this.events = new events.EventEmitter()
this.events.on('ping', (x) => console.log('connector event "ping" from', x))
4 years ago
}
register(connector) {
this.Connectors[ connector.name ] = connector
connector.Hook(this)
4 years ago
}
transmit(name, msg, params) {
4 years ago
if (!this.Connectors[ name ]) throw "not registred"
//md5(JSON.stringify([name, ...params])),
this.Connectors[ name ].transmitMessage(msg, params)
4 years ago
}
supportDuplex(name) {
if (!this.Connectors[ name ]) throw "not registred"
return this.Connectors[ name ].duplexCapable
}
reportState(msg, uuid, state) {
this.events.emit(`msg:status`, msg.id, uuid, state)
}
reportFail(msg, uuid) {
this.events.emit(`msg:status:${ msg.id }:failed`)
this.reportState(msg, uuid, 'failed')
}
reportDelivered(msg, uuid) {
this.events.emit(`msg:status:${ msg.id }:delivered`, msg, uuid)
this.reportState(msg, uuid, 'delivered')
}
//W.I.P
receive(id, data) {
}
4 years ago
}
const registry = new ConnectorRegistry()
module.exports = registry