uci-base/src/base.mjs

149 lines
4.1 KiB
JavaScript

// import { Socket, Consumer } from '@uci/socket'
import UCISocket from '../../uci-socket/src'
import defaultProcessing from './default-processing.mjs'
import EventEmitter from 'events'
export default class Base extends EventEmitter {
constructor(opts={}) {
super()
this.id = opts.id || opts.name || 'uci-base:'+ new Date().getTime()
this.desc = opts.desc // additional details for humans
this.socket={}
this._default = defaultProcessing
opts.sockets.split(/[,|\s]+/).forEach( socketStr => {
let socket = {}
socketStr.split(/[>#]+/).map(function(prop,index) {
socket[SOCKET_INFO_KEYS[index]] = prop
})
if (!opts[socket.name]) opts[socket.name] = {}
if (socket.transport ==='n') opts[socket.name].np = true
opts[socket.name].id = this.id +':'+ socket.name
// console.log(TRANSLATIONS[socket.type])
this.socket[socket.name] = new UCISocket[TRANSLATIONS[socket.type]](opts[socket.name])
// console.log(socket.name, this.socket[socket.name].send)
Object.assign(this.socket[socket.name],socket) // copy socket info props to new socket
this.socket[socket.name]._packetProcess = this._packetProcess.bind(this,socket.name)
})
} // end constructor
async init () {
let sockets = []
for(let name of Object.keys(this.socket)){
if (this.socket[name].type ==='s') {
sockets.push(this.socket[name].create())
// setTimeout(this.socket[type].create,200) // test for consumer retry for delayed socket creation
}
else {
sockets.push(this.socket[name].connect())
}
}
await Promise.all(sockets)
} // init
async send (name, packet) {
if (typeof name === 'string') {
if (this.socket[name]) await this.socket[name].send(packet)
}
else {
packet = name
for(let name of Object.keys(this.socket)){
if (this.socket[name].type ==='c') {
await this.socket[name].send(packet)
}
}
}
this.emit('packet', packet) // emits on instance for instance use
}
// registerPacketProcessor(name, func) {
// if (typeof name === 'string') {
// if (this.socket[name]) await this.socket[name]._packetProcess = func
// }
// else {
// func = name
// for(let name of Object.keys(this.socket)){
// this.socket[name].send(packet)
// }
// }
// }
// }
// registerConsumerProcessor(func) {
// this._packetProces = func
// }
// registerSocketProcessor(func) {
// this._packetProces = func
// }
getSocket(name) {return this.socket[name]}
sendSocket(name) {}
sendTransport(trans) {}
amendConsumerProcessing(funcs,trans) {
Object.assign(this._default.c[trans],funcs)
}
amendSocketProcessing(funcs,trans) {
if (trans) {
if (!this._default.s[trans]) this._default.s[trans] ={}
Object.assign(this._default.s[trans],funcs)
}
Object.assign(this._default.s,funcs)
}
amendNamedProcessing(name,funcs) {
if(!this._default[name]) this._default[name] ={}
Object.assign(this._default[name],funcs)
}
beforeHook (type,funcs){}
afterHook(type,funcs){}
addProcessing(name,type) {}
consumerProcessor(func) {
this._default.c._process = func
}
socketProcessor(func) {
this._default.s._process = func
}
packetProcessor(func) {
this._packetProcess = func
}
/*
*
* Private Methods
*
*/
_transport(name) {return this.socket[name].transport}
_type(name) {return this.socket[name].type}
// default packet processor for all sockets
async _packetProcess (sname,packet) {
return this._default[this.socket[sname].type]._process.bind(this)(packet,sname)
}
} // end class
const SOCKET_INFO_KEYS = ['name','type','transport']
const TRANSLATIONS = {t:'TCP',s:'Socket',c:'Consumer',n:'Named Pipe',}
// this.on('packet', packet => {
// console.log('incoming packet ==========>',packet)
// app[TRANSLATIONS[packet._socket.type]].bind(app)(packet)
// })
//
// // console.log('IN EMIT FUNCTION',socket_name,packet)
// // let s = this.socket[socket_name]
// // packet._socket = {name:s.name,type:s.type,transport:s.transport}
// // this.emit('packet', packet)