gather port or path option into proper socket entry
last commit before major refactor for pluginsmaster
parent
599b3242ea
commit
439b5dbd7e
695
src/base.js
695
src/base.js
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,6 @@
|
||||||
import to from 'await-to-js'
|
import to from 'await-to-js'
|
||||||
import logger from '@uci-utils/logger'
|
import logger from '@uci-utils/logger'
|
||||||
let log = logger({ package: 'base',file:'processing.js'})
|
let log = logger({ package: 'base', file: 'processing.js' })
|
||||||
|
|
||||||
// this._processing refers to this module/hash
|
// this._processing refers to this module/hash
|
||||||
|
|
||||||
|
@ -9,41 +9,63 @@ let log = logger({ package: 'base',file:'processing.js'})
|
||||||
// messaging errors on socket will not be fatal to the entire socket server
|
// messaging errors on socket will not be fatal to the entire socket server
|
||||||
|
|
||||||
// common processor, will call based on type s or c the ones below
|
// common processor, will call based on type s or c the ones below
|
||||||
const cmdProcessor = async function (packet,socket) {
|
const cmdProcessor = async function (packet, socket) {
|
||||||
let [err,res] = await to(_process[this.getSocket(socket).type].call(this,packet,socket))
|
let [err, res] = await to(
|
||||||
|
_process[this.getSocket(socket).type].call(this, packet, socket)
|
||||||
|
)
|
||||||
if (err) {
|
if (err) {
|
||||||
let error = {cmd:'error', error:err, packet:packet, socket:socket, function:'processor', line: 15, msg:`'unhandled error in packet command function ${packet.cmd}`}
|
let error = {
|
||||||
|
cmd: 'error',
|
||||||
|
error: err,
|
||||||
|
packet: packet,
|
||||||
|
socket: socket,
|
||||||
|
function: 'processor',
|
||||||
|
line: 15,
|
||||||
|
msg: `'unhandled error in packet command function ${packet.cmd}`,
|
||||||
|
}
|
||||||
log.error(error)
|
log.error(error)
|
||||||
res = Object.assign({},packet,error)
|
res = Object.assign({}, packet, error)
|
||||||
if (process.env.UCI_PUSH_UNHANDLED==='true') this.push(res)
|
if (process.env.UCI_PUSH_UNHANDLED === 'true') this.push(res)
|
||||||
if (process.env.UCI_SHOW_UNHANDLED==='true') console.log(error)
|
if (process.env.UCI_SHOW_UNHANDLED === 'true') console.log(error)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// default processors for socket/server and consumer/client
|
// default processors for socket/server and consumer/client
|
||||||
const _process = {
|
const _process = {
|
||||||
s: async function (packet,socket) {
|
s: async function (packet, socket) {
|
||||||
if (!packet.cmd) return {error: 'no command (cmd:) in packet for socket', packet: packet }
|
if (!packet.cmd)
|
||||||
|
return {
|
||||||
|
error: 'no command (cmd:) in packet for socket',
|
||||||
|
packet: packet,
|
||||||
|
}
|
||||||
// this call will search the namespace and envoke a function and return a repsonse packet
|
// this call will search the namespace and envoke a function and return a repsonse packet
|
||||||
let response = await this._callCmdFunc(packet,socket); if(response!=='failed') return response
|
let response = await this._callCmdFunc(packet, socket)
|
||||||
return {error: 'no socket processing function supplied for command', packet: packet }
|
if (response !== 'failed') return response
|
||||||
|
return {
|
||||||
|
error: 'no socket processing function supplied for command',
|
||||||
|
packet: packet,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
c: async function (packet,socket) {
|
c: async function (packet, socket) {
|
||||||
// the the end of life for a consumer packet that has been sent and returned or a packet that was pushed.
|
// the the end of life for a consumer packet that has been sent and returned or a packet that was pushed.
|
||||||
if (packet.error) packet.cmd='error'
|
if (packet.error) packet.cmd = 'error'
|
||||||
if (!packet.cmd) packet.cmd ='reply'
|
if (!packet.cmd) packet.cmd = 'reply'
|
||||||
let response = await this._callCmdFunc(packet,socket); if(response!=='failed') return response
|
let response = await this._callCmdFunc(packet, socket)
|
||||||
packet = {error:`no consumer return processing function supplied for ${packet.cmd}`, packet:packet}
|
if (response !== 'failed') return response
|
||||||
|
packet = {
|
||||||
|
error: `no consumer return processing function supplied for ${packet.cmd}`,
|
||||||
|
packet: packet,
|
||||||
|
}
|
||||||
this._c.error(packet)
|
this._c.error(packet)
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// default name spaces
|
// default name spaces
|
||||||
const namespaces = {
|
const namespaces = {
|
||||||
s: ['_s'], // default command functions below
|
s: ['_s'], // default command functions below
|
||||||
c: ['_c'], // default command functions below
|
c: ['_c'], // default command functions below
|
||||||
cn: ['_cn'],
|
cn: ['_cn'],
|
||||||
ct: ['_ct'],
|
ct: ['_ct'],
|
||||||
cm: ['_cm'],
|
cm: ['_cm'],
|
||||||
|
@ -55,13 +77,13 @@ const namespaces = {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* Default packet command processing functions
|
* Default packet command processing functions
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const defaultCmds ={
|
const defaultCmds = {
|
||||||
s:{
|
s: {
|
||||||
echo: function (packet) {
|
echo: function (packet) {
|
||||||
packet.processed = true
|
packet.processed = true
|
||||||
packet.msg = 'default socket echo'
|
packet.msg = 'default socket echo'
|
||||||
|
@ -71,36 +93,81 @@ const defaultCmds ={
|
||||||
ack: async function (packet) {
|
ack: async function (packet) {
|
||||||
packet.cmd = 'reply'
|
packet.cmd = 'reply'
|
||||||
packet.ack = true
|
packet.ack = true
|
||||||
packet.msg = 'this is the base default ack, superceed in your instance or extended class'
|
packet.msg =
|
||||||
|
'this is the base default ack, superceed in your instance or extended class'
|
||||||
return packet
|
return packet
|
||||||
},
|
},
|
||||||
ready: async function (packet) {
|
ready: async function (packet) {
|
||||||
const event = packet.event || packet.name || packet.id
|
const event = packet.event || packet.name || packet.id
|
||||||
delete(packet._header)
|
delete packet._header
|
||||||
this.emit(event,packet.ready,packet)
|
this.emit(event, packet.ready, packet)
|
||||||
this.emit('log', {level:'ready', msg:'change in ready state received via send', ready:packet.ready, packet:packet})
|
this.emit('ready', packet, packet)
|
||||||
setTimeout(()=>this.emit('log', {level:'state', msg:'new ready state', state:this.ready.state}),1000)
|
this.emit('log', {
|
||||||
return {cmd:'reply', msg:'consumer sent event was emitted event at socket process', event:event}
|
level: 'ready',
|
||||||
}
|
msg: 'change in ready state received via send',
|
||||||
|
ready: packet.ready,
|
||||||
|
packet: packet,
|
||||||
|
})
|
||||||
|
setTimeout(
|
||||||
|
() =>
|
||||||
|
this.emit('log', {
|
||||||
|
level: 'state',
|
||||||
|
msg: 'new ready state',
|
||||||
|
state: this.ready.state,
|
||||||
|
}),
|
||||||
|
1000
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
cmd: 'reply',
|
||||||
|
msg: 'consumer sent event was emitted event at socket process',
|
||||||
|
event: event,
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
c:{
|
c: {
|
||||||
error: function (packet) { // default
|
error: function (packet) {
|
||||||
log.error({error:packet.error, packet:packet, msg:'==========Consumer Sent Packet returned with ERROR ========='})
|
// default
|
||||||
|
log.error({
|
||||||
|
error: packet.error,
|
||||||
|
packet: packet,
|
||||||
|
msg: '==========Consumer Sent Packet returned with ERROR =========',
|
||||||
|
})
|
||||||
return packet
|
return packet
|
||||||
},
|
},
|
||||||
reply: function(packet) {
|
reply: function (packet) {
|
||||||
if (process.env.UCI_ENV==='dev') log.debug({packet:packet, msg:'====Packet returned from socket - default reply logger==='})
|
if (process.env.UCI_ENV === 'dev')
|
||||||
|
log.debug({
|
||||||
|
packet: packet,
|
||||||
|
msg: '====Packet returned from socket - default reply logger===',
|
||||||
|
})
|
||||||
return packet
|
return packet
|
||||||
},
|
},
|
||||||
ready: async function (packet) {
|
ready: async function (packet) {
|
||||||
const event = packet.event || packet.name || packet.id
|
const event = packet.event || packet.name || packet.id
|
||||||
delete(packet._header)
|
delete packet._header
|
||||||
this.emit(event,packet.ready,packet)
|
this.emit(event, packet.ready, packet)
|
||||||
this.emit('log', {level:'ready', msg:'change in ready state received via push', ready:packet.ready, packet:packet})
|
this.emit('ready', packet)
|
||||||
setTimeout(()=>this.emit('log', {level:'state', msg:'new ready state', state:this.ready.state}),1000)
|
this.emit('log', {
|
||||||
return {cmd:'reply', msg:'ready packet event was emitted at consumer process from push'}
|
level: 'ready',
|
||||||
}
|
msg: 'change in ready state received via push',
|
||||||
}
|
ready: packet.ready,
|
||||||
|
packet: packet,
|
||||||
|
})
|
||||||
|
setTimeout(
|
||||||
|
() =>
|
||||||
|
this.emit('log', {
|
||||||
|
level: 'state',
|
||||||
|
msg: 'new ready state',
|
||||||
|
state: this.ready.state,
|
||||||
|
}),
|
||||||
|
1000
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
cmd: 'reply',
|
||||||
|
msg: 'ready packet event was emitted at consumer process from push',
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export { cmdProcessor, defaultCmds, namespaces }
|
export { cmdProcessor, defaultCmds, namespaces }
|
||||||
|
|
Loading…
Reference in New Issue