/tags/V_0_8_3/AgCSP/src/libPVMAgCSP/Supervisor.cpp
C++ | 652 lines | 406 code | 101 blank | 145 comment | 82 complexity | 543e816fbdaa5a501a10a5363e00518d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
- // Copyright (C) 1998 DIDIERJEAN <Fabrice.Didierjean@sophia.inria.fr>
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; either version 2 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- // $Date: 2000-07-04 22:18:09 +0200 (Tue, 04 Jul 2000) $
- // $Id: Supervisor.cpp 80 2000-07-04 20:18:09Z coredmp $
- // $Revision: 80 $
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <sys/types.h>
- #include <fcntl.h>
- #ifndef WIN32
- #include <unistd.h>
- #endif
- #include <sys/stat.h>
- #include <stdio.h>
- #include <pvm3.h>
- #include <iomanip>
- #include <fstream>
- //#include "ListSupAlg.h"
- #include "Command.h"
- #include "SupAlg.h"
- #include "Packet.h"
- #include "PacketPVM.h"
- #include "Feeder.h"
- #include "Error.h"
- #include "Common.h"
- #include "Deme.h"
- #include "Parametable.h"
- #include "Operator.h"
- #include "Term.h"
- #include "Client.h"
- #include "Supervisor.h"
- #include "AlleleSet.h"
- #include "Constraint.h"
- #include "Gene.h"
- #include "Operator.h"
- #include "Term.h"
- #include "FeedTube.h"
- // #include "Res_String.h"
- // #include "Res_Double.h"
- // #include "Res_Long.h"
- // #include "Res_Bool.h"
- // #include "Res_NumGeneration.h"
- // #include "Res_MeilleurFit.h"
- // #include "Res_MeilleurEval.h"
- // #include "Res_MoyenneFit.h"
- // #include "Res_MoyenneEval.h"
- // #include "Res_NbEvaluation.h"
- // #include "Res_Etat.h"
- // #include "Res_TpsCPU.h"
- // #include "Res_MeilleurChr.h"
- // #include "Res_TailleGen.h"
- // #include "Res_NbCross.h"
- // #include "Res_NbMutation.h"
- // #include "Res_NbKill.h"
- // #include "Res_NbSel.h"
- // #include "Res_NbTstConstraints.h"
- // #include "Res_Success.h"
- #include "ResFactory.h"
- #include "AgCSP.h"
- //#include <AgCSP/ResultGraph.h>
- const int Supervisor::MOTEUR_INDET = 1;
- const int Supervisor::PACKET_INCONNU = 2;
- const int Supervisor::COMMANDE_INCONNUE = 3;
- const int Supervisor::ERREUR_SYSTEME = 4;
- const int Supervisor::QUIT = 1;
- const int Supervisor::OK = 2;
- //string Supervisor::identifiant("Supervisor");
- Supervisor::Supervisor() : cmd(NULL), etat(OK),mot(NULL) , idxclt(0),flg(false)
- {
- ;
- }
- Supervisor::~Supervisor()
- {
- _delete();
- }
- void Supervisor::_delete()
- {
- if(cmd)
- delete cmd;
- while(clients.size()>0) {
- clients[clients.size()-1]->quit();
- delete clients[clients.size()-1];
- clients.pop_back();
- }
- }
- Client *Supervisor::addClient(const string &nm, const string &fc, const string &rep)
- {
- Client *clt;
- PDEBUG("Addclient start\nRecup client de nom "<<nm<<"fich conf "<<fc<<" dans rep "<<rep<<" :",5);
- clt = new Client(nm,Common::getPwd());
- PDEBUG(" Ok\n",5);
- // ret = clt->checkState();
- // PDEBUG(" Ok\n",5);
- // if(ret == Client::CLIENT_MORT)
- // throw Error("Supervisor::addClient()","Le client est mort (ne repond plus)",Error::CLIENT_MORT);
- // else if(ret == Client::CLIENT_EN_ERREUR) {
- // throw Error("Supervisor::addClient()","Le client est est un erreur",Error::CLIENT_EN_ERREUR);
- // }
- if(rep!="") {
- PDEBUG("Changement de rep ",5);
- clt->setRepExec(rep);
- PDEBUG(" Ok\n",5);
- }
- if(fc!="") {
- PDEBUG("Chargement du fichier "<<fc<<" :",5);
- clt->loadProbleme(fc);
- PDEBUG(" Ok\n",5);
- }
- PDEBUG("Ajout client dans donnees :",5);
- clients.push_back(clt);
- PDEBUG(" Ok\n",5);
- return(clt);
- }
-
- void Supervisor::setSuperviseur(const string &nm)
- {
- mot = SupAlg::getRegistered(nm);
- if(mot != NULL)
- mot->setSuperviseur(this);
- }
- int Supervisor::getState()
- {
- return(etat);
- }
- void Supervisor::launch(Feeder *fd)
- {
- feeder = fd;
- Packet *pack;
- init();
- while(getState() != Supervisor::QUIT) {
- while(!feeder->isPacket() && getState() != Supervisor::QUIT) ;
- if(getState() != Supervisor::QUIT) {
- feeder->recvPacket();
- pack = &feeder->getPacket();
- traiterPacket(*pack);
- }
- }
- }
- void Supervisor::traiterPacket(Packet &p)
- {
- if(p.getType()==Packet::COMMANDE)
- traiterCommande(p.getCommande());
- else {
- #ifdef _DEBUG
- p.aff(*debug_stream);
- #endif
- throw Error("Supervisor::traiterPacket()","Packet de type inconnu",Error::PACKET_INCONNU);
- }
-
- }
- void Supervisor::traiterCommande(Command &cmd)
- {
- string nm = cmd.getName();
- if(nm=="QUIT")
- Quit();
- else if(nm=="COM") {
- ;
- // else if(nm=="SETREPEXEC") {
- // *out << "Definition du repertoire d'execution (" <<cmd.getParameter("REPERTOIRE").getString()<<") :"<< flush;
- // setRepExec(cmd.getParameter("REPERTOIRE").getString());
- // *out << " OK\n";
- } else if(nm=="SETSUPERVISEUR") {
- *out << "Definition du Supervisor de gestion ("<<cmd.getParameter("NOM").getString()<<") :"<<flush;
- setSuperviseur(cmd.getParameter("NOM").getString());
- *out << " OK\n";
- } else if(nm=="SETPARAMETRESUPERVISEUR") {
- *out << "Fixation parametre du superviseur (" \
- <<cmd.getParameter("NOMPARAMETRE").getString()\
- <<"="<<cmd.getParameter("VALEUR").getString()<<") :"<<flush;
- if(mot == NULL)
- throw Error("Supervisor::traiterCommande()","Deme non defini",Error::MOTEUR_INCONNU);
- mot->setParameter(Common::upcase(cmd.getParameter("NOMPARAMETRE").getString()),cmd.getParameter("VALEUR").getString());
- *out << " OK\n";
- } else if(nm=="LAUNCH") {
- *out << "Lancement du Moteur de gestion \n"<<flush;
- if(mot == NULL)
- throw Error("Supervisor::traiterCommande()","Deme non defini",Error::MOTEUR_INCONNU);
- mot->run();
- *out << " Fin Deme Gestion\n"<<flush;
- //mot->makeReport("Rapport.tex");
- } else if(nm == "MAKERAPPORT") {
- mot->makeReport(cmd.getParameter("TYPE").getString());
- } else if(nm == "ADDVARPARCLT") {
- *out << "Ajout du parametre variable sur le client ["<<cmd.getParameter("VARCLT").getString()<<"] prenant ses valeur dans ["<<cmd.getParameter("FICHVARCLT").getString()<<"] :"<<flush;
- mot->addVarParClt(cmd.getParameter("VARCLT").getString(),cmd.getParameter("FICHVARCLT").getString());
- *out << " OK\n";
- } else if(nm == "ADDVARPARSPV") {
- *out << "Ajout du parametre variable pour le superviseur ["<<cmd.getParameter("VARSPV").getString()<<"] prenant ses valeur dans ["<<cmd.getParameter("FICHVARSPV").getString()<<"] :"<<flush;
- mot->addVarParSpv(cmd.getParameter("VARSPV").getString(),cmd.getParameter("FICHVARSPV").getString());
- *out << " Ok\n";
- } else if(nm == "ADDRES") {
- *out << "Ajout du resultat :["<<cmd.getParameter("NOM_RES").getString()<<" ] :"<<flush;
- mot->addResult(cmd.getParameter("NOM_RES").getString());
- *out << " Ok\n";
- } else
- mot->traiterCommande(cmd);
- // throw Error("Supervisor::traiterCommande()","Command "+nm+ " inconnu pour un SupMoteur.",Error::COMMANDE_INCONNUE);
- }
-
- // const string &Supervisor::getRepExec()
- // {
- // return(repExec);
- // }
- void Supervisor::remplaceClient(Client *cl,const string &fc)
- {
- string nm = cl->getName();
- delete cl;
- cl = new Client(nm,AgCSP::getAgCSP()->getResultsDir()+"/"+getID());
- //cl->setRepExec(repExec);
- if(fc!="")
- cl->loadProbleme(fc);
- }
- void Supervisor::setRepExec(const string &r)
- {
- string cmd;
- int rep=0;
-
- cmd = "mv "+getID()+" "+r;
- rep = system(cmd.c_str());
- if(rep)
- throw Error("Supervisor::setRepExec()","Error Command Systeme",Error::ERREUR_SYSTEME);
- Common::chDir(r);
- }
- void Supervisor::setID(const string &nm)
- {
- identifiant=nm;
- }
- const string &Supervisor::getID()
- {
- return(identifiant);
- }
- void Supervisor::subClient(const string &nm)
- {
- PDEBUG("subClient Start \n",5);
- vector<Client *>::iterator pos = clients.begin();
- int cpt=0;
- PDEBUG("Recherche du client "<<nm<<" : ",5);
- while(pos != clients.end() && (*pos)->getName()!=nm) {
- pos++;
- cpt++;
- }
-
- if((*pos)->getName() == nm) {
- PDEBUG(" Ok\nAjout du resultat partiel :",5);
- //(*pos)->makeReport();
- mot->addResPart(mot->calcResult(*pos));
- PDEBUG(" Ok\nClient quit ",5);
- (*pos)->quit();
- PDEBUG("Ok\nEffacement :",5);
- delete *pos;
- clients.erase(pos);
- if(cpt<=idxclt)
- idxclt--;
- if(idxclt<0)
- idxclt=0;
- PDEBUG(" Ok\n",5);
- } else
- PDEBUG(" non trouve \n",5);
- }
- void Supervisor::run(const string &nm)
- {
- int sz = clients.size();
- int i = 0;
- if(nm=="") {
- for(i=0; i<sz; i++)
- clients[i]->run();
- } else {
- while(nm!=clients[i]->getName() && i<sz)
- i++;
- if(i<sz)
- clients[i]->run();
- }
-
- }
- void Supervisor::initialize()
- {
- // readConf();
- //Command::setReg();
- //initRes();
- struct tm *temps;
- time_t timet;
- char buff[301];
- packet=new Packet(-1);
- cmd = new Command;
- timet = time(NULL);
- temps = localtime(&timet);
- sprintf(buff,"%02d_%02d_%02d-%02dh%02dm%02ds-spv%0x",temps->tm_year+1900,temps->tm_mon+1,temps->tm_mday,temps->tm_hour,temps->tm_min,temps->tm_sec,pvm_mytid());
- setID(buff);
- ;
- }
- // void Supervisor::initRes()
- // {
- // (void)new Res_Success(true);
- // (void)new Res_NumGeneration(true);
- // (void)new Res_MeilleurFit(true);
- // (void)new Res_MeilleurEval(true);
- // (void)new Res_MoyenneFit(true);
- // (void)new Res_MoyenneEval(true);
- // (void)new Res_NbEvaluation(true);
- // (void)new Res_Etat(true);
- // (void)new Res_TpsCPU(true);
- // (void)new Res_MeilleurChr(true);
- // (void)new Res_TailleGen(true);
- // (void)new Res_NbCross(true);
- // (void)new Res_NbMutation(true);
- // (void)new Res_NbKill(true);
- // (void)new Res_NbSel(true);
- // (void)new Res_NbTstConstraints(true) ;
- // }
- // void Supervisor::readConf()
- // {
- // char *h=getenv("HOME");
- // if(h == NULL)
- // throw Error("Deme::readConf()","No HOME variable defined");
- // home = h;
- // string fichConf=home+"/.agcsprc";
- // string ligne,cmd,par;
- // int ret;
- // unsigned int pos;
- // struct stat st;
- // char buff[256];
- // #ifdef _DEBUG
- // dl = 0;
- // #endif
- // ret = stat(fichConf.c_str(),&st);
-
- // if(ret)
- // throw Error("Deme::readConf()","No configuration file ["+fichConf+"]");
- // ifstream in(fichConf.c_str());
- // while(!in.eof()) {
- // in.getline(buff,255);
- // ligne = buff;
- // ligne=Common::trim(ligne);
- // if(ligne.size()>0) {
- // pos = ligne.find('=');
- // if(pos>=ligne.size())
- // throw Error("Deme::readConf()","Syntax error missing \"=\"",Error::ERREUR_SYNTAXE);
- // cmd = Common::getat(ligne,0,pos-1);
- // par = Common::getat(ligne,pos+1,ligne.size()-1);
- // cmd = Common::upcase(cmd);
- // traiteConf(cmd,par);
- // }
- // }
- // // if(repDonnees=="")
- // // throw Error("Deme::readConf()","No data directory defined (line DATA=...) \n");
- // if(Common::getBase()=="")
- // throw Error("Supervisor::readConf()","No results directory defined (line BASE=...)\n");
- // }
- // const string &Supervisor::getRepDonnees() const
- // {
- // return(repDonnees);
- // }
- // void Supervisor::traiteConf(const string &cmd, const string &par)
- // {
- // //*out << "Reading configuration file :"<<flush;
- // // PDEBUG("Ligne lu :"<<ligne <<endl,5);
- // if(cmd == "BASE") {
- // Common::setBase(Common::convertSlash(home+"/"+Common::trim(par," /\n\t"),false));
- // } else if(cmd=="DATA") {
- // repDonnees = Common::convertSlash(home +"/"+ par,false);
- // } else if(cmd == "DEBUG_LEVEL") {
- // #ifdef _DEBUG
- // dl = atoi(par.c_str());
- // #else
- // *out << "Release Version\n";
- // #endif
- // } else
- // *out << "Unknown parameter ["<< cmd<<"] with value "<<par<<endl;
-
- // }
- void Supervisor::init()
- {
- Common::mkDir(getID());
- Common::chDir(getID());
- }
- Client *Supervisor::getStopped()
- {
- int sz = clients.size();
- int i=0;
- Client *ret=NULL;
- while(i<sz && clients[i]->getState() != Deme::STOP)
- i++;
- if(i<sz)
- ret = clients[i];
-
- return(ret);
- }
- // vector<Client *> &Supervisor::getClients()
- // {
- // return(clients);
- // }
- Client * Supervisor::getClient(unsigned int n)
- {
- assert(n<clients.size() && n>=0);
- return(clients[n]);
- }
- int Supervisor::getNbClients() const
- {
- return(clients.size());
- }
- Client *Supervisor::getTerminated()
- {
- int sz = clients.size();
- int i=0;
- Client *ret=NULL;
- Deme::State et;
- bool go=true;
- Term::State stt;
- if(sz>0)
- et = clients[i]->getState();
- while(i<sz && go) {
- if(clients[i]->getState()==Deme::STOP) {
- stt = clients[i]->getTermState();
- if(stt==Term::TROUVE || stt==Term::ECHEC)
- go=false;
- else
- i++;
- } else
- i++;
- }
- if(i<sz)
- ret = clients[i];
-
- return(ret);
- }
- void Supervisor::waitReady()
- {
- int sz = clients.size();
- for(int i=0; i<sz; i++)
- clients[i]->waitReady();
- }
- void Supervisor::stop()
- {
- int sz = clients.size();
- for(int i=0; i<sz; i++)
- clients[i]->stop();
- }
- void Supervisor::Quit()
- {
- *out << "Terminaison du Supervisor demandee\n";
- stop();
- etat = Supervisor::QUIT;
- if(mot)
- mot->Quit();
- }
- void Supervisor::setMoteur(SupAlg *m)
- {
- if(mot)
- delete mot;
-
- mot=m;
- }
- const Client *Supervisor::getClientPid(unsigned int pid) const
- {
- unsigned int i=0;
- Client *ret=NULL;
- while(i<clients.size() && clients[i]->getPid()!=pid)
- i++;
- if(i<clients.size())
- ret = clients[i];
- return(ret);
- }
-
- Client *Supervisor::getClientPid(unsigned int pid)
- {
- unsigned int i=0;
- Client *ret=NULL;
- while(i<clients.size() && clients[i]->getPid()!=pid)
- i++;
- if(i<clients.size())
- ret = clients[i];
- return(ret);
- }
-
- bool Supervisor::isPacket(Packet::Type tp, int from) const
- {
- const Client *clt;
- unsigned int i;
- bool ret = false ;
- if(clients.size()>0) {
- if(from!=-1) {
- clt=getClientPid(from);
- if(clt==NULL)
- throw Error("Supervisor::isPacket()","Error, no client whith this PID ",Error::NON_TROUVE);
- else if(clt->getTube()->isPacket(tp))
- ret = true;
- else
- ret=false;
-
- } else {
- i=0;
- while(!ret && i<clients.size()) {
- clt = clients[idxclt];
- if(clt->getTube()->isPacket(tp)) {
- ret = true;
- flg=true;
- } else {
- idxclt++;
- i++;
- if(idxclt>=clients.size())
- idxclt=0;
- }
- }
- }
- }
- return(ret);
- }
- void Supervisor::recvPacket(Packet::Type tp, int from)
- {
- Client *clt;
-
- if(from!=-1)
- clt=getClientPid(from);
- else {
- clt=clients[idxclt];
- flg=false;
- }
- clt->getTube()->recvPacket(tp);
- *packet=clt->getTube()->getPacket();
- }
- ostream *Supervisor::getOut()
- {
- return(NULL);
- }
- istream *Supervisor::getIn()
- {
- return(NULL);
- }
- void Supervisor::sendPacket( const Packet &pck) const
- {
- Client *clt;
- clt=clients[idxclt];
- clt->getTube()->sendPacket(pck);
- }
- void Supervisor::replyPacket( const Packet &pck) const
- {
- Client *clt;
- clt=clients[idxclt];
- clt->getTube()->replyPacket(pck);
- }
- void Supervisor::sendPacket() const
- {
- Client *clt;
- clt=clients[idxclt];
- clt->getTube()->sendPacket(*packet);
- }
- void Supervisor::replyPacket() const
- {
- Client *clt;
- clt=clients[idxclt];
- clt->getTube()->replyPacket(*packet);
- }