socket:
  removed all 'client' names and replaced with 'consumer'
  switched to using MAP for holding consumers
  incoming consumer includes passing opts.data for passing consumer specific information to socket
consumer:
   supports opts.data for passing to socket
updated examples accordingly
master
David Kebler 2020-01-06 23:01:34 -08:00
parent 70e16fa08d
commit 8a4fdf067c
5 changed files with 95 additions and 90 deletions

View File

@ -70,6 +70,12 @@ client.on('status', event => {
console.log('===============') console.log('===============')
} }
setTimeout(()=> {
console.log('=============Consumer now Offline=============')
process.exit(1)
}
,120000)
})().catch(err => { })().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err) console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
}) })

View File

@ -38,6 +38,7 @@ class Test extends Socket {
// } // }
// const PATH = '/opt/bogus/socket' // const PATH = '/opt/bogus/socket'
const PATH = true const PATH = true
const PUSHES = 3
// options.conPacket = {cmd:'onconnect', data:'this is a packet data sent consumer after handshake/authentification'} // options.conPacket = {cmd:'onconnect', data:'this is a packet data sent consumer after handshake/authentification'}
const TOKENS = ['cheetos'] const TOKENS = ['cheetos']
let test = new Test({path:PATH, tokens:TOKENS}) let test = new Test({path:PATH, tokens:TOKENS})
@ -72,14 +73,14 @@ test.registerPacketProcessor(processor)
let count = 0 let count = 0
const push = setInterval( () => { const push = setInterval( () => {
count++ count++
test.push({cmd:'pushed', count:count, status:`some pushed data ${count}`}) test.push({cmd:'pushed', count:count, status:`pushing some data ${count} of ${PUSHES}`})
if (count >3) { if (count >PUSHES) {
clearInterval(push) clearInterval(push)
test.push({cmd:'pushed',status:'now will simulate server going offline by stopping to send ping for 10 seconds'}) test.push({cmd:'pushed',status:'now will simulate server going offline by stopping to send ping for 15 seconds'})
test.disablePing() test.disablePing()
setTimeout( () => { setTimeout( () => {
test.enablePing() test.enablePing()
},10000) },15000)
} }
},3000) },3000)

View File

@ -1,6 +1,6 @@
{ {
"name": "@uci/socket", "name": "@uci/socket",
"version": "0.2.28", "version": "0.2.29",
"description": "JSON packet intra(named)/inter(TCP) host communication over socket", "description": "JSON packet intra(named)/inter(TCP) host communication over socket",
"main": "src", "main": "src",
"scripts": { "scripts": {
@ -16,8 +16,7 @@
"client": "node -r esm examples/client", "client": "node -r esm examples/client",
"devc": "UCI_ENV=dev ./node_modules/.bin/nodemon -r esm examples/client", "devc": "UCI_ENV=dev ./node_modules/.bin/nodemon -r esm examples/client",
"devc:token": "UCI_CLIENT_TOKEN='cheetos' npm run devc", "devc:token": "UCI_CLIENT_TOKEN='cheetos' npm run devc",
"devc:debug": "UCI_LOG_LEVEL=debug npm run devc", "devc:debug": "UCI_LOG_LEVEL=debug npm run devc"
"c2": "node -r esm examples/client2"
}, },
"author": "David Kebler", "author": "David Kebler",
"license": "MIT", "license": "MIT",

View File

@ -46,10 +46,13 @@ class SocketConsumer extends Socket {
opts.path = path.join(DEFAULT_PIPE_DIR, opts.path) opts.path = path.join(DEFAULT_PIPE_DIR, opts.path)
} }
this.opts = opts this.opts = opts
this._data = {id:this.id, name:opts.name||this.id}
Object.assign(this._data,opts.data||{}) // holds consumer specific data that will be passed to server in header and on connection
// default is keepAlive true, must set to false to explicitly disable // default is keepAlive true, must set to false to explicitly disable
// if keepAlive is true then consumer will also be reconnecting consumer // if keepAlive is true then consumer will also be reconnecting consumer
// initTimeout > 4 means socketInit will return a promise
this.initTimeout = opts.initTimeout > 4 ? opts.initTimeout * 1000 : null this.initTimeout = opts.initTimeout > 4 ? opts.initTimeout * 1000 : null
this.pingFailedTimeout = opts.pingFailedTimeout * 1000 || 10000 this.pingFailedTimeout = opts.pingFailedTimeout * 1000 || 5000
this.reconnectLimit = opts.reconnectLimit || 0 this.reconnectLimit = opts.reconnectLimit || 0
this.retryWait = opts.retryWait==null ? 5000 : opts.retryWait * 1000 this.retryWait = opts.retryWait==null ? 5000 : opts.retryWait * 1000
this.heartBeat = !!process.env.HEARTBEAT || opts.heartBeat this.heartBeat = !!process.env.HEARTBEAT || opts.heartBeat
@ -116,7 +119,6 @@ class SocketConsumer extends Socket {
, this.initTimeout) , this.initTimeout)
const successHandler = (ev) => { const successHandler = (ev) => {
console.log('initial success', ev.state)
if (ev.state === 'connected') { if (ev.state === 'connected') {
clearTimeout(initTimeout) clearTimeout(initTimeout)
this.removeListener('connection:socket',successHandler) this.removeListener('connection:socket',successHandler)
@ -125,7 +127,7 @@ class SocketConsumer extends Socket {
} }
} }
this.on('connection:socket',successHandler) this.on('connection:socket',successHandler.bind(this))
}) })
} }
@ -174,7 +176,7 @@ class SocketConsumer extends Socket {
id: Math.random() id: Math.random()
.toString() .toString()
.slice(2), // need this for when multiple sends for different consumers use same packet instanceack .slice(2), // need this for when multiple sends for different consumers use same packet instanceack
sender: { name: this.name, instanceID: this.id }, sender: { data: this._data, instanceID: this.id},
path: this.opts.path, path: this.opts.path,
port: this.opts.port, port: this.opts.port,
host: this.opts.host host: this.opts.host
@ -286,7 +288,7 @@ async function handshake (packet) {
this.notify('handshake') this.notify('handshake')
let authPacket = this._authenticate() || {} let authPacket = this._authenticate() || {}
authPacket._authenticate = true authPacket._authenticate = true
authPacket.clientName = this.id authPacket.data = this._data
let res = await this._authenticateSend(authPacket) let res = await this._authenticateSend(authPacket)
this.stream.removeAllListeners('message') this.stream.removeAllListeners('message')
clearTimeout(this._doneAuthenticate) clearTimeout(this._doneAuthenticate)
@ -331,7 +333,7 @@ function messageHandler(packet) {
// assume all errors are fatal and the socket needs to be disconnected/reconnected // assume all errors are fatal and the socket needs to be disconnected/reconnected
async function errorHandler (err) { async function errorHandler (err) {
this.disconnect() this.disconnect()
let msg = `error, socket has been disconnected, trying reconnect in ${this.retryWait/1000} secs` let msg = `error, this consumer is disconnected, trying reconnect in ${this.retryWait/1000} secs`
this.notify('error',{error:err,msg:msg}) this.notify('error',{error:err,msg:msg})
this.log('error',msg) this.log('error',msg)
this._errorRetry = setTimeout(this.reconnect.bind(this),this.retryWait) this._errorRetry = setTimeout(this.reconnect.bind(this),this.retryWait)

View File

@ -39,7 +39,7 @@ export default function socketClass(Server) {
* @param {String} options.host a tcp host name nornally not used as 0.0.0.0 is set by default * @param {String} options.host a tcp host name nornally not used as 0.0.0.0 is set by default
* @param {String} options.port a tcp * @param {String} options.port a tcp
* @param {String | Boolean} options.path xeither full path to where socket should be created or if just 'true' then use default * @param {String | Boolean} options.path xeither full path to where socket should be created or if just 'true' then use default
* @param {Boolean} options.clientTracking track connected clients for push notifications - default: true * @param {Boolean} options.consumerTracking track connected consumers for push notifications - default: true
* @param {Object} options.conPacket A json operson's property * @param {Object} options.conPacket A json operson's property
* *
*/ */
@ -61,14 +61,14 @@ export default function socketClass(Server) {
this.allowAnonymous = (!opts.tokens || !!process.env.UCI_ANON || opts.allowAnonymous) ? true : false this.allowAnonymous = (!opts.tokens || !!process.env.UCI_ANON || opts.allowAnonymous) ? true : false
this.tokens = opts.tokens || [] this.tokens = opts.tokens || []
this.keepAlive = 'keepAlive' in opts ? opts.keepAlive : true this.keepAlive = 'keepAlive' in opts ? opts.keepAlive : true
this.pingInterval = opts.pingInterval === false ? opts.pingInterval : (opts.pingInterval * 1000 || 5000) this.pingInterval = opts.pingInterval === false ? 0 : (opts.pingInterval * 1000 || 5000)
this.clients = [] // track consumers (i.e. clients) TODO use a Map this.consumers = new Map() // track consumers (i.e. clients) TODO use a Map
this.nextClientID = 0 // incrementer for default initial client ID this.nextConsumerID = 0 // incrementer for default initial consumer ID
this.opts = opts // for use to recover from selected errors this.opts = opts // for use to recover from selected errors
this.errorCount = 0 this.errorCount = 0
//self bindings //self bindings
this.create = this.create.bind(this) this.create = this.create.bind(this)
this.authenticateClient = this.authenticateClient.bind(this) this.authenticateConsumer = this.authenticateConsumer.bind(this)
this._authenticate = this._authenticate.bind(this) this._authenticate = this._authenticate.bind(this)
this.close = promisify(this.close).bind(this) this.close = promisify(this.close).bind(this)
log = logger({ log = logger({
@ -141,11 +141,12 @@ export default function socketClass(Server) {
this.emit('log', errors) this.emit('log', errors)
} }
}) })
let obj = {method:'create', line:54, msg:'socket server created and listening at', address:this.address()} let msg = `socket ready and listening ${typeof this.address() ==='string' ? `at ${this.address()}` : `on port ${this.address().port}`}`
let obj = {method:'create', line:54, msg:msg}
log.info(obj) log.info(obj)
this.on('connection', this._connectionHandler.bind(this)) this.on('connection', this._connectionHandler.bind(this))
this.emit('log:',) this.emit('log:',)
resolve(`socket ready and listening at ${this.address().address}:${this.address().port}`) resolve(msg)
}) })
super.listen(this.opts) super.listen(this.opts)
@ -167,7 +168,7 @@ export default function socketClass(Server) {
enablePing () { enablePing () {
if (this.pingInterval > 499) { if (this.pingInterval > 499) {
this._ping = setInterval( async () =>{ this._ping = setInterval( async () =>{
if (this.clients.length > 0) this.push({pingInterval:this.pingInterval},'ping') if (this.consumers.size > 0) this.push({pingInterval:this.pingInterval},'ping')
},this.pingInterval) },this.pingInterval)
} }
} }
@ -212,7 +213,7 @@ export default function socketClass(Server) {
/** /**
* push - pushes a supplied UCI object packet to all connected clients * push - pushes a supplied UCI object packet to all connected consumers
* *
* @param {object} packet Description * @param {object} packet Description
* @param {string} id the header id string of the pushed packet, default: 'pushed' * @param {string} id the header id string of the pushed packet, default: 'pushed'
@ -220,12 +221,12 @@ export default function socketClass(Server) {
*/ */
async push(packet={},id) { async push(packet={},id) {
packet._header = {id: id || 'pushed'} packet._header = {id: id || 'pushed'}
if (this.clients.length > 0) { if (this.consumers.size > 0) {
log.trace({method:'push', line:142, id:packet._header.id, opts: this.opts, packet: packet, msg:'pushing a packet to all connected consumers'}) log.trace({method:'push', line:142, id:packet._header.id, opts: this.opts, packet: packet, msg:'pushing a packet to all connected consumers'})
// TODO should do a map and single promise // TODO should do a map and single promise
this.clients.forEach(async client => { this.consumers.forEach(async consumer => {
if (client.writable) { if (consumer.writable) {
let [err] = await btc(this._send)(client,packet) let [err] = await btc(this._send)(consumer,packet)
if (err) log.error({msg:err, error:err}) if (err) log.error({msg:err, error:err})
} }
}) })
@ -234,56 +235,47 @@ export default function socketClass(Server) {
} }
} }
// TODO won't need this if moving to a Map
getClientIndex(sid) { removeConsumer (sid) {
return this.clients.findIndex(client => {return client.sid === sid }) let consumer=this.consumers.get(sid)
this.emit('log',{level:'info', msg:'a consumer disconnected', consumer:consumer.data, sid:consumer.sid})
this.emit('connection:consumer',{state:'disconnected', msg:'a consumer disconnected', consumer:consumer.data, sid:consumer.sid})
consumer.removeAllListeners()
consumer.stream.removeAllListeners()
this.consumers.delete(sid)
log.warn({msg:'consumer removed from tracking',sid:sid, curConsumerCount:this.consumers.size})
} }
getClient(sid) { async authenticateConsumer(consumer) {
return this.clients[this._getClientIndex(sid)]
}
removeClient (sid) {
let index = this.getClientIndex(sid)
let client=this.clients[index]
this.emit('log',{level:'info', msg:'a consumer disconnected', name:client.name, id:client.id})
this.emit('connection:consumer',{state:'disconnected', msg:'a consumer disconnected', name:client.name, id:client.id})
client.removeAllListeners()
client.stream.removeAllListeners()
this.clients.splice(index,1)
log.warn({msg:'consumer removed from tracking',sid:sid, curClientCount:this.clients.length})
}
async authenticateClient(client) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
// when consumer gets the handshake they must follow with authentication // when consumer gets the handshake they must follow with authentication
client.stream.on('message', authenticate.bind(this,client)) consumer.stream.on('message', authenticate.bind(this,consumer))
let [err] = await btc(this._send)(client,{_handshake: true, sid:client.sid}) let [err] = await btc(this._send)(consumer,{_handshake: true, sid:consumer.sid})
if (err) { if (err) {
log.error({msg:'error in handshake send', error:err}) log.error({msg:'error in handshake send', error:err})
reject(err) reject(err)
} }
async function authenticate (client,packet) { async function authenticate (consumer,packet) {
log.debug({msg:`authentication packet from client ${client.name}:${client.id}:${client.sid}`, packet:packet}) log.debug({msg:`authentication packet from consumer ${consumer.name}:${consumer.id}:${consumer.sid}`, packet:packet})
client.stream.removeAllListeners('message') consumer.stream.removeAllListeners('message')
if (!packet._authenticate) reject('first client packet was not authentication') if (!packet._authenticate) reject('first consumer packet was not authentication')
else { else {
let [err, res] = await btc(this._authenticate)(packet) let [err, res] = await btc(this._authenticate)(packet)
client.authenticated = this.allowAnonymous ? 'anonymous' : (err ? false : res) consumer.authenticated = this.allowAnonymous ? 'anonymous' : (err ? false : res)
client.name = packet.clientName consumer.data = packet.data
packet.authenticated = client.authenticated packet.authenticated = consumer.authenticated
packet.reason = err || null packet.reason = err || null
log.debug({msg:'sending authorization result to client', packet:packet}) log.debug({msg:'sending authorization result to consumer', packet:packet})
await this._send(client,packet) // send either way await this._send(consumer,packet) // send either way
if (err && !this.allowAnonymous) { if (err && !this.allowAnonymous) {
log.info({msg:'client authentication failed', client:client.name, client_sid:client.sid, reason:err}) log.info({msg:`consumer ${consumer.data.name} authentication failed`, consumer:consumer.data, consumer_sid:consumer.sid, reason:err})
reject(packet.reason) reject(packet.reason)
} }
else { else {
log.info({msg:'client authenticated successfuly', client:client.name}) log.info({msg:`consumer ${consumer.data.name} authenticated successfuly`, consumer:consumer.data})
if (this.allowAnonymous) log.warn({msg:'socket consumer connected anonymously', consumer:client.name}) if (this.allowAnonymous) log.warn({msg:`consumer ${consumer.data.name}, connected anonymously`, consumer:consumer.data})
resolve(client.authenticated) resolve(consumer.authenticated)
} }
} }
} }
@ -299,29 +291,30 @@ export default function socketClass(Server) {
return false return false
} }
// default authenticator - reject value should be reason which is returned to client // default authenticator - reject value should be reason which is returned to consumer
async _authenticate (packet) { async _authenticate (packet) {
if (!this._validateToken(packet.token)) return Promise.reject('invalid token') if (!this._validateToken(packet.token)) return Promise.reject('invalid token')
return true return true
} }
// async _connectionHandler({consumer, server}) { // this gets called for each client connection and is unique to // async _connectionHandler({consumer, server}) { // this gets called for each consumer connection and is unique to
async _connectionHandler(consumer) { // this gets called for each client connection and is unique to each async _connectionHandler(consumer) { // this gets called for each consumer connection and is unique to each
const stream = new JSONStream() const stream = new JSONStream()
consumer.stream = stream consumer.stream = stream
consumer.data = {}
consumer.connected = true consumer.connected = true
// add listeners // add listeners
consumer.on('error', (err) => { consumer.on('error', (err) => {
log.error({msg:'client connection error',error:err}) log.error({msg:'consumer connection error',error:err})
// TODO do more handling than just logging // TODO do more handling than just logging
}) })
consumer.on('end', (err) => { consumer.on('end', (err) => {
log.error({msg:'consumer connected ended',error:err}) log.error({msg:`'consumer connection ended: ${consumer.data.name}`, error:err})
if (consumer.sid) this.removeClient(consumer.sid) if (consumer.sid) this.removeConsumer(consumer.sid)
else { else {
consumer.removeAllListeners() consumer.removeAllListeners()
consumer.stream.removeAllListeners() consumer.stream.removeAllListeners()
@ -335,9 +328,9 @@ export default function socketClass(Server) {
// TODO do more handling than just logging // TODO do more handling than just logging
}) })
consumer.authenticated = true // consumer.authenticated = true
let [err] = await btc(this.authenticateClient)(consumer) let [err] = await btc(this.authenticateConsumer)(consumer)
if (!this.allowAnonymous) { if (!this.allowAnonymous) {
if (err) { if (err) {
consumer.removeAllListeners() consumer.removeAllListeners()
@ -347,18 +340,17 @@ export default function socketClass(Server) {
return return
} }
} }
// authenticated consumer, add to list of clients // authenticated consumer, add to list of consumers
consumer.sid = ++this.nextClientID // server assigned ID consumer.sid = ++this.nextConsumerID // server assigned ID
consumer.socketSide = true // consumer.authenticated = true
consumer.authenticated = true this.consumers.set(consumer.sid, consumer) // add current consumer to consumers
this.clients.push(consumer) // add current consumer to clients
consumer.setKeepAlive(this.keepAlive,30) consumer.setKeepAlive(this.keepAlive,30)
const clientCloseHandler = (sid) => { const consumerCloseHandler = (sid) => {
log.warn({msg:'consumer connection was closed',sid:sid}) log.warn({msg:'consumer connection was closed',sid:sid})
this.removeClient(sid) this.removeConsumer(sid)
} }
consumer.on('close', clientCloseHandler.bind(this,consumer.sid)) consumer.on('close', consumerCloseHandler.bind(this,consumer.sid))
log.debug({method:'_listen', line:364, msg:'new consumer connected/authenticated', cname:consumer.name, cid:consumer.id, totalConsumers:this.clients.length}) log.debug({method:'_listen', line:364, msg:'new consumer connected/authenticated', cname:consumer.name, cid:consumer.id, totalConsumers:this.consumers.size})
// all's set enable main incoming message processor // all's set enable main incoming message processor
stream.on('message', messageProcess.bind(this, consumer)) stream.on('message', messageProcess.bind(this, consumer))
@ -369,12 +361,17 @@ export default function socketClass(Server) {
} }
this.emit('log',{level:'info', msg:'a consumer connected and authenticated', name:consumer.name, id:consumer.id}) this.emit('log',{level:'info', msg:'a consumer connected and authenticated', name:consumer.name, id:consumer.id})
this.emit('connection:consumer',{state:'connected', msg:'a consumer connected and authenticated', name:consumer.name, id:consumer.id}) this.emit('connection:consumer',{state:'connected', msg:`consumer ${(consumer.data ||{}).name} connected and authenticated to socket ${this.id}`,
name:(consumer.data ||{}).name ||(consumer.data ||{}).id || consumer.sid,
sid:consumer.sid,
data:consumer.data,
authenticated:consumer.authenticated
})
// that's it. Connection is active // that's it. Connection is active
async function messageProcess(client, packet) { async function messageProcess(consumer, packet) {
log.debug({method:'_listen', line:179, packet: packet, client:client.name, msg:'incoming packet on socket side'}) log.debug({method:'_listen', line:179, packet: packet, consumer:consumer.data, msg:'incoming packet on socket side'})
let res = (await this._packetProcess(clone(packet))) || {} let res = (await this._packetProcess(clone(packet))) || {}
if (Object.keys(res).length === 0) if (Object.keys(res).length === 0)
res = { res = {
@ -390,7 +387,7 @@ export default function socketClass(Server) {
res._header.responder = { name: this.name, instanceID: this.id } res._header.responder = { name: this.name, instanceID: this.id }
res._header.socket = this.address() res._header.socket = this.address()
if (!res.cmd) res.cmd = this.defaultReturnCmd || 'reply' // by default return command is 'reply' if (!res.cmd) res.cmd = this.defaultReturnCmd || 'reply' // by default return command is 'reply'
let [err] = await btc(this._send)(client,res) let [err] = await btc(this._send)(consumer,res)
if (err) log.error({msg:err, error:err}) if (err) log.error({msg:err, error:err})
} // end message process } // end message process
@ -402,9 +399,9 @@ export default function socketClass(Server) {
// this.push() // this.push()
clearInterval(this._ping) clearInterval(this._ping)
await this.close() await this.close()
this.clients.forEach(client => { this.consumers.forEach(consumer => {
client.removeAllListeners() consumer.removeAllListeners()
client.stream.removeAllListeners() consumer.stream.removeAllListeners()
}) })
log.debug({method:'_destroy', line:219, msg:'all connections closed....exiting'}) log.debug({method:'_destroy', line:219, msg:'all connections closed....exiting'})
process.exit() process.exit()
@ -417,18 +414,18 @@ export default function socketClass(Server) {
}) })
} }
async _send(client, packet) { async _send(consumer, packet) {
log.trace({msg:`sending to client:${client.id}`, packet:packet}) log.trace({msg:`sending to consumer:${consumer.sid}:${consumer.data.name}`, consumer:consumer.data, packet:packet})
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
if (!client.writable) { if (!consumer.writable) {
reject('socket stream closed can not send packet') reject('socket stream closed can not send packet')
return return
} }
let [err,ser] = await btc(client.stream.serialize)(packet) let [err,ser] = await btc(consumer.stream.serialize)(packet)
if (err) reject('unable to serialze the packet') if (err) reject('unable to serialze the packet')
const cb = () => resolve('packet written to socket stream') const cb = () => resolve('packet written to socket stream')
if (!client.write(ser)) { if (!consumer.write(ser)) {
client.once('drain', cb) consumer.once('drain', cb)
} else { } else {
process.nextTick(cb) process.nextTick(cb)
} }