Salome HOME
CCAR:
[modules/kernel.git] / src / Batch / Batch_JobInfo_PBS.cxx
1 //  Copyright (C) 2007-2008  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.
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_PBS.cxx : 
24  *
25  * Auteur : Ivan DUTKA-MALEN - EDF R&D
26  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
27  * Date   : Fri Nov 21 09:42:06 2003
28  * Projet : Salome 2
29  *
30  */
31
32 #include <cstdio>
33 #include <sstream>
34 //#include "MEDMEM_STRING.hxx"
35 #include "Batch_Parametre.hxx"
36 #include "Batch_Environnement.hxx"
37 #include "Batch_RunTimeException.hxx"
38 #include "Batch_JobInfo_PBS.hxx"
39 using namespace std;
40
41 namespace Batch {
42
43   // Constructeurs
44   JobInfo_PBS::JobInfo_PBS(struct batch_status * list, bool tobedeleted) : JobInfo()
45   {
46     // On ne considere que le premier element de la liste
47     // Si tout est OK, la liste ne devrait contenir qu'un element
48     // Sinon on leve une exception.
49     struct batch_status * p_job = list;
50     int i;
51     for(i=0; p_job; p_job = p_job->next) i++;
52     if (i == 0) throw RunTimeException("Liste vide (le job est absent de la file)");
53     if (i > 1) {
54       //MEDMEM::STRING sst;
55       ostringstream sst;
56       sst << "JobInfo_PBS::JobInfo_PBS(struct batch_status * list, bool tobedeleted) : la liste contient "
57           << i << " elements" << " (1 seul requis)" << endl;
58       throw RunTimeException(sst.str());
59     }
60     p_job = list;
61
62     // On remplit les membres _param et _env
63
64     if (p_job->name && strlen(p_job->name)) _param[ID]   = p_job->name;
65     if (p_job->text && strlen(p_job->text)) _param[TEXT] = p_job->text;
66
67     for(struct attrl * p_attr = p_job->attribs; p_attr; p_attr = p_attr->next) {
68
69       string name, res, value;
70       if (p_attr->name && strlen(p_attr->name)) name = p_attr->name;
71       if (p_attr->resource && strlen(p_attr->resource)) res = p_attr->resource;
72       if (p_attr->value && strlen(p_attr->value)) value = p_attr->value;
73
74       if (name == ATTR_N) {
75         _param[NAME] = value;
76       
77       } else if (name == ATTR_owner) {
78         _param[USER] = value;
79
80       } else if (name == ATTR_state) {
81         _param[STATE] = value;
82
83       } else if (name == ATTR_queue) {
84         _param[QUEUE] = value;
85
86       } else if (name == ATTR_A) {
87         _param[ACCOUNT] = value;
88
89       } else if (name == ATTR_M) {
90         _param[MAIL] = value;
91
92       } else if (name == ATTR_c) {
93         if (!strcmp(value.c_str(), CHECKPOINT_UNSPECIFIED)) _param[CHECKPOINT] = 1L;
94         else _param[CHECKPOINT] = 0L;
95
96       } else if (name == ATTR_h) {
97         if (!strcmp(value.c_str(), NO_HOLD)) _param[HOLD] = 0L;
98         else _param[HOLD] = 1L;
99
100       } else if (name == ATTR_ctime) {
101         _param[CREATIONTIME] = atol(value.c_str());
102
103       } else if (name == ATTR_etime) {
104         _param[ELIGIBLETIME] = atol(value.c_str());
105
106       } else if (name == ATTR_mtime) {
107         _param[MODIFICATIONTIME] = atol(value.c_str());
108
109       } else if (name == ATTR_qtime) {
110         _param[QUEUEDTIME] = atol(value.c_str());
111
112       } else if (name == ATTR_exechost) {
113         _param[EXECUTIONHOST] = value;
114
115       } else if (name == ATTR_session) {
116         _param[PID] = atol(value.c_str());
117
118       } else if (name == ATTR_euser) {
119         _param[EUSER] = value;
120
121       } else if (name == ATTR_egroup) {
122         _param[EGROUP] = value;
123
124       } else if (name == ATTR_l) {
125         if (res == "cput") {
126           _param[MAXCPUTIME] = HMStoLong(value);
127
128         } else if (res == "walltime") {
129           _param[MAXWALLTIME] = HMStoLong(value);
130
131         }
132
133       } else if (name == ATTR_used) {
134         if (res == "cput") {
135           _param[USEDCPUTIME] = HMStoLong(value);
136
137         } else if (res == "walltime") {
138           _param[USEDWALLTIME] = HMStoLong(value);
139
140         }
141
142       } else if (name == ATTR_v) {
143         int deb = 0;
144         int pos = 0;
145         bool ok = true;
146
147         while (ok) {
148           pos = value.find(",", deb);
149           string sub = value.substr(deb, pos-deb);
150           deb = pos + 1;
151           if (pos < 0) ok = false;
152
153           int eq = sub.find("=");
154           _env[sub.substr(0, eq)] = sub.substr(eq+1);
155         }
156
157       }
158     }
159
160
161     if (tobedeleted) pbs_statfree(list);
162   }
163
164   // Destructeur
165   JobInfo_PBS::~JobInfo_PBS()
166   {
167     // Nothing to do
168   }
169   
170   // Convertit une date HH:MM:SS en secondes
171   long JobInfo_PBS::HMStoLong(const string & s)
172   {
173     long hour, min, sec;
174
175     sscanf( s.c_str(), "%ld:%ld:%ld", &hour, &min, &sec);
176     return ( ( ( hour * 60L ) + min ) * 60L ) + sec;
177   }
178
179   // Methode pour l'interfacage avec Python (SWIG) : affichage en Python
180   string JobInfo_PBS::__str__() const
181   {
182     //MEDMEM::STRING sst; 
183     ostringstream sst;
184     sst << "<JobInfo_PBS (" << this << ") :" << endl;
185     sst << " ID = " <<_param[ID] << endl;
186
187     sst << "  + Parametre :" << endl;
188     Parametre::const_iterator itp;
189     for(itp=_param.begin(); itp!=_param.end(); itp++) {
190       if ( (*itp).first != ID ) {
191         sst << "    * " << (*itp).first << " = " << (*itp).second << endl;
192       }
193     }
194     return sst.str();
195   }
196
197
198 }