diff --git a/lib/server/socket.js b/lib/server/socket.js index f72e116c..855c708a 100644 --- a/lib/server/socket.js +++ b/lib/server/socket.js @@ -119,60 +119,17 @@ */ function onMessage(pConnNum, pSocket) { return function(pCommand) { - var lMsg, lWinCommand, lExec_func, lDir, firstChar, + var lMsg, lWinCommand, lExec_func, firstChar, connName, - lHome, lError, lRet, lExecSymbols, isContain; + lError, lRet, lExecSymbols, isContain; connName = '#' + pConnNum + ': '; Util.log(connName + pCommand); - if (Util.isContainStrAtBegin(pCommand, CLOUDCMD)) { - pCommand = Util.removeStr(pCommand, CLOUDCMD); - - if( Util.isContainStrAtBegin(pCommand, ' ') ) { - pCommand = Util.removeStr(pCommand, ' '); - - if( Util.isContainStrAtBegin(pCommand, 'update') && update ) { - update.get(); - lRet = true; - } - - if( Util.strCmp(pCommand, 'exit') ) - if(WIN32) - pCommand = 'taskkill -f /PID ' + process.pid; - else - pCommand = 'kill -9 ' + process.pid; - } - else { - jsonSend(pSocket, { - stdout : CLOUDCMD + ' exit \n' + - CLOUDCMD + ' update \n', - stderr : null - }); - - lRet = true; - } - } else if( Util.isContainStrAtBegin(pCommand, 'cd ') ) { - lDir = Util.removeStr(pCommand, 'cd '); - lHome = process.env.HOME; - - if ( Util.isContainStr(lDir, '~') ) - lDir = Util.replaceStr(lDir, '~', lHome); - - lError = Util.tryCatchLog(function() { - process.chdir(lDir); - }); - - if (!lError) - lRet = true; - else { - lError = Util.stringifyJSON(lError); - - jsonSend(pSocket, { - stderr: lError - }); - } - } + if (Util.isContainStrAtBegin(pCommand, CLOUDCMD)) + lRet = onCloudCmd(pCommand, pSocket); + else if( Util.isContainStrAtBegin(pCommand, 'cd ') ) + lRet = onCD(pCommand, pSocket); if (!lRet) { /* if we on windows and command is build in @@ -272,6 +229,63 @@ cmd.on('error', Util.retFalse); } + function onCloudCmd(pCommand, pSocket) { + var lRet; + + pCommand = Util.removeStr(pCommand, CLOUDCMD); + + if( Util.isContainStrAtBegin(pCommand, ' ') ) { + pCommand = Util.removeStr(pCommand, ' '); + + if( Util.isContainStrAtBegin(pCommand, 'update') && update ) { + update.get(); + lRet = true; + } + + if( Util.strCmp(pCommand, 'exit') ) + if(WIN32) + pCommand = 'taskkill -f /PID ' + process.pid; + else + pCommand = 'kill -9 ' + process.pid; + } + else { + jsonSend(pSocket, { + stdout : CLOUDCMD + ' exit \n' + + CLOUDCMD + ' update \n', + stderr : null + }); + + lRet = true; + } + + return lRet; + } + + function onCD(pCommand, pSocket) { + var lRet, lDir, lHome, lError; + + lDir = Util.removeStr(pCommand, 'cd '); + lHome = process.env.HOME; + + if ( Util.isContainStr(lDir, '~') ) + lDir = Util.replaceStr(lDir, '~', lHome); + + lError = Util.tryCatchLog(function() { + process.chdir(lDir); + }); + + if (!lError) + lRet = true; + else { + lError = Util.stringifyJSON(lError); + + jsonSend(pSocket, { + stderr: lError + }); + } + } + + function log(pConnNum, pStr, pType) { var lRet, lType = ' ';