Пример #1
0
/// Creates an observer
int luaEvent::createObserver( lua_State *L )
{
#ifdef DEBUG_OBSERVER
    stackDump(luaL);
#endif

    // recupero a referencia da celula
    lua_rawgeti(luaL, LUA_REGISTRYINDEX, ref);

    
    // flags para a definição do uso de compressão
    // na transmissão de datagramas e da visibilidade
    // dos observadores Udp Sender 
    bool compressDatagram = false, obsVisible = true;

    int top = lua_gettop(luaL);
    int typeObserver = (int)luaL_checkinteger(luaL, 1);

    QStringList allAttribs, cols;

    allAttribs.push_back("EventTime");
    allAttribs.push_back("Periodicity");
    allAttribs.push_back("Priority");

    // Recupera a tabela de parametros
    //if(! lua_istable(luaL, top - 1) )
    //{
    //    if (! QUIET_MODE )
    //        qWarning("Warning: Parameter table not found.");
    //}
    //else
    //{
    lua_pushnil(luaL);
    while(lua_next(luaL, top - 1) != 0)
    {   
        QString key;
        if (lua_type(luaL, -2) == LUA_TSTRING)
            key = QString( luaL_checkstring(luaL, -2));

        switch (lua_type(luaL, -1))
        {
        case LUA_TSTRING:
            {
                QString value( luaL_checkstring(luaL, -1));
                cols.push_back(value);
                break;
            }

        case LUA_TBOOLEAN:
            {
                bool val = lua_toboolean(luaL, -1);
                if (key == "visible")
                    obsVisible = val;
                else // if (key == "compress")
                    compressDatagram = val;
            }
        default:
            break;
        }
        lua_pop(luaL, 1);
    }
    // }

    if (cols.isEmpty())
    {
        if (! QUIET_MODE )
            qWarning("Warning: The Parameters Table is empty.");
        cols << "" << "";
    }

    ObserverTextScreen *obsText = 0;
    ObserverTable *obsTable = 0;
    ObserverLogFile *obsLog = 0;
    ObserverUDPSender *obsUDPSender = 0;

    int obsId = -1;

    switch (typeObserver)
    {
        case TObsTextScreen:
            obsText = (ObserverTextScreen*) EventSubjectInterf::createObserver(TObsTextScreen);
            if (obsText)
            {
                obsId = obsText->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", TerraMEObserver::MEMORY_ALLOC_FAILED);
            }
            break;

        case TObsLogFile:
            obsLog = (ObserverLogFile*) EventSubjectInterf::createObserver(TObsLogFile);
            if (obsLog)
            {
                obsId = obsLog->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", TerraMEObserver::MEMORY_ALLOC_FAILED);
            }
            break;

        case TObsTable:
            obsTable = (ObserverTable *) EventSubjectInterf::createObserver(TObsTable);
            if (obsTable)
            {
                obsId = obsTable->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", TerraMEObserver::MEMORY_ALLOC_FAILED);
            }
            break;

        case TObsUDPSender:
            obsUDPSender = (ObserverUDPSender *) EventSubjectInterf::createObserver(TObsUDPSender);
            if (obsUDPSender)
            {
                obsId = obsUDPSender->getId();
                obsUDPSender->setCompressDatagram(compressDatagram);

                if (obsVisible)
                    obsUDPSender->show();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", TerraMEObserver::MEMORY_ALLOC_FAILED);
            }
            break;

        default:
            if (! QUIET_MODE )
            {
                qWarning("Error: In this context, the code '%s' does not "
                    "correspond to a valid type of Observer.",  getObserverName(typeObserver) );
            }
            return 0;
    }

    QStringList obsAttribs;
    obsAttribs = allAttribs;
    observedAttribs = allAttribs;

    /// Define alguns parametros do observador instanciado ---------------------------------------------------

    if (obsLog)
    {
        obsLog->setHeaders(obsAttribs);

        if (cols.at(0).isNull() || cols.at(0).isEmpty())
        {
            if (! QUIET_MODE )
            {
                qWarning("Warning: Filename was not specified, using a "
                    "default \"%s\".", qPrintable(DEFAULT_NAME));
            }
            obsLog->setFileName(DEFAULT_NAME + ".csv");
        }
        else
        {
            obsLog->setFileName(cols.at(0));
        }

        // caso não seja definido, utiliza o default ";"
        if ((cols.size() < 2) || cols.at(1).isNull() || cols.at(1).isEmpty())
        {
            if (! QUIET_MODE )
                qWarning("Warning: Separator not defined, using \";\".");
            obsLog->setSeparator();
        }
        else
        {
            obsLog->setSeparator(cols.at(1));
        }
        lua_pushnumber(luaL, obsId);
        return 1;
    }

    if (obsText)
    {
        obsText->setHeaders(obsAttribs);

        lua_pushnumber(luaL, obsId);
        return 1;
    }

    if (obsTable)
    {
        if ((cols.size() < 1) || (cols.size() < 2) || cols.at(0).isNull() || cols.at(0).isEmpty()
                || cols.at(1).isNull() || cols.at(1).isEmpty())
        {
            if (! QUIET_MODE )
                qWarning("Warning: Column title not defined.");
        }
        obsTable->setColumnHeaders(cols);
        obsTable->setLinesHeader(obsAttribs);

        lua_pushnumber(luaL, obsId);
        return 1;
    }

    if (obsUDPSender)
    {
        obsUDPSender->setAttributes(obsAttribs);

        if (cols.isEmpty())
        {
            if (! QUIET_MODE )
                qWarning("Warning: Port not defined.");
        }
        else
        {
            obsUDPSender->setPort(cols.at(0).toInt());
        }

        // broadcast
        if ((cols.size() == 1) || ((cols.size() == 2) && cols.at(1).isEmpty()) )
        {
            if (! QUIET_MODE )
                qWarning("Warning: Observer will send to broadcast.");
            obsUDPSender->addHost(BROADCAST_HOST);
        }
        else
        {
            // multicast or unicast
            for(int i = 1; i < cols.size(); i++){
                if (! cols.at(i).isEmpty())
                    obsUDPSender->addHost(cols.at(i));
            }
        }
        lua_pushnumber(luaL, obsId);
        return 1;
    }
    return 0;
}
Пример #2
0
/// Creates several types of observers
/// parameters: observer type, observeb attributes table, observer type parameters
// verif. ref (endereco na pilha lua)
// olhar a classe event
int luaCell::createObserver( lua_State *L )
{	
    // recupero a referencia da celula
    lua_rawgeti(luaL, LUA_REGISTRYINDEX, ref);
            
    // flags para a definição do uso de compressão
    // na transmissão de datagramas e da visibilidade
    // dos observadores Udp Sender 
    bool compressDatagram = false, obsVisible = true;

    // recupero a tabela de
    // atributos da celula
    int top = lua_gettop(luaL);

    // Nao modifica em nada a pilha
    // recupera o enum referente ao tipo
    // do observer
    int typeObserver = (int)luaL_checkinteger(luaL, -4);
    bool isGraphicType = (typeObserver == TObsDynamicGraphic) || (typeObserver == TObsGraphic);

    //------------------------
    QStringList allAttribs, obsAttribs;

    // Pecorre a pilha lua recuperando todos os atributos celula
    lua_pushnil(luaL);
    while(lua_next(luaL, top) != 0)
    {
        QString key( luaL_checkstring(luaL, -2) );

        allAttribs.push_back(key);
        lua_pop(luaL, 1);
    }

    //------------------------
    // pecorre a pilha lua recuperando
    // os atributos celula que se quer observar
    lua_settop(luaL, top - 1);
    top = lua_gettop(luaL);

    // Verificacao da sintaxe da tabela Atributos
    if(! lua_istable(luaL, top) )
    {
        qFatal("Error: Attributes table not found. Incorrect sintax.\n");
        return -1;
    }

    bool attribTable = false;

    lua_pushnil(luaL);
    while(lua_next(luaL, top - 1 ) != 0)
    {
        QString key( luaL_checkstring(luaL, -1) );
        attribTable = true;

        // Verifica se o atributo informado não existe deve ter sido digitado errado
        if (allAttribs.contains(key))
        {
            obsAttribs.push_back(key);
            if (! observedAttribs.contains(key))
                observedAttribs.push_back(key);
        }
        else
        {
            if ( ! key.isNull() || ! key.isEmpty())
            {
                qFatal("Error: Attribute name '%s' not found.\n", qPrintable(key));
                return -1;
            }
        }
        lua_pop(luaL, 1);
    }
    //------------------------

    //QStringList lines;

    if ((obsAttribs.empty() ) && (! isGraphicType))
    {
        obsAttribs = allAttribs;
        observedAttribs = allAttribs;
    }

    if(! lua_istable(luaL, top) )
    {
        qWarning("Warning: Parameter table not found. Incorrect sintax.");
        return 0;
    }

    QStringList cols;

    // Recupera a tabela de parametros os observadores do tipo Table e Graphic
    // caso não seja um tabela a sintaxe do metodo esta incorreta
    lua_pushnil(luaL);
    while(lua_next(luaL, top) != 0)
    {   
        QString key;
        if (lua_type(luaL, -2) == LUA_TSTRING)
            key = QString( luaL_checkstring(luaL, -2));

        switch (lua_type(luaL, -1))
        {
        case LUA_TSTRING:
            {
                QString value( luaL_checkstring(luaL, -1));
                cols.push_back(value);
                break;
            }

        case LUA_TBOOLEAN:
            {
                bool val = lua_toboolean(luaL, -1);
                if (key == "visible")
                    obsVisible = val;
                else // if (key == "compress")
                    compressDatagram = val;
            }
        default:
            break;
        }
        lua_pop(luaL, 1);
    }

    // Caso não seja definido nenhum parametro,
    // e o observador não é TextScreen então
    // lança um warning
    if ((cols.isEmpty()) && (typeObserver != TObsTextScreen))
    {
        if (! QUIET_MODE )
            qWarning("Warning: The Parameters Table is empty.");
    }
    //------------------------

    ObserverTextScreen *obsText = 0;
    ObserverTable *obsTable = 0;
    ObserverGraphic *obsGraphic = 0;
    ObserverLogFile *obsLog = 0;
    ObserverUDPSender *obsUDPSender = 0;

    int obsId = -1;

    switch (typeObserver)
    {
        case TObsTextScreen			:
            obsText = (ObserverTextScreen*) 
                CellSubjectInterf::createObserver(TObsTextScreen);
            if (obsText)
            {
                obsId = obsText->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", TerraMEObserver::MEMORY_ALLOC_FAILED);
            }
            break;

        case TObsLogFile:
            obsLog = (ObserverLogFile*) 
                CellSubjectInterf::createObserver(TObsLogFile);
            if (obsLog)
            {
                obsId = obsLog->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", TerraMEObserver::MEMORY_ALLOC_FAILED);
            }
            break;

        case TObsTable:
            obsTable = (ObserverTable *) 
                CellSubjectInterf::createObserver(TObsTable);
            obsId = obsTable->getId();
            break;

        case TObsDynamicGraphic:
            obsGraphic = (ObserverGraphic *) 
                CellSubjectInterf::createObserver(TObsDynamicGraphic);
           
            if (obsGraphic)
            {
                obsGraphic->setObserverType(TObsDynamicGraphic);
                obsId = obsGraphic->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", TerraMEObserver::MEMORY_ALLOC_FAILED);
            }
            break;

        case TObsGraphic:
            obsGraphic = (ObserverGraphic *) 
                CellSubjectInterf::createObserver(TObsGraphic);
            if (obsGraphic)
            {
                obsId = obsGraphic->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", TerraMEObserver::MEMORY_ALLOC_FAILED);
            }
            break;

        case TObsUDPSender:
            obsUDPSender = (ObserverUDPSender *) 
                CellSubjectInterf::createObserver(TObsUDPSender);
            if (obsUDPSender)
            {
                obsId = obsUDPSender->getId();
                obsUDPSender->setCompressDatagram(compressDatagram);

                if (obsVisible)
                    obsUDPSender->show();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", TerraMEObserver::MEMORY_ALLOC_FAILED);
            }
            break;

        case TObsMap:
        default:
            if (! QUIET_MODE )
            {
                qWarning("Warning: In this context, the code '%s' does not correspond to a "
                         "valid type of Observer.",  getObserverName(typeObserver) );
            }
            return 0;
    }

    //@RODRIGO
    //serverSession->add(obsKey);

    /// Define alguns parametros do observador instanciado ---------------------------------------------------

    if (obsLog)
    {
        obsLog->setHeaders(obsAttribs);

        if (cols.at(0).isNull() || cols.at(0).isEmpty())
        {
            if (! QUIET_MODE )
                qWarning("Warning: Filename was not specified, using a "
                         "default \"%s\".", qPrintable(DEFAULT_NAME));
            obsLog->setFileName(DEFAULT_NAME + ".csv");
        }
        else
        {
            obsLog->setFileName(cols.at(0));
        }

        // caso não seja definido, utiliza o default ";"
        if ((cols.size() < 2) || cols.at(1).isNull() || cols.at(1).isEmpty())
        {
            if (! QUIET_MODE )
                qWarning("Warning: Separator not defined, using \";\".");
            obsLog->setSeparator();
        }
        else
        {
            obsLog->setSeparator(cols.at(1));
        }

        lua_pushnumber(luaL, obsId);
        return 1;
    }

    if (obsText)
    {
        obsText->setHeaders(obsAttribs);
        lua_pushnumber(luaL, obsId);
        return 1;
    }

    if (obsTable)
    {
        if ((cols.size() < 2) || cols.at(0).isNull() || cols.at(0).isEmpty()
                || cols.at(1).isNull() || cols.at(1).isEmpty())
        {
            if (! QUIET_MODE )
                qWarning("Warning: Column title not defined.");
        }

        obsTable->setColumnHeaders(cols);
        obsTable->setLinesHeader(obsAttribs);

        lua_pushnumber(luaL, obsId);
        return 1;
    }

    if (obsGraphic)
    {
        obsGraphic->setLegendPosition();
        if (obsAttribs.size() <= 2)
        {
            obsGraphic->setHeaders(obsAttribs);
        }
        else
        {
            //printf("\nError: This observer works only with one or two elements.\n");
            qFatal("Error: This observer works only with one or two elements.\n");
            return -1;
        }

        // titulo do gráfico
        if (cols.at(0).isNull() || cols.at(0).isEmpty())
        {
            if (! QUIET_MODE )
                qWarning("Warning: Graphic title not defined.");
            obsGraphic->setGraphicTitle();
        }
        else
        {
            obsGraphic->setGraphicTitle(cols.at(0));
        }

        // nome da curva
        if ((cols.size() < 2) || cols.at(1).isNull() || cols.at(1).isEmpty())
        {
            if (! QUIET_MODE )
                qWarning("Warning: Curve name not defined.");
            obsGraphic->setCurveTitle();
        }
        else
        {
            obsGraphic->setCurveTitle(cols.at(1));
        }

        // nome dos eixos
        // FIX-ME: Separar as chamadas do nome dos eixos
        if ( (cols.size() < 3) || (cols.size() < 4) || cols.at(2).isNull() || cols.at(2).isEmpty()
             || cols.at(3).isNull() || cols.at(3).isEmpty() )
        {
            if (! QUIET_MODE )
                qWarning("Warning: Axis name not defined.");
            obsGraphic->setAxisTitle();
        }
        else
        {
            obsGraphic->setAxisTitle(cols.at(2), cols.at(3));
        }

        lua_pushnumber(luaL, obsId);
        return 1;
    }

    if(obsUDPSender)
    {
        obsUDPSender->setAttributes(obsAttribs);

        // if (cols.at(0).isEmpty())
        if (cols.isEmpty())
        {
            if (! QUIET_MODE )
                qWarning("Warning: Port not defined.");
        }
        else
        {
            obsUDPSender->setPort(cols.at(0).toInt());
        }

        // broadcast
        if ((cols.size() == 1) || ((cols.size() == 2) && cols.at(1).isEmpty()) )
        {
            if (! QUIET_MODE )
                qWarning("Warning: Observer will send to broadcast.");
            obsUDPSender->addHost(BROADCAST_HOST);
        }
        else
        {
            // multicast or unicast
            for(int i = 1; i < cols.size(); i++){
                if (! cols.at(i).isEmpty())
                    obsUDPSender->addHost(cols.at(i));
            }
        }
        lua_pushnumber(luaL, obsId);
        return 1;
    }
    return 0;
}