Exemplo n.º 1
0
int CTimer::StartTimer(double first, double interval){
	if(m_timer == 0){
		NLOG_ERROR("Timer Not Tnitialized!");
		return -1;
	}

	//first == 0
	if(fabs(first) < 0.000000001){
		NLOG_ERROR("arg first [%lf] Is Invalid!", first);
		return -1;
	}
	//interval == 0
	if(fabs(interval) < 0.000000001){
		NLOG_ERROR("arg interval [%lf] Is Invalid!", interval);
		return -1;
	}

	m_first = first;
	m_interval = interval;
	
	 struct  itimerspec timespec;
	 memset(&timespec, 0, sizeof(timespec));
	 timespec.it_value.tv_sec = Floor(first);
	 timespec.it_value.tv_nsec = ToNanoseconds(Frac(first));
	 timespec.it_interval.tv_sec = Floor(interval);
	 timespec.it_interval.tv_nsec =ToNanoseconds(Frac(interval));
	/* NLOG_DEBUG("Timer(%s) Start! First Run[%.6lf], Interval[%.6lf]",
					m_name,first, interval);
	*/
	return timer_settime(m_timer, 0, &timespec, NULL);		 
}
Exemplo n.º 2
0
ngx_chain_t* ngx_http_shmtest_resp(ngx_http_request_t *r, const char* output, int size){
	ngx_chain_t* chain = ngx_alloc_chain_link(r->pool);
	if(chain == NULL){
		NLOG_ERROR("Failed to allocate response chain");
		return NULL;
	}
	
    u_char* buf = (u_char*)ngx_pcalloc(r->pool, size);
	if(buf == NULL){
		NLOG_ERROR("Failed to allocate response buffer.");
        return NULL;
	}
	ngx_memcpy(buf, output, size);
	
    ngx_buf_t    *b;
    b = (ngx_buf_t*)ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    if (b == NULL) {
		NLOG_ERROR("Failed to allocate response buffer.");
        return NULL;
    }
    b->memory = 1;
    b->last_buf = 1;

    chain->buf = b;
    chain->next = NULL;
    b->pos = (u_char*)buf;
    b->last = (u_char*)(b->pos+size);

	return chain;

}
Exemplo n.º 3
0
/**
 * 上下文初始化
 */
int TestContext::ctx_init(ngx_cycle_t* cycle)
{
	test_config_t* cfg = &this->m_config;
	this->m_cycle = cycle;
	this->m_mysql_pool = mysql_pool_new(cfg->mysql_config.conns, &cfg->mysql_config);
	NLOG_INFO("[%d] mysql_pool: %p", ngx_getpid(), this->m_mysql_pool);
	#define SQL_CREATE_TBL "drop table if exists ngx_test; "\
				"create table ngx_test(`key` varchar(32) primary key,value int default 0); "\
				"insert into ngx_test values('testkey', 0);"
	int ret = 0;
	MYSQL* mysql = this->mysql_get();
	if(mysql == NULL){
		NLOG_ERROR("get mysql connection failed!");
		ret = ERRNO_SYSTEM;
		return ret; 
	}
	NLOG_INFO("init ngx_test table!");
	ret = mysql_queryex(mysql, SQL_CREATE_TBL);
	NLOG_INFO("init ngx_test table ret:%d", ret);
	if(ret == 0){ 
		mysql_free_all_result(mysql);
	}else{ 
		NLOG_ERROR("init table failed!ret:%d, sql:[%s]", ret, SQL_CREATE_TBL);
		ret = ERRNO_SYSTEM;
	}
	// 在Init中取得的链接必须关闭掉,因为这个链接不是非阻塞的。
	this->mysql_close(mysql);

	return NGX_OK;  

}
Exemplo n.º 4
0
static ngx_int_t ngx_http_shmtest_counter_inc_int(ngx_http_request_t *r)
{
	ngx_int_t rc = NGX_HTTP_OK;
	ngx_str_t key = ngx_null_string;
	ngx_str_t szn = ngx_null_string;
	ngx_int_t n = 1;
	int64_t cur = 0;
	
	if(ngx_http_arg(r, (u_char*)"key", 3, &key)!=NGX_OK){
		NLOG_ERROR("get arg 'key' failed!");
		return NGX_HTTP_BAD_REQUEST;
	}

	if(ngx_http_arg(r, (u_char*)"n", 1, &szn)==NGX_OK){
		n = ngx_atoi(szn.data, szn.len);
	}

	shmtest_main_conf_t* smcf;
	smcf = ngx_http_get_module_main_conf(r, ngx_http_shmtest_module);
	if(smcf == NULL){
		NLOG_ERROR("get module ngx_http_shmtest_module's main conf failed!");
		return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}

	ngx_shm_zone_t* zone = smcf->shmap;
	u_char* rsp = ngx_pcalloc(r->connection->pool, 256);
	int rsp_len = 0;

	rc = ngx_shmap_inc_int(zone, &key, n,0, &cur);

	
	if(rc == 0){
		rsp_len = ngx_sprintf(rsp, "inc_int(key=%V,n=%l)=%l\n",
						&key,n, cur)-rsp;
	}else{
		rsp_len = ngx_sprintf(rsp, "inc_int(key=%V,n=%l) failed!\n", &key,n)-rsp;
	}

	ngx_chain_t* chain = ngx_http_shmtest_resp(r, (char*)rsp, rsp_len);
	if(chain != NULL){
	    r->headers_out.content_length_n = rsp_len;
	}else{
		r->headers_out.content_length_n = 0;
	}

    rc = ngx_http_send_header(r);

    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
        
    }else{
    	rc = ngx_http_output_filter(r, chain);
    }

	return rc;	
}
Exemplo n.º 5
0
int CTimer::InitThreadTimer(TimerProc proc, void* proc_arg, size_t threadStackSize){
	if(m_timer != 0){
		NLOG_ERROR("Timer Inited ! m_timer(=%ld) != 0", (long)m_timer);
		return -1;
	}
	
	pthread_attr_t pthread_attr;
	pthread_attr_init(&pthread_attr);
	pthread_attr_setstacksize(&pthread_attr, threadStackSize * 1024);

	m_timerArg.proc = proc;
	m_timerArg.args = proc_arg;
	
	struct sigevent evt;
	memset(&evt, 0, sizeof(evt));
	
	evt.sigev_notify = SIGEV_THREAD;
	evt.sigev_value.sival_ptr = &m_timerArg;
	evt.sigev_notify_function = &s_timer_proc;
	evt.sigev_notify_attributes = &pthread_attr;

	int ret = timer_create(CLOCK_REALTIME, &evt, &m_timer);

	pthread_attr_destroy(&pthread_attr);
	if(m_name[0] == 0){//Name is NULL
		sprintf(m_name, "ThreadTimer-%ld", (long)m_timer);
	}	
	return ret;
}
Exemplo n.º 6
0
int CTimer::InitSignalTimer(TimerProc proc, void* proc_arg){
	if(m_timer != 0){
		NLOG_ERROR("Timer Inited ! m_timer(=%ld) != 0", (long)m_timer);
		return -1;
	}

	m_timerArg.proc = proc;
	m_timerArg.args = proc_arg;
#if 0
	sigset_t sigset;
	sigfillset (&sigset);
	sigdelset (&sigset, SIGUSR2);
	sigprocmask (SIG_SETMASK, &sigset, NULL);
#endif

	struct sigaction action;
	memset(&action, 0, sizeof(action));
	sigemptyset(&action.sa_mask);
	action.sa_flags = SA_SIGINFO|SA_RESTART;
	action.sa_sigaction = &s_sigaction;
	if(sigaction(SIGUSR2, &action, NULL) <0){
		NLOG_ERROR("sigactin failed! err:%s", strerror(errno));
		return -1;
	}

	struct sigevent evt;
	memset(&evt, 0, sizeof(evt));
	evt.sigev_notify = SIGEV_SIGNAL;
	evt.sigev_signo = SIGUSR2;
	evt.sigev_value.sival_ptr = (void*)&m_timerArg;
	int ret = timer_create(CLOCK_REALTIME, &evt, &m_timer);
	if(m_name[0] == 0){//Name is NULL
		sprintf(m_name, "SignalTimer-%ld", (long)m_timer);
	}	

	return ret;
}
Exemplo n.º 7
0
static ngx_int_t ngx_http_shmtest_handler(ngx_http_request_t *r)
{
    //ngx_int_t     rc = NGX_DONE;

	//NLOG_DEBUG("### %.*s ###", r->request_line.len,r->request_line.data);

	if(r->uri.len == 2){
		char func = r->uri.data[1];
	    switch(func){
		case 'a': //add
			return ngx_http_shmtest_add_or_update(r, FUNC_ADD);
		case 's': //set
			return ngx_http_shmtest_add_or_update(r, FUNC_SET);
		case 'r': //replace
			return ngx_http_shmtest_add_or_update(r, FUNC_REPLACE);
		case 'd': //delete
			
		break;
		case 'g': //get
			return ngx_http_shmtest_get(r);
		case 'c': //counter
			return ngx_http_shmtest_counter_inc_int(r);
		default:
			NLOG_ERROR("Req [%.*s] Not Found! uri[%.*s]",
				r->request_line.len,r->request_line.data,
				r->uri.len,r->uri.data);
			return NGX_HTTP_NOT_FOUND;
	    }
		return NGX_HTTP_OK;
	}else{//未处理的请求。。
		NLOG_ERROR("Req [%.*s] Not Found! uri[%.*s]",
			r->request_line.len,r->request_line.data,
			r->uri.len,r->uri.data);
		return NGX_HTTP_NOT_FOUND;
	}
    return NGX_DONE;
}
Exemplo n.º 8
0
void SyncFiles::Run()
{
	stCpyFile orden;

	SetId("SyncFiles");

	while (IsRunning())
	{
		bool _sincronizando = false;
		this->sleep(10);
		while (Get(orden))
		{
			NLOG_DEBUG("FTP. Sincronizando Fichero %s en %s", orden.src.c_str(), orden.ipto.c_str());
			_sincronizando = _sincronizando ? true : orden.bRecargar;
#ifdef _WIN32
			if (LocalConfig::cfg.winSyncFtp())
#endif
			{
				FtpClient ftp(orden.ipto, "root", "");
				try
				{
					/** Copia ficheros en remoto... */
					ftp.Login();
					ftp.Upload(orden.src, orden.dest);
					ftp.Close();
					NLOG_DEBUG("FTP. Fichero %s Sincronizado.", orden.src.c_str());
				}
				catch(FtpClientException x)
				{
					/** Normalmente si salta el error vacio la lista para evitar encadenar errores. */
					ordenes.clear();
					NLOG_ERROR("FTP EXCEPTION: %s", x.what().c_str());
				}
			}
		}
		if (_sincronizando)
		{
			/** Avisa Cambio Ficheros... */			
			HttpClient::http.AvisaCambioConfiguracion(orden.ipto + ":" + LocalConfig::cfg.PuertoEscucha());
		}
	}
}
Exemplo n.º 9
0
void *CThread::sRun(void *pParam)
{	
    CThread *pTh = (CThread *)pParam;
	((CThread *)pParam)->m_Dead.Start();
	((CThread *)pParam)->m_bLive = true;
	try
	{
		((CThread *)pParam)->Run();
	}
	catch(...)
	{
        NLOG_ERROR("Error Irrecuperable en THREAD %s\n", pTh->m_id.c_str());
		sleep(500);
		assert(0);
	}

	((CThread *)pParam)->m_bLive = false;
	((CThread *)pParam)->m_Dead.Signal();

	return 0;
}
Exemplo n.º 10
0
static ngx_int_t ngx_http_shmtest_get(ngx_http_request_t *r){
	ngx_int_t rc = NGX_HTTP_OK;
	ngx_str_t key = ngx_null_string;
	ngx_str_t value = ngx_null_string;
	int32_t ikey = 0;
	uint8_t value_type = VT_BINARY;
	uint32_t exptime = 0;
	uint32_t user_flags = 0;
	
	if(ngx_http_arg(r, (u_char*)"key", 3, &key)!=NGX_OK){
		NLOG_ERROR("get arg 'key' failed!");
		return NGX_HTTP_BAD_REQUEST;
	}
	if(key.len > 2 && key.data[0] == '0' &&	key.data[1] == 'x'){
		key.data += 2;
		key.len -= 2;
		ikey = ngx_hextoi(key.data, key.len);
		ngx_str_set_int32(&key, &ikey);
		NLOG_DEBUG("use int key ikey=%d", ikey);
	} 

	shmtest_main_conf_t* smcf;
	smcf = ngx_http_get_module_main_conf(r, ngx_http_shmtest_module);
	if(smcf == NULL){
		NLOG_ERROR("get module ngx_http_shmtest_module's main conf failed!");
		return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}

	ngx_shm_zone_t* zone = smcf->shmap;
	u_char* rsp = ngx_pcalloc(r->connection->pool, 256);
	int rsp_len = 0;

	rc = ngx_shmap_get(zone, &key, &value, &value_type,
				&exptime, &user_flags);

	if(ikey != 0){
		if(rc == 0){
			rsp_len = ngx_sprintf(rsp, "get(%d)={value=%V,exptime=%d,user_flags=%d}!\n",
							ikey,&value,exptime,user_flags)-rsp;
		}else{
			rsp_len = ngx_sprintf(rsp, "get(%d) failed!\n", ikey)-rsp;
		}
	}else{
		if(rc == 0){
			rsp_len = ngx_sprintf(rsp, "get(%V)={value=%V,exptime=%d,user_flags=%d}!\n",
							&key,&value,exptime,user_flags)-rsp;
		}else{
			rsp_len = ngx_sprintf(rsp, "get(%V) failed!\n", &key)-rsp;
		}
	}

	ngx_chain_t* chain = ngx_http_shmtest_resp(r, (char*)rsp, rsp_len);
	if(chain != NULL){
	    r->headers_out.content_length_n = rsp_len;
	}else{
		r->headers_out.content_length_n = 0;
	}

    rc = ngx_http_send_header(r);

    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
        
    }else{
    	rc = ngx_http_output_filter(r, chain);
    }

	return rc;
}
Exemplo n.º 11
0
static ngx_int_t ngx_http_shmtest_add_or_update(ngx_http_request_t *r,int func){
	ngx_int_t rc = NGX_HTTP_OK;
	ngx_str_t key = ngx_null_string;
	int32_t ikey = 0;
	ngx_str_t value = ngx_null_string;
 	char* szFunc = funcs[func];
	
	if(ngx_http_arg(r, (u_char*)"key", 3, &key)!=NGX_OK){
		NLOG_ERROR("get arg 'key' failed!");
		return NGX_HTTP_BAD_REQUEST;
	}

	if(ngx_http_arg(r, (u_char*)"value", 5, &value)!=NGX_OK){
		NLOG_ERROR("get arg 'value' failed!");
		return NGX_HTTP_BAD_REQUEST;
	}

	//如果key开始为0x 表示使用数字的KEY.
	if(key.len > 2 && key.data[0] == '0' &&	key.data[1] == 'x'){
		key.data += 2;
		key.len -= 2;
		ikey = ngx_hextoi(key.data, key.len);
		ngx_str_set_int32(&key, &ikey);
		NLOG_DEBUG("use int key ikey=%d", ikey);
	}
	
	uint64_t exptime = 0;
	ngx_str_t sexptime = ngx_null_string;
	if(ngx_http_arg(r, (u_char*)"exptime", 7, &sexptime)==NGX_OK){
		exptime = ngx_parse_time(&sexptime, 1);
	}

	if(ikey != 0){
		NLOG_DEBUG("%s(key=%d,value=%V,exptime=%d)", szFunc,ikey,&value,exptime);
	}else{
		NLOG_DEBUG("%s(key=%V,value=%V,exptime=%d)", szFunc,&key,&value,exptime);
	}
	shmtest_main_conf_t* smcf;
	smcf = ngx_http_get_module_main_conf(r, ngx_http_shmtest_module);
	if(smcf == NULL){
		NLOG_ERROR("get module ngx_http_shmtest_module's main conf failed!");
		return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}

	ngx_shm_zone_t* zone = smcf->shmap;
	
	int ret = 0;
	switch(func){
	case FUNC_ADD:
		ret = ngx_shmap_add(zone, &key,&value,VT_STRING,exptime,0);
	break;
	case FUNC_SET:
		ret = ngx_shmap_set(zone, &key,&value,VT_STRING,exptime,0);
	break;
	case FUNC_REPLACE:
		ret = ngx_shmap_replace(zone, &key,&value,VT_STRING,exptime,0);
	break;
	default:
		NLOG_ERROR("un process type [%d]", func);
		return NGX_HTTP_BAD_REQUEST;
	}

	char* rsp = ngx_pcalloc(r->connection->pool, 256);
	int rsp_len = 0;
	if(ret == 0){
		rsp_len = sprintf(rsp, "%s success!\n", szFunc);
	}else{
		rsp_len = sprintf(rsp, "%s failed!\n", szFunc);
	}

	ngx_chain_t* chain = ngx_http_shmtest_resp(r, rsp, rsp_len);
	if(chain != NULL){
	    r->headers_out.content_length_n = rsp_len;
	}else{
		r->headers_out.content_length_n = 0;
	}

    rc = ngx_http_send_header(r);

    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
        
    }else{
    	rc = ngx_http_output_filter(r, chain);
    }

	return rc;
}
Exemplo n.º 12
0
int RunTcpTestClient(const char* ip, int port, unsigned int threads, 
			unsigned int request_count, FTcpRequest RequestCb, void* args,
			FClientCtxNew ClientNewCb, FClientCtxFree ClientFreeCb,int expectedCode, int num)
{
	if(threads > MAX_TEST_THREADS){
		NLOG_ERROR("threads(%d) > MAX_TEST_THREADS(%d)", threads, MAX_TEST_THREADS);
		return -1;
	}
	signal(SIGINT, test_signal_handler);
	signal(SIGQUIT, test_signal_handler);

	int connections = threads;

	g_thread_cnt = threads;
	g_timeTimer.InitThreadTimer(&TimerAdd, NULL);
	g_timeTimer.SetTimerName("timeTimer");
	g_timeTimer.StartTimer(0.001, 0.001);

	memset(&g_errors, 0, sizeof(int)*0xFFFF);
	memset(&g_threads, 0, sizeof(CThread*)*MAX_TEST_THREADS);
	memset(&g_ctxs, 0, sizeof(ClientCtx*)*MAX_TEST_THREADS);
	
	unsigned int i;
	NLOG_INFO("################## Test Begin ###################");
	NLOG_INFO("################# Connect Begin #################");
	for(i=0;ClientNewCb != NULL && i < connections; i++){
		g_ctxs[i] = ClientNewCb(ip, port); 
		if(g_ctxs[i] == NULL){
			NLOG_ERROR("Connec to [%s:%d] failed!", ip,port);
			return -1;
		}
	}
	NLOG_INFO("#################  Connect End  #################");

	g_start = second();
	g_now_second = second();

	char threadName[32];
	for(i=0;i < threads; i++){
		sprintf(threadName, "thread-%d", i);
		
		NLOG_INFO("########## One Thread One Socket ##############");
		g_threads[i] = new CTcpTestThread(threadName, g_ctxs[i],
			request_count, &g_counter, RequestCb, args, num);
		
		
		g_threads[i]->Start();

	}
	
	for(i=0;i < threads; i++){
		g_threads[i]->Join();
		delete g_threads[i];
		g_threads[i] = NULL;
	}
	
	for(i=0;ClientFreeCb != NULL&&i < connections; i++){
		ClientFreeCb(g_ctxs[i]);
		g_ctxs[i] = NULL;
	}
	
	NLOG_INFO("################## Test End ###################");
	if((unsigned)g_counter > request_count){//修正for循环引起的超界问题。
		g_counter = request_count;
	}
	Report(expectedCode);

	return 0;
}