<commit_before>FS           = require('fs')
Path         = require('path')
EventEmitter = require('events').EventEmitter
express      = require('express')
sio          = require('socket.io')
eco          = require('eco')

class DebugServer extends EventEmitter
    constructor : (opts) ->
        {@browsers} = opts
        if !@browsers?
            throw new Error("Missing arguments")
        @server = express.createServer()
        @server.configure () =>
            @server.use(express.bodyParser())
            @server.use(express.cookieParser())
            @server.use(express.session({secret: '!secure'}))
            @server.set('views', Path.join(__dirname, '..', '..', 'views'))
            @server.set('view options', {layout : false})
        @server.register(".eco", eco)
        @server.get '/', (req, res) =>
            res.render 'debug.eco', browsers: @browsers.browsers
        @server.get '/:browser', (req, res) =>
            browser = @browsers.find(req.params.browser)
            if browser
                res.render 'debug_browser.eco', browser: browser
        @io = sio.listen(@server)
        @io.set('log level', 1)
        @io.sockets.on 'connection', @handleSocket

    listen : (port) ->
        @server.listen port, () =>
            @emit('listen')
            console.log("Debug server listening on port: #{port}")

    close : () ->
        @server.once 'close', () =>
            @emit('close')
        @server.close()

    handleSocket : (socket) =>
        socket.on 'attach', (browserID) =>
            browser = @browsers.find(browserID)
            if browser
                browser.on 'log', (msg) ->
                    socket.emit('browserLog', msg)
                socket.on 'evaluate', (cmd) =>
                    try
                        rv = browser.window.run(cmd, 'remote-debug')
                        socket.emit('evalRV', rv)
                    catch e
                        socket.emit('evalRV', e.stack)
                FS.readFile browser.logPath, 'utf8', (err, data) ->
                    if !err
                        socket.emit('browserLog', data)


module.exports = DebugServer<commit_msg>DebugServer now cleans up disconnected clients.<commit_after>FS           = require('fs')
Path         = require('path')
EventEmitter = require('events').EventEmitter
express      = require('express')
sio          = require('socket.io')
eco          = require('eco')

class DebugServer extends EventEmitter
    constructor : (opts) ->
        {@browsers} = opts
        if !@browsers?
            throw new Error("Missing arguments")
        @server = express.createServer()
        @server.configure () =>
            @server.use(express.bodyParser())
            @server.use(express.cookieParser())
            @server.use(express.session({secret: '!secure'}))
            @server.set('views', Path.join(__dirname, '..', '..', 'views'))
            @server.set('view options', {layout : false})
        @server.register(".eco", eco)
        @server.get '/', (req, res) =>
            res.render 'debug.eco', browsers: @browsers.browsers
        @server.get '/:browser', (req, res) =>
            browser = @browsers.find(req.params.browser)
            if browser
                res.render 'debug_browser.eco', browser: browser
        @io = sio.listen(@server)
        @io.set('log level', 1)
        @io.sockets.on 'connection', @handleSocket

    listen : (port) ->
        @server.listen port, () =>
            @emit('listen')
            console.log("Debug server listening on port: #{port}")

    close : () ->
        @server.once 'close', () =>
            @emit('close')
        @server.close()

    handleSocket : (socket) =>
        socket.on 'attach', (browserID) =>
            browser = @browsers.find(browserID)
            if browser
                # Send the existing log file contents.
                FS.readFile browser.logPath, 'utf8', (err, data) ->
                    if !err
                        socket.emit('browserLog', data)

                logListener = (msg) ->
                    socket.emit('browserLog', msg)
                browser.on 'log', logListener

                socket.on 'evaluate', (cmd) =>
                    try
                        rv = browser.window.run(cmd, 'remote-debug')
                        socket.emit('evalRV', rv)
                    catch e
                        socket.emit('evalRV', e.stack)

                socket.on 'disconnect', () ->
                    browser.removeListener('log', logListener)


module.exports = DebugServer
{'fraction_non_alphanumeric': 0.1458196900240122, 'fraction_numerical': 0.0008731717965509714, 'mean_word_length': 1.6921269095182139, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4249530', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1220, 'n_words': 262}