Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
feat(logs): improve logs
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Sep 11, 2016
1 parent c85b2c9 commit dfbde59
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
15 changes: 8 additions & 7 deletions src/agreement.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const debug = require('debug')
const log = debug('multistream:agreement')
log.error = debug('multistream:agreement:error')

exports.select = (multicodec, callback) => {
exports.select = (multicodec, callback, msThreadId) => {
const stream = handshake({
timeout: 60 * 1000
}, callback)

const shake = stream.handshake

log('writing multicodec %s', multicodec)
log('(%s) writing multicodec %s', msThreadId, multicodec)
writeEncoded(shake, new Buffer(multicodec + '\n'), callback)

lp.decodeFromReader(shake, (err, data) => {
Expand All @@ -28,14 +28,14 @@ exports.select = (multicodec, callback) => {
return callback(new Error(`"${multicodec}" not supported`), shake.rest())
}

log('multicodec ack')
log('(%s) received ack: %s', msThreadId, protocol)
callback(null, shake.rest())
})

return stream
}

exports.handlerSelector = (rawConn, handlersMap) => {
exports.handlerSelector = (rawConn, handlersMap, msThreadId) => {
const cb = (err) => {
// incoming errors are irrelevant for the app
log.error(err)
Expand All @@ -54,17 +54,18 @@ exports.handlerSelector = (rawConn, handlersMap) => {
if (err) {
return cb(err)
}
log('received: %s', data.toString())
log('(%s) received: %s', msThreadId, data.toString())
const protocol = data.toString().slice(0, -1)
const result = Object.keys(handlersMap).filter((id) => id === protocol)
const key = result && result[0]

if (key) {
log('ack: %s', protocol)
log('(%s) send ack back of: %s', msThreadId, protocol)
writeEncoded(shake, data, cb)
handlersMap[key](new Connection(shake.rest(), rawConn))
} else {
log('received multicodec of not supported protocol: %s', protocol)
log('(%s) not supported protocol: %s',
msThreadId, protocol)
writeEncoded(shake, new Buffer('na\n'))
next()
}
Expand Down
22 changes: 16 additions & 6 deletions src/dialer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,39 @@ const log = debug('multistream:dialer')
const PROTOCOL_ID = require('./constants').PROTOCOL_ID
const agrmt = require('./agreement')

function getRandomId () {
return ((~~(Math.random() * 1e9)).toString(36))
}

module.exports = class Dialer {
constructor () {
this.conn = null
this.msThreadId = getRandomId()
}

// perform the multistream handshake
handle (rawConn, callback) {
log('handling connection')
log('(%s) dialer handle conn', this.msThreadId)
const ms = agrmt.select(PROTOCOL_ID, (err, conn) => {
if (err) {
return callback(err)
}
log('handshake success')
log('(%s) handshake success', this.msThreadId)

this.conn = new Connection(conn, rawConn)

callback()
})
pull(rawConn, ms, rawConn)
}, this.msThreadId)

pull(
rawConn,
ms,
rawConn
)
}

select (protocol, callback) {
log('dialer select %s', protocol)
log('(%s) dialer select %s', this.msThreadId, protocol)
if (!this.conn) {
return callback(new Error('multistream handshake has not finalized yet'))
}
Expand All @@ -43,7 +53,7 @@ module.exports = class Dialer {
return callback(err)
}
callback(null, new Connection(conn, this.conn))
})
}, this.msThreadId)

pull(
this.conn,
Expand Down
16 changes: 11 additions & 5 deletions src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const varint = require('varint')
const isFunction = require('lodash.isfunction')
const assert = require('assert')
const debug = require('debug')
const log = debug('libp2p:multistream:listener')
const log = debug('multistream:listener')
const Connection = require('interface-connection').Connection

const PROTOCOL_ID = require('./constants').PROTOCOL_ID
Expand All @@ -21,7 +21,8 @@ module.exports = class Listener {

// perform the multistream handshake
handle (rawConn, callback) {
log('handling connection')
const msThreadId = getRandomId()
log('(%s) listener handle conn', msThreadId)

const selectStream = agrmt.select(PROTOCOL_ID, (err, conn) => {
if (err) {
Expand All @@ -30,7 +31,8 @@ module.exports = class Listener {

const hsConn = new Connection(conn, rawConn)

const handlerSelector = agrmt.handlerSelector(hsConn, this.handlers)
const handlerSelector =
agrmt.handlerSelector(hsConn, this.handlers, msThreadId)

pull(
hsConn,
Expand All @@ -39,7 +41,7 @@ module.exports = class Listener {
)

callback()
})
}, msThreadId)

pull(
rawConn,
Expand All @@ -50,7 +52,7 @@ module.exports = class Listener {

// be ready for a given `protocol`
addHandler (protocol, handler) {
log('handling %s', protocol)
log('adding handler: %s', protocol)

assert(isFunction(handler), 'handler must be a function')

Expand Down Expand Up @@ -91,3 +93,7 @@ module.exports = class Listener {
)
}
}

function getRandomId () {
return ((~~(Math.random() * 1e9)).toString(36))
}

0 comments on commit dfbde59

Please sign in to comment.