pthread_t proc_thread_select::get_select(const std::string& key)
{
	dzlog_info("thread select, key = %s", key.c_str());
	pthread_t ret = 0;
	boost::unordered_map<std::string, proc_thread_node>::iterator iter;
	_select_map_mutex.lock();
	iter = _select_map.find(key);
	if (_select_map.end() != iter)
	{
		//判断是否超时, 查看下是否超時
		/*if (time(NULL) - iter->second._last_select > _out_interval)
		{
			_select_map.erase(iter);
			ret = new_thread();
			if (0 != ret)
			{
				proc_thread_node new_node;
				new_node.key = key;
				new_node._last_select = time(NULL);
				new_node._pthread_id = ret;
				_select_map.insert(std::pair<std::string, proc_thread_node>(key, new_node));
			}
			else
			{

			}
		}
		else
		{*/
			ret = iter->second._pthread_id;
		//}		
	}
	else
	{
		//新连接,判断哪个线程的用户量少就选哪个
		ret = new_thread();

		if (0 != ret)
		{
			proc_thread_node new_node;
			new_node.key = key;
			new_node._last_select = time(NULL);
			new_node._pthread_id = ret;
			_select_map.insert(std::pair<std::string, proc_thread_node>(key, new_node));
			dzlog_info("select new thread, key = %s, id = %ld", key.c_str(), ret);
		}
		else
		{
			dzlog_info("select thread error, key = %s", key.c_str());
		}
	}
	_select_map_mutex.unlock();
	dzlog_info("select thread, key = %s, id = %ld", key.c_str(), ret);
	return ret;
}
Exemple #2
0
int raptor_proxy_finit(RaptorProxy* rp)
{
	dzlog_info("destory raptor proxy");
	if (rp->client_socket) zmq_close(rp->client_socket);
	if (rp->worker_socket) zmq_close(rp->worker_socket);
	if (rp->zctx) zmq_term(rp->zctx);
	return 0;
}
Exemple #3
0
int main() 
{
    if (!MyLog::Instance().Init())
    {
        return 0;
    }
	
	connpool = DBPool::GetInstance();
	//初始化连接,创建参数中maxSize一半的连接  
	connpool.initPool(DBHOST, USER, PASSWORD, 100);
    dzlog_info("数据库连接池数据化!");

	///////////////////////////////////////////
	activemq::library::ActiveMQCPP::initializeLibrary();
    dzlog_info("初始化ActiveMQCPP库");

    //实时行情处理类
	QuoteReal quoteReal;
	quoteReal.init();

    //k线分时线管理类
	KLineManager klineManager;
    klineManager.Init();

    //原始行情接收类
	QuoteConsumer quoteConsumer;
	quoteConsumer.SetCallback(&quoteReal, &klineManager);
	quoteConsumer.init(BROKER_URI);
    quoteConsumer.StartRecvMsg();

    //cgi请求管理类
	QuoteCgiResponseManager quoteCgiResponseManager;
	quoteCgiResponseManager.SetCallback(&quoteReal, &klineManager);
	quoteCgiResponseManager.Init();

	while (std::cin.get() != 'q') {}
	quoteConsumer.uninit();

	activemq::library::ActiveMQCPP::shutdownLibrary();
	///////////////////////
    dzlog_info("main end!");
	return 0;
}
Exemple #4
0
void KLineProdcut::Reset()
{
    _rwlock.wrlock();
	_lastStockTime = 0;
	_lastPrice = "";
	_klineInfolist.clear();
	_curKlineInfo = KlineInfo2();
    LoadHisKlineData();
    _rwlock.unlock();
    dzlog_info("%s 重置数据个数:%d", _productId.c_str(), _klineInfolist.size());
}
Exemple #5
0
int main(int argc, char** argv)
{
	int rc;
	int k;
	int i;

	if (argc != 2) {
		printf("test_leak ntime\n");
		return -1;
	}

	rc = zlog_init("test_leak.conf");

	k = atoi(argv[1]);
	while (k-- > 0) {
		i = rand();
		switch (i % 4) {
		case 0:
			rc = dzlog_init("test_leak.conf", "xxx");
			dzlog_info("init");
			break;
		case 1:
			rc = zlog_reload(NULL);
			dzlog_info("reload null");
			break;
		case 2:
			rc = zlog_reload("test_leak.2.conf");
			dzlog_info("reload 2");
			break;
		case 3:
			zlog_fini();
			printf("fini\n");
	//		printf("zlog_finish\tj=[%d], rc=[%d]\n", j, rc);
			break;
		}
	}

	zlog_fini();
	return 0;
}
Exemple #6
0
void readIPfile()
{
     ipfile=(struct ipaddr *)calloc(38850,sizeof(struct ipaddr));
     if(NULL == ipfile)
     {
         dzlog_info("ipaddr calloc failed");
         abort();
         return;
     }
     char buffer[256] = {0};
     FILE *file = fopen("./geoip_2013.1.0.0.conf","r");
     if(NULL == file)
     {
         dzlog_info("open geoip file failed!!!");
         abort();
         return;
     }

     int index = 0;
     while(fgets(buffer,256,file))
     {
         char *ordination=(char*)buffer;
         strcpy(ipfile[index].startip, strsep(&ordination, "#"));
         strcpy(ipfile[index].endip, strsep(&ordination, "#"));
         strcpy(ipfile[index].region, strsep(&ordination, "#"));
         strcpy(ipfile[index].territory, strsep(&ordination, "#"));
         strcpy(&ipfile[index].flag, strsep(&ordination, "#"));
        
        //printf("ipfile->startip==%s, endip==%s, region==%s, territory==%s, flag==%s\n", ipfile[index].startip, ipfile[index].endip, ipfile[index].region, ipfile[index].territory, ipfile[index].flag);
         fflush(file);
         index++;
    }

    dzlog_debug("compile read line number index = %d", index);

    return;
}
Exemple #7
0
void signalHandle(int sig)
{
    switch (sig) {
        case SIGTERM:
        case SIGSEGV:
        case SIGUSR1:
            dzlog_info("Capture Signal and Exit");
            exit(0);
        case SIGUSR2:
            break;
        case SIGCHLD:
            break;  
        default:
            break;
    }

}
Exemple #8
0
int main(int argc, char** argv)
{
	int rc;

	rc = dzlog_init("test_profile.conf", "my_cat");
	if (rc) {
		printf("init failed\n");
		return -1;
	}

	dzlog_info("hello, zlog");

	zlog_profile();

	zlog_fini();
	
	return 0;
}
pthread_t proc_thread_select::new_thread()
{
	dzlog_info("new thread, map size = %lu", _thread_count_map.size());
	pthread_t ret = 0;
	//新连接,判断哪个线程的用户量少就选哪个
	int min_count = std::numeric_limits<int>::max();
	boost::unordered_map<pthread_t, int>::iterator count_iter = _thread_count_map.begin();
	boost::unordered_map<pthread_t, int>::iterator count_end = _thread_count_map.end();
	for (; count_iter != count_end; ++count_iter)
	{
		if (min_count > count_iter->second)
		{
			ret = count_iter->first;
			min_count = count_iter->second;
		}
	}

	return ret;
}
Exemple #10
0
// worker thread function
static void* worker_routine (void *context)
{
	// socket to talk to dispatcher
	void* receiver = zmq_socket(context, ZMQ_REP); 
	if (!receiver) 
	{
		dzlog_error("create socket to talk to dispatcher failed, err: %s", zmq_strerror(errno));
		return NULL;
	}

	if (zmq_connect(receiver, "inproc://workers") != 0)
	{
		dzlog_error("worker thread connect worker_socket failed, err: %s", zmq_strerror(errno));
		return NULL;
	}

	dzlog_info("worker thread run ...");

	while (1) 
	{
		zmq_msg_t request;
		zmq_msg_init(&request);
		zmq_recvmsg(receiver, &request, 0); 
		printf("Recv Hello\n"); 
		zmq_msg_close(&request);

		sleep(1);
		
		zmq_msg_t reply;
		zmq_msg_init_size(&reply, 5);
		memcpy(zmq_msg_data (&reply), "World", 5); 
		zmq_sendmsg(receiver, &reply, 0); 
		zmq_msg_close(&reply);
	}

	zmq_close (receiver);
	return NULL;
}
Exemple #11
0
int main(int argc, char* argv[])
{
	// parse command line
	const char* config_file = NULL;
	const char* zlog_config_file = NULL;
	if (argc == 2) 
	{
		config_file = argv[1];
	}
	else if (argc == 3)
	{
		config_file = argv[1];
		zlog_config_file = argv[2];	
	}
	else 
	{
		fprintf(stderr, "usage: raptor <application_config> [zlog_config]\n");
		return -1;
	}

	// zlog init
    int ret = dzlog_init(zlog_config_file, "raptor");
    if (ret) 
    {
        fprintf(stderr, "zlog init failed\n");
        return -1;
    }

    // load config file
    json_t* json_root = NULL;
	json_error_t error;

	dzlog_info("load config file: %s", config_file);

	json_root = json_load_file(config_file, 0, &error);
	if (!json_root) 
	{
		dzlog_error("load config file failed: %s", error.text);
		goto to_exit;
	}

	// create mysql proxy 
	RaptorProxy rp;
	
	json_t* json_raptor_server = json_object_get(json_root, "raptor_proxy");
	if (!json_is_object(json_raptor_server)) 
	{
		dzlog_error("get 'raptor_proxy' config item failed");
		goto to_exit;
	}

	json_t* json_raptor_server_address = json_object_get(json_raptor_server, "address");
	if (!json_is_string(json_raptor_server_address))
	{
		dzlog_error("get 'raptor_proxy:address' config item failed");
		goto to_exit;
	}
	rp.address = json_string_value(json_raptor_server_address);

	json_t* json_raptor_server_workers = json_object_get(json_raptor_server, "workers");
	if (!json_is_integer(json_raptor_server_workers))
	{
		dzlog_error("get 'raptor_proxy:workers' config item failed");
		goto to_exit;
	}
	rp.workers = json_integer_value(json_raptor_server_workers);

	if (raptor_proxy_init(&rp) != 0)
	{
		dzlog_error("create raptor proxy failed");
		goto to_exit;
	}
	/*
	json_t* json_raptor_server = json_object_get(json_root, "raptor_server");
	json_t* json_raptor_server_address = json_object_get(json_raptor_server, "address");
	printf("%s\n", json_string_value(json_raptor_server_address));
	*/
to_exit:
	if (raptor_proxy_finit(&rp) != 0)
		dzlog_error("destroy raptor proxy failed");
	if (!json_root) 
		json_decref(json_root);
	dzlog_info("raptor exit!");
    zlog_fini();
	return 0;
}
Exemple #12
0
//Main Funtion
int main(int argc,char *argv[])
{
    int status=0;
    status=nInit();
    sprintf(sLogfile,"%s",argv[0]);
    SOAP_SOCKET m,s;
    int i=0;
    struct soap Onlsoap; 
    soap_init(&Onlsoap);
    Onlsoap.fget = http_get;
    Onlsoap.fpost = http_post;
    struct soap * soap_thr[MAX_THR];
    pthread_t tid[MAX_THR];

    sigset(SIGUSR1, signalHandle);
    sigset(SIGTERM, signalHandle);
    sigset(SIGSEGV, signalHandle);

    // 设置UTF-8编码方式
    //soap_set_mode(&Onlsoap, SOAP_C_UTFSTRING);
    
    soap_set_mode(&Onlsoap, SOAP_C_MBSTRING);
    soap_set_namespaces(&Onlsoap, namespaces);
    // ----------
    m = soap_bind(&Onlsoap,NULL,lPort,BACKLOG);
    //循环直至服务套接字合法
    while (!soap_valid_socket(m))
    {
        dzlog_error("Bind port error!");
        m = soap_bind(&Onlsoap,NULL,lPort,BACKLOG);
        sleep(2);
    }
    //锁和条件变量初始化
    pthread_mutex_init(&queue_cs,NULL);
    pthread_cond_init(&queue_cv,NULL);
    //生成服务线程
    for(i = 0; i <MAX_THR; i++)
    {
      soap_thr[i] = soap_copy(&Onlsoap);
      dzlog_info("Starting thread %d ",i);
      pthread_create(&tid[i],NULL,(void*(*)(void*))process_queue,(void*)soap_thr[i]);
      
    }
   
   for(;;)
   {

       s=soap_accept(&Onlsoap);
       if(!soap_valid_socket(s))
       {
            if (Onlsoap.errnum) 
            { 
                 soap_print_fault(&Onlsoap,stderr);
                 continue;
            } 
            else
            {
                dzlog_error("Server timed out.");
                break;
            }
       }
     
       dzlog_info("Main Process[%d] accepted connection from IP=%d.%d.%d.%d", getpid(),
              (Onlsoap.ip >> 24)&0xFF, (Onlsoap.ip >> 16)&0xFF, (Onlsoap.ip >> 8)&0xFF, Onlsoap.ip&0xFF);

        while(enqueue(s) == SOAP_EOM)
         sleep(1);
   }
   
    //服务结束后的清理工作
    for(i = 0; i < MAX_THR; i++)
    {
       while (enqueue(SOAP_INVALID_SOCKET) == SOAP_EOM) 
       {
           sleep(1);
       }
    }
    for(i=0; i< MAX_THR; i++)
    {
        pthread_join(tid[i],NULL);
        dzlog_debug("terminated[%d] ",i);
        soap_done(soap_thr[i]);
        free(soap_thr[i]);
    }
   
    pthread_mutex_destroy(&queue_cs);
    pthread_cond_destroy(&queue_cv);
    soap_done(&Onlsoap);
    DbPoolFree(&conn_pool);
	zlog_fini();
    return 0;
}