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
|
2019-04-26 11:05:10 -07:00
|
|
|
|
|
|
|
// common processor, will call based on type s or c the ones below
|
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) {
|
2019-04-26 11:05:10 -07:00
|
|
|
let error = {error:err, socket:socket, packet:packet, function:'processor', line: 15, msg:'some possibly unhandled badness happened during packet processing'}
|
|
|
|
log.error(error)
|
2019-03-17 13:45:19 -07:00
|
|
|
if (process.env.UCI_SHOW_UNHANDLED==='true') console.log(error)
|
|
|
|
}
|
|
|
|
else return res
|
2018-02-05 22:05:38 -08:00
|
|
|
}
|
|
|
|
|
2019-04-26 11:05:10 -07:00
|
|
|
export { processor, defaultCmds, namespaces }
|
2018-02-05 22:05:38 -08:00
|
|
|
|
2019-04-26 11:05:10 -07:00
|
|
|
// default processors for socket/server and consumer/client
|
2019-03-17 13:45:19 -07:00
|
|
|
const _process = {
|
2018-02-06 18:30:00 -08:00
|
|
|
s: async function (packet,socket) {
|
2019-04-26 11:05:10 -07:00
|
|
|
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
|
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
|
|
|
return {error: 'no socket processing function supplied for command', packet: packet }
|
|
|
|
},
|
|
|
|
|
2018-02-06 18:30:00 -08:00
|
|
|
c: async function (packet,socket) {
|
|
|
|
|
2019-04-26 11:05:10 -07:00
|
|
|
// the the end of life for a consumer packet that has been sent and returned or a packet that was pushed.
|
|
|
|
if (packet.error) return await this._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
|
2019-04-26 11:05:10 -07:00
|
|
|
packet = {error:'no consumer reply processing function supplied for command',packet:packet}
|
|
|
|
this._c.error(packet)
|
2018-02-05 22:05:38 -08:00
|
|
|
} else {
|
2019-04-26 11:05:10 -07:00
|
|
|
packet = {error:'[consumer] no command in reply packet',packet:packet}
|
|
|
|
return await this._c.error(packet)
|
2018-02-05 22:05:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 18:30:00 -08:00
|
|
|
const namespaces = {
|
2019-04-26 11:05:10 -07:00
|
|
|
s: ['_s'],
|
|
|
|
c: ['_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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-04-26 11:05:10 -07:00
|
|
|
const defaultCmds ={
|
2018-02-05 22:05:38 -08:00
|
|
|
s:{
|
|
|
|
echo: async packet => {
|
2019-04-20 16:57:44 -07:00
|
|
|
packet.processed = true
|
|
|
|
packet.info = 'default socket echo'
|
|
|
|
return packet
|
|
|
|
},
|
|
|
|
ack: async packet => {
|
|
|
|
packet.cmd = 'reply'
|
|
|
|
packet.ack = true
|
2019-04-28 10:00:08 -07:00
|
|
|
packet.info = 'this is the base default ack, superceed in your extended class'
|
2019-04-20 16:57:44 -07:00
|
|
|
return packet
|
2018-02-05 22:05:38 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
c:{
|
|
|
|
error: function (packet) {
|
2019-04-26 11:05:10 -07:00
|
|
|
if (process.env.UCI_ENV==='dev') log.error({error:packet.error, packet:packet, msg:'==========Consumer Sent Packet returned with ERROR ========='})
|
2018-02-05 22:05:38 -08:00
|
|
|
},
|
|
|
|
reply: function(packet) {
|
2019-04-26 11:05:10 -07:00
|
|
|
if (process.env.UCI_ENV==='dev') log.debug({packet:packet, msg:'====Packet returned from socket - debug default reply logger==='})
|
2018-02-05 22:05:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|