?? sendun.cpp.bak
字號:
//此程序是中國聯通9020發送程序,實現功能:(號段的判斷)//1、單條發送一般的文本信息//2、發送免提短信//3、鈴聲圖片的發送:從MSGCONTENT中取出3位十進制轉換為二進制后發送出去//4、支持群發功能:當MsgFormat=99時,從指定文件中取出手機號碼,連接到目的手機號上,發送出去//5、有號段的判斷,但號段表中的數據只是本省的,故只能判斷本省。// 但在號段的判斷時考慮比較多,當接收方付費時,判斷接收方的省份,將接收方不是本省的插入到COM_SEND_OTHER中,// 且考慮了第三方付費的情況,將根據不同情況將信息發送出去。可能一條信息會被拆分成兩條:一條接收方,一條付費方// ***所有的信息都會作一次嘗試性發送#include <stdio.h>#include <string.h>#include <time.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <pthread.h>#include "../include/define.h"#include "mysql/mysql.h"#include "../include/sm_tools.h"#include "../include/sm_db.h"#include "../include/sgip.h"#include "send.h"#define MAX_COMMAND 10#define SELF_TYPE "send_9020"void send_sm();int gbtounicode(char *msg,char *result);static int Send_Demo(SEND_STRUC sendstruc,char *demo);pthread_mutex_t mutex;//********************************************************SENDSTRUC *waitheader;SENDSTRUC *waittail;int waitnum;SENDSTRUC *okheader;SENDSTRUC *oktail;//********************************************************//初始化鏈表int initlist(){ waitheader = NULL; waittail = NULL; okheader = NULL; oktail = NULL; waitnum = 0; return 0;}//從已處理的鏈表中取信息,以便將COM_SEND表中的FLAG置為相應的值int getoklist(SENDSTRUC **sendstruc){ SENDSTRUC *point; pthread_mutex_lock(&mutex); if(okheader == NULL) { pthread_mutex_unlock(&mutex); return 0; } point = okheader; okheader = okheader->next; if(okheader == NULL) { oktail = NULL; } *sendstruc = point; pthread_mutex_unlock(&mutex); return 1;}//從等待鏈表中取信息后發送出去int getwaitlist(SENDSTRUC **sendstruc){ SENDSTRUC *point; pthread_mutex_lock(&mutex); if(waitheader == NULL) { pthread_mutex_unlock(&mutex); return 0; } point = waitheader; waitheader = waitheader->next; if(waitheader == NULL) { waittail = NULL; } *sendstruc = point; if(waitnum > 0) { waitnum --; } pthread_mutex_unlock(&mutex); return 1;}//從COM_SEND表中取出信息添加到等待鏈表中,等待發送int addwaitlist(SENDSTRUC *sendstruc){ pthread_mutex_lock(&mutex); if(waitheader == NULL) { waitheader = sendstruc; waittail = sendstruc; } else { waittail->next = sendstruc; waittail = waittail->next; } waittail -> next = NULL; waitnum ++; pthread_mutex_unlock(&mutex); return 1;}//將發送完的記錄加入到OK鏈表中int addoklist(SENDSTRUC *sendstruc){ pthread_mutex_lock(&mutex); if(okheader == NULL) { okheader = sendstruc; oktail = sendstruc; } else { oktail->next = sendstruc; oktail = oktail->next; } oktail -> next = NULL; pthread_mutex_unlock(&mutex); return 1;}//***********************************************************//***********************************************************#define INIFILE "../config/sm.ini"#define INIFILE_UN "../config/sms_130.ini"static int MAX_WAITLIST;static int THREAD_NUM;static char PROVINCE[10];static char FFSTRING[2000][500];static int FFSTR_NUM;char db_host[100],db_user[100],db_passwd[100],db_dbname[100];MYSQL *myconn;//變量的初始化int InitPara(){ myconn = NULL; MAX_WAITLIST = GetProfileInt(INIFILE,"SEND","MAX_WAITLIST",100); THREAD_NUM = GetProfileInt(INIFILE,"SEND","THREAD_NUM",6); GetProfileString(INIFILE,"DB_LOCAL","HOST","localhost",db_host); GetProfileString(INIFILE,"DB_LOCAL","LOGNAME","root",db_user); GetProfileString(INIFILE,"DB_LOCAL","LOGPASS","",db_passwd); GetProfileString(INIFILE,"DB_LOCAL","DBNAME","NW_SM_TEST",db_dbname); GetProfileString(INIFILE,"HD","PROVINCE","",PROVINCE); return 0;}int main(int argc,char * argv[]){ char buf[100]; int i,retval1; pthread_t thread_id; char sql[500]; MYSQL_ROW row; MYSQL_RES *res = NULL; InitPara(); //連接數據庫 myconn = dbconnect(db_host,db_user,db_passwd,db_dbname); if(myconn == NULL) { puts("連接數據庫失敗,請查看日志文件!"); return 0; } puts(db_dbname); //初始化數據庫,將FLAG為90的置為0 if(DoSQL("UPDATE COM_SEND SET FLAG='0' WHERE ICPID='UN' AND FLAG='90'",&myconn)!=0) { writelog(SELF_TYPE,"初始化Send表出錯!"); } //初始化鏈表 initlist(); //初始化API函數 if(InitSGIPAPI(INIFILE_UN) != 0) { puts("初始化CMPPAPI失敗!"); writelog(SELF_TYPE,"初始化CMPPAPI失敗!"); return 0; } //將非法字符調入內存 i=0; sprintf(sql,"SELECT STR_FIND FROM WEB_WORDS"); retval1 = QuerySQL(sql,&myconn,&res); if(retval1 != 0) { writelog(SELF_TYPE,"查詢指令出錯"); return 0; } if(res == NULL) { return 0; } while(row = mysql_fetch_row(res)) { strcpy(FFSTRING[i],trim(row[0],' ')); //puts(FFSTRING[i]); i=i+1; } mysql_free_result(res); FFSTR_NUM=i; printf("非法字符數量%d",FFSTR_NUM); //非法字符調入內存 end; pthread_mutex_init(&mutex,NULL); //創建發送進程 for(i=0;i<THREAD_NUM;i++) { pthread_create(&thread_id, NULL, (void* (*)(void*))send_sm, NULL); } if(DoThread() != 0) { puts("查詢信息出錯!"); return 0; } return 0;}//從COM_SEND表中取出記錄加入到等待列表中等待發送int DoThread(){ int retval; while(1) { retval = ScanMsg(); if(retval <= 0) { sleep(2); continue; } usleep(10); }}int ScanMsg(){ char sql[500],day[20]; MYSQL_ROW row,row2; MYSQL_RES *res = NULL,*res2 = NULL; SENDSTRUC *com_buf; SEND_STRUC sendstruc; int retval,retval2,i,slen,j,flag1,flag2,retval1,pos,x,y,z,ffstrlen,ffstrtemplen,retval3; char result[1001],temp[10],errmsg[100]; float feecode; char msg[1001],ffstr[500],ffstrtemp[500],ffbuf[500]; memset(sql,0,sizeof(sql)); //處理發送過的數據,從發送完的鏈表中取出信息,將FLAG的值置為其發送后的返回值,并釋放所占空間 while(getoklist(&com_buf)) { sprintf(sql, "UPDATE COM_SEND SET FLAG='%d',DEAL_DATE='%s' WHERE ID=%s", com_buf->flag,getnowdate(day),com_buf->id); if(DoSQL(sql,&myconn) != 0) { free(com_buf); writelog(SELF_TYPE,"更新數據庫出錯!"); return 0; } free(com_buf); } if(waitnum > MAX_WAITLIST) { return 0; } //從COM_SEND表中要發送的聯通用戶的記錄FLAG=0:未處理,FLAG=89:已處理,但發送時隊列滿,要重發 sprintf(sql, "SELECT ID,NEEDREPLY,MSGLEVEL,SERVICEID,MSGFORMAT,FEETYPE,FEECODE,VALIDTIME,ATTIME,SRCTERMID, DESTTERMID,MSGCONTENT,FEEUSERTYPE,FEETERMINALID, TPPID,TPUDHI,LINKID FROM COM_SEND WHERE ICPID='UN' AND (FLAG='0') ORDER BY STEP,ID LIMIT 0,%d",MAX_COMMAND); retval = QuerySQL(sql,&myconn,&res); if(retval != 0) { writelog(SELF_TYPE,"查詢指令出錯COM_SEND,FLAG=0"); puts("com_send失敗"); return 0; } if(res == NULL) { return 0; } retval1 = 0; while(row = mysql_fetch_row(res)) { retval1 ++; com_buf = (SENDSTRUC *) malloc(sizeof(SENDSTRUC)); if(com_buf == NULL) { writelog(SELF_TYPE,"申請內存失敗!"); mysql_free_result(res); return 0; } //對鏈表變量賦值 puts(com_buf->id); strcpy(com_buf->id,row[0]); com_buf->nNeedReply = atoi(row[1]); com_buf->nMsgLevel = atoi(row[2]); strcpy(com_buf->sServiceID,row[3]); com_buf->nMsgFormat = atoi(row[4]); strcpy(com_buf->sFeeType,row[5]); feecode=atof(row[6])*100; sprintf(com_buf->sFeeCode,"%06d",(int)feecode); strcpy(com_buf->sValidTime,""); strcpy(com_buf->sAtTime,""); strcpy(com_buf->sSrcTermID,row[9]); strcpy(com_buf->sDesTermID,row[10]); strcpy(com_buf->sMsgContent,row[11]); com_buf->nMsgLen = strlen(com_buf->sMsgContent); strcpy(com_buf->sMsgID,com_buf->id); com_buf->cFeeUserType = atoi(row[12]); strcpy(com_buf->sFeeTerminalID,row[13]); com_buf->cTpPid=atoi(row[14]); com_buf->cTpUdhi=atoi(row[15]); strcpy(com_buf->linkid,row[16]); puts(com_buf->linkid); puts(com_buf->sDesTermID); //對特定用戶實行免費發送 begin 20031013 xcy sprintf(sql,"SELECT MSISDN FROM INFO_FREE_PHONE WHERE MSISDN='%s' AND FLAG='1'",com_buf->sDesTermID); printf("%s\n",sql); retval2 = QuerySQL(sql,&myconn,&res2); if(retval2 != 0) { writelog(SELF_TYPE,"查詢指令出錯INFO_FREE_PHONE"); return 0; } if(res2 == NULL) { return 0; } if (row2 = mysql_fetch_row(res2)) { if (strcmp(com_buf->sFeeType,"02")==0) { strcpy(com_buf->sFeeType,"01"); } else if (strcmp(com_buf->sFeeType,"03")==0 && com_buf->nNeedReply==2) { mysql_free_result(res2); sprintf(sql, "UPDATE COM_SEND SET FLAG='1' WHERE ID=%s",com_buf->id); if(DoSQL(sql,&myconn) != 0) { free(com_buf); writelog(SELF_TYPE,"更新數據庫出錯!"); return 0; } free(com_buf); continue; } } mysql_free_result(res2); //對特定用戶實行免費發送 end 20031013 xcy //對結構體變量賦值,用于插入COM_SEND_OTHER表 sendstruc.nNeedReply = atoi(row[1]); sendstruc.nMsgLevel = atoi(row[2]); strcpy(sendstruc.sServiceID,row[3]); sendstruc.nMsgFormat = atoi(row[4]); strcpy(sendstruc.sFeeType,row[5]); sendstruc.sFeeCode=atof(row[6]); strcpy(sendstruc.sValidTime,""); strcpy(sendstruc.sAtTime,""); strcpy(sendstruc.sSrcTermID,row[9]); strcpy(sendstruc.sDesTermID,row[10]); strcpy(msg,row[11]); trim(msg,' '); //判斷發送內容的長度是否超過500,若超過了500,則不發送,直接置為1,并寫LOG begin 2003.06.25 邢 if (strlen(msg)>500) { sprintf(sql,"UPDATE COM_SEND SET FLAG='1',DEAL_DATE='%s' WHERE ID=%s", getnowdate(day),com_buf->id); if(DoSQL(sql,&myconn) != 0) { free(com_buf); writelog(SELF_TYPE,"更新數據庫出錯!"); mysql_free_result(res); return 0; } sprintf(errmsg,"id:%s,srctermid:%s,serviceid:%s",com_buf->id,com_buf->sDesTermID,com_buf->sServiceID); writelog(SELF_TYPE,errmsg); free(com_buf); continue; } //判斷發送內容的長度是否超過500,若超過了500,則不發送,直接置為1,并寫LOG end 2003.06.25 邢 strcpy(sendstruc.sMsgContent,msg); sendstruc.cFeeUserType = atoi(row[12]); strcpy(sendstruc.sFeeTerminalID,row[13]); sendstruc.cTpPid=atoi(row[14]); sendstruc.cTpUdhi=atoi(row[15]); strcpy(sendstruc.sMsgContent,row[16]); /*當內容中含有非法字符時進行處理*/ strcpy(ffstr,""); strcpy(ffbuf,""); strcpy(ffstr,com_buf->sMsgContent); ffstrlen=strlen(ffstr); puts(ffstr); printf("ffstr is %d",ffstrlen); pos=0; x=0; while(pos<=ffstrlen) { //掃描字符串 y=0; z=0; while(z<FFSTR_NUM) { strcpy(ffstrtemp,""); strcpy(ffstrtemp,FFSTRING[z]); ffstrtemplen=strlen(ffstrtemp); if(strncmp(ffstr + pos,ffstrtemp,ffstrtemplen) == 0) { ffbuf[x++]='*'; pos+=ffstrtemplen; y=1; break; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -