selfcare init
This commit is contained in:
53
proxy_c/dba/Makefile
Normal file
53
proxy_c/dba/Makefile
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
MODULE = dba
|
||||
TYPE = plat
|
||||
|
||||
DBUG_FLAGS_ADD =
|
||||
RELS_FLAGS_ADD =
|
||||
|
||||
##Default commonly as below
|
||||
|
||||
BUILD = lib
|
||||
CFG = debug
|
||||
|
||||
|
||||
PLT_LIB = -DDEBUG
|
||||
|
||||
APP_LIB =
|
||||
LIB_ADD =
|
||||
|
||||
SRC_PATH = ./src
|
||||
INC_PATH = ./src/include
|
||||
PLT_PATH = ../../plat
|
||||
APP_PATH = ../../mss
|
||||
|
||||
OBJ_ADD =
|
||||
TEST_OBJ_PATH =
|
||||
|
||||
PREPROC_CMD =
|
||||
POSTPROC_CMD =
|
||||
|
||||
##---------------------------------------------------------------------##
|
||||
##--------------------------------------
|
||||
##
|
||||
## Make configuration(Customer define)
|
||||
##
|
||||
##--------------------------------------
|
||||
|
||||
## CCFLAG_SWITCH = on/off => gcc flag show on/off
|
||||
## COVER_NEED = yes/no => PTF cover report needed
|
||||
## COVER_REPORT_PATH = [path ] => PTF cover report path
|
||||
|
||||
CCFLAG_SWITCH = off
|
||||
COVER_NEED = no
|
||||
COVER_REPORT_PATH = ./output
|
||||
|
||||
##---------------------------------------------------------------------##
|
||||
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## include makefile.rules (Do not change)
|
||||
##
|
||||
##--------------------------------------
|
||||
include ./scripts/Makefile.rules
|
||||
345
proxy_c/dba/scripts/Makefile.rules
Normal file
345
proxy_c/dba/scripts/Makefile.rules
Normal file
@@ -0,0 +1,345 @@
|
||||
|
||||
##----------------------------------------------------------##
|
||||
## ##
|
||||
## Universal Makefile Rules ##
|
||||
## ##
|
||||
## Created : Wei Liu 07/03/07 ##
|
||||
## Revision: [Last]Wei Liu 07/07/07 ##
|
||||
## ##
|
||||
##----------------------------------------------------------##
|
||||
|
||||
|
||||
UMAKE_VERSION := V2.0
|
||||
|
||||
##-------------------------------------
|
||||
##
|
||||
## Work Directory : /usr/local/include
|
||||
## Default Target : all
|
||||
##
|
||||
##-------------------------------------
|
||||
default: all
|
||||
.PHONY: all clean rebuild test indent splint doc \
|
||||
dir config check bk lsbk rmbk unzip umakever usage\
|
||||
FORCE
|
||||
.SUFFIXES:
|
||||
|
||||
umakever:
|
||||
@echo "Universal Makefile (UMake) Version: $(UMAKE_VERSION)"
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## Makefile CFG defination check
|
||||
##
|
||||
##--------------------------------------
|
||||
|
||||
ifeq "$(MODULE)" ""
|
||||
$(error Please input the module name (MODULE = )in makefile. )
|
||||
endif
|
||||
|
||||
ifeq "$(CFG)" ""
|
||||
CFG=debug
|
||||
$(warnning No configuration specified for CFG. Defaulting to $(MODULE) - debug. )
|
||||
endif
|
||||
|
||||
ifeq "$(BUILD)" ""
|
||||
BUILD=lib
|
||||
$(warnning No configuration specified for BUILD. Defaulting to create lib$(MODULE).a. )
|
||||
endif
|
||||
|
||||
ifeq "$(SRC_PATH)" ""
|
||||
SRC_PATH=.
|
||||
$(warnning No configuration specified for SRC_PATH. Defaulting to ./. )
|
||||
endif
|
||||
|
||||
COVER_NEED ?= no
|
||||
PLT_PATH ?= ../../plat
|
||||
APP_PATH ?= ../../app
|
||||
TYPE ?= plat
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## Gcc Flag for debug or release
|
||||
##
|
||||
##--------------------------------------
|
||||
CC := gcc
|
||||
GCC_CFLAGS := -Wall -MM
|
||||
AR_LINK := ar -r
|
||||
|
||||
RELS_FLAGS_ADD += -DNDEBUG
|
||||
RELEASE_CFLAGS += -g -Wall -I. $(RELS_FLAGS_ADD) -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient
|
||||
RELEASE_LINK_CFLAGS = -g $(RELS_FLAGS_ADD) -o
|
||||
DEBUG_CFLAGS += -g -Wall -rdynamic -DDEBUG -I. $(DBUG_FLAGS_ADD) -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient -I/usr/include/openssl
|
||||
DEBUG_LINK_CFLAGS = -g -rdynamic -DDEBUG -o
|
||||
|
||||
GLIB_CFLAGS = -I/usr/include/glib-2.0
|
||||
GLIB_CFLAGS1 = -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
|
||||
|
||||
ifeq "$(COVER_NEED)" "yes"
|
||||
DEBUG_CFLAGS += -fprofile-arcs -ftest-coverage -pg
|
||||
endif
|
||||
|
||||
GCC_CFLAGS=$(DEBUG_CFLAGS) $(GLIB_CFLAGS) $(GLIB_CFLAGS1)
|
||||
GCC_LINK_CFLAGS=$(DEBUG_LINK_CFLAGS)
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## Project setting
|
||||
##
|
||||
##--------------------------------------
|
||||
OBJDIR:=./obj
|
||||
LIBDIR:=./lib
|
||||
UTDIR :=./ut
|
||||
DOCDIR:=./doc
|
||||
DIRBUILD=$(OBJDIR)
|
||||
|
||||
ifeq "$(BUILD)" "lib"
|
||||
BINDIR:=./bin
|
||||
OUTFILE=$(LIBDIR)/lib$(MODULE).a
|
||||
DIRNEED=$(UTDIR) $(DOCDIR)
|
||||
DIRBUILD+=$(LIBDIR)
|
||||
else
|
||||
BINDIR:=.
|
||||
OUTFILE=$(BINDIR)/$(MODULE)
|
||||
DIRNEED=
|
||||
DIRBUILD+=$(BINDIR)
|
||||
endif
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## source , object and dependencies files
|
||||
##
|
||||
##--------------------------------------
|
||||
|
||||
SRC_SUBDIR := $(shell find $(SRC_PATH) -type d)
|
||||
|
||||
vpath %.c $(SRC_SUBDIR)
|
||||
vpath %.o $(OBJDIR)
|
||||
vpath %.d $(OBJDIR)
|
||||
|
||||
SRC_FULL_PATH = $(foreach dir,$(SRC_SUBDIR),$(wildcard $(dir)/*.c))
|
||||
SRC_FILES = $(foreach file, $(notdir $(SRC_FULL_PATH)) ,$(OBJDIR)/$(file))
|
||||
COMMON_OBJ = $(SRC_FILES:%.c=%.o)
|
||||
|
||||
TEST_OBJ_PATH ?= ../../obj
|
||||
|
||||
TEST_OBJ = $(foreach dir,$(TEST_OBJ_PATH),$(wildcard $(dir)/*.o))
|
||||
|
||||
OBJ=$(COMMON_OBJ) $(OBJ_ADD)
|
||||
ALL_OBJ := $(OBJ) $(TEST_OBJ)
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## Lib setting
|
||||
##
|
||||
##--------------------------------------
|
||||
|
||||
ifeq "$(COVER_NEED)" "yes"
|
||||
LIBCOVER=-lgcov
|
||||
endif
|
||||
|
||||
MODULE_PLT_LIB=$(foreach lib,$(PLT_LIB), -L$(PLT_PATH)/$(lib)/lib/ -l$(lib) )
|
||||
MODULE_PLT_LIB+=-lm
|
||||
|
||||
|
||||
MODULE_APP_LIB=$(foreach lib,$(APP_LIB),-L$(APP_PATH)/$(lib)/lib -l$(lib))
|
||||
|
||||
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## Rules
|
||||
##
|
||||
##--------------------------------------
|
||||
CCFLAG_SWITCH ?= off
|
||||
CC_COMPILE =$(CC) $(GCC_CFLAGS) -c $< -o $@
|
||||
CC_PRG_LINK=$(CC) $(GCC_LINK_CFLAGS) $(OUTFILE) $(ALL_OBJ) $(LIBCOVER) $(MODULE_APP_LIB) $(MODULE_PLT_LIB) $(LIB_ADD)
|
||||
CC_LIB_LINK=$(AR_LINK) $(OUTFILE) $(ALL_OBJ)
|
||||
|
||||
COMPILE=$(CC_COMPILE)
|
||||
PRG_LINK=$(CC_PRG_LINK)
|
||||
LIB_LINK=$(CC_LIB_LINK)
|
||||
|
||||
ifeq "$(BUILD)" "exef"
|
||||
LINK=$(PRG_LINK)
|
||||
else
|
||||
LINK=$(LIB_LINK)
|
||||
endif
|
||||
|
||||
# Build rules
|
||||
|
||||
all: preproc start dir $(ALL_OBJ) #prtdebug
|
||||
@echo Linking :$(OUTFILE)
|
||||
ifeq "$(CCFLAG_SWITCH)" "off"
|
||||
@$(LINK)
|
||||
else
|
||||
$(LINK)
|
||||
endif
|
||||
@$(POSTPROC_CMD)
|
||||
@echo -e "\n================================================================================\n"
|
||||
|
||||
sinclude $(DEPENDS)
|
||||
|
||||
release : CC_COMPILE =$(CC) $(RELEASE_CFLAGS) -c $< -o $@
|
||||
release : CC_PRG_LINK=$(CC) $(RELEASE_LINK_CFLAGS) $(OUTFILE) $(ALL_OBJ) $(MODULE_APP_LIB) $(MODULE_PLT_LIB) $(LIB_ADD)
|
||||
release : all
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## Make command to use for dependencies
|
||||
##
|
||||
##--------------------------------------
|
||||
|
||||
MAKE :=make
|
||||
RM :=rm
|
||||
MKDIR :=mkdir
|
||||
|
||||
preproc:
|
||||
@$(PREPROC_CMD)
|
||||
|
||||
start:
|
||||
@echo -e "\n================================================================================\n"
|
||||
@echo "[Building Project]: $(notdir $(MODULE))"
|
||||
ifeq "$(CCFLAG_SWITCH)" "off"
|
||||
@echo "Show Gcc Flags switch = OFF"
|
||||
endif
|
||||
|
||||
prtdebug:
|
||||
@echo "$(MODULE)-$(BUILD)[$(CFG)] build source file:" "$(SRC_FULL_PATH)"
|
||||
@echo SRC_SUBDIR: $(SRC_SUBDIR)
|
||||
@echo SRC_FULL_PATH : $(SRC_FULL_PATH)
|
||||
@echo SRC_FILES : $(SRC_FILES)
|
||||
@echo ALL_OBJ : $(ALL_OBJ)
|
||||
@echo LIB:$(MODULE_PLT_LIB)
|
||||
@echo PLT_LIB: $(PLT_LIB)
|
||||
@echo CCFLAG_SWITCH :$(CCFLAG_SWITCH)
|
||||
|
||||
config: dir
|
||||
|
||||
dir:
|
||||
@$(foreach dir,$(DIRNEED),$(MKDIR) -p $(DIRNEED) --mode=0777; )
|
||||
@$(foreach dir,$(DIRBUILD),$(MKDIR) -p $(dir) --mode=0777; )
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## Make Rebuild and clean
|
||||
##
|
||||
##--------------------------------------
|
||||
|
||||
ifneq "$(PROJ)" ""
|
||||
FRIEND_PROJ := $(shell )
|
||||
endif
|
||||
|
||||
jumprebuild:
|
||||
ifneq "$(PROJ)" ""
|
||||
@cd $(FRIEND_PROJ); mak rebuild ; cd -
|
||||
endif
|
||||
|
||||
# Rebuild this project
|
||||
rebuild: jumprebuild cleanall all
|
||||
|
||||
# Clean this project and all dependencies
|
||||
cleanall: clean
|
||||
|
||||
# Clean this project
|
||||
clean:
|
||||
@echo -e "||--------------------------------------------------------------- "
|
||||
@echo -e "|| Umake clean gcc , lcov, doxygen generated and temporary files. "
|
||||
@echo -e "||--------------------------------------------------------------- "
|
||||
@$(RM) -rf $(OBJDIR) $(OUTFILE) $(COVER_REPORT_PATH) ./doc/doxygen.conf ./doc/html ./doc/latex ./doc/rtf $(foreach dir,$(SRC_SUBDIR),$(dir)/*~)
|
||||
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## indent Makefile.indent
|
||||
##
|
||||
##--------------------------------------
|
||||
include Makefile.indent
|
||||
|
||||
indent:
|
||||
@chmod 777 $(SRC_FULL_PATH)
|
||||
@echo ===================================Indent START=================================
|
||||
@echo
|
||||
@echo "[Indent source file ]: $(SRC_FULL_PATH)"
|
||||
$(call MAKE_INDENT , $(SRC_FULL_PATH))
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## splint makefile.splint
|
||||
##
|
||||
##--------------------------------------
|
||||
include Makefile.splint
|
||||
|
||||
SPLINT_FLAG_SWITCH ?= off
|
||||
|
||||
splint:
|
||||
@chmod 777 $(SRC_FULL_PATH)
|
||||
@echo =================================Splint START==================================
|
||||
@echo
|
||||
ifeq "$(SPLINT_FLAG_SWITCH)" "on"
|
||||
@echo "[Splint flags ]: $(SPLINT_FLAGS)"
|
||||
endif
|
||||
@echo "[Lint Clean Project]: $(notdir $(MODULE))"
|
||||
$(call MAKE_SPLINT, $(SRC_FULL_PATH))
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## doc Makefile.doxygen
|
||||
##
|
||||
##--------------------------------------
|
||||
include Makefile.doxygen
|
||||
|
||||
doc:
|
||||
@chmod 777 $(SRC_FULL_PATH)
|
||||
@echo ==================================Doxygen START=================================
|
||||
@echo
|
||||
$(call MAKE_DOC, $(SRC_FULL_PATH))
|
||||
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## backup Makefile.backup
|
||||
##
|
||||
##--------------------------------------
|
||||
include Makefile.backup
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## cov Makefile.cov
|
||||
##
|
||||
##--------------------------------------
|
||||
include Makefile.cov
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## usage Makefile.usage
|
||||
##
|
||||
##--------------------------------------
|
||||
include Makefile.usage
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## Make dependencies
|
||||
##
|
||||
##--------------------------------------
|
||||
|
||||
$(OBJDIR)/%.d:%.c
|
||||
@$(CC) $< -MM -MD -o $@
|
||||
|
||||
$(OBJDIR)/%.o:%.c
|
||||
|
||||
ifeq "$(CCFLAG_SWITCH)" "off"
|
||||
@echo -e "building: $(notdir $@) \t\t\t\t please wait ..."
|
||||
@$(COMPILE)
|
||||
else
|
||||
$(COMPILE)
|
||||
endif
|
||||
|
||||
DEPENDS=$(COMMON_OBJ:.o=.d)
|
||||
|
||||
##--------------------------------------
|
||||
##
|
||||
## Make force
|
||||
##
|
||||
##--------------------------------------
|
||||
FORCE:
|
||||
|
||||
138
proxy_c/dba/src/dba_cfg.c
Normal file
138
proxy_c/dba/src/dba_cfg.c
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "./include/db_adaptor.h"
|
||||
|
||||
#define DBA_CFG "./conf/db_adapotr.cfg"
|
||||
|
||||
_db_cfg db_cfg;
|
||||
|
||||
int dba_read_cfg()
|
||||
{
|
||||
char s[80],s1[80];
|
||||
int len;
|
||||
unsigned char conf_state=0xFF;
|
||||
FILE *fpConf;
|
||||
_db_link *db_ptr=NULL;
|
||||
|
||||
memset(&db_cfg, 0x00, sizeof(_db_cfg));
|
||||
|
||||
fpConf = fopen(DBA_CFG,"rb");
|
||||
|
||||
if(fpConf == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
strcpy(s,"");
|
||||
strcpy(s1,"");
|
||||
while(fgets(s,1024,fpConf) !=(char *)0)
|
||||
{
|
||||
if( (int *)strchr(s,'#') !=NULL) continue;
|
||||
if( !strlen(s) ) continue;
|
||||
len = strlen(s);
|
||||
if(len < 2) continue;
|
||||
|
||||
if(s[len-1] == 0x0d ||s[len-1] == 0x0a)
|
||||
{
|
||||
if(s[len-2] == 0x0d ||s[len-2] == 0x0a)
|
||||
{
|
||||
s[len-2] = 0;
|
||||
len -= 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
s[len-1] = 0;
|
||||
len -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(!strncasecmp(s,"[db0]",5))
|
||||
{
|
||||
conf_state = 0;
|
||||
db_ptr = &db_cfg.db_link[0];
|
||||
continue;
|
||||
}
|
||||
else if(!strncasecmp(s,"[db1]",5))
|
||||
{
|
||||
conf_state = 1;
|
||||
db_ptr = &db_cfg.db_link[1];
|
||||
continue;
|
||||
}
|
||||
else if(!strncasecmp(s,"[db2]",5))
|
||||
{
|
||||
conf_state = 2;
|
||||
db_ptr = &db_cfg.db_link[2];
|
||||
continue;
|
||||
}
|
||||
else if(!strncasecmp(s,"[db3]",5))
|
||||
{
|
||||
conf_state = 3;
|
||||
db_ptr = &db_cfg.db_link[3];
|
||||
continue;
|
||||
}
|
||||
|
||||
switch(conf_state)
|
||||
{
|
||||
case 0:
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
if(strncasecmp(s,"enable=",7)==0)
|
||||
{
|
||||
if(strncasecmp(&s[7],"on", 2) == 0)
|
||||
db_ptr->enable = 1;
|
||||
else
|
||||
db_ptr->enable = 0;
|
||||
}
|
||||
else if(strncasecmp(s,"role=",5)==0)
|
||||
{
|
||||
db_ptr->role = atoi(&s[5]);
|
||||
}
|
||||
else if(strncasecmp(s,"host_ip=",8)==0)
|
||||
{
|
||||
strcpy(db_ptr->str_host_ip, &s[8]);
|
||||
db_ptr->host_ip = inet_addr(&s[8]);
|
||||
}
|
||||
else if(strncasecmp(s,"host_port=",10)==0)
|
||||
{
|
||||
db_ptr->host_port = atoi(&s[10]);
|
||||
}
|
||||
else if(strncasecmp(s,"db_name=",8)==0)
|
||||
{
|
||||
strcpy(db_ptr->db_name, &s[8]);
|
||||
}
|
||||
else if(strncasecmp(s,"client_name=",12)==0)
|
||||
{
|
||||
strcpy(db_ptr->client_name, &s[12]);
|
||||
}
|
||||
else if(strncasecmp(s,"client_pwd=",11)==0)
|
||||
{
|
||||
strcpy(db_ptr->client_pwd, &s[11]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fpConf);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dba_crm_set_prefix_table_created()
|
||||
{
|
||||
db_cfg.prefix_file_created = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dba_crm_get_prefix_table_flag()
|
||||
{
|
||||
return db_cfg.prefix_file_created;
|
||||
}
|
||||
350
proxy_c/dba/src/dba_conn.c
Normal file
350
proxy_c/dba/src/dba_conn.c
Normal file
@@ -0,0 +1,350 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "./include/db_adaptor.h"
|
||||
|
||||
extern _db_cfg db_cfg;
|
||||
|
||||
|
||||
extern char *GetAsciiTime();
|
||||
|
||||
struct timeval tv;
|
||||
|
||||
/*-------------------------------------------------*/
|
||||
|
||||
int dbc_connect_host(u8 db_index)
|
||||
{
|
||||
char host_info[128];
|
||||
_db_link *db_ptr=NULL;
|
||||
unsigned int timeout = 20;
|
||||
MYSQL*tmp_conn = NULL;
|
||||
|
||||
db_ptr = &db_cfg.db_link[db_index];
|
||||
if(db_ptr->enable == 0 || db_ptr->conn_status == 1)
|
||||
return 0;
|
||||
|
||||
if ((db_ptr->db_conn = mysql_init(NULL)) == NULL) {
|
||||
myLOG_D("DBA: mysql_init():failed\n");
|
||||
return -1;
|
||||
}
|
||||
//unsigned int sslmode = SSL_MODE_DISABLED;
|
||||
mysql_options(db_ptr->db_conn, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&timeout);
|
||||
//mysql_options(db_ptr->db_conn, MYSQL_OPT_SSL_MODE, (const char *)&sslmode);
|
||||
|
||||
sprintf(host_info,"%s", db_ptr->str_host_ip);
|
||||
|
||||
tmp_conn = mysql_real_connect(db_ptr->db_conn, host_info, db_ptr->client_name, db_ptr->client_pwd, db_ptr->db_name, 0, NULL, 0 );
|
||||
|
||||
if(tmp_conn != NULL)
|
||||
{
|
||||
db_ptr->conn_status = 1;
|
||||
db_cfg.total_active_links ++;
|
||||
myLOG_D("DBA: MYSQL connect to SERVER [%d] succeed @%s\n", db_index, GetAsciiTime());
|
||||
}
|
||||
else
|
||||
{
|
||||
db_ptr->conn_status = 0;
|
||||
myLOG_D("DBA: MYSQL connect to SERVER [%d] failed @%s, %s\n", db_index, GetAsciiTime(), mysql_error(db_ptr->db_conn));
|
||||
mysql_close(db_ptr->db_conn);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(db_cfg.total_active_links == 1)
|
||||
{
|
||||
db_cfg.current_working_link_id = db_index;
|
||||
}
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
MYSQL *dba_get_working_conn(int db_index)
|
||||
{
|
||||
MYSQL *db_conn = NULL;
|
||||
//u8 index = db_cfg.current_working_link_id;
|
||||
u8 index = db_index;
|
||||
|
||||
if(db_cfg.total_active_links == 0)
|
||||
return NULL;
|
||||
if( index < MAX_DB_CONN)
|
||||
{
|
||||
db_conn = db_cfg.db_link[index].db_conn;
|
||||
}
|
||||
//*db_index = index;
|
||||
return db_conn;
|
||||
}
|
||||
|
||||
int dba_close_conn(int index)
|
||||
{
|
||||
if(index>=MAX_DB_CONN)
|
||||
return 0;
|
||||
|
||||
//mysql_close(db_cfg.db_link[index].db_conn);
|
||||
//free(db_cfg.db_link[index].db_conn);
|
||||
|
||||
db_cfg.db_link[index].conn_status = 0;
|
||||
db_cfg.total_active_links --;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dba_my_ping()
|
||||
{
|
||||
int i;
|
||||
int length;
|
||||
char ping_sql[256]={"Replace" };
|
||||
MYSQL *db_conn;
|
||||
int rc;
|
||||
|
||||
for(i=0; i<MAX_DB_CONN; i++)
|
||||
{
|
||||
if(db_cfg.db_link[i].conn_status == 0)
|
||||
continue;
|
||||
|
||||
db_conn = db_cfg.db_link[i].db_conn;
|
||||
if(db_conn == NULL)
|
||||
continue;
|
||||
|
||||
sprintf(ping_sql, "INSERT INTO %s.tb_ping values(%ld)", db_cfg.db_link[i].db_name, tv.tv_sec);
|
||||
length = strlen(ping_sql);
|
||||
|
||||
rc = mysql_real_query(db_conn, ping_sql, length);
|
||||
if(rc != 0)
|
||||
{
|
||||
myLOG_D("DBA: Server [%d] is down by ping @%s error [errno = %d]: [%s]\n", i, GetAsciiTime(), mysql_errno(db_conn),mysql_error(db_conn));
|
||||
|
||||
dba_close_conn(i);
|
||||
|
||||
if(db_cfg.current_working_link_id == i)
|
||||
{
|
||||
if(db_cfg.db_link[1-i].conn_status == 1) //here only support 2 links
|
||||
{
|
||||
db_cfg.current_working_link_id = 1 - i;
|
||||
}
|
||||
else
|
||||
db_cfg.current_working_link_id = INVALID_DB_LINK_ID;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
dba_db_sql_execute_crm(const char *sql, char result[][1024], int MAX_ROWS, int MAX_FIELDS, int dump_flag, FILE *fp)
|
||||
{
|
||||
int index = 0;
|
||||
|
||||
return dba_db_sql_execute_real(index, sql, result, MAX_ROWS, MAX_FIELDS, dump_flag, fp);
|
||||
}
|
||||
|
||||
dba_db_sql_execute_ocs(const char *sql, char result[][1024], int MAX_ROWS, int MAX_FIELDS, int dump_flag, FILE *fp)
|
||||
{
|
||||
int index = 1;
|
||||
|
||||
return dba_db_sql_execute_real(index, sql, result, MAX_ROWS, MAX_FIELDS, dump_flag, fp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int dba_db_sql_execute_real(int index, const char *sql, char result[][1024], int MAX_ROWS, int MAX_FIELDS, int dump_flag, FILE *fp)
|
||||
{
|
||||
int rc;
|
||||
int i, rows, fields;
|
||||
u16 length;
|
||||
MYSQL *db_conn;
|
||||
MYSQL_RES *res_ptr;
|
||||
MYSQL_ROW row;
|
||||
|
||||
if(sql == NULL)
|
||||
return 0;
|
||||
|
||||
db_conn = dba_get_working_conn(index);
|
||||
if(db_conn == NULL)
|
||||
return 0;
|
||||
|
||||
if(0)
|
||||
{
|
||||
if(dump_flag != 2)
|
||||
{
|
||||
if(strlen(sql)<256)
|
||||
{
|
||||
myLOG_D(sql);
|
||||
myLOG_D("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
char tmpbuf[512]={""};
|
||||
memcpy(tmpbuf, sql, 256);
|
||||
myLOG_D(tmpbuf);
|
||||
myLOG_D("\n");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
length = strlen(sql);
|
||||
rc = mysql_real_query(db_conn, sql, length);
|
||||
if(rc != 0)
|
||||
{
|
||||
myLOG_D("mysql_real_query error [errno = %d]: [%s]\n",mysql_errno(db_conn),mysql_error(db_conn));
|
||||
dba_close_conn(index);
|
||||
//release_mysql_connection(db_conn);
|
||||
return 0;
|
||||
}
|
||||
if(MAX_ROWS == 0)//update not query
|
||||
return 1;
|
||||
|
||||
res_ptr = mysql_store_result(db_conn);
|
||||
if(res_ptr == NULL)
|
||||
{
|
||||
myLOG_D("mysql_store_result error [errno = %d]: [%s]\n",mysql_errno(db_conn),mysql_error(db_conn));
|
||||
return 0;
|
||||
}
|
||||
|
||||
rows = mysql_num_rows(res_ptr);
|
||||
if(rows == 0)
|
||||
goto empty;
|
||||
|
||||
rows = 0;
|
||||
fields = mysql_num_fields(res_ptr);
|
||||
if(fields > MAX_FIELDS)
|
||||
fields = MAX_FIELDS;
|
||||
while ((row = mysql_fetch_row(res_ptr)))
|
||||
{
|
||||
if(dump_flag == 1)
|
||||
{
|
||||
if(fp != NULL)
|
||||
{
|
||||
for (i = 0; i < fields; i++)
|
||||
{
|
||||
if(i == fields - 1)
|
||||
fprintf(fp, "%s\r\n", row[i]);
|
||||
else
|
||||
fprintf(fp, "%s,", row[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = 0; i < fields; i++)
|
||||
{
|
||||
if(row[i] == NULL)
|
||||
{
|
||||
result[i][0] = 0;
|
||||
if(0)
|
||||
{
|
||||
fields = 0;
|
||||
rows = 0;
|
||||
goto empty;
|
||||
}
|
||||
}
|
||||
else
|
||||
strcpy(result[i], row[i]);
|
||||
}
|
||||
}
|
||||
if(rows++ >= MAX_ROWS)
|
||||
break;
|
||||
}
|
||||
|
||||
empty:
|
||||
mysql_free_result(res_ptr);
|
||||
|
||||
return rows;//fields;
|
||||
}
|
||||
|
||||
static int sql_query_only_counter = 0;
|
||||
int dba_db_sql_query_only_execute_real(const char *sql, char result[][1024], int MAX_ROWS, int MAX_FIELDS )
|
||||
{
|
||||
int index;
|
||||
_db_link *db_ptr=NULL;
|
||||
|
||||
//load balance for tariff query, db_conn#1/2 always
|
||||
if(sql == NULL)
|
||||
return 0;
|
||||
|
||||
index = sql_query_only_counter%2;
|
||||
sql_query_only_counter ++;
|
||||
|
||||
db_ptr = &db_cfg.db_link[index+1];
|
||||
if(db_ptr->enable == 0 || db_ptr->conn_status == 1)
|
||||
{
|
||||
db_ptr = &db_cfg.db_link[(1-index)+1];
|
||||
if(db_ptr->enable == 0 || db_ptr->conn_status == 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
index = (1-index) + 1;
|
||||
}
|
||||
else
|
||||
index = index + 1;
|
||||
|
||||
return dba_db_sql_execute_real(index, sql, result, MAX_ROWS, MAX_FIELDS, 0, NULL);
|
||||
|
||||
}
|
||||
|
||||
int dba_conn_thread(void *param)
|
||||
{
|
||||
register u32 check_conn_timer = 1;
|
||||
register u32 check_sms_notification_timer = 0;
|
||||
|
||||
db_cfg.current_working_link_id = INVALID_DB_LINK_ID;
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
dba_init_tariff_sql();
|
||||
crm_pxy_plan_record_init();
|
||||
|
||||
while(1)
|
||||
{
|
||||
if(check_conn_timer % 1000 == 0)//10s
|
||||
{
|
||||
dbc_connect_host(0);
|
||||
dbc_connect_host(1);
|
||||
//dbc_connect_host(2);
|
||||
|
||||
if(0)
|
||||
dba_my_ping();
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
crm_pxy_plan_record_timer();
|
||||
|
||||
}
|
||||
|
||||
if(dba_crm_get_prefix_table_flag() == 0)
|
||||
{
|
||||
dba_crm_dump_prefix_table_into_file();
|
||||
}
|
||||
|
||||
if(rest_provsioning_enabled())
|
||||
{
|
||||
|
||||
if(check_sms_notification_timer%100 == 0) //1seconds==>2S 100==>200
|
||||
{
|
||||
subs_provisioning_command_process();
|
||||
#if 1//def ENABLE_CRM_REST_AND_Z1
|
||||
sms_notification_process();
|
||||
#endif
|
||||
//dba_crm_scan_bundle_thread(tv.tv_sec);
|
||||
}
|
||||
}
|
||||
|
||||
logWriter_rt();
|
||||
|
||||
check_conn_timer ++;
|
||||
check_sms_notification_timer ++;
|
||||
|
||||
usleep(10000); //10ms
|
||||
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
1735
proxy_c/dba/src/dba_crm.c
Normal file
1735
proxy_c/dba/src/dba_crm.c
Normal file
File diff suppressed because it is too large
Load Diff
1731
proxy_c/dba/src/dba_crm.c.bak
Normal file
1731
proxy_c/dba/src/dba_crm.c.bak
Normal file
File diff suppressed because it is too large
Load Diff
50
proxy_c/dba/src/dba_main.c
Normal file
50
proxy_c/dba/src/dba_main.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "./include/db_adaptor.h"
|
||||
|
||||
extern int dba_conn_thread(void *param);
|
||||
|
||||
int dba_main(int argc, char **argv)
|
||||
{
|
||||
int i=0;
|
||||
pthread_t th[MAX_DB_CONN];
|
||||
u8 param[2];
|
||||
|
||||
dba_read_cfg();
|
||||
logWriter_init();
|
||||
|
||||
i = 0;
|
||||
param[0] = i;
|
||||
if(1)
|
||||
{
|
||||
pthread_create(&th[i],NULL,dba_conn_thread, (void *)param);
|
||||
//pthread_join(th[i],NULL);
|
||||
}
|
||||
else
|
||||
dba_conn_thread(NULL);
|
||||
|
||||
if(0)
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
logWriter_rt();
|
||||
sleep(5);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void dba_init()
|
||||
{
|
||||
dba_read_cfg();
|
||||
logWriter_init();
|
||||
}
|
||||
|
||||
62
proxy_c/dba/src/hmac_md5.c
Normal file
62
proxy_c/dba/src/hmac_md5.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
** hmac_md5 -- implements RFC 2104
|
||||
** This hmac_md5 function requires an OpenSSL-compatible MD5
|
||||
** implementation. There are Public Domain MD5 implementations by Colin
|
||||
** Plumb and by Solar Designer. You probably want to use one of these.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "md5.h"
|
||||
|
||||
|
||||
const int blocksize = 64;
|
||||
const int hashsize = 16;
|
||||
|
||||
|
||||
/*
|
||||
** The computed HMAC will be written to `digest'.
|
||||
** Ensure digest points to hashsize bytes of allocated memory.
|
||||
*/
|
||||
void
|
||||
hmac_md5(unsigned char *text, int textlen, unsigned char *key, int keylen,
|
||||
unsigned char *digest)
|
||||
{
|
||||
int i;
|
||||
MD5_CTX context;
|
||||
unsigned char ipad[blocksize];
|
||||
unsigned char opad[blocksize];
|
||||
|
||||
/* too long keys are replaced by their hash value */
|
||||
if (keylen > blocksize) {
|
||||
MD5_Init(&context);
|
||||
MD5_Update(&context, key, keylen);
|
||||
MD5_Final(digest, &context);
|
||||
key = digest;
|
||||
keylen = hashsize;
|
||||
}
|
||||
|
||||
/* copy the key into the pads */
|
||||
memset(ipad, 0, sizeof(ipad));
|
||||
memcpy(ipad, key, keylen);
|
||||
|
||||
memset(opad, 0, sizeof(opad));
|
||||
memcpy(opad, key, keylen);
|
||||
|
||||
/* xor the pads with their ``basic'' value */
|
||||
for (i=0; i<blocksize; i++) {
|
||||
ipad[i] ^= 0x36;
|
||||
opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
/* inner pass (ipad ++ message) */
|
||||
MD5_Init(&context);
|
||||
MD5_Update(&context, ipad, sizeof(ipad));
|
||||
MD5_Update(&context, text, textlen);
|
||||
MD5_Final(digest, &context);
|
||||
|
||||
/* outer pass (opad ++ result of inner pass) */
|
||||
MD5_Init(&context);
|
||||
MD5_Update(&context, opad, sizeof(opad));
|
||||
MD5_Update(&context, digest, hashsize);
|
||||
MD5_Final(digest, &context);
|
||||
}
|
||||
47
proxy_c/dba/src/include/db_adaptor.h
Normal file
47
proxy_c/dba/src/include/db_adaptor.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef __DB_ADAPTOR_H
|
||||
#define __DB_ADAPTOR_H
|
||||
|
||||
#include "mysql.h"
|
||||
#include "errmsg.h"
|
||||
#include <dirent.h>
|
||||
|
||||
#ifndef _T_TYPE
|
||||
#define _T_TYPE
|
||||
|
||||
typedef unsigned char u8;
|
||||
typedef unsigned short u16;
|
||||
typedef unsigned int u32;
|
||||
typedef unsigned long DWORD;
|
||||
typedef unsigned long long u64;
|
||||
|
||||
#endif
|
||||
|
||||
#define MAX_DB_CONN 4
|
||||
#define DB_NAME_LEN 32
|
||||
#define DB_PWD_LEN 16
|
||||
#define INVALID_DB_LINK_ID 0xFF
|
||||
|
||||
typedef struct db_link
|
||||
{
|
||||
u8 enable;
|
||||
u8 role; /*1=master, 0=slave */
|
||||
u8 conn_status; /*0=not connected, 1=connected */
|
||||
char str_host_ip[16];
|
||||
u32 host_ip;
|
||||
u16 host_port;
|
||||
char db_name[DB_NAME_LEN];
|
||||
char client_name[DB_NAME_LEN];
|
||||
char client_pwd[DB_PWD_LEN];
|
||||
MYSQL *db_conn;
|
||||
}_db_link;
|
||||
|
||||
typedef struct db_cfg
|
||||
{
|
||||
u8 total_active_links;
|
||||
u8 current_working_link_id;
|
||||
u8 prefix_file_created;
|
||||
_db_link db_link[MAX_DB_CONN];
|
||||
}_db_cfg;
|
||||
|
||||
|
||||
#endif
|
||||
201
proxy_c/dba/src/logWrite.c
Normal file
201
proxy_c/dba/src/logWrite.c
Normal file
@@ -0,0 +1,201 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <termio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/io.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
static int new_file_flag = 1;
|
||||
static char system_name[128]="dba";
|
||||
static char log_file_name[256] = {0};
|
||||
static char log_path[256]="/var/log";
|
||||
static int log_max_files=100;
|
||||
static int max_file_size=10240; //10M
|
||||
|
||||
FILE *logFile_fp = NULL;
|
||||
|
||||
|
||||
void logWriter_read_param()
|
||||
{
|
||||
char cnf_file[]="./conf/logWriter.conf";
|
||||
char info_str[512], *buf_ptr;;
|
||||
FILE *fp=NULL;
|
||||
|
||||
fp = fopen(cnf_file,"r");
|
||||
|
||||
|
||||
if(fp == NULL)
|
||||
return;
|
||||
|
||||
while (!feof(fp))
|
||||
{
|
||||
info_str[0] = '\n';
|
||||
fgets(info_str, 500, fp);
|
||||
if (info_str[0] == '#' || info_str[0] == '\n')
|
||||
continue;
|
||||
buf_ptr = info_str;
|
||||
|
||||
if(!strncasecmp(info_str,"sysName=",8))
|
||||
{
|
||||
strsep(&(buf_ptr), "=\n");
|
||||
snprintf(system_name, sizeof(system_name), "%s", strsep(&(buf_ptr), "=\n") );
|
||||
}
|
||||
if(!strncasecmp(info_str,"logPath=",8))
|
||||
{
|
||||
strsep(&(buf_ptr), "=\n");
|
||||
snprintf(log_path, sizeof(log_path), "%s", strsep(&(buf_ptr), "=\n") );
|
||||
}
|
||||
if(!strncasecmp(info_str,"logMaxFiles=",12))
|
||||
{
|
||||
log_max_files = atoi(&info_str[12]);
|
||||
if ( log_max_files > 500 )
|
||||
{
|
||||
log_max_files = 500;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(!strncasecmp(info_str,"logMaxFileSize(KB)=",19))
|
||||
{
|
||||
max_file_size = atoi(&info_str[19]);
|
||||
// Set the min file size to 100KB to control the CPU loading
|
||||
if (max_file_size < 100)
|
||||
{
|
||||
max_file_size = 100;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
printf(" sysName = %s\n logPath = %s\n logMaxFiles = %d\n logMaxFileSize(KB) = %d\n", \
|
||||
system_name, log_path, log_max_files, max_file_size);
|
||||
}
|
||||
|
||||
|
||||
int logWriter_init()
|
||||
{
|
||||
|
||||
logWriter_read_param();
|
||||
|
||||
if(access(log_path,0) == -1 )
|
||||
{
|
||||
if (mkdir(log_path,0777))
|
||||
{
|
||||
printf("creat log_path %s failed!!!", log_path );
|
||||
}
|
||||
}
|
||||
|
||||
snprintf(log_file_name, sizeof(log_file_name), "%s/%s.log", log_path, system_name);
|
||||
logFile_fp = fopen ( log_file_name, "a" );
|
||||
|
||||
printf("logWriter Init Complete!\n");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int logWriter_write_log_to_disk(char *log_record)
|
||||
{
|
||||
short len;
|
||||
if(log_record == NULL)
|
||||
return 0;
|
||||
|
||||
len = strlen(log_record);
|
||||
fwrite ( log_record, len, 1, logFile_fp );
|
||||
fflush ( logFile_fp ); // force to write to harddisk
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int myLOG_D( const char *fmt, ...)
|
||||
{
|
||||
va_list apa;
|
||||
char buf[10240];
|
||||
//return 0;
|
||||
va_start(apa, fmt);
|
||||
vsprintf(buf, fmt, apa);
|
||||
va_end(apa);
|
||||
|
||||
logWriter_write_log_to_disk(buf);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void logWriter_rename_log_file()
|
||||
{
|
||||
int i;
|
||||
char old_file_name[256], new_file_name[256];
|
||||
|
||||
if (log_max_files == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for ( i=(log_max_files-1); i>0; i-- )
|
||||
{
|
||||
snprintf(old_file_name, sizeof(old_file_name), "%s/%s.log.%d", log_path, system_name, i);
|
||||
if (1 == i)
|
||||
{
|
||||
snprintf(new_file_name, sizeof(new_file_name), "%s/%s.log", log_path, system_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(new_file_name, sizeof(new_file_name), "%s/%s.log.%d", log_path, system_name, (i-1) );
|
||||
}
|
||||
if ( access(new_file_name, R_OK) == 0 )
|
||||
{
|
||||
rename(new_file_name, old_file_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int logWriter_rt()
|
||||
{
|
||||
struct stat buf;
|
||||
char real_path[128];
|
||||
|
||||
if (log_file_name[0] != 0)
|
||||
{
|
||||
realpath(log_file_name, real_path);
|
||||
stat(real_path, &buf);
|
||||
if ( buf.st_size >= (max_file_size*1024) )
|
||||
{
|
||||
new_file_flag = 1;
|
||||
logWriter_rename_log_file();
|
||||
}
|
||||
}
|
||||
|
||||
if ( 1 == new_file_flag )
|
||||
{
|
||||
if (logFile_fp != NULL)
|
||||
{
|
||||
fclose ( logFile_fp );
|
||||
logFile_fp = NULL;
|
||||
}
|
||||
logFile_fp = fopen ( log_file_name, "a" );
|
||||
new_file_flag = 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
//int main(int argc, char **argv)
|
||||
//{
|
||||
// logWriter_init();
|
||||
//
|
||||
// while(1)
|
||||
// {
|
||||
// logWriter_rt();
|
||||
// usleep(10);
|
||||
// }
|
||||
//
|
||||
// return 1;
|
||||
//}
|
||||
188
proxy_c/dba/src/plan_record.c
Normal file
188
proxy_c/dba/src/plan_record.c
Normal file
@@ -0,0 +1,188 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
typedef unsigned char u8;
|
||||
typedef unsigned int u32;
|
||||
typedef unsigned short u16;
|
||||
|
||||
#define PLAN_RECORDS 60000
|
||||
#define INVALID_PLAN_INDEX 0xFFFFFFFF
|
||||
|
||||
typedef struct plan_record
|
||||
{
|
||||
u32 index;
|
||||
u8 used_flag;
|
||||
u32 plan_id;
|
||||
u32 updated_time;
|
||||
}__attribute__((packed)) _plan_record;
|
||||
|
||||
typedef struct plan_table
|
||||
{
|
||||
u32 record_num;
|
||||
u32 cur_index;
|
||||
_plan_record plan_record[PLAN_RECORDS+1];
|
||||
}__attribute__((packed)) _plan_table;
|
||||
|
||||
/* ------------ Variables --------------*/
|
||||
static GHashTable *htb_plan = NULL;
|
||||
|
||||
static _plan_table plan_table;
|
||||
|
||||
extern struct timeval tv;
|
||||
|
||||
/* ------------ resouce functions --------------*/
|
||||
|
||||
void free_mem(gpointer data)
|
||||
{
|
||||
free(data);
|
||||
}
|
||||
|
||||
int crm_pxy_init_hash_table()
|
||||
{
|
||||
|
||||
if(htb_plan != NULL)
|
||||
g_hash_table_destroy(htb_plan);
|
||||
|
||||
htb_plan = g_hash_table_new(g_direct_hash,g_direct_equal);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int hash_add_plan_entity(_plan_record *plan_ptr)
|
||||
{
|
||||
if(plan_ptr != NULL)
|
||||
g_hash_table_insert(htb_plan, GINT_TO_POINTER(plan_ptr->plan_id), plan_ptr);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int hash_clean_plan_entity(_plan_record *plan_ptr)
|
||||
{
|
||||
if(plan_ptr != NULL)
|
||||
g_hash_table_remove(htb_plan, GINT_TO_POINTER(plan_ptr->plan_id));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int crm_pxy_get_plan_index(u32 plan_id)
|
||||
{
|
||||
u32 index = INVALID_PLAN_INDEX;
|
||||
_plan_record *ptr=NULL;
|
||||
|
||||
ptr = (_plan_record *)g_hash_table_lookup(htb_plan, GINT_TO_POINTER(plan_id));
|
||||
if(ptr != NULL)
|
||||
index = ptr->index;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
int crm_pxy_get_plan_record(u32 plan_id, _plan_record *plan_ptr)
|
||||
{
|
||||
u32 index = crm_pxy_get_plan_index(plan_id);
|
||||
|
||||
return index;
|
||||
|
||||
if(index != INVALID_PLAN_INDEX)
|
||||
{
|
||||
plan_ptr = &plan_table.plan_record[index];
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
_plan_record *crm_pxy_assign_plan_index(u32 plan_id)
|
||||
{
|
||||
register int loop;
|
||||
int index;
|
||||
_plan_record *plan_ptr;
|
||||
|
||||
for(loop = 0; loop < PLAN_RECORDS; loop++)
|
||||
{
|
||||
index = plan_table.cur_index++;
|
||||
plan_table.cur_index %= PLAN_RECORDS;
|
||||
if(plan_table.plan_record[index].used_flag == 0)
|
||||
{
|
||||
plan_table.record_num ++;
|
||||
|
||||
plan_ptr = &plan_table.plan_record[index];
|
||||
plan_ptr->used_flag = 1;
|
||||
plan_ptr->index = index;
|
||||
plan_ptr->plan_id = plan_id;
|
||||
plan_ptr->updated_time = tv.tv_sec;
|
||||
return plan_ptr;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int crm_pxy_purge_plan_record(_plan_record *plan_ptr)
|
||||
{
|
||||
|
||||
hash_clean_plan_entity(plan_ptr);
|
||||
|
||||
plan_ptr->used_flag = 0;
|
||||
plan_table.record_num --;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int crm_pxy_add_plan_record(u32 plan_id)
|
||||
{
|
||||
_plan_record *plan_ptr = NULL;
|
||||
|
||||
plan_ptr = crm_pxy_assign_plan_index(plan_id);
|
||||
|
||||
if(plan_ptr == NULL)
|
||||
return 0;
|
||||
|
||||
hash_add_plan_entity(plan_ptr);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int crm_pxy_check_plan_record(u32 plan_id)
|
||||
{
|
||||
u32 index = crm_pxy_get_plan_index(plan_id);
|
||||
|
||||
if(index != INVALID_PLAN_INDEX)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
void crm_pxy_plan_record_init()
|
||||
{
|
||||
crm_pxy_init_hash_table();
|
||||
}
|
||||
|
||||
static int EXPIRED_TIME_SEC = (86400)*(15);
|
||||
|
||||
void crm_pxy_plan_record_timer()
|
||||
{
|
||||
static int crm_pxy_scan_ptr=0;
|
||||
int i;
|
||||
_plan_record *plan_ptr = NULL;
|
||||
|
||||
for(i=0; i<1000; i++)
|
||||
{
|
||||
plan_ptr = &plan_table.plan_record[crm_pxy_scan_ptr++];
|
||||
crm_pxy_scan_ptr %= PLAN_RECORDS;
|
||||
|
||||
if(plan_ptr->used_flag == 1)
|
||||
{
|
||||
if((plan_ptr->updated_time + EXPIRED_TIME_SEC)<tv.tv_sec)
|
||||
hash_clean_plan_entity(plan_ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user