Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 5x 5x 5x 6x 36x 6x 6x 1x | const
Dispatcher = require('./dispatcher'),
Emitter = require('./emitter'),
Context = require('./context')
/**
* Responsible for attaching a dispacther to all data events on a socket
*/
class ConnectionObserver
{
/**
* @param {Logger} log
*/
static from(log)
{
const
dispatcher = Dispatcher.from(log),
emitter = Emitter.from(log),
createContext = Context.from,
observer = new ConnectionObserver(log, dispatcher, emitter, createContext)
return observer
}
/**
* @param {Logger} log
* @param {Dispatcher} dispatcher
* @param {Emitter} emitter
* @param {Context~from} createContext
*/
constructor(log, dispatcher, emitter, createContext)
{
this.log = log
this.dispatcher = dispatcher
this.emitter = emitter
this.createContext = createContext
}
/**
* @param {net.Socket} socket
*/
onConnection(socket)
{
this.logSocketEvents(socket, this.log)
const context = this.createContext(socket, this.emitter)
this.attachDataEventToDispatcherWithAContext(socket, this.dispatcher, context)
}
/**
* @protected
* @param {net.Socket} socket
* @param {Logger} log
*/
logSocketEvents(socket, log)
{
['close','connection','drain','end','lookup','timeout']
.forEach((event) => socket.on(event, () => log.info('connection:', event)))
socket.on('error', (...a) => log.info('connection:', 'error', ...a))
}
/**
* @protected
* @param {net.Socket} socket
* @param {Dispatcher} dispatcher
* @param {Context} context
*/
attachDataEventToDispatcherWithAContext(socket, dispatcher, context)
{
socket.on('data', dispatcher.dispatch.bind(dispatcher, context))
}
}
module.exports = ConnectionObserver
|