Salome HOME
First test qui HPsalome container is running.
[modules/yacs.git] / src / runtime / SalomeComponent.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 "RuntimeSALOME.hxx"
21 #include "SalomeComponent.hxx"
22 #include "SalomeContainer.hxx"
23 #include "SalomeHPContainer.hxx"
24 #include "CORBANode.hxx"
25 #include "AutoLocker.hxx"
26
27 #ifdef SALOME_KERNEL
28 #include "SALOME_NamingService.hxx"
29 #include "SALOME_LifeCycleCORBA.hxx"
30 #endif
31
32 #include <omniORB4/CORBA.h>
33 #include <iostream>
34 #include <sstream>
35
36 //#define _DEVDEBUG_
37 #include "YacsTrace.hxx"
38
39 using namespace YACS::ENGINE;
40 using namespace std;
41
42 const char SalomeComponent::KIND[]="Salome";
43
44 //! SalomeComponent constructor
45 SalomeComponent::SalomeComponent(const std::string& name): ComponentInstance(name)
46 {
47   _objComponent=CORBA::Object::_nil();
48 }
49
50 //! SalomeComponent copy constructor
51 SalomeComponent::SalomeComponent(const SalomeComponent& other):ComponentInstance(other)
52 {
53   _objComponent=CORBA::Object::_nil();
54 }
55
56 SalomeComponent::~SalomeComponent()
57 {
58 }
59
60 std::string SalomeComponent::getKind() const
61 {
62   return KIND;
63 }
64
65 //! Unload the component 
66 void SalomeComponent::unload(Task *askingNode)
67 {
68   //Not implemented
69   std::cerr << "SalomeComponent::unload : not implemented " << std::endl;
70 }
71
72 //! Is the component instance already loaded ?
73 bool SalomeComponent::isLoaded(Task *askingNode) const
74 {
75   if(CORBA::is_nil(_objComponent))
76     return false;
77   else
78     return true;
79 }
80
81 //#ifdef SALOME_KERNEL
82 //! Load the component 
83 void SalomeComponent::load(Task *askingNode)
84 {
85   if(_container)
86     {
87       SalomeContainer *salomeContainer(dynamic_cast<SalomeContainer *>(_container));
88       if(salomeContainer)
89         {
90           _objComponent=salomeContainer->loadComponent(askingNode);
91           return ;
92         }
93       SalomeHPContainer *salomeContainer2(dynamic_cast<SalomeHPContainer *>(_container));
94       if(salomeContainer2)
95         {
96           SalomeContainerHelper *lmt(0);
97           {
98             YACS::BASES::AutoLocker<Container> altck(salomeContainer2);
99             lmt=salomeContainer2->getHelperOfTask(askingNode);
100           }
101           SalomeContainer tmpCont(*salomeContainer2,salomeContainer2->getContainerInfo(),lmt,
102                                   salomeContainer2->getComponentNames(),salomeContainer2->getShutdownLev());
103           _objComponent=tmpCont.loadComponent(askingNode);
104           return ;
105         }
106       throw Exception("Unrecognized type of Container ! Only Salome and HPSalome container are supported by the Salome components !");
107     }
108   //throw Exception("SalomeComponent::load : no container specified !!! To be implemented in executor to allocate default a Container in case of presenceOfDefaultContainer.");
109   //This component has no specified container : use default container policy
110   SALOME_NamingService ns(getSALOMERuntime()->getOrb());
111   SALOME_LifeCycleCORBA LCC(&ns);
112   Engines::ContainerParameters params;
113   LCC.preSet(params);
114   params.resource_params.name = "localhost";
115   params.container_name ="FactoryServer";
116   _objComponent=LCC.LoadComponent(params,_compoName.c_str());
117 }
118 /*#else
119 void SalomeComponent::load(Task *askingNode)
120 {
121   throw Exception("YACS has been built without SALOME support");
122 }
123 #endif*/
124
125 //! Create a ServiceNode with this component instance and no input or output port
126 /*!
127  *   \param name : node name
128  *   \return       a new SalomeNode node
129  */
130 ServiceNode* SalomeComponent::createNode(const std::string& name)
131 {
132    SalomeNode* node=new SalomeNode(name);
133    node->setComponent(this);
134    return node;
135 }
136
137 //! Clone the component instance
138 ComponentInstance* SalomeComponent::clone() const
139 {
140   if(_isAttachedOnCloning)
141     {
142       incrRef();
143       return (ComponentInstance*) (this);
144     }
145   else
146     return new SalomeComponent(*this);
147 }
148
149 std::string SalomeComponent::getFileRepr() const
150 {
151   ostringstream stream;
152   stream << "<component>" << getCompoName() << "</component>";
153   return stream.str();
154 }
155
156 bool SalomeComponent::setContainer(Container *cont)
157 {
158   if(ComponentInstance::setContainer(cont))
159     {
160       if(_container)
161         _container->addComponentName(_compoName);
162       return true;
163     }
164   else
165     return false;
166 }
167
168 void SalomeComponent::shutdown(int level)
169 {
170   DEBTRACE("SalomeComponent::shutdown " << level);
171   if(_container)
172     _container->shutdown(level);
173 }