<commit_before>var MessageStream = require('message-stream')
var net = require('net')
var argv = require('optimist').argv
var EventEmitter = require('events').EventEmitter

var seedPort = argv.port || 2503
var myIp = require('my-local-ip')() || 'localhost'
var seedHost = argv.host || myIp
var serverPort = argv.server || 2503
var id = myIp + ':' + serverPort
var name = argv.name || 'Anonymous'

function Chat(id, name) {
    var chat = new EventEmitter()
    chat.setMaxListeners(Infinity)

    var history = chat.history = []
    var clock = chat.clock = {}

    chat.send = function (text) {
        chat.emit('message', {
            text: text,
            name: name,
            id: id,
            time: Date.now()
        })
    }

    function seen(clock, message) {
        return clock[message.id] && clock[message.id] >= message.time
    }

    chat.createStream = function () {
        var stream = MessageStream(function (message) {
            if (message.clock) {
                history.forEach(function (e) {
                    if (!seen(message.clock, e)) stream.queue(e)
                })
            } else if (message.text) {
                chat.emit('message', message, stream)
            }
        })

        chat.on('message', function (message, source) {
            if (source !== stream) stream.queue(message)
        })

        stream.queue({ clock: chat.clock, id: id })

        return stream
    }

    chat.on('message', function (message) {
        if (!seen(clock, message)) {
            clock[message.id] = message.time

            chat.history.push(message)
            console.log(message.name + ' > ' + message.text)
        }
    })

    return chat
}

var chat = Chat(id, name)
process.stdin.on('data', function (text) {
    chat.send(String(text))
})

net.createServer(function (stream) {
    stream.pipe(chat.createStream()).pipe(stream)
}).listen(serverPort)

function randomPeer(clock, defaults) {
    var hosts = Object.keys(clock).map(function (peer) {
        var parts = peer.split(':')
        return { host: parts[0], port: parts[1] }
    }).filter(function (peer) {
        return serverPort !== peer.port || myIp !== peer.host
    })

    return hosts[~~(Math.random() * hosts.length)] || defaults
}

;(function connect () {
    var peer = randomPeer(chat.clock, { host: seedHost, port: seedPort })

    if (peer.host === myIp && peer.port === serverPort) {
        return setTimeout(connect, 1000)
    }

    var client = net.connect(peer.port, peer.host, function () {
        client.pipe(chat.createStream()).pipe(client)
        setTimeout(client.end.bind(client), 5000)
    })<commit_msg>store clock for instant connect<commit_after>var MessageStream = require('message-stream')
var net = require('net')
var argv = require('optimist').argv
var EventEmitter = require('events').EventEmitter

var seedPort = argv.port || 2503
var myIp = require('my-local-ip')() || 'localhost'
var seedHost = argv.host || myIp
var serverPort = argv.server || 2503
var id = myIp + ':' + serverPort
var name = argv.name || 'Anonymous'

function Chat(id, name) {
    var chat = new EventEmitter()
    chat.setMaxListeners(Infinity)

    var history = chat.history = []
    var clock = chat.clock = {}

    chat.send = function (text) {
        chat.emit('message', {
            text: text,
            name: name,
            id: id,
            time: Date.now()
        })
    }

    function seen(clock, message) {
        return clock[message.id] && clock[message.id] >= message.time
    }

    chat.createStream = function () {
        var stream = MessageStream(function (message) {
            if (message.clock) {
                clock[message.id] = clock[message.id] || 0
                history.forEach(function (e) {
                    if (!seen(message.clock, e)) stream.queue(e)
                })
            } else if (message.text) {
                chat.emit('message', message, stream)
            }
        })

        chat.on('message', function (message, source) {
            if (source !== stream) stream.queue(message)
        })

        stream.queue({ clock: chat.clock, id: id })

        return stream
    }

    chat.on('message', function (message) {
        if (!seen(clock, message)) {
            clock[message.id] = message.time

            chat.history.push(message)
            console.log(message.name + ' > ' + message.text)
        }
    })

    return chat
}

var chat = Chat(id, name)
process.stdin.on('data', function (text) {
    chat.send(String(text))
})

net.createServer(function (stream) {
    stream.pipe(chat.createStream()).pipe(stream)
}).listen(serverPort)

function randomPeer(clock, defaults) {
    var hosts = Object.keys(clock).map(function (peer) {
        var parts = peer.split(':')
        return { host: parts[0], port: parts[1] }
    }).filter(function (peer) {
        return serverPort !== peer.port || myIp !== peer.host
    })

    return hosts[~~(Math.random() * hosts.length)] || defaults
}

;(function connect () {
    var peer = randomPeer(chat.clock, { host: seedHost, port: seedPort })

    if (peer.host == myIp && peer.port == serverPort) {
        return setTimeout(connect, 1000)
    }

    var client = net.connect(peer.port, peer.host, function () {
        client.pipe(chat.createStream()).pipe(client)
        setTimeout(client.end.bind(client), 5000)
    })
{'fraction_non_alphanumeric': 0.14869129385557825, 'fraction_numerical': 0.006868386857248932, 'mean_word_length': 2.3549190535491906, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3119463', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1579, 'n_words': 367}