2018-01-27 23:20:33 -08:00
|
|
|
import Base from '../src/base'
|
|
|
|
|
2018-02-13 14:19:18 -08:00
|
|
|
// const USOCKET = __dirname + '/sample.sock'
|
2018-01-30 21:12:38 -08:00
|
|
|
|
2018-02-04 14:18:21 -08:00
|
|
|
const socketfuncs = {
|
2018-02-05 22:05:38 -08:00
|
|
|
write: {
|
|
|
|
happy: function(packet){
|
|
|
|
return new Promise( async (resolve) => {
|
|
|
|
let res = {}
|
2018-02-13 14:19:18 -08:00
|
|
|
res.response=packet.data+ ' and Im a happy puppy :)'
|
2018-02-05 22:05:38 -08:00
|
|
|
return resolve(res)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
sad: function(packet){
|
|
|
|
return new Promise( async (resolve) => {
|
|
|
|
let res = {}
|
|
|
|
res.response='Im a sad dog :('
|
|
|
|
return resolve(res)
|
|
|
|
})
|
|
|
|
}
|
2018-01-30 21:12:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 23:20:33 -08:00
|
|
|
|
|
|
|
|
2018-02-06 18:30:00 -08:00
|
|
|
const tcpfuncs = {
|
|
|
|
write: {
|
|
|
|
happy: function(packet){
|
|
|
|
return new Promise( async (resolve) => {
|
|
|
|
let res = {}
|
|
|
|
res.cmd='reply'
|
2018-02-13 14:19:18 -08:00
|
|
|
res.response=packet.data+ ' and Im a TCP happy puppy :)'
|
2018-02-06 18:30:00 -08:00
|
|
|
return resolve(res)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
sad: function(packet){
|
|
|
|
return new Promise( async (resolve) => {
|
|
|
|
let res = {}
|
|
|
|
res.cmd='reply'
|
|
|
|
res.response='Im a TCP sad dog :('
|
|
|
|
return resolve(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 23:20:33 -08:00
|
|
|
|
2018-02-13 14:19:18 -08:00
|
|
|
// const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
2018-01-30 21:12:38 -08:00
|
|
|
|
2018-02-06 18:30:00 -08:00
|
|
|
;
|
|
|
|
(async () => {
|
2018-01-29 21:51:13 -08:00
|
|
|
|
2018-05-20 15:44:31 -07:00
|
|
|
let fio = new Base({sockets:'uc#c>n,us#s>n,tc#c>t,ts#s>t,mqtts#s>m,webs#s>w', webs:{ port:8090 }, mqtts:{ topics:'write'}, id:'four-in-one'})
|
2018-02-06 18:30:00 -08:00
|
|
|
// let fio = new Base({sockets:'uc#c>n,us#s>n', id:'four-in-one'})
|
2018-05-20 15:44:31 -07:00
|
|
|
let err = await fio.init()
|
|
|
|
// console.log(err)
|
|
|
|
if(!err.find(socket => {
|
|
|
|
console.log(socket.name)
|
|
|
|
return socket.name==='mqtts'}))
|
|
|
|
|
|
|
|
{
|
|
|
|
const mqttProcess = async function (packet,topic) {
|
|
|
|
console.log('==============mqtt incoming packet/topic processor =========')
|
|
|
|
console.log(packet, topic, fio.s[topic][packet.cmd])
|
|
|
|
if (packet.cmd) console.log(await fio.s[topic][packet.cmd](packet))
|
|
|
|
console.log('===========================')
|
|
|
|
}
|
|
|
|
|
|
|
|
fio.socket.mqtts.registerPacketProcessor(mqttProcess)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-06 18:30:00 -08:00
|
|
|
fio.s = socketfuncs
|
|
|
|
fio.st = tcpfuncs
|
|
|
|
fio.ct = {reply: packet =>{
|
|
|
|
console.log('==============Packet Displayed for TCP consumer received packets only')
|
2018-02-13 14:19:18 -08:00
|
|
|
console.dir(packet.response)
|
|
|
|
console.log('===========================')
|
2018-02-06 18:30:00 -08:00
|
|
|
}}
|
|
|
|
fio.cn = {
|
|
|
|
reply: function (packet) {
|
|
|
|
console.log('==============Replay for only Named Pipe Consumer=========')
|
2018-02-13 14:19:18 -08:00
|
|
|
console.dir(packet.response)
|
2018-02-06 18:30:00 -08:00
|
|
|
console.log('===========================')
|
|
|
|
}
|
|
|
|
}
|
2018-02-04 14:18:21 -08:00
|
|
|
|
2018-02-06 18:30:00 -08:00
|
|
|
fio.good = {
|
|
|
|
bad: function(packet){
|
|
|
|
return new Promise( async (resolve) => {
|
|
|
|
let res = {}
|
|
|
|
res.req = packet
|
|
|
|
res.cmd='good/ugly'
|
|
|
|
res.response='The Good The Bad and The Ugly'
|
|
|
|
return resolve(res)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
ugly: function (packet) {
|
|
|
|
console.log('==============reply from Good Bad command =========')
|
|
|
|
console.log(packet.response)
|
|
|
|
console.log('===========================')
|
|
|
|
}
|
|
|
|
}
|
2018-01-29 21:51:13 -08:00
|
|
|
|
2018-05-20 15:44:31 -07:00
|
|
|
let packet = {}
|
|
|
|
console.log('=============sending============')
|
|
|
|
packet = {cmd:'echo', data:'some data to echo'}
|
|
|
|
console.log(packet)
|
|
|
|
await fio.send(packet,'uc')
|
|
|
|
packet = {cmd:'write:happy', data:'My name is Zoe'}
|
|
|
|
console.log(packet)
|
|
|
|
console.log(await fio.send(packet))
|
|
|
|
console.log(fio.getPacketByName('uc',await fio.send(packet)).response)
|
|
|
|
packet = {cmd:'write:sad', data:'data to write'}
|
|
|
|
console.log(packet)
|
|
|
|
await fio.send(packet)
|
|
|
|
packet = {cmd:'write:sad', data:'sent only via tcp'}
|
|
|
|
console.log(packet)
|
|
|
|
console.log(fio.getPacketByName('tc',await fio.sendTCP(packet)))
|
|
|
|
packet = {cmd:'good:bad'}
|
|
|
|
console.log(packet)
|
|
|
|
await fio.send(packet)
|
2018-02-04 14:18:21 -08:00
|
|
|
|
2018-05-20 15:44:31 -07:00
|
|
|
// process.kill(process.pid, 'SIGTERM')
|
2018-01-27 23:20:33 -08:00
|
|
|
|
|
|
|
})().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
|
|
|
})
|