예제 #1
0
파일: config.cpp 프로젝트: jimmy486/breeze
bool ServerConfig::parse(std::string filename, ServerNode ownNode, NodeIndex ownIndex)
{
    _ownServerNode = ownNode;
    _ownNodeIndex = ownIndex;


    lua_State *L = luaL_newstate();
    if (L == NULL)
    {
        return EXIT_FAILURE;
    }
    luaL_openlibs(L);  /* open libraries */
    lua_atpanic(L, panichHandler);

    luaL_dofile(L, filename.c_str());
    lua_getfield(L, -1, "traits");
    lua_getfield(L, -1, "platid");
    _platid = (unsigned short)luaL_checkinteger(L, -1);
    lua_pop(L, 1);
    lua_getfield(L, -1, "areaid");
    _areaid = (unsigned short)luaL_checkinteger(L, -1);
    lua_pop(L, 2);

    lua_getfield(L, -1, "db");
    lua_pushnil(L);
    while (lua_next(L, -2))
    {
        if (!lua_isstring(L, -2))
        {
            LOGE("config parse db false. key is not string type");
            return false;
        }
        

        DBConfig lconfig;
        lua_getfield(L, -1, "ip");
        lconfig._ip = lua_tostring(L, -1);
        lua_pop(L, 1);

        lua_getfield(L, -1, "port");
        lconfig._port = (unsigned short)lua_tointeger(L, -1);
        lua_pop(L, 1);

        lua_getfield(L, -1, "db");
        lconfig._db = lua_tostring(L, -1);
        lua_pop(L, 1);

        lua_getfield(L, -1, "user");
        lconfig._user = lua_tostring(L, -1);
        lua_pop(L, 1);

        lua_getfield(L, -1, "pwd");
        lconfig._pwd = lua_tostring(L, -1);
        lua_pop(L, 1);

        lua_getfield(L, -1, "db");
        lconfig._db = lua_tostring(L, -1);
        lua_pop(L, 1);

        lconfig._id = toDBConfigID(lua_tostring(L, -2));
        if (lconfig._id != InvalidDB)
        {
            _configDB.push_back(lconfig);
            LOGI("DBConfig=" << lconfig);
        }
        else
        {
            LOGE("unknown DBConfig=" << lconfig);
        }

        //saved key to next while.
        lua_pop(L, 1);
    }
    //pop "db" table.
    lua_pop(L, 1);


    lua_getfield(L, -1, "listen");
    lua_pushnil(L);
    while (lua_next(L, -2))
    {
        if (!lua_isstring(L, -2))
        {
            LOGE("config parse listen false. key is not string type");
            return false;
        }
        std::string node = lua_tostring(L, -2);

        lua_pushnil(L);
        while (lua_next(L, -2))
        {
            NodeListenConfig lconfig;

            lua_getfield(L, -1, "ip");
            lconfig._ip = lua_tostring(L, -1);
            lua_pop(L, 1);

            lua_getfield(L, -1, "port");
            lconfig._port = (unsigned short)lua_tointeger(L, -1);
            lua_pop(L, 1);

            lua_getfield(L, -1, "index");
            lconfig._index = (NodeIndex)lua_tointeger(L, -1);
            lua_pop(L, 1);


            lconfig._node = toServerNode(node);
            if (lconfig._node != InvalidServerNode)
            {
                _configListen.push_back(lconfig);
                LOGI("ListenConfig=" << lconfig);
            }
            else
            {
                LOGE("UNKNOWN ListenConfig=" << lconfig);
            }

            //saved key to next while.
            lua_pop(L, 1);
        }
        
        //saved key to next while.
        lua_pop(L, 1);
    }
    //pop "listen" table.
    lua_pop(L, 1);


    lua_getfield(L, -1, "connect");
    lua_pushnil(L);
    while (lua_next(L, -2))
    {
        if (!lua_isstring(L, -2))
        {
            LOGE("config parse connect false. key is not string type");
            return false;
        }
        std::string node = lua_tostring(L, -2);

        lua_pushnil(L);
        while (lua_next(L, -2))
        {
            NodeConnectConfig lconfig;

            lua_getfield(L, -1, "ip");
            lconfig._remoteIP = lua_tostring(L, -1);
            lua_pop(L, 1);

            lua_getfield(L, -1, "port");
            lconfig._remotePort = (unsigned short)lua_tointeger(L, -1);
            lua_pop(L, 1);

            lua_getfield(L, -1, "index");
            lconfig._dstNodeIndex = (NodeIndex)lua_tointeger(L, -1);
            lua_pop(L, 1);

            lua_getfield(L, -1, "dstNode");
            lconfig._dstNode = toServerNode(lua_tostring(L, -1));
            lua_pop(L, 1);

            lconfig._srcNode = toServerNode(node);

            if (lconfig._srcNode != InvalidServerNode && lconfig._dstNode != InvalidServerNode)
            {
                _configConnect.push_back(lconfig);
                LOGI("_configConnect=" << lconfig);
            }
            else
            {
                LOGE("UNKNOWN ConnectConfig=" << lconfig);
            }

            //saved key to next while.
            lua_pop(L, 1);
        }

        //saved key to next while.
        lua_pop(L, 1);
    }
    //pop "connect" table.
    lua_pop(L, 1);

    lua_close(L);
    return true;
}
예제 #2
0
bool ServerConfig::Parse(std::string filename, ServerNode ownNode, NodeIndex ownIndex)
{
	m_ownServerNode = ownNode;
	m_ownNodeIndex = ownIndex;
	try
	{
		boost::property_tree::ptree pt;
		boost::property_tree::read_xml(filename, pt);

		auto server = pt.get_child("server");
		m_platid = server.get<unsigned int>("<xmlattr>.platid");
		m_areaid = server.get<unsigned int>("<xmlattr>.areaid");

		auto listener = pt.get_child("listen");
		for (auto iter = listener.begin(); iter != listener.end(); ++iter)
		{
			std::string strNode = iter->first;
			ListenConfig lconfig;
			lconfig.ip = iter->second.get<std::string>("<xmlattr>.ip");
			lconfig.port = iter->second.get<unsigned short>("<xmlattr>.port");
			lconfig.index = iter->second.get<unsigned int>("<xmlattr>.index");
			lconfig.node = toServerNode(strNode);
			m_configListen.push_back(lconfig);
			LOGD("strNode=" << strNode << ", ip=" << lconfig.ip << ", port=" << lconfig.port << ", lconfig.index=" << lconfig.index);
		}


		auto connecter = pt.get_child("connect");
		for (auto iter = connecter.begin(); iter != connecter.end(); ++iter)
		{
			std::string srcStrNode = iter->first;
			ConnectorConfig lconfig;
			std::string dstStrNode = iter->second.get<std::string>("<xmlattr>.dstNode");
			lconfig.remoteIP = iter->second.get<std::string>("<xmlattr>.ip");
			lconfig.remotePort = iter->second.get<unsigned short>("<xmlattr>.port");
			lconfig.srcNode = toServerNode(srcStrNode);
			lconfig.dstNode = toServerNode(dstStrNode);
			m_configConnect.push_back(lconfig);
			LOGD("srcStrNode=" << srcStrNode << ", remoteIP=" << lconfig.remoteIP << ", remotePort=" << lconfig.remotePort << ", dstStrNode=" << dstStrNode);
		}

		auto mongoParse = pt.get_child("mongo");
		for (auto iter = mongoParse.begin(); iter != mongoParse.end(); ++iter)
		{
			MongoConfig lconfig;
			lconfig.ip = iter->second.get<std::string>("<xmlattr>.ip");
			lconfig.port = iter->second.get<unsigned short>("<xmlattr>.port");
			lconfig.db = iter->second.get<std::string>("<xmlattr>.db");
			lconfig.user = iter->second.get<std::string>("<xmlattr>.user");
			lconfig.pwd = iter->second.get<std::string>("<xmlattr>.pwd");
			lconfig.needAuth = iter->second.get<bool>("<xmlattr>.needAuth");
			if (iter->first == AuthMongoDB)
			{
				m_authMongo = lconfig;
			}
			else if (iter->first == InfoMongoDB)
			{
				m_infoMongo = lconfig;
			}
			else if (iter->first == LogMongoDB)
			{
				m_logMongo = lconfig;
			}
			
		}

	}
	catch (std::string err)
	{
		LOGE("ServerConfig catch exception: error=" << err);
		return false;
	}
	catch (std::exception e)
	{
		LOGE("ServerConfig catch exception: error=" << e.what());
		return false;
	}
	catch (...)
	{
		LOGE("ServerConfig catch exception: unknow exception.");
		return false;
	}
	return true;
}