<commit_before>'use strict';

//Menu service used for managing  menus
angular.module('core').service('Peer', [
    '$timeout', '$http', '$q',
    function($timeout, $http, $q) {
        var that = this;
        that.name = '';
        that.retry = 1000;
        that.keepAlive = 3 * 60 * 1000;
        that.autoRefresh = 60 * 1000;
        that.notifications = $q.defer();

        that.peers = [];

        function status(what, params) {
            that.status = what;
            console.log('Peer Status: ' + what,params);
            that.notifications.notify(what);
        }

        function kick() {
            if (that.status !== 'connected')
                return;
            $http.put('/connections/' + that.connection._id)
                .then(function() {
                    $timeout(kick, that.keepAlive);
                }, function(err) {
                    console.error('kick', err); // FIXME: handle this
                });
        }

        function initPeer(p) {
            p.notifications = $q.defer();
            p.notify = function() {
                return p.notifications.promise
            };
            p.conn.on('data', function(d) {
                p.notifications.notify(d);
            });
            p.send = function(m) {
                p.conn.send(m);
            };
            p.conn.on('close', function() {
                that.peers.splice(_.findIndex(that.peers, {
                    _id: p._id
                }), 1);
            });
            p.conn.on('error', function(err) {
                console.log('peer error', err);
                $http.delete('/connections/'+that.connection._id,{id:p.pid});
                // FIXME handle this
            });
        }

        function trackPeers() {
            if (that.status !== 'connected')
                return;
            $http.get('/connections')
                .then(function(resp) {
                    var connections = resp.data;
                    that.peers
                        .filter(function(p) {
                            return p._id !== that.connection._id; // Eliminate my own entry
                        })
                        .map(function(p) {
                            return _.findIndex(that.peers, {  // Eliminate peers that I'm already connected to
                                _id: p._id
                            });
                        })
                        .reverse()
                        .forEach(function(i) {
                            if (i >= 0)
                                connections.splice(i, 1);
                        });
                    connections.forEach(function(p) {
                        p.conn = that.peer.connect(p.pid);
                        initPeer(p);
                        p.conn.on('open', function() {
                            console.log('connection opened with ', p);
                            that.peers.push(p);
                            console.log(that.connection.name);
                            p.conn.send(that.connection);
                        });
                    });
                    $timeout(trackPeers, that.autoRefresh);
                });
        }

        function connect() {
            status('connecting');
            //that.peer = new Peer('123456', {host: location.host.split(':')[0], port: location.host.split(':')[1]});
            that.peer = new Peer({
                key: '<KEY>' // per application
            });
            that.peer.on('open', function(id) {
                status('connected', id);
                that.id = id;
                that.retry = 1000;
                $http.post('/connections', {
                        pid: id,
                        name: that.name
                    })
                    .then(function(resp) {
                        that.connection = resp.data;
                        that.name = that.connection.name;
                        status(that.status, that.name);
                        $timeout(kick, that.keepAlive);
                        $timeout(trackPeers, that.retry);
                    }, function(err) {
                        console.error('connect/post', err); // FIXME: handle this
                    });
            });
            that.peer.on('disconnected', function() {
                status('disconnected', that.retry);
                $timeout(function() {
                    that.retry += 1000;
                    status('connecting');
                    if (that.peer.destroyed)
                        connect();
                    else
                        that.peer.reconnect();
                }, that.retry);
            });
            that.peer.on('error', function(err) {
                //status('error', err);
                $http.delete('/connections/'+that.connection._id,{pid:that.id}).then(function(res){console.log(res);});
                //console.log('Error', err);
                // FIXME: handle this
            });
            that.peer.on('connection', function(conn) {
                conn.on('data', function(m) {
                    var ndx = _.findIndex(that.peers, {
                        _id: m._id
                    });
                    if (ndx >= 0) {<commit_msg>Revert "Some Fixes"<commit_after>'use strict';

//Menu service used for managing  menus
angular.module('core').service('Peer', [
    '$timeout', '$http', '$q',
    function($timeout, $http, $q) {
        var that = this;
        that.name = '';
        that.retry = 1000;
        that.keepAlive = 3 * 60 * 1000;
        that.autoRefresh = 60 * 1000;
        that.notifications = $q.defer();

        that.peers = [];

        function status(what, params) {
            that.status = what;
            console.log('Peer Status: ' + what, params);
            that.notifications.notify(what);
        }

        function kick() {
            if (that.status !== 'connected')
                return;
            $http.put('/connections/' + that.connection._id)
                .then(function() {
                    $timeout(kick, that.keepAlive);
                }, function(err) {
                    console.error('kick', err); // FIXME: handle this
                });
        }

        function initPeer(p) {
            p.notifications = $q.defer();
            p.notify = function() {
                return p.notifications.promise
            };
            p.conn.on('data', function(d) {
                p.notifications.notify(d);
            });
            p.send = function(m) {
                p.conn.send(m);
            };
            p.conn.on('close', function() {
                that.peers.splice(_.findIndex(that.peers, {
                    _id: p._id
                }), 1);
            });
            p.conn.on('error', function(err) {
                console.log('peer error', err);
                // FIXME handle this
            });
        }

        function trackPeers() {
            if (that.status !== 'connected')
                return;
            $http.get('/connections')
                .then(function(resp) {
                    var connections = resp.data;
                    that.peers
                        .filter(function(p) {
                            return p._id !== that.connection._id; // Eliminate my own entry
                        })
                        .map(function(p) {
                            return _.findIndex(that.peers, {  // Eliminate peers that I'm already connected to
                                _id: p._id
                            });
                        })
                        .reverse()
                        .forEach(function(i) {
                            if (i >= 0)
                                connections.splice(i, 1);
                        });
                    connections.forEach(function(p) {
                        p.conn = that.peer.connect(p.pid);
                        initPeer(p);
                        p.conn.on('open', function() {
                            console.log('connection opened with ', p);
                            that.peers.push(p);
                            p.conn.send(that.connection);
                        });
                    });
                    $timeout(trackPeers, that.autoRefresh);
                });
        }

        function connect() {
            status('connecting');
            //that.peer = new Peer('123456', {host: location.host.split(':')[0], port: location.host.split(':')[1]});
            that.peer = new Peer({
                key: '<KEY>' // per application
            });
            that.peer.on('open', function(id) {
                status('connected', id);
                that.id = id;
                that.retry = 1000;
                $http.post('/connections', {
                        pid: id,
                        name: that.name
                    })
                    .then(function(resp) {
                        that.connection = resp.data;
                        that.name = that.connection.name;
                        status(that.status, that.name);
                        $timeout(kick, that.keepAlive);
                        $timeout(trackPeers, that.retry);
                    }, function(err) {
                        console.error('connect/post', err); // FIXME: handle this
                    });
            });
            that.peer.on('disconnected', function() {
                status('disconnected', that.retry);
                $timeout(function() {
                    that.retry += 1000;
                    status('connecting');
                    if (that.peer.destroyed)
                        connect();
                    else
                        that.peer.reconnect();
                }, that.retry);
            });
            that.peer.on('error', function(err) {
                //status('error', err);
                console.log('Error', err);
                // FIXME: handle this
            });
            that.peer.on('connection', function(conn) {
                conn.on('data', function(m) {
                    var ndx = _.findIndex(that.peers, {
                        _id: m._id
                    });
                    if (ndx >= 0) {
{'fraction_non_alphanumeric': 0.13032898578937122, 'fraction_numerical': 0.007202647459606774, 'mean_word_length': 0.9912790697674418, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '930030', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2440, 'n_words': 446}