Beispiel #1
0
//read config from file
int NDInstanceBase::ReadConfig(const char *configname) 
{
	int ret;
	ndxml *xmlroot ;
	ndxml_root xmlfile;

	ret = ndxml_load(config_file, &xmlfile) ;
	if(0!=ret) {
		nd_logfatal("load xml from file\n") ;
		return -1;
	}

	xmlroot = ndxml_getnode(&xmlfile,"root") ;
	if(!xmlroot) {		
		ndxml_destroy(&xmlfile);
		nd_logfatal("xml read error : root-node can not found \n") ;
		return -1;
	}

	if(-1==read_config(xmlroot, configname, &m_config) ) {
		ndxml_destroy(&xmlfile);
		nd_logfatal("xml config error %s node \n", configname) ;
		return -1;
	}

	ndxml_destroy(&xmlfile);
    
	return 0;

}
int apoAttrCalcHelper::place_param_name(char *input, char *buf, int size)
{
	int i;

	if (!input || *input == 0) {
		nd_logfatal("parse error input param is NUMM\n");
		return -1;
	}

	role_attr_description  *pwa = m_wahelper_bufs;
	for (i = 0; i<m_wa_num; i++, pwa++) {
		if (pwa->wa_id == INVALID_ATTR_ID || !pwa->name.c_str()) {
			continue;
		}
		if (0 == strcmp(input, pwa->name.c_str()) || 0 == ndstricmp(input, pwa->real_name.c_str())) {
			snprintf(buf, size, "[%d]", pwa->wa_id);
			if (m_current_parse != i) {
				//pwa->infections[(pwa->infection_num)++] = i;		
				//m_wahelper_bufs[m_current_parse].need_buf[(m_wahelper_bufs[m_current_parse].need_num)++] = i;		

				pwa->infections.push_back(m_current_parse);
				m_wahelper_bufs[m_current_parse].need_buf.push_back(i);
			}
			return 0;
		}
	}

	//
	nd_logfatal("parse error :can not find[%s]\n" AND input);
	return -1;
}
Beispiel #3
0
int thmsg_test()
{
	struct nd_thsrv_createinfo srv_info = {0} ;
	
	srv_info.run_module =SUBSRV_MESSAGE;					//srv_entry run module (ref e_subsrv_runmod)
	srv_info.srv_entry =NULL;		//service entry
	srv_info.srv_param = NULL ;					//param of srv_entry 
	
	ndstrcpy(srv_info.srv_name, "receiver") ;

	_recv_id = nd_thsrv_create(&srv_info) ;
	if(-1==_recv_id) {
		nd_logfatal("create recv server error !") ;
		exit(1) ;
	}
	
	srv_info.run_module =SUBSRV_RUNMOD_LOOP;					//srv_entry run module (ref e_subsrv_runmod)
	srv_info.srv_entry =sender;		//service entry
	srv_info.srv_param = NULL ;					//param of srv_entry 
	
	ndstrcpy(srv_info.srv_name, "sender") ;
	_send_id = nd_thsrv_create(&srv_info);
	if(-1== _send_id) {
		nd_logfatal("create sender server error !") ;
		exit(1) ;
	}
	nd_thsrv_hook(nd_thsrv_gethandle(_send_id),_msg_func) ;
	nd_thsrv_hook(nd_thsrv_gethandle(_recv_id),_msg_func) ;

	nd_thsrv_wait(_send_id) ;
	nd_thsrv_wait(_recv_id) ;
	/*context = nd_thsrv_gethandle(_send_id) ;
	if(context->th_handle)
		nd_waitthread(context->th_handle) ;

	//fprintf(stderr, "wait for received thread!\n") ;
	//nd_waitthread(_recv_id) ;
	context = nd_thsrv_gethandle(_recv_id) ;
	if(context)
		nd_waitthread(context->th_handle) ;
	*/

	ndprintf("wait received success exit!\n") ;
	
	nd_thsrv_release_all() ;
	
	nd_timer_destroy(__s_timer,0) ;
	nd_timer_destroy(__r_timer,0) ;
//	nd_common_release() ;
	
//	getch();
	return 0;
}
Beispiel #4
0
int nd_net_bind(int port, int listen_nums,nd_handle net_handle) 
{
	ndsocket_t fd;
	struct nd_netsocket *node = (struct nd_netsocket *)net_handle ;
	//SOCKADDR_IN *remote = NULL;

	nd_assert(node) ;
	node->myerrno = NDERR_SUCCESS ;

	fd = nd_socket_openport(port,node->sock_type, node->sock_protocol, node->bindip, listen_nums) ;
	
	if(-1==fd){
		nd_logfatal("open port %s" AND nd_last_error()) ;
		node->myerrno = NDERR_OPENFILE ;
		return -1 ;
	}
	if(0==port && SOCK_DGRAM==node->sock_type) {
		short tmp = nd_sock_getport(fd) ;
		port =(int) ntohs(tmp); 
	}

	node->fd = fd ;
	node->port = htons(port) ;
	node->status = 1 ;
	return 0 ;

}
int apoAttrCalcHelper::preParseFormula()
{
	m_current_parse = 0;
	role_attr_description  *pwa = m_wahelper_bufs;
	for (int i = 0; i<m_wa_num; i++, pwa++) {
		if (pwa->wa_id == INVALID_ATTR_ID) {
			continue;
		}
		
		if (!pwa->input_for.empty()) {
			m_current_parse = i;

			char f_buf[APO_FORMULA_SIZE] ;
			size_t size = vm_parse_expression(pwa->input_for.c_str(),f_buf, sizeof(f_buf), (vm_param_replace_func)place_name_entry, this);
			if (size == 0) {
				nd_logfatal("parse [%d] formula %s error " ,i, pwa->input_for.c_str());
				return -1;
			}
			pwa->cmd_data.Setcmd(f_buf, (int)size);
		}
	}


	if (parse_run_sort() == -1) {
		return -1;
	}
 
	return 0;

}
//replace name on running
static int apo_place_name_runtime(char *input, char *buf, int size, void *user_data)
{
	int i;
	if (!input || *input == 0) {
		nd_logfatal("parse error input param is NUMM\n");
		return -1;
	}
	apoAttrCalcHelper *waHelper = (apoAttrCalcHelper*)user_data;
	role_attr_description  *pwa = waHelper->m_wahelper_bufs;
	for (i = 0; i < waHelper->m_wa_num; i++, pwa++) {
		if (pwa->wa_id == INVALID_ATTR_ID) {
			continue;
		}
		if (0 == strcmp(input, pwa->name.c_str()) || 0 == ndstricmp(input, pwa->real_name.c_str())) {
			snprintf(buf, size, "[%d]", pwa->wa_id);
			return 0;
		}
	}


	nd_logfatal("parse error, can not found[%s]\n" AND input);
	return -1;
}
Beispiel #7
0
int read_dbconfig(const char *fileName, const char *dbCfgname ,struct nd_db_config *db_cfg)
{
	//ND_TRACE_FUNC() ;
	
	int ret = -1;
	ndxml *xmlroot, *xmlsub, *xmlnode ;
	ndxml_root xmlfile;
	
#define GET_VAL(name)                       \
	xmlnode = ndxml_refsub(xmlsub, #name) ; \
	if (!xmlnode){                          \
		ret = -1;                               \
		goto READ_EXIT ;                        \
	}                                       \
	strncpy(db_cfg->db_##name, ndxml_getval(xmlnode), sizeof(db_cfg->db_##name) )
	
	
	if ( 0 != ndxml_load(fileName , &xmlfile ) ){
		nd_logfatal("read file %s error %s\n", fileName, nd_last_error() ) ;
		return -1;
	}
	
	xmlroot = ndxml_getnode( &xmlfile, "root" ) ;
	if ( !xmlroot )   {
		goto READ_EXIT;
	}
	//read connect config
	xmlsub = ndxml_refsub( xmlroot, dbCfgname ) ;
	if ( !xmlsub )   {
		goto READ_EXIT;
	}
	
	GET_VAL( host ) ;
	GET_VAL( database ) ;
	GET_VAL( user ) ;
	GET_VAL( password) ;
	ret = 0 ;
	
	
READ_EXIT:
	ndxml_destroy( &xmlfile );
	return ret ;
}
// parse run-sort
int apoAttrCalcHelper::parse_run_sort()
{
	int i;
	int number = m_wa_num;
	int done = 0;

	attrid_t walked[APO_ATTR_CAPACITY *2];

	memset(walked, 0, sizeof(walked));

	do {
		done = 0;
		for (i = 0; i<m_wa_num; i++) {
			if (m_wahelper_bufs[i].wa_id == INVALID_ATTR_ID) {
				continue;
			}

			if (walked[m_wahelper_bufs[i].wa_id]) {
				continue;
			}
			if (m_wahelper_bufs[i].cmd_data.size > 0) {
				if (0 == check_canbe_run(&m_wahelper_bufs[i], walked)) {
					m_run_sort.push_back(m_wahelper_bufs[i].wa_id);
					//m_run_sort.sort_buf[m_run_sort.num++] = m_wahelper_bufs[i].wa_id;
					walked[m_wahelper_bufs[i].wa_id] = 1;
				}
				else {
					++done;
				}
			}
			else {
				walked[m_wahelper_bufs[i].wa_id] = 1;
			}
		}
	} while (done && (--number > 0));
	if (number <= 0) {

		nd_logfatal("parse error the input-param reference each other\n");
		return -1;
	}

	return 0;
}
Beispiel #9
0
int NDInstanceBase::Create(int argc,const char *argv[])
{
	int i ;
	const char *logfileName = NULL ;
	if (m_bCreated)	{
		return 0;
	}

    system_signals_init() ;
    
	nd_arg(argc, argv);	
	
	//get config file 	
	for (i=1; i<argc; i++){
		if(0 == strcmp(argv[i],"-f" ) && i< argc-1) {
			NDInstanceBase::config_file = argv[++i] ;
		}
		else if(0== strcmp(argv[i], "-c") && i< argc-1) {
			m_config_name = argv[++i] ;
		}
		else if(0==strcmp(argv[i],"-nodev")) {
			m_un_develop = 1;
		}
		
		else if(0==strcmp(argv[i],"-log") && i<argc -1) {
			logfileName = argv[++i];
		}
		else if(0==strcmp(argv[i],"-workdir") &&i<argc -1 ) {
			nd_chdir(argv[++i]) ;
		}
		else if(0==strcmp(argv[i],"-v") || 0==strcmp(argv[i],"--version") ) {
			fprintf(stdout, "version : %s \n" , __g_version_desc) ;
			exit(0) ;
		}
		else if(0==strcmp(argv[i],"-l") || 0==strcmp(argv[i],"--rlimit") ) {
			char buf[1024] ;
			fprintf(stdout, "rlimit: \n %s\n" , get_rlimit_info(buf, sizeof(buf)) ) ;
			exit(0) ;
		}

        else if(0==strcmp(argv[i],"-pid") && i<argc -1){
            FILE *pf = fopen(argv[i+1], "w") ;
            if (pf) {
                fprintf(pf, "%d", nd_processid()) ;
                fclose(pf) ;
            }
            else {
                fprintf(stderr,"write pid error %s file not exist\n", argv[i+1]) ;
                exit(1) ;
            }
        }
	}

	if(!config_file|| !m_config_name) {
		printf("usage: -f config-file -c config-name\n press ANY key to continue\n") ;
		//getch() ;
		exit(1) ;
		//return -1 ;
	}

	if(-1==ReadConfig(m_config_name) ) {
		printf("Read config %s file error \n press ANY key to continue\n", m_config_name) ;
		//getch() ;
		exit(1) ;
	}
	
	nd_set_exit_callback(applib_exit_callback) ;

	if (m_config.i_cfg.open_dump){
#ifndef ND_UNIX
		::SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)nd_unhandler_except);
#else
		enable_core_dump() ;
#endif
	}
	size_t max_connector = m_config.l_cfg.max_connect ;
	if ( max_connector > get_maxopen_fd() ) {
		size_t new_fd_num = set_maxopen_fd(max_connector) ;
		nd_logmsg("set max connect %d real max connect =%d\n",m_config.l_cfg.max_connect, (int) new_fd_num);
	}
	
	if(logfileName && logfileName[0]) {
		strncpy(m_config.i_cfg.log_file, logfileName, sizeof(m_config.i_cfg.log_file)) ;
	}
	if (m_config.i_cfg.log_file[0]){
		set_log_file(m_config.i_cfg.log_file) ;
		if (m_config.i_cfg.log_file_size > 0) {
			nd_setlog_maxsize(m_config.i_cfg.log_file_size) ;
		}
		nd_setlog_func(nd_default_filelog) ;
	}

	if (m_config.i_cfg.callstack_file[0]){
		if(CALLSTACK_INIT(m_config.i_cfg.callstack_file)==-1) {
			nd_logfatal("create map file %s error!\n" AND m_config.i_cfg.callstack_file) ;
			return -1 ;
		}
	}

	nd_net_set_crypt((nd_netcrypt)nd_TEAencrypt, (nd_netcrypt)nd_TEAdecrypt, sizeof(tea_v)) ;
	
// 	if (g_base_inst==NULL){
// 		g_base_inst = this ;
// 	}

	OnCreate();

	m_bCreated = 1;
	return 0 ;

}