<commit_before>$.Cow.Websocket.prototype = {
    
    _onOpen: function() {
        
        console.log('connected');
    },
    _onMessage: function(message) {
        var core = this.obj.core;
        console.debug('message: '+message.data);
        var data = JSON.parse(message.data);
        var uid = data.uid;
        var UID = core.UID; 
        var action = data.action;        
        var payload = data.payload;    
        var target = data.target;
        switch (action) {
        //Messages from Server
            //webscocket confirms connection by returning a CID
            case 'connected':
                this.obj._onConnect(payload)
            break;
            //the server noticed a peer disconnecting and send its connection-id to the pool
            case 'peerGone':
                this.obj._onPeerGone(payload);
            break;            
        //Messages from Peers: targeted messages
            //the client has joined and receives the status from peers: connection-id, uid, extent
            case 'informPeer':
                if(target == this.obj.core.UID) {
                    this.obj._onInformPeer(payload,uid);
                }
            break;
            //the alpha peer sends a sync message with new features and a feature request
            case 'syncPeer':
                if(target == this.obj.core.UID) {
                    this.obj._onSyncPeer(payload,uid);
                }
            break;
            //requested feats are returning from peer
            case 'requestedFeats':
                if(uid != UID) {
                    this.obj._onRequestedFeats(payload,uid);
                }
            break;
            
        //Messages from Peers: broadcasted messages
            //a peer is gone and everybody has a new connection-id, recieve a connectionID with UID
            case 'updatePeers':
                if(uid != UID) {            
                    this.obj._onUpdatePeers(payload,uid);
                }
            break;
            //a new peer just joined, recieve its status: connection-id, uid, extent
            case 'newPeer':
                if(uid != this.obj.core.UID) {
                    this.obj._onNewPeer(payload,uid);
                }
            break;
            //a new peer just sent it's fidlist
            case 'newPeerFidList':
                if(uid != this.obj.core.UID) {
                    this.obj._onNewPeerFidList(payload,uid);
                }
            break;
            //a peer has changed, update the peer
            case 'peerUpdated':
                if(uid != UID) {
                    this.obj._onPeerUpdated(payload,uid);
                }
            break;
            
            
            //a new object was drawn or updated by a peer
            case 'newFeature':
                if(uid != UID) {
                    var item = JSON.parse(payload);
                    if (self.core.options.activeHerd == item.feature.properties.store)
                        core.featurestore().updateItem(item);
                }
            break;
            case 'updateFeature':
                if(uid != UID) {
                    var item = JSON.parse(payload);
                    if (self.core.options.activeHerd == item.feature.properties.store)
                        core.featurestore().updateItem(item);
                }
            break;
        }
    },
    _onClose: function(event) {
        var code = event.code;
        var self = this;
        var reason = event.reason;
        var wasClean = event.wasClean;
        console.log('disconnected');
        this.close();
        this.obj.core.trigger('ws-disconnected');    
        //TODO: doe iets slimmers, hij hangt nu af van de global variable 'core'....
        var restart = function(){
            try{
                core.ws.closews();
            }
            catch(err){
                console.warn(err);
            }
            core.ws.openws();
        }
        setTimeout(restart,5000);
    },
    _onError: function(event, error) {
        //alert(error);
        console.warn('error: ' + event);
    },
    sendData: function(data, action, target){
        //TODO: check if data is an object
        var message = {};        
        message.uid = this.core.UID;
        message.target = target;
        message.action = action;
        message.payload = data;
        if (this.ws && this.ws.readyState == 1){
            this.ws.send(JSON.stringify(message));
        }
    },
    _onConnect: function(payload) {
        var self = this;
        console.log('onConnect');
       
        var name = $('#'+this.core.options.namefield).val();
       
        var options = {};
       
        options.uid = this.core.UID;
        options.cid = payload.cid;        
       options.family = 'alpha'; //used to check if client is able to act as alpha peer
        var me = this.core.peers(options);
        console.log('nr peers: '+this.core.peers().length);
        this.core.trigger('ws-connected');        
        this.sendData(options,'newPeer');
        //triggers _onNewPeer()
        var herd = core.getHerdById(self.core.options.activeHerd);
        me.owner({"name":name});
        me.herd(herd);
        
        var sendFidList = function(){
            var store = core.featurestore();
            var fids = store.getIdList();
            var message = {};
            message.fids = fids;
            message.storename = self.core.options.activeHerd;
            self.core.websocket().sendData(message, "newPeerFidList");
            
        }
        //TODO TT: at the moment we only check for 1 store to be loaded
        if (this.core.featurestore().loaded == true)
            sendFidList();
        else
            setTimeout(sendFidList, 2000);
            
        
    },
    
    //You just joined and you get from each peer the relevant info
    //Add it to your peerList
    _onInformPeer: function(payload,uid) {        
        console.log('Got peerinfo from: '+uid);        
        if(payload.options.uid !== undefined && payload.options.cid !== undefined) {
            var me = this.core.peers(payload.options);
            me.view({"extent":payload.extent});
            me.position({"point":payload.position});
            me.owner({"name":payload.extent});
            me.herd({"herd":payload.herd});
            this.core.trigger('ws-peerInfo');    
        }
        else console.log('badpeer '+uid);
    },
    //A new peer has joined, send it your info, compare its features and add it to your
    //peerList
    _onNewPeer: function(payload,uid) {
        console.log('This peer just connected: '+uid);
        if(payload.uid !== undefined && payload.cid !== undefined) {
            console.log('adding peer');
            this.core.peers(payload);
            var message = {};
            message.options = this.core.me().options;
            message.view = this.core.me().view().extent;
            message.owner = this.core.me().owner();
            message.position = this.core.me().position().point;
            message.herd = this.core.me().herd();
            this.sendData(message,'informPeer',uid);
            this.core.trigger('ws-newPeer');
        }
        else console.warn('badpeer '+uid);
    },
    _amIAlpha: function(id,herd){ //find out wether I am alpha
        if (this.core.me().options.herd == herd){ //I need to be part of the herd<commit_msg>fixed the herd in informPeer<commit_after>$.Cow.Websocket.prototype = {
    
    _onOpen: function() {
        
        console.log('connected');
    },
    _onMessage: function(message) {
        var core = this.obj.core;
        console.debug('message: '+message.data);
        var data = JSON.parse(message.data);
        var uid = data.uid;
        var UID = core.UID; 
        var action = data.action;        
        var payload = data.payload;    
        var target = data.target;
        switch (action) {
        //Messages from Server
            //webscocket confirms connection by returning a CID
            case 'connected':
                this.obj._onConnect(payload)
            break;
            //the server noticed a peer disconnecting and send its connection-id to the pool
            case 'peerGone':
                this.obj._onPeerGone(payload);
            break;            
        //Messages from Peers: targeted messages
            //the client has joined and receives the status from peers: connection-id, uid, extent
            case 'informPeer':
                if(target == this.obj.core.UID) {
                    this.obj._onInformPeer(payload,uid);
                }
            break;
            //the alpha peer sends a sync message with new features and a feature request
            case 'syncPeer':
                if(target == this.obj.core.UID) {
                    this.obj._onSyncPeer(payload,uid);
                }
            break;
            //requested feats are returning from peer
            case 'requestedFeats':
                if(uid != UID) {
                    this.obj._onRequestedFeats(payload,uid);
                }
            break;
            
        //Messages from Peers: broadcasted messages
            //a peer is gone and everybody has a new connection-id, recieve a connectionID with UID
            case 'updatePeers':
                if(uid != UID) {            
                    this.obj._onUpdatePeers(payload,uid);
                }
            break;
            //a new peer just joined, recieve its status: connection-id, uid, extent
            case 'newPeer':
                if(uid != this.obj.core.UID) {
                    this.obj._onNewPeer(payload,uid);
                }
            break;
            //a new peer just sent it's fidlist
            case 'newPeerFidList':
                if(uid != this.obj.core.UID) {
                    this.obj._onNewPeerFidList(payload,uid);
                }
            break;
            //a peer has changed, update the peer
            case 'peerUpdated':
                if(uid != UID) {
                    this.obj._onPeerUpdated(payload,uid);
                }
            break;
            
            
            //a new object was drawn or updated by a peer
            case 'newFeature':
                if(uid != UID) {
                    var item = JSON.parse(payload);
                    if (self.core.options.activeHerd == item.feature.properties.store)
                        core.featurestore().updateItem(item);
                }
            break;
            case 'updateFeature':
                if(uid != UID) {
                    var item = JSON.parse(payload);
                    if (self.core.options.activeHerd == item.feature.properties.store)
                        core.featurestore().updateItem(item);
                }
            break;
        }
    },
    _onClose: function(event) {
        var code = event.code;
        var self = this;
        var reason = event.reason;
        var wasClean = event.wasClean;
        console.log('disconnected');
        this.close();
        this.obj.core.trigger('ws-disconnected');    
        //TODO: doe iets slimmers, hij hangt nu af van de global variable 'core'....
        var restart = function(){
            try{
                core.ws.closews();
            }
            catch(err){
                console.warn(err);
            }
            core.ws.openws();
        }
        setTimeout(restart,5000);
    },
    _onError: function(event, error) {
        //alert(error);
        console.warn('error: ' + event);
    },
    sendData: function(data, action, target){
        //TODO: check if data is an object
        var message = {};        
        message.uid = this.core.UID;
        message.target = target;
        message.action = action;
        message.payload = data;
        if (this.ws && this.ws.readyState == 1){
            this.ws.send(JSON.stringify(message));
        }
    },
    _onConnect: function(payload) {
        var self = this;
        console.log('onConnect');
       
        var name = $('#'+this.core.options.namefield).val();
       
        var options = {};
       
        options.uid = this.core.UID;
        options.cid = payload.cid;        
       options.family = 'alpha'; //used to check if client is able to act as alpha peer
        var me = this.core.peers(options);
        console.log('nr peers: '+this.core.peers().length);
        this.core.trigger('ws-connected');        
        this.sendData(options,'newPeer');
        //triggers _onNewPeer()
        var herd = core.getHerdById(self.core.options.activeHerd);
        me.owner({"name":name});
        me.herd(herd);
        
        var sendFidList = function(){
            var store = core.featurestore();
            var fids = store.getIdList();
            var message = {};
            message.fids = fids;
            message.storename = self.core.options.activeHerd;
            self.core.websocket().sendData(message, "newPeerFidList");
            
        }
        //TODO TT: at the moment we only check for 1 store to be loaded
        if (this.core.featurestore().loaded == true)
            sendFidList();
        else
            setTimeout(sendFidList, 2000);
            
        
    },
    
    //You just joined and you get from each peer the relevant info
    //Add it to your peerList
    _onInformPeer: function(payload,uid) {        
        console.log('Got peerinfo from: '+uid);        
        if(payload.options.uid !== undefined && payload.options.cid !== undefined) {
            var me = this.core.peers(payload.options);
            me.view({"extent":payload.extent});
            me.position({"point":payload.position});
            me.owner({"name":payload.extent});
            me.herd(payload.herd);
            this.core.trigger('ws-peerInfo');    
        }
        else console.log('badpeer '+uid);
    },
    //A new peer has joined, send it your info, compare its features and add it to your
    //peerList
    _onNewPeer: function(payload,uid) {
        console.log('This peer just connected: '+uid);
        if(payload.uid !== undefined && payload.cid !== undefined) {
            console.log('adding peer');
            this.core.peers(payload);
            var message = {};
            message.options = this.core.me().options;
            message.view = this.core.me().view().extent;
            message.owner = this.core.me().owner();
            message.position = this.core.me().position().point;
            message.herd = this.core.me().herd();
            this.sendData(message,'informPeer',uid);
            this.core.trigger('ws-newPeer');
        }
        else console.warn('badpeer '+uid);
    },
    _amIAlpha: function(id,herd){ //find out wether I am alpha
        if (this.core.me().options.herd == herd){ //I need to be part of the herd
{'fraction_non_alphanumeric': 0.11523489932885907, 'fraction_numerical': 0.0013422818791946308, 'mean_word_length': 1.475660408705765, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 3, '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': '2931214', 'n_tokens_mistral': 4047, 'n_tokens_neox': 3937, 'n_words': 963}