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

📄 gatewayinit.cpp.bak

📁 SMS gateway. SMS protocol for CHINA mobile, unicom, lingtong. Using mysql to exchange message.
💻 BAK
字号:
/**********************************************************************  FileName            : GateWayInit.cpp  Description         : 网关初始化函数集  Version             : 1.0  Date                : 2003年6月17日  Author              : 刘荣辉  Other               : ***********************************************************************/#include "GateWay.h"/*本文件包含以下函数:CGateWay::CGateWay()CGateWay::~CGateWay()void CGateWay::InitAsDaemon()int CGateWay::Initialize();  //初始化函数(全局的文件/数据库等对象初始化)int CGateWay::GetParam();    //从INIFILE中读取参数值*/int  CGateWay::ToExit = 0;	//静态成员初始化//====================构造函数=======================CGateWay::CGateWay(){}//====================虚构函数=======================CGateWay::~CGateWay(){  WrDnSubmitLog->RWClose();  delete WrDnSubmitLog;  WrSystemLog->RWClose();  delete WrSystemLog;  //delete ConnPool;  MyDB->DBClose();  delete MyDB;  MtDB->DBClose();  delete MtDB;  MoDB->DBClose();  delete MoDB;  //SybaseDB->DBClose();  // delete SybaseDB;    pthread_mutex_destroy(&MsgID_Lock);  pthread_mutex_destroy(&SockSetLock); }//====================初始化为守护进程=======================void CGateWay::InitAsDaemon(){	if (fork() > 0)		exit(0);	setsid();	/*	signal(SIGINT, SIG_IGN);	signal(SIGHUP, SIG_IGN);	signal(SIGQUIT, SIG_IGN);	signal(SIGPIPE, SIG_IGN);	signal(SIGTTOU, SIG_IGN);	signal(SIGTTIN, SIG_IGN);	signal(SIGCHLD, SIG_IGN);	*/	if (fork() > 0)		exit(0);	chdir("/");	//?????? umask(0);}//==========================网关系统初始化==============================int CGateWay::Initialize(){  int RetCode;  char sSysEvent[SYS_EVENT_LEN];  time_t nowtime;  CGateWay::ToExit = 0;		//网关系统初始为正常状态0  FD_ZERO(&DnSockSet);	//清空下端连接套接字集  MaxSock = 0;  DnSock = new TCPsocket(6000);  time(&nowtime);  MsgID_Echo = (long)nowtime;  //MsgID_Echo = *(int*)&nowtime;  pthread_mutex_init(&MsgID_Lock,NULL);  pthread_mutex_init(&SockSetLock,NULL);  //获取网关系统配置参数  RetCode = GetParam();	  if(RetCode)  {     printf("Error[%d]: Failed to load GateWay's Parameters from File[%s],EXIT!",RetCode,INIFILE);          exit(1);  }  printf("\nGateWay's Parameters loaded from [%s] sucessfully!\n",INIFILE);  //----------------网关系统初始化-------  //打开系统日志文件  WrSystemLog = new FileOpr();    RetCode=WrSystemLog->RWOpen(SystemLogFile, "w");  if(RetCode)  {	printf("Error[%d]: Opening SystemLog file.",RetCode);	return 1 ;  }   //打开下端CMPP_Submit包接收日志文件  WrDnSubmitLog = new FileOpr();    RetCode = WrDnSubmitLog->RWOpen(DnSubmitLogFile, "w");  if(RetCode)  {	printf("Error[%d]: Opening DnSubmitLog file.",RetCode);	return 1 ;  }  //=================================  MyDB = new DB_MySQL;  RetCode = MyDB->DBInit(DB_ConfigFile);  if(RetCode)  {     sprintf(sSysEvent,"Error[%d]: Failed to Initialize Database[%s]! EXIT!",RetCode,DB_ConfigFile);	 WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG); 	 return 1 ;  }  RetCode = MyDB->DBConn();  if(RetCode)  {     sprintf(sSysEvent,"Error[%d]: Failed to Connect to Database[%s]! EXIT!",RetCode,DB_ConfigFile);     	 WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG);      return 1 ;  }  //=================================  MtDB = new DB_MySQL;  RetCode = MtDB->DBInit(DB_ConfigFile);  if(RetCode)  {     sprintf(sSysEvent,"Error[%d]: Failed to Initialize Database[%s]! EXIT!",RetCode,DB_ConfigFile);	 WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG); 	 return 1 ;  }  RetCode = MtDB->DBConn();  if(RetCode)  {     sprintf(sSysEvent,"Error[%d]: Failed to Connect to Database[%s]! EXIT!",RetCode,DB_ConfigFile);     	 WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG);      return 1 ;  }  //=================================  MoDB = new DB_MySQL;  RetCode = MoDB->DBInit(DB_ConfigFile);  if(RetCode)  {     sprintf(sSysEvent,"Error[%d]: Failed to Initialize Database[%s]! EXIT!",RetCode,DB_ConfigFile);	 WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG); 	 return 1 ;  }  RetCode = MoDB->DBConn();  if(RetCode)  {     sprintf(sSysEvent,"Error[%d]: Failed to Connect to Database[%s]! EXIT!",RetCode,DB_ConfigFile);     	 WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG);      return 1 ;  }  //=================================  DnNodeNum = MyDB->GetDnNodeInfo(CP_Array,MAX_DNNODE);  if(DnNodeNum > MAX_DNNODE)  {     DnNodeNum = MAX_DNNODE;	 sprintf(sSysEvent,"Warning: DownNode records in Database is [%d],while MAX_DNNODE=[%d].",RetCode,MAX_DNNODE);	 WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG);   }  else  {	 sprintf(sSysEvent,"LOG: Got [%d] DownNodes' information from Database.",DnNodeNum);	 WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG); 	 //------------debug------------	 for(int i=0;i<DnNodeNum;i++)		 printf("\nDnNode[%s]->IP=[%s].",CP_Array[i].ServiceCode,CP_Array[i].NodeIp);	 //------------debug end------------  }    /*printf("\n Initializing Connection Pool.......................................\n");  //建立数据库连接(池)  ConnPool = new ConnectionPool("61.145.116.115", "smsdev", "MtimeMarry5", "gateway", 0, NULL, 0, 3, 30, 1000);  //从数据库中获取下节点信息  CONNECTION *Connect = ConnPool->GetConnection();  if (Connect==NULL)  {	 strcpy(sSysEvent,"Error: Failed to get connection from the connection pool!\n");	 WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG);	 return 1;  }  DB_MySQL DBOpr;  DBOpr.GetConn(Connect->pConn);  DnNodeNum = DBOpr.GetDnNodeInfo(CP_Array,MAX_DNNODE);  if(DnNodeNum > MAX_DNNODE)  {     DnNodeNum = MAX_DNNODE;	 sprintf(sSysEvent,"Warning: DownNode records in Database is [%d],while MAX_DNNODE=[%d].",RetCode,MAX_DNNODE);	 WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG);   }  ConnPool->FreeConnection(Connect);  */  /*SybaseDB = new SMGP_DB_Sybase;  RetCode = SybaseDB->DBInit();  if(RetCode)	  {     printf("Error[%d]: Failed to Initialize Sybase Database! EXIT!",RetCode);          return 1 ;  }    RetCode = SybaseDB->DBConn();  if(RetCode)  {     printf("Error[%d]: Failed to Connect to Sybase Database! EXIT!",RetCode);          return 1 ;  }  */  Code_CP_Map.Map_Init(DnNodeNum);	//服务代码--下节点对象指针映射表初始化  Sock_CP_Map.Map_Init(DnNodeNum);	//套接字--下节点对象指针映射表初始化  //-------------------从数据库获取MT号段表放入内存映射区-------------------  char SqlCommand[200];  strcpy(SqlCommand,"select count(*) from MT_RouteTable;");  int RecordNum=0;  RecordNum = MyDB->GetRecordNum(SqlCommand);	//统计MT路由表的记录数  if(RecordNum<=0)  { 	 sprintf(sSysEvent,"Warning: Got [%d] Records from MT_RouteTable!",RecordNum);	 WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG);  }  else  {	  int PageNum;	  if(RecordNum%MAX_MTROUTE==0)	//读取MT路由表时的页数		  PageNum = RecordNum/MAX_MTROUTE;	  else		  PageNum = RecordNum/MAX_MTROUTE +1;	  MtRouteInfo MtRoute[MAX_MTROUTE];	  for(int i=0;i<PageNum;i++)	  {		  sprintf(SqlCommand,"select Segment,GWID from MT_RouteTable where Id>%d and Id<=%d;",\			  MAX_MTROUTE*i, MAX_MTROUTE*(i+1));		  int GotNum = MyDB->GetMtRoute(MtRoute, SqlCommand);		  #ifdef DEBUG		  //printf("\n GatewayInit: GotNum=%d.\n",GotNum);		  #endif		  for(int j=0; j<GotNum; j++)	//逐条记录放入号段--网关ID的路由映射表		  {			  MtRoute_Map[MtRoute[j].PhoneSeg] = MtRoute[j].UpNodeID;		  }	  }//for逐页获取记录,防止一次读取过多记录时出错	  #ifdef DEBUG	  //printf("\n GatewayInit: size of MtRoute_Map is [%d], RecordNum=%d, PageNum=%d.\n",MtRoute_Map.size(),RecordNum,PageNum);	  #endif	  sprintf(sSysEvent,"LOG: Got [%d] Records from MT_RouteTable successfully!",RecordNum);	  WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG);  }  return 0;}//====================互斥获取下节点共用的MsgID值====================long long CGateWay::GetMsgId(){  long long temp;  pthread_mutex_lock(&MsgID_Lock);  temp = MsgID_Echo ++;  pthread_mutex_unlock(&MsgID_Lock);  return temp ;	}//====================获取网关系统参数=======================int CGateWay::GetParam(){  	int RetCode;  	int i;  	char tmp[10];  	char tmpname[20];  	CFunc myfun;	FileOpr *Fileopr=new FileOpr();	RetCode=Fileopr->RWOpen(INIFILE, "r");	if(RetCode)     //打开配置文件	{		printf("Error[%d]: Failed to open [%s].",RetCode,INIFILE);		return RetCode;	}	//-------------------------------	RetCode=Fileopr->RWRead("SystemLogFile",SystemLogFile);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'SystemLogFile' from [%s].",RetCode,INIFILE);		return RetCode;	}	RetCode=Fileopr->RWRead("DnSubmitLogFile",DnSubmitLogFile);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'DnSubmitLogFile' from [%s].",RetCode,INIFILE);		return RetCode;	}	//memset(GWCode,0,sizeof(GWCode));	RetCode=Fileopr->RWRead("GWCode",GWCode);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'GWCode' from [%s].",RetCode,INIFILE);		return RetCode;	}	//memset(GWIp,0,sizeof(GWIp));	RetCode=Fileopr->RWRead("GWIp",GWIp);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'GWIp' from [%s].",RetCode,INIFILE);		return RetCode;	}	RetCode=Fileopr->RWRead("GWPort",&GWPort);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'GWPort' from [%s].",RetCode,INIFILE);		return RetCode;	}	RetCode=Fileopr->RWRead("AdminPort",&AdminPort);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'AdminPort' from [%s].",RetCode,INIFILE);		return RetCode;	}	RetCode=Fileopr->RWRead("AdminUser",AdminUser);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'AdminUser' from [%s].",RetCode,INIFILE);		return RetCode;	}	RetCode=Fileopr->RWRead("AdminPass",AdminPass);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'AdminPass' from [%s].",RetCode,INIFILE);		return RetCode;	}	RetCode=Fileopr->RWRead("Sel_Timeout",&Sel_Timeout);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'Sel_Timeout' from [%s].",RetCode,INIFILE);		return RetCode;	}	if (Sel_Timeout<50000 || Sel_Timeout>3000000)	{		Sel_Timeout=1000000;	//Sel_Timeout超限时的缺省值(300毫秒)	}	RetCode=Fileopr->RWRead("RecvQ_Empty_Wait",&RecvQ_Empty_Wait);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'RecvQ_Empty_Wait' from [%s].",RetCode,INIFILE);		return RetCode;	}	if (RecvQ_Empty_Wait<50000 || RecvQ_Empty_Wait>2000000)	{		RecvQ_Empty_Wait=500000;	//RecvQ_Empty_Wait超限时的缺省值(0.5秒)	}	RetCode=Fileopr->RWRead("SendQ_Empty_Wait",&SendQ_Empty_Wait);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'SendQ_Empty_Wait' from [%s].",RetCode,INIFILE);		return RetCode;	}	if (SendQ_Empty_Wait<100000 || SendQ_Empty_Wait>3000000)	{		SendQ_Empty_Wait=1000000;	//SendQ_Empty_Wait超限时的缺省值(300毫秒)	}	RetCode=Fileopr->RWRead("MT_DB_Interval",&MT_DB_Interval);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'MT_DB_Interval' from [%s].",RetCode,INIFILE);		return RetCode;	}	if (MT_DB_Interval<500000 || MT_DB_Interval>60000000)	{		MT_DB_Interval=3000000;	//MT_DB_Interval超限时的缺省值(3秒)	}	RetCode=Fileopr->RWRead("MO_DB_Interval",&MO_DB_Interval);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'MO_DB_Interval' from [%s].",RetCode,INIFILE);		return RetCode;	}	if (MO_DB_Interval<500000 || MO_DB_Interval>60000000)	{		MO_DB_Interval=3000000;	//MO_DB_Interval超限时的缺省值(4秒)	}	RetCode=Fileopr->RWRead("Max_MO_Fetch",&Max_MO_Fetch);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'Max_MO_Fetch' from [%s].",RetCode,INIFILE);		return RetCode;	}	if (Max_MO_Fetch<1 || Max_MO_Fetch>500)	{		Max_MO_Fetch=100;	//Max_MO_Fetch超限时的缺省值	}	RetCode=Fileopr->RWRead("Max_MT_Fetch",&Max_MT_Fetch);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'Max_MT_Fetch' from [%s].",RetCode,INIFILE);		return RetCode;	}	if (Max_MT_Fetch<1 || Max_MT_Fetch>500)	{		Max_MT_Fetch=100;	//Max_MT_Fetch超限时的缺省值	}	RetCode=Fileopr->RWRead("Max_RelayTimes",&Max_RelayTimes);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'Max_RelayTimes' from [%s].",RetCode,INIFILE);		return RetCode;	}	if (Max_RelayTimes<1 || Max_RelayTimes>20)	{		Max_RelayTimes=3;	//Max_RelayTimes超限时的缺省值	}	RetCode=Fileopr->RWRead("Relay_Interval",&Relay_Interval);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'Relay_Interval' from [%s].",RetCode,INIFILE);		return RetCode;	}	if (Relay_Interval<60 || Relay_Interval>1200)	//Relay_Interval超限时的缺省值(180秒)	{		Relay_Interval=180;	}	RetCode=Fileopr->RWRead("DB_ConfigFile",DB_ConfigFile);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'DB_ConfigFile' from [%s].",RetCode,INIFILE);		return RetCode;	}		for(i=0;i<UPNODE_NUM;i++)	{		strcpy(tmpname,"UpNode_IniFile_");		strcat(tmpname,myfun.lrhitoa(i+1,tmp));		RetCode=Fileopr->RWRead(tmpname,UpNode_IniFile[i]);		if(RetCode)		{			printf("Error[%d]: Failed to Read '%s' from [%s].",RetCode,tmpname,INIFILE);			return RetCode;		}	}	RetCode=Fileopr->RWRead("DnQueueBak",DnQueueBak);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'QueueBak' from [%s].",RetCode,INIFILE);		return RetCode;	}	RetCode=Fileopr->RWRead("DnRecvQ_File",DnRecvQ_File);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'DnRecvQ_File' from [%s].",RetCode,INIFILE);		return RetCode;	}		RetCode=Fileopr->RWRead("DnSendQ_File",DnSendQ_File);	if(RetCode)	{		printf("Error[%d]: Failed to Read 'DnSendQ_File' from [%s].",RetCode,INIFILE);		return RetCode;	}				//-------------------------------	RetCode=Fileopr->RWClose();	if(RetCode)    //关闭配置文件	{		printf("Error[%d]: Failed to Close [%s].",RetCode,INIFILE);		return RetCode;	}  	delete Fileopr;  return 0;}

⌨️ 快捷键说明

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