⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendsms.cpp

📁 一个在linux下连接Sybase数据库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			switch(MaxStrategy[SAlarmsTable[i].ATable.AlarmCode.type].AlarmMode)
			{
			case 0:
				{
					if((unsigned long)time(0)>SAlarmsTable[i].Count)
					{
						SAlarmsTable[i].Count=0;
						SendGSMOntime(&SAlarmsTable[i]);
					}
				}
				break;
			case 1:
				{
					if(SAlarmsTable[i].Count>(unsigned long)MaxStrategy[SAlarmsTable[i].ATable.AlarmCode.type].DelayCount)
					{
						SAlarmsTable[i].Count=0;
						SendGSMOntime(&SAlarmsTable[i]);
					}
				}
				break;
			}
		}
	}
	AManage.DetectIdleTime();
	StrategyUpdate();
/*	static int i;
	if(i>Cfg.InterVal)
	{
//		WriteRealtimeStatus();
		i=0;
	}i++;
*/	
}
int SendSms::SendGSMOntime(_SAlarmsTable * pTable)
{
	char *lpTemp=MaxStrategy[pTable->ATable.AlarmCode.type].MSISDN_AlarmList;
	while(lpTemp)
	{
	
		if(*lpTemp!='+'&&!(*lpTemp>='0'&&*lpTemp<='9'))//判断数字的合法性
			return 0;
		unsigned long MaxSN=time(0);
		static unsigned long i;
		GSMOntime Sms;
		memset(&Sms,0,sizeof(Sms));
		sprintf(Sms.ACCOUNT,"joyit@zyj%d",(int)MaxSN);
		sscanf(lpTemp,"%[^,]",Sms.CALLED);
		lpTemp=strstr(lpTemp,",");
		lpTemp=lpTemp?lpTemp+1:0;
		memcpy(Sms.CONTENT,pTable->ATable.Info,sizeof(Sms.CONTENT));
		sprintf(Sms.SRVPARAM,"%08d",(int)(MaxSN+i));
		sprintf(Sms.SERVICEID,"%s",Cfg.MSISDN);
		Sms.REGIDX=MaxSN+i;
		strcpy(Sms.CURSRVDT,"zyj@joyit.com.cn");
		Sms.RESENDCOUNT=2;
		int ret=::SendGSMOntime(&Sms,0);
		if(0==ret)
		{
			SMS *pMs=(SMS*)malloc(sizeof(SMS));
			if(pMs)
			{
				memcpy(&pMs->STable,&pTable->ATable,sizeof(pMs->STable));
				AManage.Add(MaxSN+i,pMs,Cfg.MaxTime);
				i++;
			}
#if TEST
			printf("告警表处理成功 type=%d:%s\n",(int)pMs->STable.AlarmCode.type,getLastError());
#endif
		}
		else
		{
			fprintf(stderr,"%s--SendGSMOntime function Error%s\n",GetCurrTime(),getLastError());
		}
		CommitWork();
	}
	return 0;
}

int SendSms::GetUpdate()
{
	disconnectDb(0);
	connectDb("sa","","SYBASE");
	unsigned long long DateTime[2];
	while(ReadAlarmsTable()==1)
	{
		DateTime[0]=StringTimeToInt(MaxSTable[STable.AlarmCode.type].DateTime);
		DateTime[1]=StringTimeToInt(STable.DateTime);
		if(DateTime[0]<DateTime[1])
		{
			STableState[STable.AlarmCode.type]=1;
			memcpy(&MaxSTable[STable.AlarmCode.type],&STable,sizeof(STable));
		}
	}
	while(ReadSMSendStrategy()==1)
	{
		memcpy(&MaxStrategy[Strategy.InfoType&15],&Strategy,sizeof(Strategy));
	}
	
#if TEST
	GSMOntime *pOntime;
	while((pOntime=ReadGSMOntime()))
	{
		GSMRECV Gsmrecv;
		memcpy(Gsmrecv.ser,pOntime->SRVPARAM,sizeof(Gsmrecv.ser));
		Gsmrecv.test=1;
		memcpy(Gsmrecv.mess,pOntime->CONTENT,sizeof(Gsmrecv.mess));
		memcpy(Gsmrecv.tel,pOntime->ACCOUNT,sizeof(Gsmrecv.tel));
		Gsmrecv.mtype=1;//rand()&1;
		if(0!=WriteGSMRecvTable(&Gsmrecv))
			printf("发送短信失败:%s\n",getLastError());
		else
			printf("成功写入RecvTable表\n");
	}
#endif
	while(ReadGSMRecvTable()==1)
	{
		int SN[2];
		SN[0]=atoi(GsmRecv[0].ser);
		SN[1]=atoi(GsmRecv[1].ser);
		if(SN[0]>SN[1])
		{
			memcpy(&GsmRecv[1],GsmRecv,sizeof(GsmRecv[0]));
			if(1==GsmRecv[1].mtype)
			{
				if(-1==WriteSendRecord())
				{
					fprintf(pLogFile,"%s --write SendRecord error:%s\n",GetCurrTime(),getLastError());
				}
			}
			else
			{
				fprintf(pLogFile,"%s --SMS Send error",GetCurrTime());
#if TEST
				printf("短信发送失败,并不写入结果表\n");
#endif
			}
		}
	}
	
	DeleteTable();
	disconnectDb(0);
	return 0;
}
void SendSms::DetectIdleTime()
{
	for(int i=0;i<16;i++)
	{
		if(0==SAlarmsTable[i].Strategy.AlarmMode&&TypeState[i]&&(unsigned long)time(0)>SAlarmsTable[i].Count)
		{
			SendMessage((SMS*)&SAlarmsTable[i]);
			SAlarmsTable[i].Count=~0;
			TypeState[i]=0;
		}
	}
}
int SendSms::CountDispose(SMS *pSMS)
{
	_SAlarmsTable *lpSAlarm=&SAlarmsTable[pSMS->STable.AlarmCode.type];
	memcpy(&lpSAlarm->ATable,&pSMS->STable,sizeof(AlarmsTable));
	memcpy(&lpSAlarm->Strategy,&pSMS->Strategy,sizeof(SendStrategy));
	if(++lpSAlarm->Count>=(unsigned long)pSMS->Strategy.DelayCount)
	{
		lpSAlarm->Count=0;
		if(!SendMessage(pSMS))
			return 0;
	}
#if TEST
	else
	{
		printf("短信并没有达到策略表的数量短信正在积累中!\n");
	}
#endif	
	return -1;
}
void SendSms::StrategyUpdate()
{
	static unsigned long EndTime=time(0)+Cfg.StrategyTime;
	unsigned long CurrTime=time(0);
	if(CurrTime>EndTime)
	{
		InitStrategy();
		EndTime=time(0)+Cfg.StrategyTime;
	}
}
Manage::Manage()
{
	pInfo=(LPOPERA)malloc(sizeof(OPERA));
	memset(pInfo,0,sizeof(OPERA));
	pInfo->Next=(LPOPERA)malloc(sizeof(OPERA));	
	memset(pInfo->Next,0,sizeof(OPERA));
}
Manage::~Manage()
{
	LPOPERA ptr=pInfo;
	LPOPERA lp;
	while(ptr)
	{
		lp=ptr->Next;
		if(ptr->lpvoid)free(ptr->lpvoid);
		delete ptr;
		ptr=lp;
	}
}

void *Manage::operator[](int type)
{
	LPOPERA ptr=pInfo->Next;
	while(ptr)
	{
		if(ptr->Type==type)
			return ptr->lpvoid;
		ptr=ptr->Next;
	}
	return 0;
}
void *Manage::operator -(int type)
{
	LPOPERA ptr=pInfo->Next;
	while(ptr)
	{
		if(ptr->Type==type)
		{
			LPOPERA p1=pInfo;
			while(p1->Next!=ptr)
				p1=p1->Next;
			p1->Next=ptr->Next;
			void *lp=ptr->lpvoid;
			free(ptr);
			return lp;
		}
		ptr=ptr->Next;
	}
	return 0;
}
LPOPERA Manage::ASeekToEnd()
{
	LPOPERA lp=pInfo->Next;
	while(lp->Next)
		lp=lp->Next;
	return lp;
}
void Manage::DetectIdleTime()
{
	LPOPERA ptr=pInfo->Next;
	unsigned long Time=time(0);
	while(ptr)
	{
		if(ptr->EndTime&&Time>ptr->EndTime)
		{
#if TEST
			printf("发现一个操作超时:序号=%d\n",ptr->Type);
#endif
			void *lp=(*this)-ptr->Type;
			if(lp)free(lp);
		}
		ptr=ptr->Next;
	}
}
void* Manage::Add(int type,void *lpvoid,unsigned long Time)
{
	if((*this)[type])
		return 0;
	LPOPERA lp=ASeekToEnd();
	lp->lpvoid=lpvoid;
	lp->EndTime=time(0)+Time;
	lp->Next=(LPOPERA)malloc(sizeof(OPERA));
	if(!lp->Next)
		return 0;
	lp->Type=type;
	if(lp->Next)
	{
		memset(lp->Next,0,sizeof(OPERA));
	}
	return lp->Next;
}
int SendSms::WriteSendRecord()
{
	SMSendRecord Record;
	SMS *lpOntime=(SMS*)AManage[atoi(GsmRecv[1].ser)];
	if(lpOntime)
	{
		Record.AlarmCode=lpOntime->STable.AlarmCode.type;
		Record.InfoType=lpOntime->Strategy.InfoType;
		memcpy(Record.Info,lpOntime->STable.Info,sizeof(Record.Info));
		memcpy(Record.SendTime,lpOntime->STable.DateTime,sizeof(Record.SendTime));
		if(::WriteSendRecord(&Record,0)==0)
		{
#if TEST
			printf("--成功插入告警表Table \n");
#endif
			GetRecord();//更新告警表记录
		}
#if TEST		
		else
			printf("--失败插入告警表Table \n");
#endif		
		AManage-atoi(GsmRecv[1].ser);
		free(lpOntime);
		return 0;
	}
	return 1;
}
int SendSms::WriteSendRecord(unsigned long Sn)
{
	SMSendRecord Record;
	SMS *lpOntime=(SMS*)AManage[Sn];
	if(lpOntime)
	{
		Record.AlarmCode=lpOntime->STable.AlarmCode.code;
		Record.InfoType=lpOntime->STable.AlarmCode.type;
		memcpy(Record.Info,lpOntime->STable.Info,sizeof(Record.Info));
		memcpy(Record.SendTime,lpOntime->STable.DateTime,sizeof(Record.SendTime));
		if(::WriteSendRecord(&Record,0)==0)
		{
#if TEST
			printf("--成功插入告警表Table \n");
#endif
			GetRecord();//更新告警表记录
		}
#if TEST
		else
			printf("--失败插入告警表Table %s\n",getLastError());
#endif
		CommitWork();
		AManage-Sn;
		free(lpOntime);
		return 0;
	}
	return 1;
}

void SendSms::InitStrategy()
{
	memset(MaxStrategy,0,sizeof(MaxStrategy));
	while(ReadSMSendStrategy()==1)
	{
		memcpy(&MaxStrategy[Strategy.InfoType&15],&Strategy,sizeof(Strategy));
	}
}
void SendSms::InitRecvTable()
{
	while(ReadGSMRecvTable()==1)
	{
		int SN[2];
		SN[0]=atoi(GsmRecv[0].ser);
		SN[1]=atoi(GsmRecv[1].ser);
		if(SN[0]>SN[1])
		{
			memcpy(&GsmRecv[1],GsmRecv,sizeof(GsmRecv[0]));
		}
	}
}
void SendSms::InitAlarmsTable()
{
	unsigned long DateTime[2];
	while(ReadAlarmsTable()==1)
	{
		DateTime[0]=StringTimeToInt(MaxSTable[STable.AlarmCode.type].DateTime);
		DateTime[1]=StringTimeToInt(STable.DateTime);
		if(DateTime[0]<DateTime[1])
		{
			memcpy(&MaxSTable[STable.AlarmCode.type],&STable,sizeof(MaxSTable[STable.AlarmCode.type]));
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -