Esempio n. 1
0
bool ServerConfig::parse(std::string filename, ServerType ownType, ServerNode ownNode)
{
    _ownServerType = ownType;
    _ownServerNode = ownNode;


    lua_State *L = luaL_newstate();
    if (L == NULL)
    {
        return EXIT_FAILURE;
    }
    luaL_openlibs(L);  /* open libraries */
    lua_atpanic(L, panichHandler);
    if (luaL_dofile(L, filename.c_str()))
    {
        LOGE("can't found the config file. filename=" << filename);
        return false;
    }
    lua_getfield(L, -1, "areaid");
    _areaid = (unsigned short)luaL_optinteger(L, -1, 0);
    lua_pop(L, 1);

    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 = luaL_checkstring(L, -1);
        lua_pop(L, 1);

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

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

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

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

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

        lconfig._id = toDBConfigID(luaL_checkstring(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 = luaL_checkstring(L, -2);

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

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

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

            lua_getfield(L, -1, "wip");
            lconfig._wip = luaL_optstring(L, -1, "0.0.0.0");
            lua_pop(L, 1);

            lua_getfield(L, -1, "wport");
            lconfig._wport = (unsigned short)luaL_optinteger(L, -1, 0);
            lua_pop(L, 1);

            lua_getfield(L, -1, "index");
            lconfig._index = (ServerNode)luaL_checkinteger(L, -1);
            lua_pop(L, 1);

            lua_getfield(L, -1, "white");
            if (lua_isnil(L, -1))
            {
                lua_pop(L, 1);
            }
            else
            {
                lua_pushnil(L);
                while (lua_next(L, -2))
                {
                    lconfig._whiteList.push_back(luaL_checkstring(L, -1));
                    lua_pop(L, 1);
                }
                //pop white table
                lua_pop(L, 1);
            }
            

            lconfig._node = toServerType(node);
            if (lconfig._node != InvalidServerType)
            {
                _configListen.push_back(lconfig);
                LOGI("ListenConfig=" << lconfig);
            }
            else
            {
                LOGE("UNKNOWN ListenConfig=" << lconfig);
            }
            //pop node table
            lua_pop(L, 1);
        }
        
        //pop node tables
        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 = luaL_checkstring(L, -2);

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

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

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

            lua_getfield(L, -1, "index");
            lconfig._dstServerNode = (ServerNode)luaL_checkinteger(L, -1);
            lua_pop(L, 1);

            lua_getfield(L, -1, "dstType");
            lconfig._dstType = toServerType(luaL_checkstring(L, -1));
            lua_pop(L, 1);

            lconfig._srcType = toServerType(node);

            if (lconfig._srcType != InvalidServerType && lconfig._dstType != InvalidServerType)
            {
                _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;
}
Esempio n. 2
0
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;
}