/************************************************************
* Details: 消息队列循环处理机制
* Encode: UTF-8
* Platform: Android & Linux
* Author: R&D 2
* Date: 2018-4-2
************************************************************/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "gterror.h"
#include "gt.h"
#include "gttoolkitext.h"
#include "gtencrypt.h"
#include "gtmessageloop.h"
#include "gtcommencrypt.h"
#include  "gtstruct.h"
#include "gtmem.h"
#include<iostream>
#include<string>



GtMessageLoop *GtMessageLoop::_instance = NULL;

GtMessageLoop::GtMessageLoop():_readTimeout(45)
{
    _serverSocket = -1;
    _lastSN = -1;
    _runFlag = true;
    memset(_serverIP, 0, sizeof(_serverIP));
    _serverPort = 0;
}

GtMessageLoop::~GtMessageLoop()
{

}

GtMessageLoop *GtMessageLoop::instance()
{
    if(_instance == NULL)
    {
        _instance = new GtMessageLoop();
    }
    return _instance;
}

/************************************************************
*功能：创建消息队列
*输入：size队列长度
*输出：无
*返回：true,false
*/
bool GtMessageLoop::initMessage(int size)
{
    return GtMessageQueue::instance()->initMessageQueue(size);
}

/************************************************************
*功能：停止消息队列
*输入：无
*输出：无
*返回：无
*/
void GtMessageLoop::stop()
{
    _runFlag = false;
}

/************************************************************
*功能：消息队列循环处理
*输入：无
*输出：无
*返回：无
*/
void GtMessageLoop::loop()
{
    while(_runFlag)
    {
        handleMessage();
        keepServerConnect();
    }
    GtMessageQueue::instance()->releaseMessageQueue();
}

/************************************************************
*功能：消息队列数据处理，包括超时、服务器请求、应答等
*输入：无
*输出：无
*返回：无
*/
void GtMessageLoop::handleMessage()
{
    int result = 0;
    int length = 0;
    stMessageItem item;

    memset(&item, 0, sizeof(stMessageItem));
    if(!GtMessageQueue::instance()->getNodeSecure(0, &item))
    {
        return;
    }
    if(item.unStatus == 1)
    {
        //处理超时的消息项
        time_t currentTime = time(NULL);
        if((currentTime - item.lTime) >= item.nValidTime)
        {
            GtMessageQueue::instance()->recycleNodeSecure(0);
            GtToolkitExt::logError(__FILE__, __LINE__, "清除超时消息队列项");
            return;
        }
        //内部指令
        if(item.msg.msgTag.ulTransCode == 100000)
        {
            result = serverLink((char *)&item.msg.strData[2]);
            if(result == 0)
            {
                normalResponse(&item, NULL, 0);
            }
            else
            {
                errorResponse(&item, result, (char *)"Connect to server failed");
            }
            GtMessageQueue::instance()->recycleNodeSecure(0);
            return;
        }
        //通讯加密处理
        else if(item.msg.msgTag.ulTransCode == 101000)
        {
            char pubKey[512];
            memset(pubKey, 0, sizeof(pubKey));
            result = GtCommEncrypt::instance()->generateCommEncryptKey(pubKey);
            if(result != 0)
            {
                GtMessageQueue::instance()->recycleNodeSecure(0);
                return;
            }
            else
            {
                length = sprintf((char *)&item.msg.strData[item.msg.msgTag.ulDataSize], "%s%c", pubKey, Gt::_delimiter1);
                item.msg.msgTag.ulDataSize += length;
                item.msg.strData[0] = (item.msg.msgTag.ulDataSize - 2)/256;
                item.msg.strData[1] = (item.msg.msgTag.ulDataSize - 2)%256;
            }
        }
        //检测网络状态
        if(_serverSocket == -1)
        {
            errorResponse(&item, -77777, (char *)"Server disconnect");
            GtMessageQueue::instance()->recycleNodeSecure(0);
            //GtToolkitExt::logError(__FILE__, __LINE__, "清除当前消息队列项");
            return;
        }
        GtToolkitExt::logInfo(__FILE__, __LINE__, "%s", &item.msg.strData[2]);
        if(_lastSN == item.msg.msgTag.ulTransSN)
        {
            GtToolkitExt::logError(__FILE__, __LINE__, "禁止已发送成功的交易重复请求");
            GtMessageQueue::instance()->recycleNodeSecure(0);
            return;
        }
        else
        {
            //写日志文件
            writeTradingLog((char *)&item.msg.strData[2], item.msg.msgTag.ulDataSize - 2);
            //数据加密
            if(GtCommEncrypt::instance()->getCommEncryptType() != 0)
            {
                int encryptLen = item.msg.msgTag.ulDataSize;
                result = GtCommEncrypt::instance()->commDataEncrypt(item.msg.strData, sizeof(item.msg.strData), item.msg.msgTag.ulDataSize, &encryptLen);
                if(result != 0)
                {
                    GtToolkitExt::logError(__FILE__, __LINE__, "服务器通信数据解密错误");
                }
                item.msg.msgTag.ulDataSize = encryptLen;
            }
            //发送请求到服务器
           // result = GtToolkitExt::writeN(_serverSocket, item.msg.strData, item.msg.msgTag.ulDataSize);
               char tmpBuf[1024];
                stTerminal terminal;
                memset(&terminal, 0, sizeof(stTerminal));
                int resultIP = GtMem::instance()->getMemTerminal(&terminal);
               if(resultIP != 0)
                {
                    GtToolkitExt::logError(__FILE__, __LINE__, "获取终端内存错误<%d>", resultIP);
                    return ;
                }
                 char *localIP="127.0.0";
                // GtToolkitExt::logInfo(__FILE__, __LINE__, "获取当前服务器ip<%s>", terminal.commAttr[0].strIP1);
                if(strstr(terminal.commAttr[0].strIP1, localIP) == NULL)
                {
                  // GtToolkitExt::logInfo(__FILE__, __LINE__, "加密报文发送");
                   //发送请求到服务器(加密)
                   result = GtToolkitExt::writeN(_serverSocket, item.msg.strData, item.msg.msgTag.ulDataSize);
                 }else{
                   //  GtToolkitExt::logInfo(__FILE__, __LINE__, "不加密报文发送");
                     result = GtToolkitExt::writeN(_serverSocket, &item.msg.strData[2], item.msg.msgTag.ulDataSize-2);
                 }

            if(result != 0)
            {
                close(_serverSocket);
                _serverSocket = -1;
                GtToolkitExt::logError(__FILE__, __LINE__, "发送交易到服务器错误");
                return;
            }
            _lastSN = item.msg.msgTag.ulTransSN;
        }
        updateKeepTime();
        //接收服务器应答
        memset(item.msg.strData, 0, sizeof(item.msg.strData));
        result = GtToolkitExt::readN(_serverSocket, item.msg.strData, 2, _readTimeout);
        if(result != 0)
        {
            if(result == GtError::ERROR_CLOSED)
            {
                GtToolkitExt::logError(__FILE__, __LINE__, "服务器链路已关闭");
            }
            GtToolkitExt::logError(__FILE__, __LINE__, "读取服务器应答错误<%d>", result);
            close(_serverSocket);
            _serverSocket = -1;
            return;
        }
        length = item.msg.strData[0] * 256 + item.msg.strData[1];
        if(length == 0)
        {
            GtToolkitExt::logError(__FILE__, __LINE__, "读取服务器应答错误");
            return;
        }
        result = GtToolkitExt::readN(_serverSocket, &item.msg.strData[2], length, _readTimeout);
        if(result != 0)
        {
            if(result == GtError::ERROR_CLOSED)
            {
                GtToolkitExt::logError(__FILE__, __LINE__, "服务器链路已关闭");
            }
            GtToolkitExt::logError(__FILE__, __LINE__, "读取服务器应答错误<%d>", result);
            close(_serverSocket);
            _serverSocket = -1;
            return;
        }
        updateKeepTime();
        //数据解密
        if(GtCommEncrypt::instance()->getCommEncryptType() != 0)
        {
            int dataLen = length + 2;
            result = GtCommEncrypt::instance()->commDataDecrypt(item.msg.strData, sizeof(item.msg.strData), &dataLen);
            if(result != 0)
            {
                GtToolkitExt::logError(__FILE__, __LINE__, "服务器通信数据解密错误");
                return;
            }
            length = dataLen - 2;
        }
        //写日志文件
        writeTradingLog((char *)&item.msg.strData[2], length);
//        GtToolkitExt::logInfo(__FILE__, __LINE__, "%s", &item.msg.strData[2]);
        //校验应答包
        if(!checkServerResponse(&item.msg))
        {
            GtToolkitExt::logError(__FILE__, __LINE__, "接收到服务器错误的应答包，忽略");
            return;
        }
        //返回服务器应答给本地链路
        item.unStatus = 2;
        item.msg.msgTag.ulDataSize = length + 2;
        item.msg.msgTag.lCRC = GtEncrypt::crc32(item.msg.strData, item.msg.msgTag.ulDataSize);
        if(localResponse(item.nRoute, &item.msg))
        {
            GtMessageQueue::instance()->recycleNodeSecure(0);
        }
        else
        {
            GtMessageQueue::instance()->setNodeSecure(0, &item);
        }
    }
    else if(item.unStatus == 2)
    {
        if(localResponse(item.nRoute, &item.msg))
        {
            GtMessageQueue::instance()->recycleNodeSecure(0);
        }
    }
}

/************************************************************
*功能：连接后台服务器
*输入：data服务器配置数据
*输出：无
*返回：!=0错误
*/
int GtMessageLoop::serverLink(char *data)
{
    int socketFd = -1;
    int result = 0;
    int bufLen = 0;
    char tmpBuf[128];
    char serverIP[128];
    unsigned short serverPort = 0;

    if(data == NULL)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "接口参数错误");
        return GtError::ERROR_INVALID_FUNC_PARAM;
    }
    bufLen = sizeof(tmpBuf);
    //取服务器IP
    memset(tmpBuf, 0, sizeof(tmpBuf));
    result = GtToolkitExt::parseNChar(data, tmpBuf, bufLen, Gt::_delimiter1, 6);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "服务器IP解析错误<%d>", result);
        return result;
    }
    strcpy(serverIP, tmpBuf);
    memset(_serverIP, 0, sizeof(_serverIP));
    strcpy(_serverIP, serverIP);
    //取服务器端口
    memset(tmpBuf, 0, sizeof(tmpBuf));
    result = GtToolkitExt::parseNChar(data, tmpBuf, bufLen, Gt::_delimiter1, 7);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "服务器端口解析错误<%d>", result);
        return result;
    }
    serverPort = atoi(tmpBuf);
    _serverPort = serverPort;
    //取终端ID
    memset(tmpBuf, 0, sizeof(tmpBuf));
    result = GtToolkitExt::parseNChar(data, tmpBuf, bufLen, Gt::_delimiter1, 8);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "终端ID解析错误<%d>", result);
        return result;
    }
    _terminalID = atol(tmpBuf);
    //取操作员ID
    memset(tmpBuf, 0, sizeof(tmpBuf));
    result = GtToolkitExt::parseNChar(data, tmpBuf, bufLen, Gt::_delimiter1, 9);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "操作员ID解析错误<%d>", result);
        return result;
    }
    _operatorID = atoi(tmpBuf);
    //离线模式
    memset(tmpBuf, 0, sizeof(tmpBuf));
    result = GtToolkitExt::parseNChar(data, tmpBuf, bufLen, Gt::_delimiter1, 10);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "网络模式解析错误<%d>", result);
        return result;
    }
    _trainingMode = atoi(tmpBuf);
    //与服务器建立连接
    if(_serverSocket > -1)
    {
        close(_serverSocket);
        _serverSocket = -1;
    }
    result = GtToolkitExt::tcpSocket(&socketFd);
    if(result != 0 || socketFd < 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "创建服务器通信链路错误<%d>", result);
        return result;
    }
    GtToolkitExt::logInfo(__FILE__, __LINE__, "启动连接服务器[%s:%d]", serverIP, serverPort);
    result = GtToolkitExt::tcpConnect(socketFd, serverIP, serverPort, 15);
    if(result != 0)
    {
        close(socketFd);
        GtToolkitExt::logError(__FILE__, __LINE__, "连接服务器错误[%s:%d]", serverIP, serverPort);
        return result;
    }
    _serverSocket = socketFd;
    updateKeepTime();
    GtToolkitExt::logInfo(__FILE__, __LINE__, "连接服务器成功[%s:%d]", serverIP, serverPort);
    return 0;
}

int GtMessageLoop::reconnectServer()
{
    int socketFd = -1;
    int result = 0;
    //与服务器建立连接
    if(_serverSocket > -1)
    {
        close(_serverSocket);
        _serverSocket = -1;
    }
    result = GtToolkitExt::tcpSocket(&socketFd);
    if(result != 0 || socketFd < 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "创建服务器通信链路错误<%d>", result);
        return result;
    }
    result = GtToolkitExt::tcpConnect(socketFd, _serverIP, _serverPort, 15);
    if(result != 0)
    {
        close(socketFd);
        GtToolkitExt::logError(__FILE__, __LINE__, "服务器重连错误[%s:%d]", _serverIP, _serverPort);
        return result;
    }
    _serverSocket = socketFd;
    updateKeepTime();
    GtToolkitExt::logInfo(__FILE__, __LINE__, "服务器重连成功[%s:%d]", _serverIP, _serverPort);
    return 0;
}

/************************************************************
*功能：返回本地应答
*输入：socketFd返回数据链路，msg队列消息
*输出：无
*返回：true,false
*/
bool GtMessageLoop::localResponse(int socketFd, stMessage *msg)
{
    int result = 0;
    int tagLength = 0;

    if(msg == NULL)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "接口参数错误");
        return false;
    }
    //通讯密匙解密
    if(msg->msgTag.ulTransCode == 101000)
    {
        char tmpBuf[2048];
        memset(tmpBuf, 0, sizeof(tmpBuf));
        result = GtToolkitExt::parseNChar((char *)&msg->strData[2], tmpBuf, sizeof(tmpBuf), Gt::_delimiter1, 7);
        if(result != 0)
        {
            GtToolkitExt::logError(__FILE__, __LINE__, "通讯密匙解析错误<%d>", result);
            return false;
        }
        int type = atol(tmpBuf);
        memset(tmpBuf, 0, sizeof(tmpBuf));
        result = GtToolkitExt::parseNChar((char *)&msg->strData[2], tmpBuf, sizeof(tmpBuf), Gt::_delimiter1, 6);
        if(result != 0)
        {
            GtToolkitExt::logError(__FILE__, __LINE__, "通讯加密类型解析错误<%d>", result);
            return false;
        }
        if(type != 0)
        {
            result = GtCommEncrypt::instance()->commKeyDecrypt(type, tmpBuf, strlen(tmpBuf));
            if(result != 0)
            {
                GtToolkitExt::logError(__FILE__, __LINE__, "通讯密匙解密错误");
                return false;
            }
        }
    }
    tagLength = sizeof(stMessageTag);
    result = GtToolkitExt::writeN(socketFd, (void *)msg, tagLength + msg->msgTag.ulDataSize);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "转发服务器应答到本地错误<%d>", result);
        return false;
    }
    return true;
}

/************************************************************
*功能：本地正常应答
*输入：item队列节点，data应答内容，dataLength数据长度
*输出：无
*返回：!=0错误
*/
int GtMessageLoop::normalResponse(stMessageItem *item, unsigned char *data, int dataLength)
{
    int result = 0;
    int length = 0;
    int tagLength = 0;

    tagLength = sizeof(stMessageTag);
    memset(item->msg.strData, 0, sizeof(item->msg.strData));
    length = sprintf((char *)&item->msg.strData[2], "%lu%c%d%c%d%c%lu%c%d%c", item->msg.msgTag.ulTransCode, Gt::_delimiter1,
        GtCommEncrypt::instance()->getCommEncryptType(), Gt::_delimiter1,
        0, Gt::_delimiter1,
        item->msg.msgTag.ulTransSN, Gt::_delimiter1,
        0, Gt::_delimiter1);
    if(dataLength > 0)
    {
        memcpy(&item->msg.strData[2 + length], data, dataLength);
    }
    item->msg.msgTag.ulDataSize = length + dataLength;
    item->msg.strData[0] = item->msg.msgTag.ulDataSize/256;
    item->msg.strData[1] = item->msg.msgTag.ulDataSize%256;
    item->msg.msgTag.ulDataSize += 2;
    item->msg.msgTag.lCRC = GtEncrypt::crc32(item->msg.strData, item->msg.msgTag.ulDataSize);
    result = GtToolkitExt::writeN(item->nRoute, (void *)&item->msg, tagLength + item->msg.msgTag.ulDataSize);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "转发服务器应答到本地错误<%d>", result);
        return result;
    }
    //GtToolkitExt::logInfo(__FILE__, __LINE__, "转发服务器应答到本地%s", &item->msg.strData[2]);
    return 0;
}

/************************************************************
*功能：本地错误应答
*输入：item队列节点，errorCode错误码，errorString错误内容
*输出：无
*返回：!=0错误
*/
int GtMessageLoop::errorResponse(stMessageItem *item, int errorCode, char *errorString)
{
    int result = 0;
    int length = 0;
    int tagLength = 0;
    stMessage msg;

    memset(&msg, 0, sizeof(stMessage));
    tagLength = sizeof(stMessageTag);
    memcpy(&msg.msgTag, &item->msg.msgTag, tagLength);
    length = sprintf((char *)&msg.strData[2], "%d%c%d%c%d%c%lu%c%d%c%lu%c%s%c", 999999, Gt::_delimiter1,
        GtCommEncrypt::instance()->getCommEncryptType(), Gt::_delimiter1,
        errorCode, Gt::_delimiter1,
        item->msg.msgTag.ulTransSN, Gt::_delimiter1,
        0, Gt::_delimiter1,
        item->msg.msgTag.ulTransCode, Gt::_delimiter1,
        errorString, Gt::_delimiter1);
    msg.msgTag.ulDataSize = length;
    msg.strData[0] = msg.msgTag.ulDataSize/256;
    msg.strData[1] = msg.msgTag.ulDataSize%256;
    msg.msgTag.ulDataSize += 2;
    msg.msgTag.lCRC = GtEncrypt::crc32(msg.strData, msg.msgTag.ulDataSize);
    result = GtToolkitExt::writeN(item->nRoute, (void *)&msg, tagLength + msg.msgTag.ulDataSize);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "转发服务器应答到本地错误<%d>", result);
        return result;
    }
    return 0;
}

/************************************************************
*功能：服务器应答数据校验
*输入：msg应答数据
*输出：无
*返回：true,false
*/
bool GtMessageLoop::checkServerResponse(stMessage *msg)
{
    int result = 0;
    char tmpBuf[2048];
    unsigned long transSN = 0;
    unsigned long transCode = 0;

    //验证流水号
    memset(tmpBuf, 0, sizeof(tmpBuf));
    result = GtToolkitExt::parseNChar((char *)&msg->strData[2], tmpBuf, sizeof(tmpBuf), Gt::_delimiter1, 4);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "流水号解析错误<%d>", result);
        return false;
    }
    transSN = atol(tmpBuf);
    if(transSN != msg->msgTag.ulTransSN)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "流水号校验错误");
        return false;
    }
    //验证交易码
    memset(tmpBuf, 0, sizeof(tmpBuf));
    result = GtToolkitExt::parseNChar((char *)&msg->strData[2], tmpBuf, sizeof(tmpBuf), Gt::_delimiter1, 1);
    if(result != 0)
    {
        GtToolkitExt::logError(__FILE__, __LINE__, "交易码解析错误<%d>", result);
        return false;
    }
    transCode = atol(tmpBuf);
    if(transCode == 999999)
    {
        memset(tmpBuf, 0, sizeof(tmpBuf));
        result = GtToolkitExt::parseNChar((char *)&msg->strData[2], tmpBuf, sizeof(tmpBuf), Gt::_delimiter1, 6);
        if(result != 0)
        {
            GtToolkitExt::logError(__FILE__, __LINE__, "交易码解析错误<%d>", result);
            return false;
        }
        transCode = atol(tmpBuf);
        if(transCode != msg->msgTag.ulTransCode)
        {
            GtToolkitExt::logError(__FILE__, __LINE__, "交易码校验错误");
            return false;
        }
    }
    else
    {
        if(transCode != msg->msgTag.ulTransCode)
        {
            GtToolkitExt::logError(__FILE__, __LINE__, "交易码校验错误");
            return false;
        }
    }
    return true;
}

/************************************************************
*功能：刷新心跳监控时间
*输入：无
*输出：无
*返回：无
*/
void GtMessageLoop::updateKeepTime()
{
    _keepTime = time(NULL);
}

void GtMessageLoop::keepServerConnect()
{
    int result = 0;
    int length = 0;
    time_t cTime = 0;
    unsigned long uniqueSN = 0;
    stMessage msg;

    if(_serverSocket == -1)
    {
        return;
    }
    cTime = time(NULL);
    if(_keepTime == 0 || (cTime - _keepTime < 60))
    {
        return;
    }
    memset(&msg, 0, sizeof(stMessage));
    uniqueSN = GtToolkitExt::uniqueSerialNumber();
    length = sprintf((char *)&msg.strData[2], "%d%c%d%c%lu%c%lu%c%u%c", 101100, Gt::_delimiter1,
        GtCommEncrypt::instance()->getCommEncryptType(), Gt::_delimiter1,
        uniqueSN, Gt::_delimiter1,
        _terminalID, Gt::_delimiter1,
        _operatorID, Gt::_delimiter1);
    msg.strData[0] = length/256;
    msg.strData[1] = length%256;
    msg.msgTag.ulDataSize = 2 + length;
    //通讯加密
    if(GtCommEncrypt::instance()->getCommEncryptType() != 0)
    {
        int encryptLen = msg.msgTag.ulDataSize;
        result = GtCommEncrypt::instance()->commDataEncrypt(msg.strData, sizeof(msg.strData), msg.msgTag.ulDataSize, &encryptLen);
        if(result != 0)
        {
            GtToolkitExt::logError(__FILE__, __LINE__, "服务器通信数据解密错误");
        }
        msg.msgTag.ulDataSize = encryptLen;
    }
    result = GtToolkitExt::writeN(_serverSocket, msg.strData, msg.msgTag.ulDataSize);
    if(result != 0)
    {
        //if(errno == SIGPIPE)
        {
            close(_serverSocket);
            _serverSocket = -1;
        }
        GtToolkitExt::logError(__FILE__, __LINE__, "心跳发送错误<%d>", result);
        return;
    }
    memset(&msg, 0, sizeof(stMessage));
    result = GtToolkitExt::readN(_serverSocket, msg.strData, 2, _readTimeout);
    if(result != 0)
    {
        if(result == GtError::ERROR_CLOSED)
        {
            close(_serverSocket);
            _serverSocket = -1;
        }
        GtToolkitExt::logError(__FILE__, __LINE__, "心跳发送错误<%d>", result);
        return;
    }
    length = msg.strData[0] * 256 + msg.strData[1];
    result = GtToolkitExt::readN(_serverSocket, msg.strData, length, _readTimeout);
    if(result != 0)
    {
        if(result == GtError::ERROR_CLOSED)
        {
            close(_serverSocket);
            _serverSocket = -1;
        }
        GtToolkitExt::logError(__FILE__, __LINE__, "心跳发送错误<%d>", result);
        return;
    }
    GtToolkitExt::logInfo(__FILE__, __LINE__, "心跳发送成功");
    updateKeepTime();
}

void GtMessageLoop::writeTradingLog(char *data, int size)
{
    char path[256];

    memset(path, 0, sizeof(path));
    Gt::instance()->getTradingLogFile(path);
    GtToolkitExt::limitFileSize(path, 20);
    GtToolkitExt::tradingLogFile(path, data, size);
}
