?? uaprofcomm.c
字號:
/********************************************************************
Copyright(C), 2006, Wuhan Hongxu Information Technologies Co., Ltd.
Filename : uaprofComm.c
Description: uaprof comm program.
Author : LiWeiBing
Version : Initial version
Date : 2006年12月
History :
<author> <time> <version > <desc>
********************************************************************/
#include <sys/varargs.h>
#include "uaprof.h"
#include "msspDefs.h"
#include "msspUtilReadConf.h"
extern int gLogLevel;
extern FILE * gUaprofPrintFP;
/**********************************************************
Function : uaprofReadConf
Description: 讀配置文件
Input :
Output :
Return : MSSP_FAILURE表示失敗
**********************************************************/
int uaprofReadConf(UaprofConfSt * pConfData)
{
char caFileText[5000];
memset(caFileText, 0, 5000);
if(MSSP_FAILURE == Get_Conf_String(CONF_FILE_NAME, UAPROF_BEGIN, UAPROF_END, caFileText, 5000))
{
printf("uaprofReadConf (Get_Conf_String fail)\n");
return(MSSP_FAILURE);
}
/*讀取整形數據*/
if(MSSP_FAILURE == Load_Int_ConfigValue(caFileText, 5000, "listen_port:", &(pConfData->iListenPort)))
{
printf("uaprofReadConf:Load_Int_ConfigValue listen rds port fail!\n");
return(MSSP_FAILURE);
}
if(MSSP_FAILURE == Load_Int_ConfigValue(caFileText, 5000, "http_port:", &(pConfData->iHttpPort)))
{
printf("uaprofReadConf:Load_Int_ConfigValue ismp port fail!\n");
return(MSSP_FAILURE);
}
if(MSSP_FAILURE == Load_Int_ConfigValue(caFileText, 5000, "ftp_port:", &(pConfData->iFtpPort)))
{
printf("uaprofReadConf:Load_Int_ConfigValue ftp port fail!\n");
return(MSSP_FAILURE);
}
if(MSSP_FAILURE == Load_Int_ConfigValue(caFileText, 5000, "data_base_port:", &(pConfData->iDataBasePort)))
{
printf("uaprofReadConf:Load_Int_ConfigValue data base port fail!\n");
return(MSSP_FAILURE);
}
if(MSSP_FAILURE == Load_Int_ConfigValue(caFileText, 5000, "log_level:", &(pConfData->iLogLevel)))
{
printf("uaprofReadConf:Load_Int_ConfigValue log level fail!\n");
return(MSSP_FAILURE);
}
if(MSSP_FAILURE == Load_Int_ConfigValue(caFileText, 5000, "print_to_file:", &(pConfData->iPrintFile)))
{
printf("uaprofReadConf:Load_Int_ConfigValue print file flag fail!\n");
return(MSSP_FAILURE);
}
/*讀取字符串型*/
if(MSSP_FAILURE == Load_Str_ConfigValue(caFileText, 5000, "uaprof_ip:", pConfData->caUaprofIp, 16))
{
printf("rdsReadConf get uaprof ip fail\n");
return(MSSP_FAILURE);
}
if(MSSP_FAILURE == Load_Str_ConfigValue(caFileText, 5000, "host_ip:", pConfData->caHostIp, 16))
{
printf("rdsReadConf get ismp ip fail\n");
return(MSSP_FAILURE);
}
if(MSSP_FAILURE == Load_Str_ConfigValue(caFileText, 5000, "data_base_ip:", pConfData->caDataBaseIp, 16))
{
printf("rdsReadConf get uaprof ip fail\n");
return(MSSP_FAILURE);
}
if(MSSP_FAILURE == Load_Str_ConfigValue(caFileText, 5000, "user_name:", pConfData->caUserName, 16))
{
printf("rdsReadConf get user name fail\n");
return(MSSP_FAILURE);
}
if(MSSP_FAILURE == Load_Str_ConfigValue(caFileText, 5000, "passwd:", pConfData->caPassWd, 16))
{
printf("rdsReadConf get passwd fail\n");
return(MSSP_FAILURE);
}
if(MSSP_FAILURE == Load_Str_ConfigValue(caFileText, 5000, "local_path:", pConfData->caLocalPath, 64))
{
printf("rdsReadConf get local path fail\n");
return(MSSP_FAILURE);
}
if(MSSP_FAILURE == Load_Str_ConfigValue(caFileText, 5000, "content_path:", pConfData->caContentPath, 16))
{
printf("rdsReadConf get content path fail\n");
return(MSSP_FAILURE);
}
return(MSSP_SUCCESS);
}
/**********************************************************
Function : ConnectToServer
Description: 作為客戶端向服務端建TCP連接
Input : ip:服務端IP地址 port:服務端監聽端口
Output : SOCKET 句柄iFd
Return : -1表示失敗
**********************************************************/
int ConnectToServer(char *ip, int port)
{
int iFd;
struct sockaddr_in my_addr;
if( !ip || port <= 0 )
{
printf("ConnectToServer invalid input\n");
return(-1);
}
if ((iFd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("ConnectToServer socket fail!\n");
return(-1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(port);
my_addr.sin_addr.s_addr = inet_addr(ip);
if (connect(iFd,(struct sockaddr *)&my_addr,sizeof(my_addr))== -1)
{
printf("ConnectToServer connect fail!ip=%s, port=%d\n",ip, port);
return(-1);
}
return(iFd);
}
int InsertUaprofDB(char *ip, int port, pss_uaprof_t *uaprof)
{
uaprof_database_insert_req_t InsertMsg;
int fd;
fd = ConnectToServer(ip, port);
if( fd < 0 )
{
printf("connect to db fail!\n");
return(MSSP_FAILURE);
}
memset(&InsertMsg, 0, sizeof(uaprof_database_insert_req_t));
InsertMsg.msgHead.moduleType = STREAM_DB_MODULE_TYPE_UAPROF;
InsertMsg.msgHead.msgLength = sizeof(uaprof_database_insert_req_t);
InsertMsg.msgHead.msgType = UAPROF_DATABASE_INSERT_REQ;
memcpy(&(InsertMsg.uaprof), uaprof, sizeof(pss_uaprof_t));
send(fd, &InsertMsg, InsertMsg.msgHead.msgLength, 0);
UAPROF_LOG(TRACE, "send insertmsg to db ok!\n");
sleep(1);
close(fd);
return(MSSP_SUCCESS);
}
/**********************************************************
Function : UAPROF_LOG
Description:記LOG的函數
Input : iErrCode可取ERR、WARN、TRACE,分別表示錯誤信息、警告信息、調試信息
Output :
Return :
**********************************************************/
void UAPROF_LOG(int iErrCode, const char *format, ...)
{
va_list A;
long lTLoc;
long lCurTime;
if (!format) return;
if(iErrCode < gLogLevel)
{
return;
}
if( NULL != gUaprofPrintFP )
{
(lCurTime) = time(&lTLoc);
fprintf(gUaprofPrintFP,"@[%d]%ld|%.24s ",iErrCode,getpid(),ctime(&lCurTime));
va_start( A, format );
(void)vfprintf( gUaprofPrintFP, format, A );
fflush(gUaprofPrintFP);
va_end( A );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -