<commit_before>/* module make possible connectoin thru socket.io on a server */

(function() {
    'use strict';
    
    var main            = global.cloudcmd.main,
        DIR             = main.DIR,
        SRVDIR          = main.SRVDIR,
        
        io              = main.require('socket.io'),
        update          = main.srvrequire('update'),
        
        exec            = main.child_process.exec,
        spawn           = main.child_process.spawn,
        
        Util            = main.util,
        path            = main.path,
        mainpackage     = main.mainpackage,
        equalPart       = Util.isContainStrAtBegin,
        CLOUDCMD        = mainpackage.name,
        ClientFuncs     = [],
        ClientDirs      = [],
        Clients         = [],
        INFO_LOG_LEVEL  = 2,
        ENV             = process.env,
        WIN32           = main.WIN32,
        HELP            = {
            stdout  : CLOUDCMD + ' exit     \n' +
                      CLOUDCMD + ' update   \n',
        },
        
        /* windows commands thet require 
         * unicode charset on locales 
         * different then English
         */
        Win32Commands   = ['ASSOC', 'AT', 'ATTRIB', 'BREAK', 'CACLS', 'CALL',
                            'CD', 'CHCP', 'CHDIR', 'CHKDSK', 'CHKNTFS', 'CLS',
                            'CMD', 'COLOR', 'COMP', 'COMPACT', 'CONVERT', 'COPY',
                            'DATE', 'DEL', 'DIR', 'DISKCOMP', 'DISKCOPY', 'DOSKEY',
                            'ECHO', 'ENDLOCAL', 'ERASE', 'EXIT', 'FC', 'FIND',
                            'FINDSTR', 'FOR', 'FORMAT', 'FTYPE', 'GOTO', 'GRAFTABL',
                            'HELP', 'IF', 'LABEL', 'MD', 'MKDIR', 'MODE', 'MORE',
                            'MOVE', 'PATH', 'PAUSE', 'POPD', 'PRINT', 'PROMPT',
                            'PUSHD', 'RD', 'RECOVER', 'REM', 'REN', 'RENAME',
                            'REPLACE', 'RMDIR', 'SET', 'SETLOCAL', 'SHIFT', 'SORT',
                            'START', 'SUBST', 'TIME', 'TITLE', 'TREE', 'TYPE',
                            'VER', 'VERIFY', 'VOL', 'XCOPY'];
    
    /**
     * function listen on servers port
     * @pServer {Object} started server object
     */
    exports.listen = function(pServer) {
        var lRet, lConnNum, lMsg;
        
        if (io) {
            io          = io.listen(pServer);
            lConnNum    = 0;
            
            io.set('log level', INFO_LOG_LEVEL);
            
            /* 
             * on Win7 application is crashing, 
             * when options below is used.
             * 
             * https://github.com/LearnBoost/socket.io/issues/1314
             *
             */
            if (!WIN32) {
                io.enable('browser client minification');
                io.enable('browser client gzip');
                io.enable('browser client etag');
            }
            
            io.set('transports', [
              'websocket',
              'htmlfile',
              'xhr-polling',
              'jsonp-polling'
            ]);
            
            lRet = io.sockets.on('connection', function (socket) {
                var onDisconect, onConnect;
                
                ++lConnNum;
                
                if (!Clients[lConnNum]) {
                    lMsg    = log(lConnNum, 'connected\n');
                    
                    jsonSend(socket, {
                        stdout : lMsg
                    });
                    
                    Clients[lConnNum]   = true;
                    onConnect           = onMessage(lConnNum, socket);
                    onDisconect         = Util.retFunc(disconnect, lConnNum);
                    
                    socket.on('message', onConnect);
                    socket.on('disconnect', function() {
                        socket.removeListener('message', onConnect);
                        socket.removeListener('disconnect', onDisconect);
                        
                        socket = null;
                    });
                } else {
                    lMsg = log(lConnNum, ' in use. Reconnecting...');
                    
                    jsonSend(socket, {
                        stdout: lMsg
                    });
                    
                    socket.disconnect();
                }
                
            });
        }
        
        return lRet;
    };
    
    function disconnect(connNum) {
        OnMessageFuncs  [connNum]  =
        ClientFuncs     [connNum]  = null;
        
        log(connNum, 'disconnected');
    }
    <commit_msg>refactor(socket)add onConnection<commit_after>/* module make possible connectoin thru socket.io on a server */

(function() {
    'use strict';
    
    var main            = global.cloudcmd.main,
        DIR             = main.DIR,
        SRVDIR          = main.SRVDIR,
        
        io              = main.require('socket.io'),
        update          = main.srvrequire('update'),
        
        exec            = main.child_process.exec,
        spawn           = main.child_process.spawn,
        
        Util            = main.util,
        path            = main.path,
        mainpackage     = main.mainpackage,
        equalPart       = Util.isContainStrAtBegin,
        CLOUDCMD        = mainpackage.name,
        ClientFuncs     = [],
        ClientDirs      = [],
        Clients         = [],
        INFO_LOG_LEVEL  = 2,
        ENV             = process.env,
        WIN32           = main.WIN32,
        ConNum          = 0,
        HELP            = {
            stdout  : CLOUDCMD + ' exit     \n' +
                      CLOUDCMD + ' update   \n',
        },
        
        /* windows commands thet require 
         * unicode charset on locales 
         * different then English
         */
        Win32Commands   = ['ASSOC', 'AT', 'ATTRIB', 'BREAK', 'CACLS', 'CALL',
                            'CD', 'CHCP', 'CHDIR', 'CHKDSK', 'CHKNTFS', 'CLS',
                            'CMD', 'COLOR', 'COMP', 'COMPACT', 'CONVERT', 'COPY',
                            'DATE', 'DEL', 'DIR', 'DISKCOMP', 'DISKCOPY', 'DOSKEY',
                            'ECHO', 'ENDLOCAL', 'ERASE', 'EXIT', 'FC', 'FIND',
                            'FINDSTR', 'FOR', 'FORMAT', 'FTYPE', 'GOTO', 'GRAFTABL',
                            'HELP', 'IF', 'LABEL', 'MD', 'MKDIR', 'MODE', 'MORE',
                            'MOVE', 'PATH', 'PAUSE', 'POPD', 'PRINT', 'PROMPT',
                            'PUSHD', 'RD', 'RECOVER', 'REM', 'REN', 'RENAME',
                            'REPLACE', 'RMDIR', 'SET', 'SETLOCAL', 'SHIFT', 'SORT',
                            'START', 'SUBST', 'TIME', 'TITLE', 'TREE', 'TYPE',
                            'VER', 'VERIFY', 'VOL', 'XCOPY'];
    
    /**
     * function listen on servers port
     * @pServer {Object} started server object
     */
    exports.listen = function(pServer) {
        var ret;
        
        if (io) {
            io          = io.listen(pServer);
            
            io.set('log level', INFO_LOG_LEVEL);
            
            /* 
             * on Win7 application is crashing, 
             * when options below is used.
             * 
             * https://github.com/LearnBoost/socket.io/issues/1314
             *
             */
            if (!WIN32) {
                io.enable('browser client minification');
                io.enable('browser client gzip');
                io.enable('browser client etag');
            }
            
            io.set('transports', [
              'websocket',
              'htmlfile',
              'xhr-polling',
              'jsonp-polling'
            ]);
            
            ret = io.sockets.on('connection', onConnection);
        }
        
        return lRet;
    };
    
    function onConnection(socket) {
        var msg, onDisconect, onConnect;
        
        ++ConNum;
        
        if (!Clients[ConNum]) {
            msg = log(ConNum, 'connected\n');
            
            jsonSend(socket, {
                stdout : msg
            });
            
            Clients[ConNum]   = true;
            onConnect           = onMessage(ConNum, socket);
            onDisconect         = Util.retFunc(disconnect, ConNum);
            
            socket.on('message', onConnect);
            socket.on('disconnect', function() {
                socket.removeListener('message', onConnect);
                socket.removeListener('disconnect', onDisconect);
            });
        } else {
            msg = log(ConNum, ' in use. Reconnecting...');
            
            jsonSend(socket, {
                stdout: msg
            });
            
            socket.disconnect();
        }
    }
    
    function disconnect(connNum) {
        OnMessageFuncs  [connNum]  =
        ClientFuncs     [connNum]  = null;
        
        log(connNum, 'disconnected');
    }
    
{'fraction_non_alphanumeric': 0.1237701252236136, 'fraction_numerical': 0.0033542039355992843, 'mean_word_length': 1.056794665440331, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '73288', 'n_tokens_mistral': 2498, 'n_tokens_neox': 2390, 'n_words': 489}