Example #1
0
bool YConsoleManager::process(const vector<string> &cmdList)
{
    if (!cmdList.size() || cmdList[0] == "")
        return true;

    /**
     * help
     */
     if((cmdList.size() == 1) &&
        (cmdList[0] == "help"))
     {
         help();
         return true;
     }


    /**
     * add application
     */
     if((cmdList.size() == 3) &&
        (cmdList[0] == "add") && (cmdList[1] == "app"))
     {
         if(addApplication(cmdList[2].c_str()))
            cout<<INFO<<cmdList[2]<<" is successfully added."<<ENDC<<endl;
         reportErrors();
        #ifdef WITH_READLINE
        updateAppNames(&appnames);
        #endif
         return true;
     }

    /**
     * add module
     */
     if((cmdList.size() == 3) &&
        (cmdList[0] == "add") && (cmdList[1] == "mod"))
     {
         if(addModule(cmdList[2].c_str()))
            cout<<INFO<<cmdList[2]<<" is successfully added."<<ENDC<<endl;
         reportErrors();
         return true;
     }

    /**
     * add resource
     */
     if((cmdList.size() == 3) &&
        (cmdList[0] == "add") && (cmdList[1] == "res"))
     {
         if(addResource(cmdList[2].c_str()))
            cout<<INFO<<cmdList[2]<<" is successfully added."<<ENDC<<endl;
         reportErrors();
         return true;
     }


    /**
     * load application
     */
     if((cmdList.size() == 3) &&
        (cmdList[0] == "load") && (cmdList[1] == "app"))
     {
         if(loadApplication(cmdList[2].c_str()))
         {
            //cout<<cmdList[2]<<" is successfully loaded."<<endl;
            which();
         }
         reportErrors();
         return true;
     }

    /**
     * load module
     */
    /*
     if((cmdList.size() >= 3) &&
        (cmdList[0] == "load") && (cmdList[1] == "mod"))
     {
         if(cmdList.size() > 3)
         {
            if(manager.loadModule(cmdList[2].c_str(), cmdList[3].c_str()))
                cout<<cmdList[2]<<" is successfully loaded."<<endl;
         }
        else
            if(manager.loadModule(cmdList[2].c_str()))
                cout<<cmdList[2]<<" is successfully loaded."<<endl;
         reportErrors();
         return true;
     }
    */

    /**
     * run
     */
     if((cmdList.size() == 1) &&
        (cmdList[0] == "run"))
     {
         bShouldRun = run();
         reportErrors();
         return true;
     }
     if((cmdList.size() >= 2) &&
        (cmdList[0] == "run"))
     {
        bShouldRun = false;
        for(unsigned int i=1; i<cmdList.size(); i++)
            bShouldRun |= run(atoi(cmdList[i].c_str()));
        reportErrors();
        return true;
     }

    /**
     * stop
     */
     if((cmdList.size() == 1) &&
        (cmdList[0] == "stop"))
     {
         bShouldRun = false;
         stop();
         reportErrors();
         return true;
     }
     if((cmdList.size() >= 2) &&
        (cmdList[0] == "stop"))
     {
         //bShouldRun = false;
        for(unsigned int i=1; i<cmdList.size(); i++)
            stop(atoi(cmdList[i].c_str()));
         bShouldRun = !suspended();
         reportErrors();
         return true;
     }

    /**
     * kill
     */
     if((cmdList.size() == 1) &&
        (cmdList[0] == "kill"))
     {
         bShouldRun = false;
         kill();
         reportErrors();
         return true;
     }
     if((cmdList.size() >= 2) &&
        (cmdList[0] == "kill"))
     {
         //bShouldRun = false;
         for(unsigned int i=1; i<cmdList.size(); i++)
            kill(atoi(cmdList[i].c_str()));
         bShouldRun = !suspended();
         reportErrors();
         return true;
     }

    /**
     * connect
     */
     if((cmdList.size() == 1) &&
        (cmdList[0] == "connect"))
     {
         connect();
         reportErrors();
         return true;
     }
     if((cmdList.size() >= 2) &&
        (cmdList[0] == "connect"))
     {
        for(unsigned int i=1; i<cmdList.size(); i++)
            connect(atoi(cmdList[i].c_str()));
        reportErrors();
        return true;
     }


    /**
     * disconnect
     */
     if((cmdList.size() == 1) &&
        (cmdList[0] == "disconnect"))
     {
         disconnect();
         reportErrors();
         return true;
     }
     if((cmdList.size() >= 2) &&
        (cmdList[0] == "disconnect"))
     {
        for(unsigned int i=1; i<cmdList.size(); i++)
            disconnect(atoi(cmdList[i].c_str()));
        reportErrors();
        return true;
     }


    /**
     * which
     */
     if((cmdList.size() == 1) &&
        (cmdList[0] == "which"))
     {
        which();
        return true;
     }

    /**
     * check for dependencies
     */
    if((cmdList.size() == 2) &&
        (cmdList[0] == "check") && (cmdList[1] == "dep"))
    {
        if(checkDependency())
            cout<<INFO<<"All of resource dependencies are satisfied."<<ENDC<<endl;
        reportErrors();
        return true;
    }


    /**
     * check for running state
     */
    if((cmdList.size() == 3) &&
        (cmdList[0] == "check") && (cmdList[1] == "state"))
    {
        ExecutablePContainer modules = getExecutables();
        unsigned int id = (unsigned int)atoi(cmdList[2].c_str());
        if(id>=modules.size())
        {
            cout<<FAIL<<"ERROR:   "<<INFO<<"Module id is out of range."<<ENDC<<endl;
            return true;
        }

        if(running(id))
            cout<<OKGREEN<<"<RUNNING> ";
        else
            cout<<FAIL<<"<STOPPED> ";
        cout<<INFO<<"("<<id<<") ";
        cout<<modules[id]->getCommand();
        cout<<" ["<<modules[id]->getHost()<<"]"<<ENDC<<endl;
        reportErrors();
        return true;
    }
    if((cmdList.size() == 2) &&
        (cmdList[0] == "check") && (cmdList[1] == "state"))
    {
        checkStates();
        reportErrors();
        return true;
    }


    /**
     * check for connection state
     */
    if((cmdList.size() == 3) &&
        (cmdList[0] == "check") && (cmdList[1] == "con"))
    {

        CnnContainer connections  = getConnections();
        unsigned int id = (unsigned int)atoi(cmdList[2].c_str());
        if(id>=connections.size())
        {
            cout<<FAIL<<"ERROR:   "<<INFO<<"Connection id is out of range."<<ENDC<<endl;
            return true;
        }

        if(connected(id))
            cout<<OKGREEN<<"<CONNECTED> ";
        else
            cout<<FAIL<<"<DISCONNECTED> ";

        cout<<INFO<<"("<<id<<") ";
        cout<<connections[id].from()<<" - "<<connections[id].to();
                cout<<" ["<<connections[id].carrier()<<"]"<<ENDC<<endl;
        reportErrors();
        return true;
    }
    if((cmdList.size() == 2) &&
        (cmdList[0] == "check") && (cmdList[1] == "con"))
    {
        checkConnections();
        return true;
    }



    /**
     *  list available modules
     */
    if((cmdList.size() == 2) &&
        (cmdList[0] == "list") && (cmdList[1] == "mod"))
    {
        KnowledgeBase* kb = getKnowledgeBase();
        ModulePContainer mods =  kb->getModules();
        int id = 0;
        for(ModulePIterator itr=mods.begin(); itr!=mods.end(); itr++)
        {
            string fname;
            string fpath = (*itr)->getXmlFile();

            size_t pos = fpath.rfind(PATH_SEPERATOR);
            if(pos!=string::npos)
                fname = fpath.substr(pos);
            else
                fname = fpath;
            cout<<INFO<<"("<<id++<<") ";
            cout<<OKBLUE<<(*itr)->getName()<<ENDC;
            cout<<INFO<<" ["<<fname<<"]"<<ENDC<<endl;
        }
        return true;
    }


    /**
     *  list available applications
     */
    if((cmdList.size() == 2) &&
        (cmdList[0] == "list") && (cmdList[1] == "app"))
    {
        KnowledgeBase* kb = getKnowledgeBase();
        ApplicaitonPContainer apps =  kb->getApplications();
        int id = 0;
        for(ApplicationPIterator itr=apps.begin(); itr!=apps.end(); itr++)
        {
            string fname;
            string fpath = (*itr)->getXmlFile();

            size_t pos = fpath.rfind(PATH_SEPERATOR);
            if(pos!=string::npos)
                fname = fpath.substr(pos);
            else
                fname = fpath;
            cout<<INFO<<"("<<id++<<") ";
            cout<<OKBLUE<<(*itr)->getName()<<ENDC;
            cout<<INFO<<" ["<<fname<<"]"<<ENDC<<endl;
        }
        return true;
    }

    /**
     *  list available resources
     */
    if((cmdList.size() == 2) &&
        (cmdList[0] == "list") && (cmdList[1] == "res"))
    {
        KnowledgeBase* kb = getKnowledgeBase();
        ResourcePContainer resources = kb->getResources();
        int id = 0;
        for(ResourcePIterator itr=resources.begin(); itr!=resources.end(); itr++)
        {
            Computer* comp = dynamic_cast<Computer*>(*itr);
            if(comp)
            {
                string fname;
                string fpath = comp->getXmlFile();
                size_t pos = fpath.rfind(PATH_SEPERATOR);
                if(pos!=string::npos)
                    fname = fpath.substr(pos);
                else
                    fname = fpath;
                cout<<INFO<<"("<<id++<<") ";
                if(comp->getDisable())
                    cout<<WARNING<<comp->getName()<<ENDC;
                else
                    cout<<OKBLUE<<comp->getName()<<ENDC;
                cout<<INFO<<" ["<<fname<<"]"<<ENDC<<endl;
            }
        }
        return true;
    }


    /**
     *  export knowledgebase graph
     */
    if((cmdList.size() == 2) &&
        (cmdList[0] == "export") )
    {
        if(!exportDependencyGraph(cmdList[1].c_str()))
            cout<<FAIL<<"ERROR:   "<<INFO<<"Cannot export graph to "<<cmdList[1]<<"."<<ENDC<<endl;
        return true;
    }


    /**
     * show module's information
     */
    if((cmdList.size() == 3) &&
        (cmdList[0] == "show") && (cmdList[1] == "mod"))
    {
        KnowledgeBase* kb = getKnowledgeBase();
        if(!kb->getModule(cmdList[2].c_str()))
        {
            cout<<FAIL<<"ERROR:   "<<INFO<<"'"<<cmdList[2].c_str()<<"' not found."<<ENDC<<endl;
            return true;
        }
        cout<<INFO;
        PRINT_MODULE(kb->getModule(cmdList[2].c_str()));
        cout<<ENDC;
        return true;
    }

    /**
     * set an option
     */
     if((cmdList.size() == 3) &&
        (cmdList[0] == "set"))
     {
         config.unput(cmdList[1].c_str());
         config.put(cmdList[1].c_str(), cmdList[2].c_str());

        if(cmdList[1] == string("watchdog"))
        {
            if(cmdList[2] == string("yes"))
                enableWatchDog();
            else
                disableWatchod();
        }

        if(cmdList[1] == string("auto_dependency"))
        {
            if(cmdList[2] == string("yes"))
                enableAutoDependency();
            else
                disableAutoDependency();
        }

        if(cmdList[1] == string("auto_connect"))
        {
            if(cmdList[2] == string("yes"))
                enableAutoConnect();
            else
                disableAutoConnect();
        }

        if(cmdList[1] == string("color_theme"))
        {
            if(cmdList[2] == string("dark"))
                setColorTheme(THEME_DARK);
            else if(cmdList[2] == string("light"))
                setColorTheme(THEME_LIGHT);
            else
                setColorTheme(THEME_NONE);
        }

        return true;
     }


    /**
     * get an option
     */
     if((cmdList.size() == 2) &&
        (cmdList[0] == "get"))
     {
         if(config.check(cmdList[1].c_str()))
         {
            cout<<OKBLUE<<cmdList[1]<<INFO<<" = ";
            cout<<OKGREEN<<config.find(cmdList[1].c_str()).asString()<<ENDC<<endl;
         }
         else
            cout<<FAIL<<"ERROR:   "<<INFO<<"'"<<cmdList[1].c_str()<<"' not found."<<ENDC<<endl;
         return true;
     }

    /**
     * load balancing
     */
    if((cmdList.size() == 2) &&
        (cmdList[0] == "assign") && (cmdList[1] == "hosts"))
    {
        loadBalance();
        reportErrors();
        return true;
    }

    return false;
}