예제 #1
0
void ReverseHashDServer::processTextMessage(QString message) {
    QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
    std::cout << QDateTime::currentDateTimeUtc().toString().toStdString() << " [WS] <<< " << message.toStdString() << "\n";


	QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8());
	QJsonObject jsonData = doc.object();
	
	int id = 0;
	if(jsonData.contains("id")){
		id = jsonData["id"].toInt();
	}
	
	if(!jsonData.contains("cmd")){
		this->sendMessageError(pClient, "cmd", id, Error(400, "Invalid command format (Not found cmd)"));
		return;
	}

	QString cmd = jsonData["cmd"].toString();
		
	if(m_mapCmdHandlers.contains(cmd)){
		ICmdHandler *pCmdHandler = m_mapCmdHandlers[cmd];
		pCmdHandler->handle(pClient, this, jsonData);	
	}else{
		qDebug() << "Unknown command: " << cmd;
		this->sendMessageError(pClient, cmd, id, Error(404, "Unknown command"));
	}
	
}
예제 #2
0
int TelnetCmdManager::handleCmd(const unsigned theStart, const unsigned theEnd)
{
    CmdArgsList argsList;
    //trim
    unsigned start = theStart;
    unsigned end = theEnd;
    while (' ' == cmdBufferM[start] ||
           '\t' == cmdBufferM[start] ||
           '\r' == cmdBufferM[start] ||
           '\n' == cmdBufferM[start])
    {
        start++;
        if (start >= theEnd)
        {
            break;
        }
    }
    while (end > start  
	      && ( ' ' == cmdBufferM[end - 1] ||
		   '\t' == cmdBufferM[end - 1] ||
		   '\r' == cmdBufferM[end - 1] ||
		   '\n' == cmdBufferM[end - 1]))
    {
        end--;
        if (end <= start)
        {
            break;
        }
    }

    if (start < end)    
    {
        LOG_DEBUG("handle telnet cmd:" << std::string(cmdBufferM + start, cmdBufferM + end));
    }

    unsigned argsStart = start;
    unsigned i = start;
    for (; i  < end; i++)
    {
        if (' ' == cmdBufferM[i] ||
            '\t' == cmdBufferM[i])
        {
            if (argsStart < i)
                argsList.push_back(std::string(cmdBufferM + argsStart, cmdBufferM + i));
            argsStart = i + 1;
        }

    }
    if (argsStart < i)
    {
        argsList.push_back(std::string(cmdBufferM + argsStart, cmdBufferM + i));
    }
    
	if (subCmdStackM.empty())
	{
		if (argsList.empty())
		{
			sendPrompt();
			return 0;
		}

		// the cmdHandler is not thread-safe, 
		// please make sure it is valid until the man Processor stops
		std::string cmd = argsList.front();
		argsList.pop_front();
		ICmdHandler* cmdHandler = NULL; 
		{
			boost::shared_lock<boost::shared_mutex> lock(topCmdMutexM);
			CmdMap::iterator it = allTopCmdsM.find(cmd);
			if (it == allTopCmdsM.end())
			{
				const char* const errstr = "command not found!\r\n";
				send(errstr, strlen(errstr));
				sendPrompt();
				return 0;
			}
			cmdHandler = it->second;
		}
		cmdHandler->handle(this, argsList);
	}
	else
	{
        assert(!subCmdDataStackM.empty());
		subCmdStackM.back()->handle(this, argsList, subCmdDataStackM.back());
	}
    return 0;
}