Salome HOME
updated copyright message
[modules/yacs.git] / src / runtime / CORBAComponent.cxx
1 // Copyright (C) 2006-2023  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 //To trace CORBA ref count, uncomment the following line 
21 //#define REFCNT
22 //
23 #ifdef REFCNT
24 #define private public
25 #include <omniORB4/CORBA.h>
26 #endif
27
28 #include "RuntimeSALOME.hxx"
29 #include "CORBAComponent.hxx"
30 #include "CORBANode.hxx"
31
32 #include <sstream>
33 #include <iostream>
34
35 //#define _DEVDEBUG_
36 #include "YacsTrace.hxx"
37
38 using namespace YACS::ENGINE;
39 using namespace std;
40
41 const char CORBAComponent::KIND[]="CORBA";
42
43 //! CORBAComponent constructor
44 CORBAComponent::CORBAComponent(const std::string& name): ComponentInstance(name)
45 {
46   _objComponent=CORBA::Object::_nil();
47 }
48
49 //! CORBAComponent copy constructor
50 CORBAComponent::CORBAComponent(const CORBAComponent& other):ComponentInstance(other)
51 {
52   _objComponent=CORBA::Object::_nil();
53 }
54
55 CORBAComponent::~CORBAComponent()
56 {
57 #ifdef REFCNT
58   DEBTRACE( "+++++++++++++++++" << getName() << " +++++++++++++++++" );
59   if(_objComponent != CORBA::Object::_nil())
60     {
61       std::cerr << "CORBAComponent::destructor:refcount: " <<_objComponent->_PR_getobj()->pd_refCount << std::endl;
62     }
63 #endif
64 }
65
66 std::string CORBAComponent::getKind() const
67 {
68   return KIND;
69 }
70
71 std::string CORBAComponent::getKindForNode() const
72 {
73   return KIND;
74 }
75
76 //! Unload the component 
77 void CORBAComponent::unload(Task *askingNode)
78 {
79   //Not implemented
80   std::cerr << "CORBAComponent::unload : not implemented " << std::endl;
81 }
82
83 CORBA::Object_ptr CORBAComponent::getCompoPtr()
84 {
85 #ifdef REFCNT
86   std::cerr << "CORBAComponent::getCompoPtr:refCount: " <<_objComponent->_PR_getobj()->pd_refCount << std::endl;
87 #endif
88   return CORBA::Object::_duplicate(_objComponent);
89 }
90
91 //! Is the component instance already loaded ?
92 bool CORBAComponent::isLoaded(Task *askingNode) const
93 {
94   if(CORBA::is_nil(_objComponent))
95     return false;
96   else
97     return true;
98 }
99
100 //! Load the component 
101 void CORBAComponent::load(Task *askingNode)
102 {
103   DEBTRACE( "CORBAComponent::load" );
104   CORBA::ORB_ptr orb;
105   try
106     {
107       DEBTRACE( "+++++++++++++++++" << getCompoName() << " +++++++++++++++++" );
108       orb = getSALOMERuntime()->getOrb();
109       _objComponent= orb->string_to_object(getCompoName().c_str());
110 #ifdef REFCNT
111       std::cerr << "CORBAComponent::load:refCount: " <<_objComponent->_PR_getobj()->pd_refCount << std::endl;
112 #endif
113     }
114   catch(CORBA::COMM_FAILURE& ex) 
115     {
116       cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
117            << "object." << endl;
118       throw Exception("Execution problem");
119     }
120   catch(CORBA::SystemException& ex) 
121     {
122       cerr << "Caught a CORBA::SystemException." ;
123       CORBA::Any tmp;
124       tmp <<= ex;
125       CORBA::TypeCode_var tc = tmp.type();
126       const char *p = tc->name();
127       if ( *p != '\0' ) 
128         cerr <<p;
129       else  
130         cerr  << tc->id();
131       cerr << endl;
132       throw Exception("Execution problem");
133     }
134   catch(CORBA::Exception& ex) 
135     {
136       cerr << "Caught CORBA::Exception. " ;
137       CORBA::Any tmp;
138       tmp <<= ex;
139       CORBA::TypeCode_var tc = tmp.type();
140       const char *p = tc->name();
141       if ( *p != '\0' )
142         cerr <<p;
143       else 
144         cerr  << tc->id();
145       cerr << endl;
146       throw Exception("Execution problem");
147     }
148   catch(omniORB::fatalException& fe) 
149     {
150       cerr << "Caught omniORB::fatalException:" << endl;
151       cerr << "  file: " << fe.file() << endl;
152       cerr << "  line: " << fe.line() << endl;
153       cerr << "  mesg: " << fe.errmsg() << endl;
154       throw Exception("Execution problem");
155     }
156   catch(...) 
157     {
158       cerr << "Caught unknown exception." << endl;
159       throw Exception("Execution problem");
160     }
161   if( CORBA::is_nil(_objComponent) )
162     {
163       cerr << "Can't get reference to object (or it was nil)." << endl;
164       throw Exception("Execution problem");
165     }
166   //TODO: if IOR is valid but the component does not exist, it works (bad)
167 }
168
169 //! Create a ServiceNode with this component instance and no input or output port
170 /*!
171  *   \param name : node name
172  *   \return       a new CORBANode node
173  */
174 ServiceNode* CORBAComponent::createNode(const std::string& name)
175 {
176    CORBANode* node=  new CORBANode(name);
177    node->setComponent(this);
178    return node;
179 }
180
181 //! Clone the component instance 
182 ComponentInstance* CORBAComponent::clone() const
183 {
184   //no real need to clone a CORBA Component : there is no component instance loading
185   incrRef();
186   return (ComponentInstance*)this;
187   //return new CORBAComponent(*this);
188 }
189
190 ComponentInstance* CORBAComponent::cloneAlways() const
191 {
192   return new CORBAComponent(*this);
193 }
194
195 std::string CORBAComponent::getFileRepr() const
196 {
197   ostringstream stream;
198   stream << "<ref>" << getCompoName() << "</ref>";
199   return stream.str();
200 }