refactored adding sockets so as many sockets of any type and transport can be added
parent
51986fbcc7
commit
8c61ec854d
|
@ -21,7 +21,7 @@ const basefuncs = {
|
|||
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
||||
|
||||
// let app = new Base({com:'us,uc,ts,tc', id:'example', path: USOCKET, log:false})
|
||||
let app = new Base({sockets:'uc,us,tc,ts', id:'example', log:false})
|
||||
let app = new Base({sockets:'uc#c>n,us#s>n,tc#c>t,ts#s>t', id:'four-in-one'})
|
||||
|
||||
app.amendPacketProcessing(basefuncs)
|
||||
|
||||
|
|
81
src/base.mjs
81
src/base.mjs
|
@ -1,5 +1,5 @@
|
|||
// import { Socket, Consumer } from '@uci/socket'
|
||||
import { Socket, Consumer } from '../../uci-socket/src'
|
||||
import UCISocket from '../../uci-socket/src'
|
||||
import packet from './packet.mjs'
|
||||
import EventEmitter from 'events'
|
||||
|
||||
|
@ -12,30 +12,51 @@ export default class Base extends EventEmitter {
|
|||
this.id = opts.id || opts.name || 'uci:'+ Math.random()*100
|
||||
this.desc = opts.desc // additional details for humans
|
||||
this.socket={}
|
||||
opts.sockets.split(/[,:|\s]+/).forEach( sock => {
|
||||
opts.id = this.id +':'+sock
|
||||
if (!opts[sock]) opts[sock] = {}
|
||||
switch (sock) {
|
||||
case
|
||||
'us':
|
||||
opts.us.path = opts.us.path || opts.path
|
||||
this.socket[sock] = new Socket(opts.us.path,opts)
|
||||
this.socket[sock].packet = Object.assign({},packet.socket)
|
||||
break
|
||||
case 'uc':
|
||||
opts.uc.path = opts.uc.path || opts.path
|
||||
this.socket[sock] = new Consumer(opts.uc.path,opts)
|
||||
this.socket[sock].packet = Object.assign({},packet.consumer)
|
||||
break
|
||||
case 'ts':
|
||||
this.socket[sock]= new Socket(opts.ts, opts)
|
||||
this.socket[sock].packet = Object.assign({},packet.socket)
|
||||
break
|
||||
case 'tc':
|
||||
this.socket[sock]= new Consumer(opts.tc,opts)
|
||||
this.socket[sock].packet = Object.assign({},packet.consumer)
|
||||
}
|
||||
opts.sockets.split(/[,|\s]+/).forEach( opts_socket => {
|
||||
let socket = {}
|
||||
opts_socket.split(/[>#]+/).map(function(prop,index) {
|
||||
socket[SOCKET_INFO_KEYS[index]] = prop
|
||||
})
|
||||
let args =[]
|
||||
if (!opts[socket.name]) opts[socket.name] = {}
|
||||
let path = opts[socket.name].path || opts.path
|
||||
if (socket.transport ==='n') args.push(path)
|
||||
opts.name = this.id +':'+ socket.name
|
||||
args.push(opts)
|
||||
this.socket[socket.name] = new UCISocket[TRANSLATIONS[socket.type]](...args)
|
||||
Object.assign(this.socket[socket.name],socket)
|
||||
// this will be removed when moving to emit
|
||||
this.socket[socket.name].packet = Object.assign({},packet[TRANSLATIONS[socket.type]])
|
||||
console.log(socket.name,'=========\n', this.socket[socket.name])
|
||||
})
|
||||
|
||||
|
||||
// opts.id = this.id +':'+sock
|
||||
// if (!opts[sock]) opts[sock] = {}
|
||||
// switch (sock) {
|
||||
// case
|
||||
// 'us':
|
||||
// opts.us.path = opts.us.path || opts.path
|
||||
//
|
||||
// this.socket[sock].info = {transport:'np', id:sock, }
|
||||
// this.socket[sock].packet._process = socketEmit.bind(this.socket[sock],this.emit)
|
||||
// break
|
||||
// case 'uc':
|
||||
// opts.uc.path = opts.uc.path || opts.path
|
||||
// this.socket[sock] = new Consumer(opts.uc.path,opts)
|
||||
// this.socket[sock].packet = Object.assign({},packet.consumer)
|
||||
// break
|
||||
// case 'ts':
|
||||
// this.socket[sock]= new Socket(opts.ts, opts)
|
||||
// this.socket[sock].packet = Object.assign({},packet.socket)
|
||||
// break
|
||||
// case 'tc':
|
||||
// this.socket[sock]= new Consumer(opts.tc,opts)
|
||||
// this.socket[sock].packet = Object.assign({},packet.consumer)
|
||||
// }
|
||||
// })
|
||||
|
||||
|
||||
} // end constructor
|
||||
|
||||
async init (context) {
|
||||
|
@ -52,6 +73,10 @@ export default class Base extends EventEmitter {
|
|||
}
|
||||
}
|
||||
await Promise.all(sockets)
|
||||
|
||||
this.on('packet', packet => {
|
||||
console.log('==========>',packet.socket)
|
||||
})
|
||||
} // init
|
||||
|
||||
registerPacketContext(type, context) {
|
||||
|
@ -122,3 +147,11 @@ export default class Base extends EventEmitter {
|
|||
}
|
||||
|
||||
} // end class
|
||||
|
||||
const SOCKET_INFO_KEYS = ['name','type','transport']
|
||||
const TRANSLATIONS = {t:'TCP',s:'Socket',c:'Consumer',n:'Named Pipe',}
|
||||
|
||||
const socketEmit = function (emit,packet) {
|
||||
packet.socket = this.info
|
||||
emit('packet', packet)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
export default {
|
||||
|
||||
socket:{
|
||||
Socket:{
|
||||
_process: async function (packet) {
|
||||
if (!packet.cmd) return {error: 'no command in packet', packet: packet }
|
||||
if (this.context) if (this.context[packet.cmd]) return await this.context[packet.cmd].bind(this.context)(packet)
|
||||
|
@ -17,7 +17,7 @@ export default {
|
|||
}
|
||||
},
|
||||
|
||||
consumer: {
|
||||
Consumer: {
|
||||
_process: async function (packet) {
|
||||
if (packet.error) return this.error(packet)
|
||||
if (packet.cmd) {
|
||||
|
|
Loading…
Reference in New Issue