zoukankan      html  css  js  c++  java
  • someThing about thread

    // SynThirdMgr.cpp: implementation of the CSynThirdMgr class.
    //
    //////////////////////////////////////////////////////////////////////
    //----------------------------------------------------------------------------
    // 程序名称:   SynThirdMgr.h
    // 程序说明:   业务处理类
    // 程序作者:   林江
    // 开始日期:   2009-10-26
    //----------------------------------------------------------------------------

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <strstream>
    #include <fstream>
    #include <windows.h>
    #include <direct.h>
    #include <io.h>
    #include<time.h> ///
    #include "SynThirdMgr.h"
    #include "ConfigFile.h"
    #include "shlwapi.h"
    #include   <imagehlp.h>   


    HANDLE mMutex;
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction                                         //
    //////////////////////////////////////////////////////////////////////

    CSynThirdMgr::CSynThirdMgr()
    {
    }

    CSynThirdMgr::~CSynThirdMgr()
    {
    //to do destroy
    }
    //----------------------------------------------------------------------------

    //////////////////////////////////////////////////////////////////////
    // Private Function                                                 //
    //////////////////////////////////////////////////////////////////////



    //////////////////////////////////////////////////////////////////////
    // Public Function                                                  //
    //////////////////////////////////////////////////////////////////////



    //----------------------------------------------------------------------------
    // 函数原型: prepare
    // 函数功能: 返回结果值
    // 传入参数:
    // 传出参数:
    // 函数返回:
    // 注意事项:
    //----------------------------------------------------------------------------
    bool CSynThirdMgr::prepare()
    {
    int iRet=0;
    char pstrResMsg[500];
    if(m_ConnectPool.Connect(m_strTnsName)==1)
    {
    sprintf(pstrResMsg,(char *)"数据库连接成功!");
    writeStatuslog(m_nProType,"0",0,pstrResMsg);
    cerr<< pstrResMsg <<endl;
    }
    else{
    sprintf(pstrResMsg,(char *)"数据库连接失败!!!");
    writeErrlog(m_nProType,"0",0,pstrResMsg);
    cerr<< pstrResMsg <<endl;
    return false;
    }

    return true;
    }

    //----------------------------------------------------------------------------
    // 函数原型: finish
    // 函数功能: 返回结果值
    // 传入参数:
    // 传出参数:
    // 函数返回:
    // 注意事项:
    //----------------------------------------------------------------------------
    void CSynThirdMgr::finish()
    {
    return;
    }

    //----------------------------------------------------------------------------
    // 函数原型: handle
    // 函数功能: 返回结果值
    // 传入参数:
    // 传出参数:
    // 函数返回:
    // 注意事项:
    //----------------------------------------------------------------------------
    DWORD WINAPI ReadDataProc(LPVOID lpParamter)
    {
    CSynThirdMgr *pCSynThirdMgr=(CSynThirdMgr*)lpParamter;
    /*if(0==m_ConnectPool.ReadData(
    m_strRealFile,
    pfile,
    m_strUserLabelTable,
    nFlag,
    lckID)) /// the second param is groupid
    */
    if(0==pCSynThirdMgr->m_ConnectPool.ReadData(pCSynThirdMgr->m_strRealFile,pCSynThirdMgr->m_pfile,pCSynThirdMgr->m_strUserLabelTable,pCSynThirdMgr->m_nFlag,pCSynThirdMgr->m_n64ID))
    {
    //cout<<"数据写入成功,文件名"<<szFileName<<endl;
    cout<<"手机字段:"<<pCSynThirdMgr->m_nFlag<<"\t写入成功"<<endl;
    //pfile->close();
    //continue;
    return 0;
    }
    else{
    cerr << "DB Exception!!!" << endl;
    //pCSynThirdMgr->writeErrlog(pCSynThirdMgr->m_nProType,pCSynThirdMgr->m_n64ID,0,"DB Exception!!!");
    return -1;
    }

    }
    void CSynThirdMgr::handle()
    {
    char szID[64]={0};
    char szFilePath[512]={0};
    char szFileName[128]={0};
    char szDealedLine[16]={0}; 
    char szOldFullName[1024]={0};
    char szNewFullName[1024]={0};
    char szLogBuf[1024]={0};
    //char szFileRealName[256]={0}; ///file name will be writed
    int  lReadLine=0;
    //long lckID=0;
    __int64 lckID;
    char szStatue[]="0";
    long lDownNum=0; ///need to bedown

    //
    char str_timeBeg[100];
    char str_timeEnd[100];
    struct tm *localTimeBeg=NULL;
    struct tm *localTimeEnd=NULL;


    //std::ofstream *pfile=new ofstream(); ///a point to file
    cerr <<"Program Running"<< endl;
    //download file
    //int nRet = m_ConnectPool.GetDownLoadFile(m_strPlatForm,m_strSrcTable,szID,szFilePath,szFileName,szDealedLine,m_nProType,m_nTotNumer);

    bool bRet=m_ConnectPool.bckDownLoadData(m_strCkTable,szStatue,&lReadLine,&lckID);
    if(bRet)
    {
    time_t tm_beg,tm_end;
    double result=0;
    tm_beg=time(NULL);
    localTimeBeg=localtime(&tm_beg);
    strftime(str_timeBeg, sizeof(str_timeBeg), "%Y-%m-%d,%H:%M:%S", localTimeBeg);
    //文件名格式 userlabel_YYYYMMDD24HHMISS+.txt

    char szTimeNow[64]; 
    time_t t = time(0); 
    strftime( szTimeNow, sizeof(szTimeNow), "%Y%m%d%H%M%S",localtime(&t) ); 
    sprintf(szFileName,"userlabel_%s.txt",szTimeNow);
    sprintf(m_strRealFile,"%s\\%s",m_strSucFilePath,szFileName);/// 成功文件路径
    //pfile->open(m_strRealFile,ios_base::in|ios_base::app);
    mMutex=CreateMutex(NULL,FALSE,NULL);
    m_pfile->open(m_strRealFile,ios_base::in|ios_base::app);
    //for(int nFlag=lReadLine;nFlag<=99;nFlag++)
    //int m_nFlag=0;
    m_nFlag=0;
    while(m_nFlag<99)
    {
    m_nFlag++;
    // cout<<"**********\r\n"<<nFlag<<"\r\n**********"<<endl;
    // time_t t = time(0); 
    //按时间
    /* char szTimeNow[64]; 
    strftime( szTimeNow, sizeof(szTimeNow), "%Y%m%d%H%M%S",localtime(&t) ); 
    sprintf(szFileName,"userlabel_%s.txt",szTimeNow);
    sprintf(m_strRealFile,"%s\\%s",m_strSucFilePath,szFileName);/// 成功文件路径
    */
    ///end time
    //vfile.open(szFileRealName,ios_base::in|ios_base::app); //if file no exit create,others append the data
    /*pfile->open(m_strRealFile,ios_base::in|ios_base::app);*/
    // if(!(*pfile))
    if(!(*m_pfile))
    {
    cout<<"open file error!"<<endl;
    writeErrlog(m_nProType,szID,0,"Open File error!");
    m_pfile->close();
    delete m_pfile;
    }

    HANDLE thread1,thread2;
    m_n64ID=lckID;
    thread1=CreateThread(NULL,0,ReadDataProc,NULL,0,NULL);
    thread2=CreateThread(NULL,0,ReadDataProc,NULL,0,NULL);
    CloseHandle(thread1);
    CloseHandle(thread2);
    /*
    if(0==m_ConnectPool.ReadData(
    m_strRealFile,
    pfile,
    m_strUserLabelTable,
    nFlag,
    lckID)) /// the second param is groupid
    {
    //cout<<"数据写入成功,文件名"<<szFileName<<endl;
    cout<<"手机字段:"<<nFlag<<"\t写入成功"<<endl;
    //pfile->close();
    continue;
    }
    else{
    cerr << "DB Exception!!!" << endl;
    writeErrlog(m_nProType,szID,0,"DB Exception!!!");
    }
    */
    }
    tm_end=time(NULL);
    localTimeEnd=localtime(&tm_end);
    //无论之前的时间取值是多少。在这里如果开始、结束连在一起的话可就没有时间差了
    strftime(str_timeEnd, sizeof(str_timeEnd), "%Y-%m-%d,%H:%M:%S", localTimeEnd);
    printf("***\nbegin_time:%s\t end_time:%s\n***\n",str_timeBeg,str_timeEnd);
    result=difftime(tm_end,tm_beg);
    printf("********\n耗费时间:%f  秒\n***********\n",result);
    sprintf(szLogBuf,"数据写入文件完成,最后一个文件名:%s",szFileName);
    writeStatuslog(m_nProType,szID,0,szLogBuf);
    }
    else if(bRet!=true){
    // cerr << "Finish or DB Exception!!!" << endl;
    // writeErrlog(m_nProType,szID,0,"DB Exception!!!");
    cout<<"Finish!!!\r\n"<<endl;
    }
    return;
    }


    //----------------------------------------------------------------------------
    // 函数原型: initialize
    // 函数功能: 返回结果值
    // 传入参数:
    // 传出参数:
    // 函数返回:
    // 注意事项:
    //----------------------------------------------------------------------------
    int CSynThirdMgr::initialize(int argc,char* argv[])
    {
    char strTemp[512]={0};
    char strCfgFileName[512];
    char strnumber[20]={0};
    if(strcmp(argv[1],"-stop") == 0 && argc == 3)
    {
    strcpy(strCfgFileName, argv[2]);

    }
    else if(strcmp(argv[1],"-start") == 0 && argc == 4)
    {
    strcpy(strCfgFileName, argv[3]);
    }
    else
    {
    cerr << "missing parameter" << endl;
    return -1;
    }

    if( m_cConfigFile.Open(strCfgFileName) < 0 )
    {
    cerr << "Can not open " << strCfgFileName << endl;
    return -1;
    }
    if (initCommon(argc,argv,m_cConfigFile)!=0)
    {
    return -1;
    }

    //------------------------------------[配置文件]-------------------
    //应用程序号  
    if(m_cConfigFile.GetValue((char *)"APPINFO",(char *)"ProgramID",strTemp)!=0)
    {
    cerr << "没有找到配置文件中的ProgramID的数据!"<<endl;
    return  FAIL;
    }
    m_nProgramID = atoi(strTemp);
    //应用名称
    if(m_cConfigFile.GetValue((char *)"APPINFO",(char *)"ProgramName",m_strProgramName)!=0)
    {
    cerr << "没有找到配置文件中的ProgramName的数据!"<<endl;
    return  FAIL;
    }
    //应用版本号
    if( m_cConfigFile.GetValue((char *)"APPINFO",(char *)"Version",m_strVersion)!=0 )
    {
    cerr << "没有找到配置文件中的Version的数据!"<<endl;
    return  FAIL;
    }
    //应用所在路径
    if( m_cConfigFile.GetValue((char *)"APPINFO",(char *)"AppPath",m_strAppPath)!=0 )
    {
    cerr << "没有找到配置文件中的AppPath的数据!"<<endl;
    return  FAIL;
    }
    MakeSureDirectoryPathExists(m_strAppPath);
    if( m_cConfigFile.GetValue((char *)"APPINFO",(char *)"PlatForm",m_strPlatForm)!=0 )
    {
    cerr << "没有找到配置文件中的PlatForm的数据!"<<endl;
    return  FAIL;
    }

    //休眠时间
    if( m_cConfigFile.GetValue((char *)"GLOBAL",(char *)"SleepTime",strTemp)!=0 )
    {
    cerr << "没有找到配置文件中的SleepTime的数据!"<<endl;
    return  FAIL;
    }
    m_nSleepTime = atoi(strTemp);
    //进程号
    if( m_cConfigFile.GetValue((char *)"GLOBAL",(char *)"ProType",strTemp) !=0 )
    {
    cerr << "没有找到配置文件中的ProType的数据!"<<endl;
    return  FAIL;
    }
    m_nProType = atoi(strTemp);
    //总的进程数
    if( m_cConfigFile.GetValue((char *)"GLOBAL",(char *)"TotNumer",strTemp) !=0 )
    {
    cerr << "没有找到配置文件中的TotNumer的数据!"<<endl;
    return  FAIL;
    }
    m_nTotNumer = atoi(strTemp);
    //源文件路径
    if( m_cConfigFile.GetValue((char *)"GLOBAL",(char *)"SrcFilePath",m_strSrcFilePath) !=0 )
    {
    cerr << "没有找到配置文件中的SrcFilePath的数据!"<<endl;
    return  FAIL;
    }
    MakeSureDirectoryPathExists(m_strSrcFilePath);
    //成功日志文件路径
    if( m_cConfigFile.GetValue((char *)"GLOBAL",(char *)"SucFilePath",m_strSucFilePath) !=0 )
    {
    cerr << "没有找到配置文件中的SucFilePath的数据!"<<endl;
    return  FAIL;
    }
    MakeSureDirectoryPathExists(m_strSucFilePath);
    //错误日志文件路径
    if( m_cConfigFile.GetValue((char *)"GLOBAL",(char *)"BadFilePath",m_strBadFilePath) !=0 )
    {
    cerr << "没有找到配置文件中的BadFilePath的数据!"<<endl;
    return  FAIL;
    }
    MakeSureDirectoryPathExists(m_strBadFilePath);
    //备份文件路径
    if( m_cConfigFile.GetValue((char *)"GLOBAL",(char *)"CopyFilePath",m_strCopyFilePath) !=0 )
    {
    cerr << "没有找到配置文件中的CopyFilePath的数据!"<<endl;
    return  FAIL;
    }
    MakeSureDirectoryPathExists(m_strCopyFilePath);

    //TNS
    if( m_cConfigFile.GetValue((char *)"DATABASE",(char *)"TnsName",m_strTnsName) !=0 )
    {
    cerr << "没有找到配置文件中的TnsName的数据!"<<endl;
    return  FAIL;
    }
    //源数据表
    if( m_cConfigFile.GetValue((char *)"DATABASE",(char *)"SrcTable",m_strSrcTable) !=0 )
    {
    cerr << "没有找到配置文件中的SrcTable的数据!"<<endl;
    return  FAIL;
    }

    //控制表 
    if( m_cConfigFile.GetValue((char *)"DATABASE",(char *)"CkTable",m_strCkTable) !=0 )
    {
    cerr << "没有找到配置文件中的CkTable的数据!"<<endl;
    return  FAIL;
    }
    //用户标签表
    if( m_cConfigFile.GetValue((char *)"DATABASE",(char *)"UserTable",m_strUserLabelTable) !=0 )
    {
    cerr << "没有找到配置文件中的UserTable的数据!"<<endl;
    return  FAIL;
    }
    //异常表
    if( m_cConfigFile.GetValue((char *)"DATABASE",(char *)"ExceptionTable",m_strExceptionTable) !=0 )
    {
    cerr << "没有找到配置文件中的ExceptionTable的数据!"<<endl;
    return  FAIL;
    }
    if( m_cConfigFile.GetValue((char *)"DATABASE",(char *)"CommitRow",strTemp)!=0 )
    {
    cerr << "没有找到配置文件中的CommitRow的数据!"<<endl;
    return  FAIL;
    }
    m_nCommitRow = atoi(strTemp);

    //监控路径
    if( m_cConfigFile.GetValue((char *)"MONITOR",(char *)"MonPath",m_strMonPath) !=0 )
    {
    cerr << "没有找到配置文件中的MonPath的数据!"<<endl;
    return  FAIL;
    }
    //慢时间间隔
    if( m_cConfigFile.GetValue((char *)"MONITOR",(char *)"Interval1",strTemp) !=0 )
    {
    cerr << "没有找到配置文件中的Interval1的数据!"<<endl;
    return  FAIL;
    }
    m_nInterval1 = atoi(strTemp);
    //挂死时间间隔
    if( m_cConfigFile.GetValue((char *)"MONITOR",(char *)"Interval2",strTemp) !=0 )
    {
    cerr << "没有找到配置文件中的Interval2的数据!"<<endl;
    return  FAIL;
    }
    m_nInterval2 = atoi(strTemp);
    //日志等级
    if( m_cConfigFile.GetValue((char *)"MONITOR",(char *)"LogLevel",strTemp) !=0 )
    {
    cerr << "没有找到配置文件中的LogLevel的数据!"<<endl;
    return  FAIL;
    }
    m_nLogLevel = atoi(strTemp);
    //------------------------------------[配置文件]-------------------  
    m_cConfigFile.Close();

    cout << "初始化成功!" <<endl;
    return 0;
    }


    int  CSynThirdMgr::writeErrlog(int m_intPro_type,char *str_id,int flag_type,char *pstrResMsg)
    {   
    try
    {   
    fstream fBad;
    char strBadFileName[512]={0}; //错误日志文件名
    char strQZBadFilePath[256]={0};     //前缀错误日志路径
    char strZGBadLogFileName[512]={0};  //整个错误日志路径文件名
    char strSysdate_pro[15]={0};
    char strResMsg[1024]={0};

    //获得写日志的时间
    time_t lTime;
    struct tm *rNewTime;  
    time(&lTime);
    rNewTime = localtime(&lTime);  
    sprintf(strSysdate_pro,"%04d-%02d-%02d",rNewTime->tm_year+1900,rNewTime->tm_mon+1,rNewTime->tm_mday);         
    sprintf(strBadFileName,(char *)"%d_%s%s",m_nProgramID,strSysdate_pro,LOGFILE_APPEND);
    sprintf(strQZBadFilePath,(char *)"%s",m_strBadFilePath);
    sprintf(strZGBadLogFileName,(char *)"%s%s",strQZBadFilePath,strBadFileName);

    locale loc = locale::global(locale(""));//将全局区域设为操作系统默认区域//lin
    fBad.open(strZGBadLogFileName,ios::out|ios::app);
    locale::global(loc);//还原全局区域设定//linlocale("C")
    fBad <<"时间:"<<strSysdate_pro;
    fBad <<"进程:"<<m_intPro_type<<" 处理文件id:"<<str_id;
    if (flag_type==0)
    {
    fBad <<"  "<<pstrResMsg<<endl;
    }
    else
    {
    fBad <<"  文件打开失败!!!"<<endl;
    }
    fBad.close();
    }
    catch(char *Err)
    {
    sprintf(pstrResMsg,(char *)" -401 写文件异常:%s",(char *)Err);
    return -401;
    }
    sprintf(pstrResMsg,(char *)"OK");
    return 0;
    }

    int  CSynThirdMgr::writeStatuslog(int m_intPro_type,char *str_id,int flag_type,char *pstrResMsg)
    {   
    try
    {   
    fstream fSuc;
    char strSucFileName[512]={0}; //状态日志文件名
    char strQZSucFilePath[256]={0};     //前缀状态日志路径
    char strZGSucLogFileName[512]={0};  //整个状态日志路径文件名
    char strSysdate_pro[15]={0};
    char strResMsg[1024]={0};

    //获得写日志的时间
    time_t lTime;
    struct tm *rNewTime;  
    time(&lTime);
    rNewTime = localtime(&lTime);  
    sprintf(strSysdate_pro,"%04d-%02d-%02d",rNewTime->tm_year+1900,rNewTime->tm_mon+1,rNewTime->tm_mday);         
    sprintf(strSucFileName,(char *)"%d_%s%s",m_nProgramID,strSysdate_pro,STAFILE_APPEND);
    sprintf(strQZSucFilePath,(char *)"%s",m_strSucFilePath);
    sprintf(strZGSucLogFileName,(char *)"%s%s",strQZSucFilePath,strSucFileName);

    locale loc = locale::global(locale(""));//将全局区域设为操作系统默认区域//lin
    fSuc.open(strZGSucLogFileName,ios::out|ios::app);
    locale::global(loc);//还原全局区域设定//linlocale("C")
    fSuc <<"时间:"<<strSysdate_pro;
    fSuc <<"进程:"<<m_intPro_type<<" 处理文件id:"<<str_id;
    if (flag_type==0)
    {
    fSuc <<"  "<<pstrResMsg<<endl;
    }
    else
    {
    fSuc <<"  文件打开失败!!!"<<endl;
    }
    fSuc.close();
    }
    catch(char *Err)
    {
    sprintf(pstrResMsg,(char *)" -401 写文件异常:%s",(char *)Err);
    return -401;
    }
    sprintf(pstrResMsg,(char *)"OK");
    return 0;
    }

    //.h

    // SynThirdMgr.h: interface for the CSynThirdMgr class.

    //

    //////////////////////////////////////////////////////////////////////

    //----------------------------------------------------------------------------

    // 程序名称:   SynThirdMgr.h

    // 程序说明:   业务处理类

    // 程序作者:   林江

    // 开始日期:   2009-10-26

    //----------------------------------------------------------------------------

    #ifndef WQF_SYNTHIRDMGR_H__20040919__INCLUDED_

    #define WQF_SYNTHIRDMGR_H__20040919__INCLUDED_

    #include "FlowCtrl.h"


    class CSynThirdMgr : public CFlowCtrl

    {

    public:

    CSynThirdMgr();

    virtual ~CSynThirdMgr();


    bool prepare();

    //退出

    void finish();

    //一个周期中的所有处理,如处理完一个文件,提交;如果出严重错误,抛出异常

    void handle();


    int initialize(int argc,char* argv[]);


    //private:

    public:   

    //----------开始------------------------------------

    //应用程序号

    int m_nProgramID;

    //应用名称

    char m_strProgramName[128];

    //应用版本号

    char m_strVersion[128];

    //应用所在路径

    char m_strAppPath[512];

    //平台ID

    char m_strPlatForm[32];

    //休眠时间

    int m_nSleepTime;

    //进程号

    int m_nProType;

    //总的进程数

    int m_nTotNumer;

    //源文件路径

    char m_strSrcFilePath[512];

    //成功日志文件路径

    char m_strSucFilePath[512];

    //错误日志文件路径

    char m_strBadFilePath[512];

    //备份文件路径

    char m_strCopyFilePath[512];

    //完整文件名

    char m_strRealFile[256];

    //数据库连接串

    char m_strTnsName[512];

    //源数据表

    char m_strSrcTable[128];

    //入库表

    char m_strUserLabelTable[128];

    //control table

    char m_strCkTable[128];


    //storage data table

    char m_strDownTable[128];


    //异常表

    char m_strExceptionTable[128];

    //多少条提交一次

    int m_nCommitRow;


    //监控路径

    char m_strMonPath[512];

    //慢时间间隔

    int m_nInterval1;

    //挂死时间间隔

    int m_nInterval2;

    //日志等级

    int m_nLogLevel;

    //是否有效

    bool m_bDownFlag;

    //---------------结束---------------------------------


    CConfigFile    m_cConfigFile;

    int writeErrlog(int m_intPro_type,char *str_id,int flag_type,char *pstrResMsg);

    int writeStatuslog(int m_intPro_type,char *str_id,int flag_type,char *pstrResMsg);


    DWORD WINAPI uploadDataThread(LPVOID lpParamter);

    public:

    int m_nFlag; //控制标识

    std::ofstream *m_pfile;//=new ofstream();

    __int64 m_n64ID;

    };


    #endif // WQF_SYNTHIRDMGR_H__20040919__INCLUDED_

  • 相关阅读:
    14-Reverse Integer
    13.Merge k Sorted Lists
    12-Add Digits
    11-String to Integer (atoi)
    10.Power of Two-Leetcode
    9. Delete Node in a Linked List
    使用Openmp并行化
    C++编译过程与内存空间
    C++栈溢出
    程序(进程)内存空间分布深入理解
  • 原文地址:https://www.cnblogs.com/xianqingzh/p/1614289.html
Copyright © 2011-2022 走看看