uci-base/examples/four-in-one.mjs

95 lines
2.2 KiB
JavaScript
Raw Normal View History

import Base from '../src/base'
const USOCKET = __dirname + '/sample.sock'
const socketfuncs = {
write: function(packet){
packet.cmd='reply'
packet.response='return of write command'
return packet
},
write2: function(packet){
packet.cmd='reply'
packet.response='return of write2 command'
return packet
}
}
;
(async () => {
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 fio = new Base({sockets:'uc#c>n,us#s>n,tc#c>t,ts#s>t', id:'four-in-one'})
// app.amendPacketProcessing(basefuncs)
await fio.init()
fio.amendNamedProcessing('tc',{
reply: packet => {
console.log('==============Packet returned to nameed consumer tc==========')
console.dir(packet)
console.log('===========================')
}
})
fio.reply = packet =>{
console.log('==============Packet Displayed by Generic reply function')
console.dir(packet)
}
let packet = {}
console.log('=============sending============')
packet = {cmd:'echo', data:'some data to echo'}
console.log(packet)
await fio.send(packet)
fio.amendSocketProcessing(socketfuncs)
packet = {cmd:'write', data:'data to write'}
console.log(packet)
await fio.send(packet)
// await app.send(packet)
// await delay(500)
// app.amendPacketContext(
// {write: function(packet){
// packet.cmd='log'
// packet.response='return of AMMEDED write command'
// return packet
// }}
// )
// await delay(500)
// packet = {cmd:'write', data:'2ND data to write'}
// await app.sendIPC(packet)
// packet = {cmd:'write2', data:'data to write'}
// await app.send(packet)
// //
await delay(2000)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
process.kill(process.pid, 'SIGTERM')
})
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
}
}