程序内容:话单预警机制话单重发模块

发表于:2007-05-25来源:作者:点击数: 标签:程序话单重发预警内容
/* 程序名称:recordresend.c 程序内容:话单预警机制话单重发模块 程序作者:周继国 完成日期:2004年05月27日 版权所有:上海新动信息有限公司 cc recordresend.c -I/usr/local/include/ mysql -lmysqlclient -L/usr/local/lib/mysql -o recordresend */ #

/*
   程序名称:recordresend.c
   程序内容:话单预警机制话单重发模块
   程序作者:周继国
   完成日期:2004年05月27日
   版权所有:上海新动信息有限公司
   clearcase/" target="_blank" >cc  recordresend.c  -I/usr/local/include/mysql -lmysqlclient -L/usr/local/lib/mysql -o recordresend
*/

#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <mysql.h>
#include <mysqld_error.h>
#include <my_config.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <unistd.h>
#include <time.h>
#include <md5.h>
#include <stdarg.h>
//#include <signal.h>

//定义配置数据类型
struct INITDATA
{
 char sLogHostName[256];
 char sLogDBName[256];
 char sLogDBUserName[256];
 char sLogDBPassWord[256];
 char sLogTableName[256];
 char sResendHostName[256];
 char sResendDBName[256];
 char sResendDBUserName[256];
 char sResendDBPassword[256];
 char sResendTable[256];
 char sServiceCode[256];
 char sProvider[256];
 char sSender[256];
 char sState[256];
};

int GetMonthAndDay(MYSQL mysql,char *mobile_date)
{
 MYSQL_RES *result;
 char query_string[1024];
 MYSQL_ROW row;
 int num_row;

 strcpy(query_string,"select DATE_FORMAT(now(),'%Y-%m')");
 mysql_real_query(&mysql,query_string,strlen(query_string));
 result = mysql_store_result(&mysql);
 row = mysql_fetch_row(result);
 strcpy(mobile_date,row[0]);
 mysql_free_result(result);
 return 0;
}

void CurTimeF(char* sTime)
{
 time_t t;
 struct tm* tm_buf;
 t = time(0);
 tm_buf = localtime(&t);
 sprintf(sTime, "%02d:%02d:%02d",tm_buf->tm_hour,tm_buf->tm_min,tm_buf->tm_sec);
}

/************
Name : PubAlltrimStr()
Paras : char *str 
Usage : delete the character that you not see. 
Return : char * 
************/
char* PubAlltrimStr( char *str )              
{
 int  len;
 int  i;
 char *s;
 char filterchars[8];

 bzero(filterchars, sizeof(filterchars));
 strcpy(filterchars, " \t\r\n");
 len = strlen(str);
 
 for(i = len-1; i >= 0; i--)
 {
  if( strchr((char*)filterchars, str[i]) == NULL )
  {
   str[i+1] = '';
   break;
  }
 }

 s = str;
 for(i = 0; i < len; i++)
 {
  if( strchr((char*)filterchars, *s) != NULL ) 
  {
   s++;
  }
  else
  {
   break;
  }
 }

 if(s != str)
  memmove(str, s, strlen(s) + 1);

 return str;
}

/************
Name : PubReadCfg()
Paras : char *filename,char *option,int flag, flag > 0;
Usage : get the option from the config file 
Return : int . 0 successfully,1 have the option in the config file but no configured ,
     -100 can not open configure file, -1 not found
***********/
int PubReadCfg(char *filename, char *option, int flag)
{
 char str[255+1];
 char *fret;
 int i=0;
 int ret=-1;
 int isequl=0;
 int count=0;
 char head[254];
 char tail[254];
 FILE *fp;
 int  len_str;

 fp = fopen(filename, "r");
 if (fp == NULL)
 {
  printf("\nCan not open %s!\n", filename);
  return -100;
 }
 while(1)
 {
  memset(str, '', sizeof(str));
  memset(head, '', sizeof(head));
  memset(tail, '', sizeof(tail));
  fret = fgets(str, 255, fp);
  if(fret == NULL)
   break;
  if( strlen(str) >= 255 )
  {
   printf(" %s: fromat error!\n",filename);
   break;
  }
  len_str = strlen(str);
  for(i=0; i<len_str; i++)
  {
   if (str[i]  ==  '=')
    break;
  }
  if(i >= len_str - 1)
  {
   continue; 
  }
  strncpy(tail, str+i+1, strlen(str)-i-1);
  if(strlen(tail) != 0)
  {
   memset(&str[i], '', strlen(tail));
  }
  else
  {
   memset(&str[i], '', 1);
  }
  PubAlltrimStr(&str[0]);
  strcpy(head, &str[0]);    
  PubAlltrimStr(option);
  isequl=strcmp(option, head);
  if (isequl != 0)
   continue;            
  else
  {
   count++;
   if(count == flag)
   {
    if( strlen(tail) == 0 )
    {
     memset(option, '', sizeof(option));
     ret = 1;
    }
    else
    {
     memset(option, '', sizeof(option));
     PubAlltrimStr(tail);
     strcpy(option, tail);            
     ret = 0;                        
    }
    PubAlltrimStr(option);
    break;
   }
   else
    continue;
  }
 }
 fclose(fp);
 if (ret == -1)
  memset(option, '', sizeof(option));

 return(ret);
}

void log (
 char *sLogFileName,
 char* fmt,  // 格式化字符串
 ...    // 可变参数
 )
{
 FILE *fp;
 va_list ap;
 char sLog[1024]; // 错误记录字符串
 char sErrDesc[1024];
 char sTime[20];
 char szDate[9];

 // 取得当前时间
 CurTimeF(sTime);

 // 生成错误记录字符串
 strcpy(sLog, sTime); // 当前时间
 strcat(sLog, "\t");

 va_start(ap, fmt);
 vsprintf(sErrDesc, fmt, ap);
 va_end(ap);
 strcat(sLog, sErrDesc);
 if(sErrDesc[strlen(sErrDesc) - 1] != '\n')
  strcat(sLog, "\n");
 
// CurDateS(szDate);
// strcat(szPathName, "/home/richard/program/dzwww/sgip/log");
// strcat(szPathName, szDate);
 
 fp = fopen(sLogFileName,"a+");
 while(fp == NULL){
  fp = fopen(sLogFileName,"a+");
  sleep(1);
 }
 fputs(sLog,fp);
 fclose(fp);
 
}

void SigHandler(int sig)
{
 int  DeadStatus;
 pid_t DeadPid; 
 switch(sig)
 {
 case SIGUSR2:
  break;
 case SIGPIPE:
  break;
// case SIGCLD:
//  while((DeadPid = waitpid(-1, &DeadStatus, WNOHANG)) > 0)
//  break;
 default:
  break;
 }
}

int InitSignal(void)
{
 struct sigaction act;
 struct sigaction oact;

 act.sa_handler = SIG_IGN;
 sigemptyset(&act.sa_mask);
 sigaddset (&act.sa_mask, SIGINT);
 act.sa_flags = 0;
 if (sigaction(SIGINT, &act, &oact)  == -1)
 {
  perror("install SIGINT failed: ");
  return -1;
 }

 act.sa_handler = SIG_IGN;
 act.sa_flags = 0;
 sigemptyset(&act.sa_mask);
 sigaddset (&act.sa_mask, SIGHUP);
 if (sigaction(SIGHUP, &act, &oact)  == -1)
 {
  perror("install SIGHUP failed: ");
  return -1;
 }

 act.sa_handler = SIG_IGN;
 act.sa_flags = 0;
 sigemptyset(&act.sa_mask);
 sigaddset (&act.sa_mask, SIGQUIT);
 if (sigaction(SIGQUIT, &act, &oact)  == -1)
 {
  perror("install SIGQUIT failed: ");
  return -1;
 }

 act.sa_handler = SIG_IGN;
 act.sa_flags = 0;
 sigemptyset(&act.sa_mask);
 sigaddset (&act.sa_mask, SIGTERM);
 if (sigaction(SIGTERM, &act, &oact)  == -1)
 {
  perror("install SIGTERM failed: ");
  return -1;
 }

 
 act.sa_handler = &SigHandler;
 act.sa_flags = 0;
 sigemptyset(&act.sa_mask);
 sigaddset (&act.sa_mask, SIGUSR2);
 if (sigaction(SIGUSR2, &act, &oact)  == -1)
 {
  perror("install SIGALRM failed: ");
  return -1;
 }

 act.sa_handler = &SigHandler;
 act.sa_flags = 0;
 sigemptyset(&act.sa_mask);
 sigaddset (&act.sa_mask, SIGPIPE);
 if (sigaction(SIGPIPE, &act, &oact)  == -1)
 {
  perror("install SIGPIPE failed: ");
  return -1;
 }
}

int main()
{
 MYSQL_RES *resResult,*resResult1;
    MYSQL_ROW rowRow,rowRow1; 
 MYSQL mysqlLog,mysqlResend;
 char sQueryString[1024],sTempString[256],sTempString2[256];
 int iRowNumber;
 struct INITDATA stInitData;
 int iReturnCode;
 char sLogFileName[256],sConfigFileName[256];
 char stime[20];
 char fmt[256];
 char sendtime[30];
 
 
 InitSignal();
 strcpy(sLogFileName,"recordresend.log");
 strcpy(sConfigFileName,"recordresend.ini");
 strcpy(sTempString,"LogHostName");
 
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sLogHostName,sTempString);
 strcpy(sTempString,"LogDBName");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sLogDBName,sTempString);
 strcpy(sTempString,"LogDBUserName");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sLogDBUserName,sTempString);
 strcpy(sTempString,"LogDBPassWord");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sLogDBPassWord,sTempString);
 strcpy(sTempString,"LogTableName");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sLogTableName,sTempString);
 strcpy(sTempString,"ResendHostName");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sResendHostName,sTempString);
 strcpy(sTempString,"ResendDBName");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sResendDBName,sTempString);
 strcpy(sTempString,"ResendDBUserName");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sResendDBUserName,sTempString);
 strcpy(sTempString,"ResendDBPassword");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sResendDBPassword,sTempString);
 strcpy(sTempString,"ResendTable");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sResendTable,sTempString);
 strcpy(sTempString,"ServiceCode");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sServiceCode,sTempString);
 strcpy(sTempString,"Provider");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sProvider,sTempString);
 strcpy(sTempString,"Sender");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
  strcpy(stInitData.sSender,sTempString);
 strcpy(sTempString,"State");
 iReturnCode = PubReadCfg( sConfigFileName, sTempString, 1 );
 if( iReturnCode == -100 )
 {
  strcpy(fmt,"Open configure file error!\n");
  log(sLogFileName,fmt);
  return -1;
 }
 if(iReturnCode == 0)
 strcpy(stInitData.sState,sTempString);

 //创建话单日志表连接
 if(!mysql_init(&mysqlLog))
 {
  strcpy(fmt,"创建话单日志表句柄失败!\n");
  log(sLogFileName,fmt);
  return -1;
 }

    if(!mysql_real_connect

(&mysqlLog,stInitData.sLogHostName,stInitData.sLogDBUserName,stInitData.sLogDBPassWord,stInitData.sLogDBName,0,NULL,0))
    {
     mysql_close(&mysqlLog);
     strcpy(fmt,"创建话单日志表连接失败!\n");
  log(sLogFileName,fmt);
     return -1;
    }

 //创建重发话单表连接   
    if(!mysql_init(&mysqlResend))
 {
     strcpy(fmt,"创建重发话单表句柄失败!\n");
  log(sLogFileName,fmt);
  return -1;
 }
    if(!mysql_real_connect

(&mysqlResend,stInitData.sResendHostName,stInitData.sResendDBUserName,stInitData.sResendDBPassword,stInitData.sResendDBName,0

,NULL,0))
    {
     mysql_close(&mysqlResend);
  strcpy(fmt,"创建重发话单表连接失败!\n");
  log(sLogFileName,fmt);
     return -1;
    }
   
    GetMonthAndDay(mysqlResend,sendtime);

 //查询话单日志表
 memset(sQueryString, 0, sizeof(sQueryString));
 strcat(sQueryString,"select * from ");
 strcat(sQueryString,stInitData.sLogDBName);
 strcat(sQueryString,".");
 strcat(sQueryString,stInitData.sLogTableName);
 strcat(sQueryString," where ss_provider = '");
 strcat(sQueryString,stInitData.sProvider);
 strcat(sQueryString,"' and ss_submit_time like '");
 strcat(sQueryString,sendtime);
 strcat(sQueryString,"%' and ss_send = '0' and ");
 strcat(sQueryString,stInitData.sState);
 if(strlen(stInitData.sServiceCode)!=0)
 {
  strcat(sQueryString," and ss_service_code = '"); 
  strcat(sQueryString,stInitData.sServiceCode);   
  strcat(sQueryString,"'");
 }    
 mysql_real_query(&mysqlLog, sQueryString, strlen(sQueryString));
 resResult = mysql_store_result(&mysqlLog);

 log(sLogFileName,"%s\n",sQueryString);
 
 //更新话单日志表、重新发送表
 iRowNumber = (unsigned int)(mysql_num_rows(resResult));
 if ( iRowNumber != 0)
 {
  while( (rowRow = mysql_fetch_row(resResult)) != NULL)
  {
   //更新话单日志表
   
   memset(sQueryString, 0, sizeof(sQueryString));
   strcat(sQueryString,"update ");
   strcat(sQueryString,stInitData.sLogDBName);
   strcat(sQueryString,".");
   strcat(sQueryString,stInitData.sLogTableName);
   strcat(sQueryString," set ss_send = '1' where ss_service_code = '");
   strcat(sQueryString,rowRow[0]);
   strcat(sQueryString,"' and ss_mobile = '");
   strcat(sQueryString,rowRow[1]);
   strcat(sQueryString,"'  and ss_provider = '");
   strcat(sQueryString,stInitData.sProvider);
   strcat(sQueryString,"' and ss_submit_time like '");
   strcat(sQueryString,sendtime);
   strcat(sQueryString,"%' and ss_send = '0' and ");
   strcat(sQueryString,stInitData.sState);
     mysql_real_query(&mysqlLog, sQueryString, strlen(sQueryString)); 
  //  strcpy(fmt,sQueryString);
     // strcat(fmt,"\n");
   log(sLogFileName,"%s\n",sQueryString);

     //更新话单重发表
     memset(sQueryString, 0, sizeof(sQueryString));
     strcat(sQueryString,"insert into ");
     strcat(sQueryString,stInitData.sResendDBName);
     strcat(sQueryString,".");
     strcat(sQueryString,stInitData.sResendTable);
     strcat(sQueryString," values(NULL,'");
     strcat(sQueryString,rowRow[0]);
     strcat(sQueryString,"','");
     strcat(sQueryString,rowRow[1]);
     strcat(sQueryString,"','");
     strcat(sQueryString,rowRow[4]);
     strcat(sQueryString,"','");
     strcat(sQueryString,stInitData.sSender);
     strcat(sQueryString,"','");
     strcat(sQueryString,rowRow[3]);
     strcat(sQueryString,"',now())");
     mysql_real_query(&mysqlResend, sQueryString, strlen(sQueryString));
     
   log(sLogFileName,"%s\n",sQueryString);
  }
  
  mysql_free_result(resResult);
 }   
 
 
 mysql_close(&mysqlLog);
 mysql_close(&mysqlResend);
 return 0;

原文转自:http://www.ltesting.net

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)