Salome HOME
Copyrights update 2015.
[modules/yacs.git] / src / runtime / SalomeHPContainer.cxx
1 // Copyright (C) 2006-2015  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 "SalomeHPContainer.hxx"
21 #include "SalomeHPComponent.hxx"
22 #include "SalomeContainerTmpForHP.hxx"
23 #include "AutoLocker.hxx"
24
25 #include <algorithm>
26
27 using namespace YACS::ENGINE;
28
29 const char SalomeHPContainer::KIND[]="HPSalome";
30
31 SalomeHPContainer::SalomeHPContainer():_shutdownLevel(999)
32 {
33 }
34
35 SalomeHPContainer::SalomeHPContainer(const SalomeHPContainer& other):_componentNames(other._componentNames),_shutdownLevel(999),_sct(other._sct),_initScript(other._initScript)
36 {
37 }
38
39 void SalomeHPContainer::setSizeOfPool(int sz)
40 {
41   _launchModeType.resize(sz);
42 }
43
44 int SalomeHPContainer::getSizeOfPool() const
45 {
46   return _launchModeType.size();
47 }
48
49 std::size_t SalomeHPContainer::getNumberOfFreePlace() const
50 {
51   return _launchModeType.getNumberOfFreePlace();
52 }
53
54 void SalomeHPContainer::allocateFor(const std::vector<const Task *>& nodes)
55 {
56   _launchModeType.allocateFor(nodes);
57 }
58
59 void SalomeHPContainer::release(const Task *node)
60 {
61   _launchModeType.release(node);
62 }
63
64 SalomeHPContainer::~SalomeHPContainer()
65 {
66 }
67
68 void SalomeHPContainer::lock()
69 {
70   _mutex.lock();
71 }
72
73 void SalomeHPContainer::unLock()
74 {
75   _mutex.unLock();
76 }
77
78 std::string SalomeHPContainer::getKind() const
79 {
80   return KIND;
81 }
82
83 std::string SalomeHPContainer::getDiscreminantStrOfThis(const Task *askingNode) const
84 {
85   YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(this,askingNode));
86   return tmpCont->getDiscreminantStrOfThis(askingNode);
87 }
88
89 bool SalomeHPContainer::isAlreadyStarted(const Task *askingNode) const
90 {
91   const SalomeContainerMonoHelper *helper(_launchModeType.getHelperOfTaskThreadSafe(this,askingNode));
92   return helper->isAlreadyStarted(askingNode);
93 }
94
95 void SalomeHPContainer::start(const Task *askingNode) throw(YACS::Exception)
96 {
97   SalomeContainerMonoHelper *helper(_launchModeType.getHelperOfTaskThreadSafe(this,askingNode));
98   SalomeContainerTools::Start(_componentNames,helper,_sct,_shutdownLevel,this,askingNode);
99 }
100
101 void SalomeHPContainer::shutdown(int level)
102 {
103   if(level < _shutdownLevel)
104       return;
105   _shutdownLevel=999;
106   for(std::size_t i=0;i<_launchModeType.size();i++)
107     {
108       SalomeContainerMonoHelper *helper(_launchModeType.at(i));
109       helper->shutdown();
110     }
111 }
112
113 std::string SalomeHPContainer::getPlacementId(const Task *askingNode) const
114 {
115   const SalomeContainerMonoHelper *helper(0);
116   {
117     YACS::BASES::AutoLocker<Container> alckCont(const_cast<SalomeHPContainer *>(this));
118     helper=_launchModeType.getHelperOfTask(askingNode);
119   }
120   return SalomeContainerTools::GetPlacementId(helper,this,askingNode);
121 }
122
123 std::string SalomeHPContainer::getFullPlacementId(const Task *askingNode) const
124 {
125   const SalomeContainerMonoHelper *helper(0);
126   {
127     YACS::BASES::AutoLocker<Container> alckCont(const_cast<SalomeHPContainer *>(this));
128     helper=_launchModeType.getHelperOfTask(askingNode);
129   }
130   return SalomeContainerTools::GetFullPlacementId(helper,this,askingNode);
131 }
132
133 /*!
134  * It is not a bug here ! clone for homogeneous container is not supposed to be copied !
135  */
136 Container *SalomeHPContainer::clone() const
137 {
138   incrRef();
139   return const_cast<SalomeHPContainer*>(this);
140 }
141
142 Container *SalomeHPContainer::cloneAlways() const
143 {
144   return new SalomeHPContainer(*this);
145 }
146
147 void SalomeHPContainer::setProperty(const std::string& name,const std::string& value)
148 {
149   if(name==AOC_ENTRY)//no sense to set it ! It is always true ! ignore it !
150     return ;
151   else if(name==SIZE_OF_POOL_KEY)
152     {
153       std::istringstream iss(value);
154       int val(0);
155       iss >> val;
156       setSizeOfPool(val);
157     }
158   else if(name==INITIALIZE_SCRIPT_KEY)
159     {
160       _initScript=value;
161     }
162   else
163     _sct.setProperty(name,value);
164 }
165
166 std::string SalomeHPContainer::getProperty(const std::string& name) const
167 {
168   if(name==AOC_ENTRY)
169     {
170       return std::string("1");
171     }
172   else if(name==SIZE_OF_POOL_KEY)
173     {
174       std::ostringstream oss; oss << getSizeOfPool();
175       return oss.str();
176     }
177   else if(name==INITIALIZE_SCRIPT_KEY)
178     {
179       return _initScript;
180     }
181   else
182     return _sct.getProperty(name);
183 }
184
185 void SalomeHPContainer::clearProperties()
186 {
187   _initScript.clear();
188   _sct.clearProperties();
189 }
190
191 void SalomeHPContainer::addComponentName(const std::string& name)
192 {
193   _componentNames.push_back(name);
194 }
195
196 std::map<std::string,std::string> SalomeHPContainer::getProperties() const
197 {
198   std::map<std::string,std::string> ret(_sct.getProperties());
199   std::ostringstream oss; oss << getSizeOfPool();
200   ret[SIZE_OF_POOL_KEY]=oss.str();
201   if(!_initScript.empty())
202     ret[INITIALIZE_SCRIPT_KEY]=_initScript;
203   return ret;
204 }
205
206 std::map<std::string,std::string> SalomeHPContainer::getResourceProperties(const std::string& name) const
207 {
208   return _sct.getResourceProperties(name);
209 }
210
211 void SalomeHPContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const throw(YACS::Exception)
212 {
213   if(inst->getKind()!=SalomeHPComponent::KIND)
214     throw Exception("SalomeHPContainer::checkCapabilityToDealWith : SalomeContainer is not able to deal with this type of ComponentInstance.");
215 }