예제 #1
0
int readXMLConfig::getNodeValueByName(string & filPath, string & nodeEleName, string & nodeEleValue)
{
	TiXmlDocument doc(filPath.c_str()); 
	bool loadOkay = doc.LoadFile();  
	if (!loadOkay) 
	{      
		// printf( "Could not load test file %s. Error='%s'. Exiting.\n", filPath.c_str(), doc.ErrorDesc() );
        dzlog_error("Could not load test file %s. Error='%s'. Exiting.", filPath.c_str(), doc.ErrorDesc() );  
		// exit( 1 );  
		return -1;
	}     


	TiXmlElement* root = doc.RootElement();
	string nodeAttrName = "";
	string nodeAttrValue = "";

	TiXmlNode * tmp = SelectSingleNodeByRootEle(root, nodeEleName, nodeAttrName, nodeAttrValue);
	if (NULL == tmp)
	{
		// printf("can't get node value.\n");
        dzlog_error("can't get node value.");  
		return -1;
	}
	nodeEleValue = tmp->ToElement()->GetText();
	// cout << nodeEleValue << endl;

	return 0;
}
예제 #2
0
TiXmlNode * readXMLConfig::SelectSingleNodeByRootEle(TiXmlElement* RootElement,string & nodeName,string nodeAttrName,string nodeAttrValue)  
{  
	//加载一个XML的文档对象。  
	 
	//  TiXmlDocument *xmlDoc = new TiXmlDocument(cXmlName);  
	//  if(!xmlDoc->LoadFile())  //是tinyXml会自动处理文档的BOM  
	//  {  
	//      return NULL;  
	//  }  
	//    
	//    
	//  if(xmlDoc == NULL)  
	//  {  
	//      return NULL;  
	//  }  
	 
	//获得根元素  
	//TiXmlElement *RootElement = xmlDoc->RootElement();  
    if(RootElement == NULL)  
    {  
        // cout << "parse error,can't get root element" << endl;
        dzlog_error("parse error,can't get root element.");  
        return NULL;  
    }  
     
    TiXmlNode * pNode  = NULL;  
    TiXmlNode * pSelectNode = NULL;  
    string msg = "";  
     
    for(pNode=RootElement->FirstChildElement();pNode;pNode=pNode->NextSiblingElement())  
    {  
         
        pSelectNode = selectChildNode(pNode,nodeName,nodeAttrName,nodeAttrValue);  
        if(pSelectNode)  
        {  
            break;  
        }  
    }  
     
    if(pSelectNode)  
    {  
        //cout << "解析成功" << endl;  
        //cout << pSelectNode->Value() << endl;  
        return pSelectNode;  
    }  
    else  
    {  
        // cout << "parse error,can't get node" << endl;  
        dzlog_error("parse error,can't get node.");  
        return NULL;  
    }  
     
}
예제 #3
0
int raptor_proxy_init(RaptorProxy* rp)
{
	rp->zctx = zmq_init(1);
	if (!rp->zctx)
	{
		dzlog_error("zmq_init failed, err: %s", zmq_strerror(errno));
		return -1;
	}

	rp->client_socket = zmq_socket(rp->zctx, ZMQ_ROUTER);
	if (!rp->client_socket)
	{
		dzlog_error("create client socket failed, err: %s", zmq_strerror(errno));
		return -1;
	}

	if (zmq_bind(rp->client_socket, rp->address) != 0)
	{
		dzlog_error("client socket bind failed, err: %s", zmq_strerror(errno));
		return -1;
	}

	rp->worker_socket = zmq_socket(rp->zctx, ZMQ_DEALER);
	if (!rp->worker_socket)
	{
		dzlog_error("create worker socket failed, err: %s", zmq_strerror(errno));
		return -1;
	}

	if (zmq_bind(rp->worker_socket, "inproc://workers") != 0)
	{
		dzlog_error("worker socket bind failed, err: %s", zmq_strerror(errno));
		return -1;
	}

	// launch pool of worker threads
	int count;
	for (count = 0; count < rp->workers; count++) 
	{
        pthread_t worker;
        pthread_create(&worker, NULL, worker_routine, rp->zctx);
    }

    // connect work threads to client threads via a queue proxy
    zmq_proxy (rp->client_socket, rp->worker_socket, NULL);

	return 0;
}
예제 #4
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;
}
예제 #5
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;
}
예제 #6
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;
}
예제 #7
0
bool KLineProdcut::LoadHisKlineData()
{
    bool result = true;
	_klineInfolist.clear();

	char buffer[4096] = { 0 };
	sprintf(buffer, "SELECT stock_time,opening_price,closing_price,highest_price,floor_price FROM %s \
	where stock_time > DATE_SUB(NOW(), INTERVAL %d DAY) ORDER BY stock_time ASC",
		_tableName.c_str(),
        _dayInterval
	);

	string str(buffer);
	try
	{
		sql::Connection* con = connpool.GetConnection();
		if (con != NULL)
		{
            sql::Statement* state = con->createStatement();
            if (state != NULL)
            {
                sql::ResultSet *res = state->executeQuery(str);
                if (res != NULL)
                {
                    while (res->next())
                    {

                        KlineInfo klineInfo;
                        klineInfo._open = res->getString("opening_price");
                        klineInfo._close = res->getString("closing_price");
                        klineInfo._high = res->getString("highest_price");
                        klineInfo._low = res->getString("floor_price");
                        string stockTime = res->getString("stock_time");
                        int nTime = QuoteUtils::fmtToTime(stockTime);
                        _klineInfolist[nTime] = klineInfo;
                    }

                    delete res;
                }
                else
                {
                    dzlog_error("数据库ResultSet为空!");
                    result = false;
                }
                delete state;
                
            }
            else
            {
                dzlog_error("数据库Statement为空!");
                result = false;
            }

            connpool.ReleaseConnection(con);
		}
        else
        {
            dzlog_error("数据库连接为空!");
            return false;
        }

		
	}
	catch (sql::SQLException &ex) 
    {
        dzlog_error("数据库异常! state:%s errorcode:%d", ex.getSQLStateCStr(), ex.getErrorCode());
        result = false;
	}

	return result;
}