📄 smsend.cpp
字号:
#include <process.h>
#include <signal.h>
#include "windows.h"
#include "DispenseWait.h"
#include "DispenseCache.h"
#ifdef _NEWTEST
#include "StateReport.h"
#else
#include "DispenseReport.h"
#endif
#include "SgipSend.h"
#include "log.hpp"
#include "profile.hpp"
#include "DequePool.h"
#include "Thread.h"
#include "SetDayLog.h"
static volatile sig_atomic_t nRunFlag = 1; //used for stop program
static void SignalHandler(int sig);
char m_sProcName[ 40 ] ;
char m_sDBServer[ 40 ];
char m_sDispTbName[ 40 ] ;
bool m_bIfCtrl = false ;
int m_nMaxSend ;
char m_sDispWaitName[ 40 ] ;
char m_sDispLogName[ 40 ] ;
char m_sDispReportName[ 40 ] ;
char m_sDispBatchWaitName[ 40 ] ;
char m_sDispBatchLogName[ 40 ] ;
char m_sDispBatchReportName[ 40 ] ;
LogFile g_clLog;
Profile g_clProfile;
SetDayLog m_clSetDayLog;
//COMMENT: the handler for processing system signal, now just SIGINT
//INPUT: sig -- catched system signal
//OUTPUT: none
static void SignalHandler( int sig )
{
switch ( sig )
{
case SIGINT:
nRunFlag = 0;
break ;
default:
break ;
}
}
void GetProcValue( const char *sEntry,char *sValue )
{
if ( 0 != g_clProfile.Read( m_sProcName, sEntry, sValue ) )
{
g_clLog.Panic( 0, "Unable to get %s from profile !",sEntry ) ;
}
}
void Init()
{
char sValue[ LINE_LEN+1 ] ;
char sLogName[128] ;
//set procedure name of log
g_clLog.SetProcName( m_sProcName ) ;
//log in database
GetProcValue( "database_server",m_sDBServer ) ;
Login( m_sDBServer ) ;
//open log
GetProcValue("standart_output_level",sValue );
g_clLog.SetTermLevel( (OUTPUT_LEVEL)atoi(sValue) );
GetProcValue("log_output_level",sValue);
g_clLog.SetLogLevel((OUTPUT_LEVEL)atoi(sValue));
GetProcValue( "log_file_name",sValue ) ;
m_clSetDayLog.SetDayLogName( sValue , sLogName ) ;
g_clLog.Open( sLogName ) ;
//read send config
GetProcValue( "contrl_send",sValue ) ;
if ( atoi(sValue) == 1 )
m_bIfCtrl = true ;
GetProcValue( "max_batch_send",sValue ) ;
m_nMaxSend = atoi( sValue ) ;
GetProcValue( "dispense_wait_name",m_sDispWaitName ) ;
GetProcValue( "dispense_log_name",m_sDispLogName ) ;
GetProcValue( "dispense_report_name",m_sDispReportName ) ;
//add by yshan
GetProcValue( "dispense_batch_wait_name",m_sDispBatchWaitName ) ;
GetProcValue( "dispense_batch_log_name",m_sDispBatchLogName ) ;
GetProcValue( "dispense_batch_report_name",m_sDispBatchReportName ) ;
//init WSAStartup
WSADATA wsaData;
WORD wVersionRequested;
wVersionRequested = MAKEWORD(2, 2);
if ( WSAStartup(wVersionRequested, &wsaData) != 0 )
{
exit(0);
}
}
void Exit()
{
WSACleanup() ;
XW_Logout() ;
g_clLog.Close() ;
}
CSgipSend g_cSgipSend;
void ProcessBatch(void *)
{
XW_Connection conn;
conn.Login(m_sDBServer);
CDispenseCache cDispenseWait(conn) ;
vector<StruDispense> vstDispense ;
cDispenseWait.SetDispTbName( m_sDispBatchWaitName,m_sDispBatchLogName ) ;
#ifdef _NEWTEST
CStateReport cStateReport(conn) ;
#else
CDispenseReport cDispenseReport(conn) ;
CDispenseReport.SetTbName( m_sDispBatchReportName,m_sDispBatchLogName ) ;
#endif
int nRet ;
long dwStart,dwEnd,nSendCtrlTime ;
short nOpen = 1 ;
int goon = 1 ;
while ( goon )
{
vstDispense.clear() ;
if ( (nOpen=cDispenseWait.GetDispenseCacheEx( vstDispense ,m_nMaxSend,m_bIfCtrl )) > 0 )
{
dwStart = GetTickCount() ;
nRet = g_cSgipSend.SubmitMsg( vstDispense ) ;
#ifdef _NEWTEST
if ( (nOpen=cDispenseWait.InsertDispenseLog( vstDispense )) < 0 )
g_clLog.Error( 0," Fail to Call CDispenseWait::InsertDispenseLog() !" ) ;
if ( !nRet && vstDispense[0].information_id == -2 )
{
if ( (nOpen=cStateReport.InsertReport( vstDispense[0] )) < 0 )
g_clLog.Error( 0," Fail to Call cStateReport::InsertReport() !" ) ;
}
#else
if ( vstDispense[0].information_id == -2 )
{//need state report
if ( (nOpen=cDispenseReport.InsertWait( vstDispense[0] )) < 0 )
g_clLog.Error( 0," Fail to Call cDispenseReport::InsertWait() !" ) ;
}
else
{
if ( (nOpen=cDispenseWait.InsertDispenseLog( vstDispense )) < 0 )
g_clLog.Error( 0," Fail to Call CDispenseWait::InsertDispenseLog() !" ) ;
}
#endif
dwEnd = GetTickCount() ;
if ( vstDispense.size() > 2 )
nSendCtrlTime = 1000 ;
else
nSendCtrlTime = 200 ;
if ( (dwEnd - dwStart) < nSendCtrlTime )
Sleep( nSendCtrlTime - ( dwEnd - dwStart ) ) ;
}
else
Sleep( 1000 ) ;
if ( nOpen < 0 )
{
Sleep( 1000 ) ;
g_clLog.Info( 0, "BatchConnect connect again......!!!" ) ;
conn.Logout() ;
conn.Login( m_sDBServer ) ;
nOpen = 1 ;
}
}
}
int MainLoop()
{
CDispenseWait cDispenseWait ;
vector<StruDispense> vstDispense ;
cDispenseWait.SetTbName( m_sDispWaitName,m_sDispLogName ) ;
#ifdef _NEWTEST
CStateReport cStateReport ;
#else
CDispenseReport cDispenseReport ;
CDispenseReport.SetTbName( m_sDispReportName,m_sDispLogName ) ;
#endif
int nRet ;
long dwStart,dwEnd,nSendCtrlTime ;
short nOpen = 1 ;
int goon = 1 ;
while ( goon )
{
vstDispense.clear() ;
if ( (nOpen=cDispenseWait.GetDispenseWaitEx( vstDispense ,m_nMaxSend,m_bIfCtrl )) > 0 )
{
dwStart = GetTickCount() ;
nRet = g_cSgipSend.SubmitMsg( vstDispense ) ;
#ifdef _NEWTEST
if ( (nOpen=cDispenseWait.InsertDispenseLog( vstDispense )) < 0 )
g_clLog.Error( 0," Fail to Call CDispenseWait::InsertDispenseLog() !" ) ;
if ( !nRet && vstDispense[0].information_id == -2 )
{
if ( (nOpen=cStateReport.InsertReport( vstDispense[0] )) < 0 )
g_clLog.Error( 0," Fail to Call cStateReport::InsertReport() !" ) ;
}
#else
if ( vstDispense[0].information_id == -2 )
{//need state report
if ( (nOpen=cDispenseReport.InsertWait( vstDispense[0] )) < 0 )
g_clLog.Error( 0," Fail to Call cDispenseReport::InsertWait() !" ) ;
}
else
{
if ( (nOpen=cDispenseWait.InsertDispenseLog( vstDispense )) < 0 )
g_clLog.Error( 0," Fail to Call CDispenseWait::InsertDispenseLog() !" ) ;
}
#endif
dwEnd = GetTickCount() ;
if ( vstDispense.size() > 2 )
nSendCtrlTime = 1000 ;
else
nSendCtrlTime = 200 ;
if ( (dwEnd - dwStart) < nSendCtrlTime )
Sleep( nSendCtrlTime - ( dwEnd - dwStart ) ) ;
}
else
{
m_clSetDayLog.SetNextLog();
Sleep( 300 ) ;
}
if ( nOpen < 0 )
{
Sleep( 1000 ) ;
g_clLog.Info( 0, "Connect again......!!!" ) ;
XW_Logout() ;
Login( m_sDBServer ) ;
nOpen = 1 ;
}
}
return 0 ;
}
int main(int argc, char **argv)
{
//catch SIGINT(CTRL+C) signal to process it by myself
signal(SIGINT, SignalHandler);
strcpy( m_sProcName,"smsend" ) ;
switch ( argc )
{
case 2:
//used for reading profile
strcpy( m_sProcName,argv[1] ) ;
break ;
case 3:
//used for reading profile
strcpy( m_sProcName,argv[1] ) ;
g_clProfile.SetFileName(argv[2]);
break;
}
Init() ;
g_clLog.Info(0, "Program \"%s.exe\" start !", m_sProcName);
//add by yshan
CThread batchThread;
batchThread.Start(ProcessBatch,0);
MainLoop() ;
Exit() ;
return 0 ;
}
//////////////////////////////////log
//2003-12-4:by yshan:增加群发程序
//////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -