Salome HOME
Copyright update 2021
[tools/libbatch.git] / src / Vishnu / JobInfo_Vishnu.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  *  JobInfo_Vishnu.cxx :
24  *
25  *  Created on: 24 june 2011
26  *  Author : Renaud BARATE - EDF R&D
27  */
28
29 #include <iostream>
30 #include <sstream>
31
32 #include <RunTimeException.hxx>
33 #include <Constants.hxx>
34
35 #include "JobInfo_Vishnu.hxx"
36
37 using namespace std;
38
39 namespace Batch {
40
41   JobInfo_Vishnu::JobInfo_Vishnu(const string & id, const string & queryOutput)
42     : JobInfo()
43   {
44     _param[ID] = id;
45
46     // find the status in the query output
47     istringstream iss(queryOutput);
48     string status;
49     bool statusFound = false;
50     while (!statusFound && !iss.eof()) {
51       string line;
52       getline(iss, line);
53       size_t pos = line.find(':');
54       if (pos != string::npos) {
55         string begline = line.substr(0, pos);
56         string keyword;
57         // Trim leading and trailing spaces of the string before ':'
58         size_t startpos = begline.find_first_not_of(" \t");
59         size_t endpos = begline.find_last_not_of(" \t");
60         if (startpos != string::npos && endpos != string::npos)
61           keyword = begline.substr(startpos, endpos-startpos+1);
62
63         if (keyword == "Status") {
64           statusFound = true;
65           string endline = line.substr(pos + 1);
66           startpos = endline.find_first_not_of(" \t");
67           endpos = endline.find_last_not_of(" \t");
68           if (startpos != string::npos && endpos != string::npos)
69             status = endline.substr(startpos, endpos-startpos+1);
70         }
71       }
72     }
73
74     if (status.size() == 0) {
75       // On some batch managers, the job is deleted as soon as it is finished,
76       // so we have to consider that an unknown job is a finished one, even if
77       // it is not always true.
78       _param[STATE] = FINISHED;
79     } else if (status == "QUEUED") {
80       _param[STATE] = QUEUED;
81     } else if (status == "WAITING") {
82       _param[STATE] = QUEUED;
83     } else if (status == "RUNNING") {
84       _param[STATE] = RUNNING;
85     } else if (status == "TERMINATED") {
86       _param[STATE] = FINISHED;
87     } else if (status == "CANCELLED") {
88       _param[STATE] = FAILED;
89     } else {
90       throw RunTimeException("Unknown job state code: \"" + status + "\"");
91     }
92   }
93
94   JobInfo_Vishnu::~JobInfo_Vishnu()
95   {
96   }
97
98 }