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))
|
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({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)
|
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'
|
||||||
import { Socket, Consumer } from '../../uci-socket/src'
|
import UCISocket from '../../uci-socket/src'
|
||||||
import packet from './packet.mjs'
|
import packet from './packet.mjs'
|
||||||
import EventEmitter from 'events'
|
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.id = opts.id || opts.name || 'uci:'+ Math.random()*100
|
||||||
this.desc = opts.desc // additional details for humans
|
this.desc = opts.desc // additional details for humans
|
||||||
this.socket={}
|
this.socket={}
|
||||||
opts.sockets.split(/[,:|\s]+/).forEach( sock => {
|
opts.sockets.split(/[,|\s]+/).forEach( opts_socket => {
|
||||||
opts.id = this.id +':'+sock
|
let socket = {}
|
||||||
if (!opts[sock]) opts[sock] = {}
|
opts_socket.split(/[>#]+/).map(function(prop,index) {
|
||||||
switch (sock) {
|
socket[SOCKET_INFO_KEYS[index]] = prop
|
||||||
case
|
})
|
||||||
'us':
|
let args =[]
|
||||||
opts.us.path = opts.us.path || opts.path
|
if (!opts[socket.name]) opts[socket.name] = {}
|
||||||
this.socket[sock] = new Socket(opts.us.path,opts)
|
let path = opts[socket.name].path || opts.path
|
||||||
this.socket[sock].packet = Object.assign({},packet.socket)
|
if (socket.transport ==='n') args.push(path)
|
||||||
break
|
opts.name = this.id +':'+ socket.name
|
||||||
case 'uc':
|
args.push(opts)
|
||||||
opts.uc.path = opts.uc.path || opts.path
|
this.socket[socket.name] = new UCISocket[TRANSLATIONS[socket.type]](...args)
|
||||||
this.socket[sock] = new Consumer(opts.uc.path,opts)
|
Object.assign(this.socket[socket.name],socket)
|
||||||
this.socket[sock].packet = Object.assign({},packet.consumer)
|
// this will be removed when moving to emit
|
||||||
break
|
this.socket[socket.name].packet = Object.assign({},packet[TRANSLATIONS[socket.type]])
|
||||||
case 'ts':
|
console.log(socket.name,'=========\n', this.socket[socket.name])
|
||||||
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.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
|
} // end constructor
|
||||||
|
|
||||||
async init (context) {
|
async init (context) {
|
||||||
|
@ -52,6 +73,10 @@ export default class Base extends EventEmitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await Promise.all(sockets)
|
await Promise.all(sockets)
|
||||||
|
|
||||||
|
this.on('packet', packet => {
|
||||||
|
console.log('==========>',packet.socket)
|
||||||
|
})
|
||||||
} // init
|
} // init
|
||||||
|
|
||||||
registerPacketContext(type, context) {
|
registerPacketContext(type, context) {
|
||||||
|
@ -122,3 +147,11 @@ export default class Base extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end class
|
} // 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 {
|
export default {
|
||||||
|
|
||||||
socket:{
|
Socket:{
|
||||||
_process: async function (packet) {
|
_process: async function (packet) {
|
||||||
if (!packet.cmd) return {error: 'no command in packet', packet: 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)
|
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) {
|
_process: async function (packet) {
|
||||||
if (packet.error) return this.error(packet)
|
if (packet.error) return this.error(packet)
|
||||||
if (packet.cmd) {
|
if (packet.cmd) {
|
||||||
|
|
Loading…
Reference in New Issue