2018-01-27 23:20:33 -08:00
|
|
|
import Base from '../src/base'
|
|
|
|
|
|
|
|
const USOCKET = __dirname + '/sample.sock'
|
2018-01-30 21:12:38 -08:00
|
|
|
|
|
|
|
const basefuncs = {
|
|
|
|
write: function(packet){
|
|
|
|
packet.cmd='log'
|
|
|
|
packet.response='return of write command'
|
|
|
|
return packet
|
|
|
|
},
|
|
|
|
write2: function(packet){
|
|
|
|
packet.cmd='log'
|
|
|
|
packet.response='return of write2 command'
|
|
|
|
return packet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 23:20:33 -08:00
|
|
|
;
|
|
|
|
(async () => {
|
|
|
|
|
2018-01-29 21:51:13 -08:00
|
|
|
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
2018-01-27 23:20:33 -08:00
|
|
|
|
2018-01-29 21:51:13 -08:00
|
|
|
// let app = new Base({com:'us,uc,ts,tc', id:'example', path: USOCKET, log:false})
|
2018-02-02 14:09:52 -08:00
|
|
|
let app = new Base({sockets:'uc#c>n,us#s>n,tc#c>t,ts#s>t', id:'four-in-one'})
|
2018-01-30 21:12:38 -08:00
|
|
|
|
|
|
|
app.amendPacketProcessing(basefuncs)
|
2018-01-27 23:20:33 -08:00
|
|
|
|
|
|
|
await app.init()
|
2018-01-30 21:12:38 -08:00
|
|
|
|
2018-01-29 21:51:13 -08:00
|
|
|
app.amendPacketProcessing('tc',{
|
|
|
|
log: packet => {
|
|
|
|
console.log('==============Packet returned to TCP consumer==========')
|
|
|
|
console.dir(packet)
|
|
|
|
console.log('===========================')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.registerPacketProcessor('ts',
|
|
|
|
async function (packet) {
|
|
|
|
packet.test = 'this went through custom tcp socket processor'
|
|
|
|
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[packet.cmd]) return await this[packet.cmd](packet)
|
|
|
|
return {error: 'no socket processing function supplied for command', packet: packet }
|
|
|
|
})
|
|
|
|
|
|
|
|
let packet = {}
|
2018-01-30 21:12:38 -08:00
|
|
|
console.log('=============sending============')
|
|
|
|
packet = {cmd:'echo', data:'some data to echo'}
|
|
|
|
await app.send('tc',packet)
|
2018-01-29 21:51:13 -08:00
|
|
|
|
|
|
|
app.registerPacketContext('ts',tcpfuncs)
|
2018-01-27 23:20:33 -08:00
|
|
|
packet = {cmd:'write', data:'data to write'}
|
|
|
|
await app.send(packet)
|
2018-01-30 21:12:38 -08:00
|
|
|
await delay(500)
|
2018-01-29 21:51:13 -08:00
|
|
|
app.amendPacketContext(
|
|
|
|
{write: function(packet){
|
|
|
|
packet.cmd='log'
|
|
|
|
packet.response='return of AMMEDED write command'
|
|
|
|
return packet
|
|
|
|
}}
|
|
|
|
)
|
2018-01-30 21:12:38 -08:00
|
|
|
await delay(500)
|
2018-01-29 21:51:13 -08:00
|
|
|
packet = {cmd:'write', data:'2ND data to write'}
|
2018-01-30 21:12:38 -08:00
|
|
|
await app.sendIPC(packet)
|
2018-01-27 23:20:33 -08:00
|
|
|
packet = {cmd:'write2', data:'data to write'}
|
2018-01-30 21:12:38 -08:00
|
|
|
await app.sendTCP(packet)
|
|
|
|
//
|
2018-01-27 23:20:33 -08:00
|
|
|
await delay(2000)
|
|
|
|
process.kill(process.pid, 'SIGTERM')
|
|
|
|
|
|
|
|
|
|
|
|
})().catch(err => {
|
|
|
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
2018-01-29 21:51:13 -08:00
|
|
|
process.kill(process.pid, 'SIGTERM')
|
2018-01-27 23:20:33 -08:00
|
|
|
})
|
2018-01-29 21:51:13 -08:00
|
|
|
|
2018-01-30 21:12:38 -08:00
|
|
|
|
2018-01-29 21:51:13 -08:00
|
|
|
|
|
|
|
const tcpfuncs = {
|
|
|
|
write: function(packet){
|
|
|
|
packet.cmd='log'
|
|
|
|
packet.response='return of write command'
|
|
|
|
packet.via = 'tcp write'
|
|
|
|
return packet
|
|
|
|
},
|
|
|
|
write2: function(packet){
|
|
|
|
packet.cmd='log'
|
|
|
|
packet.response='return of write2 command'
|
|
|
|
packet.via = 'tcp write2'
|
|
|
|
return packet
|
|
|
|
}
|
|
|
|
}
|