1 // Copyright (C) 2006-2012 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "ListJobs_GUI.hxx"
24 #include "RuntimeSALOME.hxx"
25 #include "GenericGui.hxx"
26 #include "QtGuiContext.hxx"
27 #include "YACSGuiLoader.hxx"
29 #include "YacsTrace.hxx"
30 #include "LoadState.hxx"
33 using namespace YACS::HMI;
34 using namespace YACS::ENGINE;
36 BatchJobsListDialog::BatchJobsListDialog(QString title,GenericGui* genericGui): QWidget(),_genericGui(genericGui),_proc(NULL) {
38 setWindowTitle(title);
40 // Connection to Salome Launcher
44 QBoxLayout* box = new QBoxLayout(QBoxLayout::TopToBottom, this);
47 _table = new QTableWidget(this);
48 _table->setColumnCount(4);
53 box->addWidget(_table);
56 QWidget* buttons = build_buttons();
57 box->addWidget(buttons);
60 _ok->setEnabled(false);
63 // Set timer to refresh table
64 _timer1 = new QTimer(this);
65 connect(_timer1, SIGNAL(timeout()), this, SLOT(build_table()));
66 _timer1->start(30000);
70 BatchJobsListDialog::~BatchJobsListDialog(){
73 void BatchJobsListDialog::getSalomeLauncher(){
74 YACS::ENGINE::RuntimeSALOME* runTime = YACS::ENGINE::getSALOMERuntime();
75 CORBA::ORB_ptr orb = runTime->getOrb();
76 SALOME_NamingService* NS = new SALOME_NamingService(orb);
77 CORBA::Object_var obj = NS->Resolve("/SalomeLauncher");
78 _salome_launcher = Engines::SalomeLauncher::_narrow(obj);
79 if (CORBA::is_nil(_salome_launcher))
80 throw YACS::Exception("Salome Launcher not reachable!!");
83 // Display list of current batch jobs and allow selection only on running jobs
84 void BatchJobsListDialog::build_table() {
90 Engines::JobsList* joblist = _salome_launcher->getJobsList();
92 int nblines = joblist->length();
93 _table->setRowCount(nblines);
96 titres << "Job Name" << "State" << "Resource" << "Launcher Id";
97 _table->setHorizontalHeaderLabels(titres);
99 for(int i=0;i<nblines;i++){
100 Engines::JobDescription descr = (*joblist)[i];
101 string jobType = CORBA::string_dup(descr.job_parameters.job_type);
102 // We display only jobs of type yacs_file
103 if(jobType.find("yacs_file")!=string::npos){
104 long jobId = descr.job_id;
107 string jobName = CORBA::string_dup(descr.job_parameters.job_name);
108 string jobState = _salome_launcher->getJobState(jobId);
109 string jobMachine = CORBA::string_dup(descr.job_parameters.resource_required.name);
110 QTableWidgetItem* item0 = new QTableWidgetItem(jobName.c_str() , type_job);
111 QTableWidgetItem* item1 = new QTableWidgetItem(jobState.c_str() , type_state);
112 QTableWidgetItem* item2 = new QTableWidgetItem(jobMachine.c_str() , type_res );
113 QTableWidgetItem* item3 = new QTableWidgetItem(sstId.str().c_str(), type_id );
114 _table->setItem(irow, 0, item0);
115 _table->setItem(irow, 1, item1);
116 _table->setItem(irow, 2, item2);
117 _table->setItem(irow, 3, item3);
118 if(jobState != "RUNNING"){
119 item0->setFlags(Qt::NoItemFlags);
120 item1->setFlags(Qt::NoItemFlags);
121 item2->setFlags(Qt::NoItemFlags);
122 item3->setFlags(Qt::NoItemFlags);
124 else if(jobId == _id){
125 item0->setSelected(true);
126 item1->setSelected(true);
127 item2->setSelected(true);
128 item3->setSelected(true);
134 connect(_table, SIGNAL( itemClicked(QTableWidgetItem*) ), this, SLOT( userCell(QTableWidgetItem*) ));
138 // Build buttons in a layout
139 // -------------------------
141 QWidget* BatchJobsListDialog::build_buttons() {
142 _ok = new QPushButton("OK" , this);
143 QPushButton* cancel = new QPushButton("Cancel", this);
145 connect(_ok , SIGNAL(clicked()), this, SLOT(userOK ()));
146 connect(cancel, SIGNAL(clicked()), this, SLOT(userCancel()));
148 QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
150 layout->addWidget(_ok);
151 layout->addWidget(cancel);
153 QWidget* buttons = new QWidget(this);
154 buttons->setLayout(layout);
159 // Click on a cell button
160 // ----------------------
162 void BatchJobsListDialog::userCell(QTableWidgetItem* cell) {
163 if ( cell->flags() ) {
164 int line = cell->row();
165 string sid = _table->item(line,3)->text().toStdString();
166 _id = atoi(sid.c_str());
167 _ok->setEnabled(true);
169 QTableWidget* table = cell->tableWidget();
170 for (int c=0; c<4; c++) {
171 QTableWidgetItem* it = table->item(line, c);
172 it->setSelected(true);
177 // Click on OK button
178 // ------------------
180 void BatchJobsListDialog::userOK() {
183 Engines::JobParameters* jobParam = _salome_launcher->getJobParameters(_id);
184 _jobFile = CORBA::string_dup(jobParam->job_file);
185 _dumpStateFile = QString("/tmp/%1/dumpState_%2.xml").arg(getenv("USER")).arg(QFileInfo(_jobFile).baseName());
187 // stop first timer and hide window
191 // replace objref by string in YACS schema job file to avoid trying to reach remote objects
194 // display yacs graph
195 YACSGuiLoader *loader = _genericGui->getLoader();
196 _proc = loader->load(_filteredJobFile.toLatin1());
198 QMessageBox msgBox(QMessageBox::Critical, "Import Batch Schema, native YACS XML format",
199 "The xml graph has not the native YACS XML format or is not readable." );
203 YACS::ENGINE::Logger* logger= _proc->getLogger("parser");
204 if(!logger->isEmpty()) {
205 DEBTRACE(logger->getStr());
207 _genericGui->createContext(_proc, _filteredJobFile, _filteredJobFile, false);
210 // start second timer to get remote graph state xml file
211 _timer2 = new QTimer(this);
212 connect(_timer2, SIGNAL(timeout()), this, SLOT(get_dump_file()));
213 _timer2->start(30000);
217 // Click on CANCEL button
218 // ----------------------
220 void BatchJobsListDialog::userCancel() {
225 // get remote graph state xml file
227 void BatchJobsListDialog::get_dump_file()
229 int execState = YACS::NOTYETINITIALIZED;
230 // get batch job state
231 string jobState = _salome_launcher->getJobState(_id);
232 // get dump state remote file
233 bool ret = _salome_launcher->getJobDumpState(_id, QFileInfo(_dumpStateFile).absolutePath().toLatin1().constData());
235 // replace objref by string in dump state file
236 filterDumpStateFile();
237 // parse dump state file and load proc
238 YACS::ENGINE::loadState(_proc,_filteredDumpStateFile.toStdString());
239 // display remote graph states
240 GuiExecutor *executor = QtGuiContext::getQtCurrent()->getGuiExecutor();
241 execState = executor->updateSchema(jobState);
243 // stop timer if job is not running
244 if((execState!=YACS::RUNNING)&&(execState!=YACS::NOTYETINITIALIZED))
248 // filtering of _jobFile to replace objref by string
249 void BatchJobsListDialog::filterJobFile()
251 _filteredJobFile = QString("/tmp/%1/%2.xml").arg(getenv("USER")).arg(QFileInfo(_jobFile).baseName());
252 // reading input file
253 ifstream infile(_jobFile.toStdString().c_str());
255 string errmsg = "File " + _jobFile.toStdString() + " doesn't exist!!";
256 throw YACS::Exception(errmsg);
259 vector<string> objref;
260 while( !infile.eof() ){
261 getline(infile,buffer);
262 // look for objref and memorize them in vector
263 if( (buffer.find("objref") != string::npos) && (buffer.find("IDL") != string::npos) ){
264 size_t pos1 = buffer.find("\"");
265 size_t pos2 = buffer.find("\"",pos1+1);
266 objref.push_back(buffer.substr(pos1+1,pos2-pos1-1));
270 // reread the input file
271 infile.open(_jobFile.toStdString().c_str());
272 // open the output file
273 ofstream outfile(_filteredJobFile.toStdString().c_str());
275 string errmsg = "Impossible to create the file " + _filteredJobFile.toStdString() + "!!";
276 throw YACS::Exception(errmsg);
278 while( !infile.eof() ){
279 getline(infile,buffer);
280 // replace objref by string
281 if( ((buffer.find("objref") == string::npos) || (buffer.find("IDL") == string::npos)) && (buffer.find("/objref") == string::npos) ){
283 for(int i=0;i<objref.size();i++){
284 size_t pos = buffer.find(objref[i]);
285 if(pos != string::npos)
286 tmp = buffer.substr(0,pos) + "string" + buffer.substr(pos+objref[i].size());
288 outfile << tmp << endl;
293 // filtering of _dumpStateFile to replace objref by string
294 void BatchJobsListDialog::filterDumpStateFile()
297 _filteredDumpStateFile = QString("/tmp/%1/filtered_%2.xml").arg(getenv("USER")).arg(QFileInfo(_dumpStateFile).baseName());
298 ifstream infile(_dumpStateFile.toStdString().c_str());
300 string errmsg = "File " + _dumpStateFile.toStdString() + " doesn't exist!!";
301 throw YACS::Exception(errmsg);
303 ofstream outfile(_filteredDumpStateFile.toStdString().c_str());
305 string errmsg = "Impossible to create the file " + _filteredDumpStateFile.toStdString() + "!!";
306 throw YACS::Exception(errmsg);
308 // replace objref by string in dump state file
309 while( !infile.eof() ){
310 getline(infile,buffer);
311 size_t pos = buffer.find("objref");
312 while(pos != string::npos){
313 buffer.replace(pos,6,"string");
314 pos = buffer.find("objref");
316 outfile << buffer << endl;