From 3c1340baf049d28e9aff6773034f3582496d740b Mon Sep 17 00:00:00 2001 From: David Kebler Date: Mon, 29 Jan 2018 21:51:13 -0800 Subject: [PATCH] fixed separation of packet properties for each socket type when customizing add amendPacket.. and changed registerPacket to accept single type changes The four-in-one example now using amend and register sucessfully --- .eslintrc.js | 1 - examples/four-in-one.mjs | 85 +++++++++++++++++++++++++++++++--------- package.json | 2 - src/base.mjs | 64 ++++++++++++++++++++++-------- src/packet.mjs | 2 +- 5 files changed, 115 insertions(+), 39 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 7a59647..bb23489 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -9,7 +9,6 @@ module.exports = { "node": true, "mocha": true }, - "parser": "babel-eslint", "parserOptions": { "ecmaVersion": 2017 ,"sourceType": "module" diff --git a/examples/four-in-one.mjs b/examples/four-in-one.mjs index f0cd21e..e890caa 100644 --- a/examples/four-in-one.mjs +++ b/examples/four-in-one.mjs @@ -1,33 +1,53 @@ import Base from '../src/base' const USOCKET = __dirname + '/sample.sock' - -const delay = time => new Promise(res=>setTimeout(()=>res(),time)) - ; (async () => { - // let app = new Base({com:'us,uc,ts,tc', id:'example', path: USOCKET, log:false}) - let app = new Base({com:'us,uc,ts,tc', id:'example'}) + 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 app = new Base({sockets:'us,uc,ts,tc', id:'example', log:false}) await app.init() + console.log('after init') + 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 = {} console.log('ready=============sending============') - let packet = {cmd:'echo', data:'some data to echo'} - await app.send(packet) - app.write = function(packet){ - packet.cmd='log' - packet.response='return of write command' - return packet - } - app.write2 = function(packet){ - packet.cmd='log' - packet.response='return of write2 command' - return packet - } - app.registerPacketContext(app) + // packet = {cmd:'echo', data:'some data to echo'} + // await app.send(packet) + + Object.assign(app,unixfuncs) + app.registerPacketContext('ts',tcpfuncs) packet = {cmd:'write', data:'data to write'} await app.send(packet) + await delay(1000) + app.amendPacketContext( + {write: function(packet){ + packet.cmd='log' + packet.response='return of AMMEDED write command' + return packet + }} + ) + await delay(1000) + packet = {cmd:'write', data:'2ND data to write'} + await app.send(packet) packet = {cmd:'write2', data:'data to write'} await app.send(packet) @@ -37,4 +57,33 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time)) })().catch(err => { console.error('FATAL: UNABLE TO START SYSTEM!\n',err) + process.kill(process.pid, 'SIGTERM') }) + +const unixfuncs = { + 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 + } +} + +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 + } +} diff --git a/package.json b/package.json index 6b89978..1117022 100644 --- a/package.json +++ b/package.json @@ -27,11 +27,9 @@ "@std/esm": "cjs", "devDependencies": { "@std/esm": "^0.18.0", - "babel-eslint": "^8.2.1", "chai": "^4.1.2", "chai-as-promised": "^7.1.1", "codecov": "^3.0.0", - "eslint": "^3.19.0", "istanbul": "^0.4.5", "mocha": "^4.0.1" }, diff --git a/src/base.mjs b/src/base.mjs index 259451a..62b53e0 100644 --- a/src/base.mjs +++ b/src/base.mjs @@ -1,9 +1,12 @@ -import { Socket, Consumer } from '@uci/socket' +// import { Socket, Consumer } from '@uci/socket' +import { Socket, Consumer } from '../../uci-socket/src' import packet from './packet.mjs' import EventEmitter from 'events' const USOCKET = __dirname + '/unix.sock' +const delay = time => new Promise(res=>setTimeout(()=>res(),time)) + export default class Base extends EventEmitter { constructor(opts={}) { super() @@ -12,50 +15,77 @@ export default class Base extends EventEmitter { this.desc = opts.desc // additional details for humans // attach unix socket OR consumer(default) only if path is supplied this.socket={} - this.com = opts.com.split(/[,:|\s]+/) - this.com.forEach( sock => { + opts.sockets.split(/[,:|\s]+/).forEach( sock => { opts.name = this.id +':'+sock - opts.type = sock + if (!opts[sock]) opts[sock] = {} switch (sock) { case 'us': - this.socket[sock] = new Socket(opts.path,opts) + opts.us.path = opts.us.path || opts.path + this.socket[sock] = new Socket(opts.us.path,opts) break case 'uc': - this.socket[sock] = new Consumer(opts.path,opts) + opts.uc.path = opts.uc.path || opts.path + this.socket[sock] = new Consumer(opts.uc.path,opts) break case 'ts': - this.socket[sock]= new Socket(opts) + this.socket[sock]= new Socket(opts.ts, opts) break case 'tc': - this.socket[sock]= new Consumer(opts) + this.socket[sock]= new Consumer(opts.tc,opts) } }) } // end constructor async init (context) { - context = context || this // context is Base if not supplied + context = context || this // context is Base for all if not supplied for(let type of Object.keys(this.socket)){ if (type.indexOf('s')!==-1) { - this.socket[type].packet = packet.socket + this.socket[type].packet = Object.assign({},packet.socket) // default packet processing await this.socket[type].create(context) + // setTimeout(context =>{this.socket[type].create(context)},500) } else { - this.socket[type].packet = packet.consumer + this.socket[type].packet = Object.assign({},packet.consumer) // default packet processing await this.socket[type].connect(context) } } } // init - registerPacketContext(context) { - for(let type of Object.keys(this.socket)){ - this.socket[type].registerPacketContext(context) + registerPacketContext(type, context) { + if (typeof type === 'string') { + this.socket[type].registerPacketContext(Object.assign({},context)) + } else { + context=type + for(let type of Object.keys(this.socket)){ + this.socket[type].registerPacketContext(context) + } } } - registerPacketProcessor(func) { - for(let type of Object.keys(this.socket)){ - this.socket[type].registerPacketProcessor(func) + amendPacketContext(type, context) { + if (typeof type === 'string') { + Object.assign(this.socket[type].packet.context,context) + } else { + context=type + for(let type of Object.keys(this.socket)){ + Object.assign(this.socket[type].packet.context,context) + } + } + } + + amendPacketProcessing(type,funcs) { + this.socket[type].packet = Object.assign({},this.socket[type].packet,funcs) + } + + registerPacketProcessor(type,func) { + if (typeof type === 'string') { + this.socket[type].packet._process = func + } else { + func = type + for(let type of Object.keys(this.socket)){ + this.socket[type].registerPacketProcessor(func) + } } } diff --git a/src/packet.mjs b/src/packet.mjs index 61bb9e9..0b2917b 100644 --- a/src/packet.mjs +++ b/src/packet.mjs @@ -12,7 +12,7 @@ export default { echo: packet => { packet.processed = true packet.cmd = 'log' - packet.info = 'echoed' + packet.info = 'default socket echo' return packet } },