<commit_before>/*************************************************************************** 
** 
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 
** All rights reserved. 
** Contact: Nokia Corporation (<EMAIL>) 
** 
** This file is part of Testability Driver Qt Agent
** 
** If you have questions regarding the use of this file, please contact 
** Nokia at <EMAIL> . 
** 
** This library is free software; you can redistribute it and/or 
** modify it under the terms of the GNU Lesser General Public 
** License version 2.1 as published by the Free Software Foundation 
** and appearing in the file LICENSE.LGPL included in the packaging 
** of this file. 
** 
****************************************************************************/ 
 


#include <QByteArray>
#include <tascoreutils.h>
#include <testabilityutils.h>
#include <testabilitysettings.h>
#include <taslogger.h>
#include <QTcpSocket>
#include <QDomElement>
#include <QDomDocument>
#include <QFile>


#include "servermonitor.h"

#ifdef Q_OS_SYMBIAN
#include <e32base.h>
_LIT( KQTasServerName, "qttasserver" );
#endif


const static QString SERVER_PATH = "qttasserver";

const static QString CLOSE_APP = "<TasCommands service=\"closeApplication\" name=\"\" id=\"\">" 
    "<Target TasId=\"Application\"><Command name=\"Close\" uid=\"0\"/></Target></TasCommands>";

const static QString SERVER_PING = "<TasCommands service=\"listApps\">" 
    "<Target TasId=\"Application\"><Command name=\"listApps\"/></Target></TasCommands>";

const static QString NOT_CONNECTED = "Down";

const static QString CONNECTED = "Connected";

const static QString NOT_RESPONDING = "Not responding";

const static QString RUNNING = "Running";

#ifdef Q_OS_SYMBIAN
const static QString SERVERINI =  "c:\\system\\data\\qttasserver.ini";
#endif

ServerMonitor::ServerMonitor(QObject* parent)
    :QObject(parent)
{
    mClient = new TasClient();
    connect(mClient, SIGNAL(error(const QString&)), this, SLOT(error(const QString&)));
    connect(mClient, SIGNAL(info(const QString&)), this, SLOT(info(const QString&)));
    connect(mClient, SIGNAL(serverResponse(const QString&)), this, SLOT(serverResponse(const QString&)));

    QString appName = TestabilityUtils::getApplicationName();
    TasLogger::logger()->setLogFile(appName+".log");         
    TasLogger::logger()->setLevel(DEBUG);                              
    TasLogger::logger()->clearLogFile();   
    
}

ServerMonitor::~ServerMonitor()
{
    delete mClient;
}

void ServerMonitor::serverState()
{
    mState = STATUS;
    emit serverDebug("Get server status..");    
    mClient->sendMessage(SERVER_PING);
}

void ServerMonitor::restartServer()
{
    emit beginMonitor();
    mState = RESTART;
    emit serverDebug("Restarting server...");
    emit serverDebug("Stopping server...");
    mClient->sendMessage(CLOSE_APP);
}

void ServerMonitor::startServer()
{
    emit beginMonitor();
    mState = START;
    emit serverDebug("Starting server....");
    QProcess::startDetached(SERVER_PATH);   
    emit serverDebug("Process started wait a bit...");
    QTimer::singleShot(3000, this, SLOT(serverState()));
}

void ServerMonitor::stopServer()
{        
    emit beginMonitor();
    mState = STOP;                               
    emit serverDebug("Stopping server...");
    mClient->sendMessage(CLOSE_APP);
    TasLogger::logger()->debug("ServerMonitor::stopServer");
}


void ServerMonitor::info(const QString& message)
{
    emit serverDebug(message);
}

void ServerMonitor::error(const QString& message)
{
    TasLogger::logger()->debug("ServerMonitor::error " + message);
    emit serverDebug(message);
    emit serverState(NOT_CONNECTED);
    if(mState != RESTART){
        emit stopMonitor();
    }
    if(mState == STOP || mState == RESTART){
        emit serverDebug("Connection was closed. Server appears to have stopped.");
        killServer();
        if(mState == RESTART){
            TasCoreUtils::wait(1000);
            startServer();
        }
    }
}

void ServerMonitor::serverResponse(const QString& message)
{
    TasLogger::logger()->debug("ServerMonitor::serverResponse " + message);
    if(mState == STOP || mState == RESTART){
        emit serverDebug("Close command ok server stopping.");    
    }
    else{
        QDomDocument doc("TasMessage");    
        if(mState == STATUS && doc.setContent(message)){
            emit serverDebug("Currently registered applications:");    
            QDomNodeList targets = doc.elementsByTagName (QString("object"));
            int count = targets.count();        
            for (int i = 0; i < count; i++){
                QDomElement target = targets.item(i).toElement();
                if(target.attribute("type") == "application"){
                    emit serverDebug(target.attribute("name"));        
                }
            }
        }
        else{
            emit serverDebug(message);    
        }
        emit serverState(RUNNING);                
        emit stopMonitor();
    }
}

void ServerMonitor::killServer()
{ 
    TasLogger::logger()->debug("ServerMonitor::killServer");
#ifdef Q_OS_SYMBIAN
    TFindProcess findProcess;
    TFullName processName;
    while ( findProcess.Next( processName ) == KErrNone )
        {
        if ( ( processName.Find( KQTasServerName ) != KErrNotFound ) )
            {
            RProcess process;
            TInt err = process.Open( findProcess );
            if( err == KErrNone)
                {
                if( process.ExitType() != EExitPending )
                    {
                    process.Close();
                    }
                else
                    {
                    process.Kill(0);
                    process.Close();
                    break;
                    }
                }              
            }
        }
#else
    //make sure connections are re opened 
    delete mClient;
    mClient = new TasClient();
    connect(mClient, SIGNAL(error(const QString&)), this, SLOT(error(const QString&)));
    connect(mClient, SIGNAL(info(const QString&)), this, SLOT(info(const QString&)));
    connect(mClient, SIGNAL(serverResponse(const QString&)), this, SLOT(serverResponse(const QString&)));
#endif
}

#ifdef Q_OS_SYMBIAN
void ServerMonitor::enablePluginLoad()<commit_msg>Win7 ui crash fix<commit_after>/*************************************************************************** 
** 
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 
** All rights reserved. 
** Contact: Nokia Corporation (<EMAIL>) 
** 
** This file is part of Testability Driver Qt Agent
** 
** If you have questions regarding the use of this file, please contact 
** Nokia at <EMAIL> . 
** 
** This library is free software; you can redistribute it and/or 
** modify it under the terms of the GNU Lesser General Public 
** License version 2.1 as published by the Free Software Foundation 
** and appearing in the file LICENSE.LGPL included in the packaging 
** of this file. 
** 
****************************************************************************/ 
 


#include <QByteArray>
#include <tascoreutils.h>
#include <testabilityutils.h>
#include <testabilitysettings.h>
#include <taslogger.h>
#include <QTcpSocket>
#include <QDomElement>
#include <QDomDocument>
#include <QFile>


#include "servermonitor.h"

#ifdef Q_OS_SYMBIAN
#include <e32base.h>
_LIT( KQTasServerName, "qttasserver" );
#endif


const static QString SERVER_PATH = "qttasserver";

const static QString CLOSE_APP = "<TasCommands service=\"closeApplication\" name=\"\" id=\"\">" 
    "<Target TasId=\"Application\"><Command name=\"Close\" uid=\"0\"/></Target></TasCommands>";

const static QString SERVER_PING = "<TasCommands service=\"listApps\">" 
    "<Target TasId=\"Application\"><Command name=\"listApps\"/></Target></TasCommands>";

const static QString NOT_CONNECTED = "Down";

const static QString CONNECTED = "Connected";

const static QString NOT_RESPONDING = "Not responding";

const static QString RUNNING = "Running";

#ifdef Q_OS_SYMBIAN
const static QString SERVERINI =  "c:\\system\\data\\qttasserver.ini";
#endif

ServerMonitor::ServerMonitor(QObject* parent)
    :QObject(parent)
{
    mClient = new TasClient();
    connect(mClient, SIGNAL(error(const QString&)), this, SLOT(error(const QString&)));
    connect(mClient, SIGNAL(info(const QString&)), this, SLOT(info(const QString&)));
    connect(mClient, SIGNAL(serverResponse(const QString&)), this, SLOT(serverResponse(const QString&)));

    QString appName = TestabilityUtils::getApplicationName();
    TasLogger::logger()->setLogFile(appName+".log");         
    TasLogger::logger()->setLevel(DEBUG);                              
    TasLogger::logger()->clearLogFile();   
    
}

ServerMonitor::~ServerMonitor()
{
    delete mClient;
}

void ServerMonitor::serverState()
{
    mState = STATUS;
    emit serverDebug("Get server status..");    
    mClient->sendMessage(SERVER_PING);
}

void ServerMonitor::restartServer()
{
    emit beginMonitor();
    mState = RESTART;
    emit serverDebug("Restarting server...");
    emit serverDebug("Stopping server...");
    mClient->sendMessage(CLOSE_APP);
}

void ServerMonitor::startServer()
{
    emit beginMonitor();
    mState = START;
    emit serverDebug("Starting server....");
    QProcess::startDetached(SERVER_PATH);   
    emit serverDebug("Process started wait a bit...");
    QTimer::singleShot(3000, this, SLOT(serverState()));
}

void ServerMonitor::stopServer()
{        
    emit beginMonitor();
    mState = STOP;                               
    emit serverDebug("Stopping server...");
    mClient->sendMessage(CLOSE_APP);
    TasLogger::logger()->debug("ServerMonitor::stopServer");
}


void ServerMonitor::info(const QString& message)
{
    emit serverDebug(message);
}

void ServerMonitor::error(const QString& message)
{
    TasLogger::logger()->debug("ServerMonitor::error " + message);
    emit serverDebug(message);
    emit serverState(NOT_CONNECTED);
    if(mState != RESTART){
        emit stopMonitor();
    }
    if(mState == STOP || mState == RESTART){
        emit serverDebug("Connection was closed. Server appears to have stopped.");
        killServer();
        if(mState == RESTART){
            TasCoreUtils::wait(1000);
            startServer();
        }
    }
}

void ServerMonitor::serverResponse(const QString& message)
{
    TasLogger::logger()->debug("ServerMonitor::serverResponse " + message);
    if(mState == STOP || mState == RESTART){
        emit serverDebug("Close command ok server stopping.");    
    }
    else{
        QDomDocument doc("TasMessage");    
        if(mState == STATUS && doc.setContent(message)){
            emit serverDebug("Currently registered applications:");    
            QDomNodeList targets = doc.elementsByTagName (QString("object"));
            int count = targets.count();        
            for (int i = 0; i < count; i++){
                QDomElement target = targets.item(i).toElement();
                if(target.attribute("type") == "application"){
                    emit serverDebug(target.attribute("name"));        
                }
            }
        }
        else{
            emit serverDebug(message);    
        }
        emit serverState(RUNNING);                
        emit stopMonitor();
    }
}

void ServerMonitor::killServer()
{ 
    TasLogger::logger()->debug("ServerMonitor::killServer");
#ifdef Q_OS_SYMBIAN
    TFindProcess findProcess;
    TFullName processName;
    while ( findProcess.Next( processName ) == KErrNone )
        {
        if ( ( processName.Find( KQTasServerName ) != KErrNotFound ) )
            {
            RProcess process;
            TInt err = process.Open( findProcess );
            if( err == KErrNone)
                {
                if( process.ExitType() != EExitPending )
                    {
                    process.Close();
                    }
                else
                    {
                    process.Kill(0);
                    process.Close();
                    break;
                    }
                }              
            }
        }
#else
    //make sure connections are re opened 
    mClient->deleteLater();
    mClient = new TasClient();
    connect(mClient, SIGNAL(error(const QString&)), this, SLOT(error(const QString&)));
    connect(mClient, SIGNAL(info(const QString&)), this, SLOT(info(const QString&)));
    connect(mClient, SIGNAL(serverResponse(const QString&)), this, SLOT(serverResponse(const QString&)));
#endif
}

#ifdef Q_OS_SYMBIAN
void ServerMonitor::enablePluginLoad()
{'fraction_non_alphanumeric': 0.15261235508972526, 'fraction_numerical': 0.0030967127203430206, 'mean_word_length': 2.8969678217821784, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 68, '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': '5433257', 'n_tokens_mistral': 3604, 'n_tokens_neox': 3466, 'n_words': 827}