DoodleManager.cpp 5.51 KB
#include "DoodlePch.h"
#include "../mainapp.h"

namespace JusDoodle {

const double Version = 1.5;

DoodleManager *DoodleManager::_pDoodleManager = NULL;

DoodleManager::DoodleManager(QObject *parent)
    : QObject(parent)
    , _SessMap()
{
	_pDoodleManager = this;
}

DoodleManager::~DoodleManager()
{

}

DoodleManager *DoodleManager::Instance()
{
    if (!_pDoodleManager) 
		_pDoodleManager = new DoodleManager;

    return _pDoodleManager;
}

void DoodleManager::onServiceEvent(const QString &name, size_t cookie, const Notification &info)
{
    if (onCallEvent(name, cookie, info))
        return;
    if (onClientEvent(name, cookie, info))
        return;
}

bool DoodleManager::init()
{
    return connect((MainApp *)qApp, SIGNAL(serviceEvent(const QString &, size_t, const Notification &)),
        SLOT(onServiceEvent(const QString &, size_t, const Notification &)));
}

bool DoodleManager::startSess(SESSID sessId, int pageCount)
{
    DoodleSession *pSess = findSess(sessId);
    if (!pSess) return false;

    // start session
    if (!pSess->start(pageCount))
    {
        DOODLE_LOG_ERROR("start session %d.", sessId);
        return false;
    }

    DOODLE_LOG_INFO("session %d.", (SESSID)sessId);

    return true;
}

void DoodleManager::stopSess(SESSID sessId)
{
    DoodleSession *pSess = findSess(sessId);
    if (!pSess) return;

    DOODLE_LOG_INFO("sess<%lld>.", sessId);

    pSess->stop();
    _SessMap.erase((SESSID)pSess);
    delete pSess;
}

void DoodleManager::setBackgroundImages(SESSID sessId, const QStringList &imagePaths)
{
    DoodleSession *pSess = findSess(sessId);
    if (!pSess) return;

    DOODLE_LOG_INFO("sess<%lld>.", sessId);
    
    pSess->setBackgroundImages(imagePaths);
}

void DoodleManager::onRecvData(SESSID sessId, const char *name, const char *data)
{
    DoodleSession *pSess = findSess(sessId);
    if (!pSess) return;

    DOODLE_LOG_INFO("sess<%lld>.", sessId);

    pSess->recvData(name, data);
}

DoodleSession *DoodleManager::findSess(SESSID sessId)
{
    std::map<SESSID, DoodleSession *>::iterator it = _SessMap.find(sessId);
    if (it != _SessMap.end())
        return it->second;

    return NULL;
}

bool DoodleManager::onCallEvent(const QString &name, size_t, const Notification &_info)
{
    Notification info = (Notification &)_info;
    DoodleSession *sess;
    int sessId;

    if (name == MtcCallConnectingNotification)
    {
        sessId = info.getIntValue(MtcCallIdKey);

        stopSess(sessId);
        // create session
        sess = new DoodleSession(sessId, this);
        if (!sess)
        {
            DOODLE_LOG_ERROR("allocate session %d.", sessId);
            return true;
        }

        // init session
        if (!sess->init())
        {
            DOODLE_LOG_ERROR("init session %d.", sessId);
            return true;
        }

        // add to map
        _SessMap[sessId] = sess;
        DOODLE_LOG_INFO("allocate session %d.", sessId);

        emit sessCreated(sessId);

        return true;
    }
    else if (name == MtcCallTermedNotification
        || name == MtcCallDidTermNotification)
    {
        // delete session
//        return true;
        sessId = info.getIntValue(MtcCallIdKey);
        sess = findSess(sessId);

//        stopSess(sessId);
        if (sess)
        {
//            _SessMap.erase(sessId);
            DOODLE_LOG_INFO("free session %d.", sessId);
//            delete sess;
        }

        return true;
    }
    else if (name == MtcCallStreamDataReceivedNotification)
    {
        // process doodle data
        sessId = info.getIntValue(MtcCallIdKey);
        sess = findSess(sessId);
        qDebug()<<"dataToInt = "<<MtcCallStreamDataReceivedNotification<<"\n";
        if (sess)
        {
            QString dataName = info.getStringValue(MtcCallDataNameKey);
            QString data = info.getStringValue(MtcCallDataValueKey);
            qDebug("CMQ dataName = %s data = %s\n",dataName.toLatin1().data(),data.toLatin1().data());
//            qDebug()<<"dataToInt = "<<data.toInt()<<"\n";
            sess->recvData(dataName, data);
        }
        return true;
    }
    else if (name == MtcCallStreamFileReceivedNotification)
    {
        // process doodle file
        sessId = info.getIntValue(MtcCallIdKey);
        sess = findSess(sessId);
        if (sess)
        {
            QString data = info.getStringValue(MtcCallUserDataKey);
            Json::Value imageInfo;
            Json::Reader reader;
            reader.parse(data.toUtf8().data(), imageInfo, false);
            int pageId = imageInfo.get(MtcDoodlePageIdKey, -1).asInt();
            if (pageId >= 0)
            {
                QString fileName = info.getStringValue(MtcCallFileNameKey);
                QString filePath = info.getStringValue(MtcCallFilePathKey);
                sess->recvFile(fileName, filePath, pageId);
                qDebug("CMQ fileName = %s filePath = %s",fileName.toLatin1().data(),filePath.toLatin1().data());
            }
        }

        return true;
    }

    return false;
}

bool DoodleManager::onClientEvent(const QString &name, size_t, const Notification &)
{
    if (name == MtcCliServerDidLogoutNotification
        || name == MtcCliServerLogoutedNotification)
    {
        // delete all sessions
        std::map<SESSID, DoodleSession *>::iterator it = _SessMap.begin();
        while (it != _SessMap.end())
        {
            delete it->second;
            _SessMap.erase(it);
            it = _SessMap.begin();
        }

        DOODLE_LOG_INFO("free all session.", NULL);
        return true;
    }

    return false;
}


} // namespace JusDoodle