Salome HOME
Copyright update 2021
[tools/libbatch.git] / src / COORM / JobInfo_COORM.cxx
1 // Copyright (C) 2012-2021  INRIA
2 //
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, or (at your option) any later version.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <cstdio>
21 #include <iostream>
22 #include <fstream>
23 #include <sstream>
24 #include <vector>
25
26 #include "Constants.hxx"
27 #include "Parametre.hxx"
28 #include "Environnement.hxx"
29 #include "RunTimeException.hxx"
30 #include "APIInternalFailureException.hxx"
31 #include "JobInfo_COORM.hxx"
32
33 using namespace std;
34
35 namespace Batch {
36
37         // Constructeurs
38         JobInfo_COORM::JobInfo_COORM(const std::string & id, const std::string & queryOutput) : JobInfo()
39         {
40     _param[ID] = id;
41
42     // read query output
43     istringstream logfile(queryOutput);
44
45                 string sline, state, assigned_hostnames;
46
47                 if (logfile)
48                 {
49                         while (getline(logfile, sline) && sline != "")
50                         {
51                                 vector<string> tokens;
52
53                                 JobInfo::Tokenize(sline, tokens, "= ");
54
55                                 if (tokens[0] == "state")
56                                 {
57                                         state = tokens[1];
58                                 }
59
60                                 if (tokens[0] == "assigned_hostnames")
61                                 {
62                                         assigned_hostnames = tokens[1];
63                                 }
64                         }
65
66                         _param[ASSIGNEDHOSTNAMES] = assigned_hostnames;
67
68                         if (state == "FINISHED")
69                         {
70                                 // Completed
71                                 _param[STATE] = FINISHED;
72                         } 
73                         else if (state == "STARTED") 
74                         { 
75                                 // Started
76                                 _param[STATE] = RUNNING;
77                         } 
78                         else if (state == "WAITING") 
79                         { 
80                                 // Waiting
81                                 _param[STATE] = QUEUED;
82                         } 
83                         else if (state == "SUBMITTED")
84                         {
85                                 // Submitted
86                                 _param[STATE] = CREATED;
87                         }
88                         else if (state == "KILLED")
89                         {
90                                 // Killed
91                                 _param[STATE] = FAILED;
92                         }
93                         else 
94                         {
95                                 cerr << "Unknown job state code: " << state << endl;
96                         }
97                 } 
98                 else 
99                 {
100                         throw RunTimeException("Error of connection on remote host");
101                 }
102         }
103
104         // Destructeur
105         JobInfo_COORM::~JobInfo_COORM()
106         {
107                 // Nothing to do
108         }
109
110         // Convertit une date HH:MM:SS en secondes
111         long JobInfo_COORM::HMStoLong(const string & s)
112         {
113                 long hour, min, sec;
114
115                 sscanf( s.c_str(), "%ld:%ld:%ld", &hour, &min, &sec);
116                 return ( ( ( hour * 60L ) + min ) * 60L ) + sec;
117         }
118
119         // Methode pour l'interfacage avec Python (SWIG) : affichage en Python
120         string JobInfo_COORM::__str__() const
121         {
122                 ostringstream sst;
123                 sst << "<JobInfo_eCOORM (" << this << ") :" << endl;
124                 sst << " ID = " <<_param[ID] << endl;
125                 sst << " STATE = " <<_param[STATE] << endl;
126
127                 return sst.str();
128         }
129 }