2019-03-17 13:45:19 -07:00
|
|
|
import to from 'await-to-js'
|
|
|
|
import logger from '@uci-utils/logger'
|
|
|
|
let log = logger({ package: 'base',file:'processing.js'})
|
2018-02-05 22:05:38 -08:00
|
|
|
|
|
|
|
// this._processing refers to this module/hash
|
|
|
|
|
2019-03-17 13:45:19 -07:00
|
|
|
// processing errors that are caught should be sent back to consumer in packets with :error property
|
|
|
|
// but they might also throw local errors/execptions so they should bubble up here and get caught and logged
|
|
|
|
// messaging errors on socket will not be fatal to the entire socket server
|
2018-02-05 22:05:38 -08:00
|
|
|
const processor = async function (packet,socket) {
|
2019-03-17 13:45:19 -07:00
|
|
|
let [err,res] = await to(_process[this.getSocket(socket).type].bind(this)(packet,socket))
|
|
|
|
if (err) {
|
|
|
|
let error = {error:err, socket:socket, packet:packet, msg:'some possibly unhandled badness happened during packet processing'}
|
|
|
|
log.warn(error)
|
|
|
|
if (process.env.UCI_SHOW_UNHANDLED==='true') console.log(error)
|
|
|
|
}
|
|
|
|
else return res
|
2018-02-05 22:05:38 -08:00
|
|
|
}
|
|
|
|
|
2018-02-06 18:30:00 -08:00
|
|
|
export { processor, commands, namespaces }
|
2018-02-05 22:05:38 -08:00
|
|
|
|
2019-03-17 13:45:19 -07:00
|
|
|
const _process = {
|
2018-02-06 18:30:00 -08:00
|
|
|
s: async function (packet,socket) {
|
2018-02-05 22:05:38 -08:00
|
|
|
// console.log('in default socket processor',packet.cmd)
|
2018-02-06 18:30:00 -08:00
|
|
|
if (!packet.cmd) return {error: '[socket] no command in packet', packet: packet }
|
|
|
|
let response = await this._callCmdFunc(packet,socket); if(response!=='failed') return response
|
2018-02-05 22:05:38 -08:00
|
|
|
return {error: 'no socket processing function supplied for command', packet: packet }
|
|
|
|
},
|
|
|
|
|
2018-02-06 18:30:00 -08:00
|
|
|
c: async function (packet,socket) {
|
|
|
|
|
2018-02-05 22:05:38 -08:00
|
|
|
// console.log('in default consumer processor',packet.cmd)
|
2018-02-06 18:30:00 -08:00
|
|
|
if (packet.error) return await this._defaultCmds.c.error(packet)
|
2018-02-05 22:05:38 -08:00
|
|
|
if (packet.cmd) {
|
2018-02-06 18:30:00 -08:00
|
|
|
let response = await this._callCmdFunc(packet,socket); if(response!=='failed') return response
|
2018-02-05 22:05:38 -08:00
|
|
|
packet = {error:'no consumer processing function supplied for command',packet:packet}
|
|
|
|
this._defaultCmds.c.error(packet)
|
|
|
|
} else {
|
2018-02-06 18:30:00 -08:00
|
|
|
packet = {error:'[consumer] no command in packet',packet:packet}
|
|
|
|
return await this._defaultCmds.c.error(packet)
|
2018-02-05 22:05:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 18:30:00 -08:00
|
|
|
const namespaces = {
|
2019-03-17 13:45:19 -07:00
|
|
|
s: ['s','_defaultCmds.s'],
|
2018-05-25 14:39:48 -07:00
|
|
|
c: ['c','_defaultCmds.c'],
|
2018-02-06 18:30:00 -08:00
|
|
|
cn: ['cn'],
|
|
|
|
ct: ['ct'],
|
|
|
|
sn: ['sn'],
|
|
|
|
st: ['st'],
|
2018-05-20 15:44:31 -07:00
|
|
|
cm: ['cm'],
|
|
|
|
sm: ['sm'],
|
2018-05-25 14:39:48 -07:00
|
|
|
sw: ['sw'],
|
2018-02-06 18:30:00 -08:00
|
|
|
}
|
|
|
|
|
2018-02-05 22:05:38 -08:00
|
|
|
/*
|
|
|
|
*
|
2018-02-06 18:30:00 -08:00
|
|
|
* Default packet command processing functions
|
2018-02-05 22:05:38 -08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
const commands ={
|
|
|
|
s:{
|
|
|
|
echo: async packet => {
|
|
|
|
return new Promise( async (resolve) => {
|
|
|
|
packet.processed = true
|
|
|
|
packet.cmd = 'reply'
|
|
|
|
packet.info = 'default socket echo'
|
|
|
|
return resolve(packet)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
c:{
|
|
|
|
error: function (packet) {
|
2018-03-02 08:35:25 -08:00
|
|
|
// TODO log and make this show only on env debug
|
2019-03-17 13:45:19 -07:00
|
|
|
log.warn({error:packet.error, packet:packet, msg:'==========Packet ERROR [consumer]========='})
|
2018-02-05 22:05:38 -08:00
|
|
|
},
|
|
|
|
reply: function(packet) {
|
2018-03-02 08:35:25 -08:00
|
|
|
// TODO log and make this show only on env debug
|
2019-03-17 13:45:19 -07:00
|
|
|
log.debug({packet:packet, msg:'====Packet returned from socket - debug reply==='})
|
2018-02-05 22:05:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|