Salome HOME
Addition of a new type of SalomeComponent to take advantage of HPContainer.
[modules/yacs.git] / src / runtime / SalomeHPContainerTools.cxx
1 // Copyright (C) 2006-2014  CEA/DEN, EDF R&D
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 "SalomeHPContainerTools.hxx"
21 #include "Exception.hxx"
22
23 #include <algorithm>
24
25 using namespace YACS::ENGINE;
26
27 void SalomeHPContainerVectOfHelper::resize(std::size_t sz)
28 {
29   std::size_t oldSize(_launchModeType.size());
30   if(sz==oldSize)
31     return;
32   checkNoCurrentWork();
33   _whichOccupied.resize(sz); std::fill(_whichOccupied.begin(),_whichOccupied.end(),false);
34   _launchModeType.clear(); _launchModeType.resize(sz);
35   for(std::size_t i=oldSize;i<sz;i++)
36       _launchModeType[i]=new SalomeContainerMonoHelper;
37   _currentlyWorking.clear();
38 }
39
40 std::size_t SalomeHPContainerVectOfHelper::getNumberOfFreePlace() const
41 {
42   return std::count(_whichOccupied.begin(),_whichOccupied.end(),false);
43 }
44
45 void SalomeHPContainerVectOfHelper::allocateFor(const std::vector<const Task *>& nodes)
46 {
47   for(std::vector<const Task *>::const_iterator it=nodes.begin();it!=nodes.end();it++)
48     {
49       if(!(*it))
50         continue;
51       if(_currentlyWorking.find(*it)!=_currentlyWorking.end())
52         throw Exception("Searching to allocate for a ServiceNode instance already declared as allocated !");
53       std::vector<bool>::iterator it2(std::find(_whichOccupied.begin(),_whichOccupied.end(),false));
54       if(it2==_whichOccupied.end())
55         throw Exception("All ressources are already occupied ! You are expected to wait for released resources !");
56       std::size_t pos(std::distance(_whichOccupied.begin(),it2));
57       _currentlyWorking[*it]=pos; _whichOccupied[pos]=true;
58     }
59 }
60
61 void SalomeHPContainerVectOfHelper::release(const Task *node)
62 {
63   if(!node)
64     return ;
65   std::map< const Task *,std::size_t >::iterator it(_currentlyWorking.find(node));
66   if(it==_currentlyWorking.end())
67     throw Exception("Request to release a resource not declared as working !");
68   _whichOccupied[(*it).second]=false;
69   _currentlyWorking.erase(it);
70 }
71
72 std::size_t SalomeHPContainerVectOfHelper::locateTask(const Task *node) const
73 {
74   std::map< const Task *,std::size_t >::const_iterator it(_currentlyWorking.find(node));
75   if(it==_currentlyWorking.end())
76     throw Exception("current Node to be located is not marked as launched !");
77   std::size_t ret((*it).second);
78   checkPosInVec(ret);
79   return ret;
80 }
81
82 const SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTask(const Task *node) const
83 {
84   return _launchModeType[locateTask(node)];
85 }
86
87 SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTask(const Task *node)
88 {
89   return _launchModeType[locateTask(node)];
90 }
91
92 void SalomeHPContainerVectOfHelper::checkNoCurrentWork() const
93 {
94   for(std::map<const Task *,std::size_t >::const_iterator it=_currentlyWorking.begin();it!=_currentlyWorking.end();it++)
95     if((*it).first)
96       throw Exception("Something wrong a node is still declared to be using the ressource !");
97   for(std::vector< YACS::BASES::AutoRefCnt<SalomeContainerMonoHelper> >::const_iterator it=_launchModeType.begin();it!=_launchModeType.end();it++)
98     if((*it)->isAlreadyStarted(0))
99       throw Exception("Some of the containers have be started ! Please shutdown them before !");
100 }
101
102 void SalomeHPContainerVectOfHelper::checkPosInVec(std::size_t pos) const
103 {
104   if(pos<0 || pos>=_launchModeType.size())
105     throw Exception("The task has been found, but its id is not in the correct range ! resize of of container size during run ?");
106 }