Salome HOME
Update copyrights 2014.
[modules/yacs.git] / src / runtime / CORBAComponent.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 //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 //! Unload the component 
72 void CORBAComponent::unload()
73 {
74   //Not implemented
75   std::cerr << "CORBAComponent::unload : not implemented " << std::endl;
76 }
77
78 CORBA::Object_ptr CORBAComponent::getCompoPtr()
79 {
80 #ifdef REFCNT
81   std::cerr << "CORBAComponent::getCompoPtr:refCount: " <<_objComponent->_PR_getobj()->pd_refCount << std::endl;
82 #endif
83   return CORBA::Object::_duplicate(_objComponent);
84 }
85
86 //! Is the component instance already loaded ?
87 bool CORBAComponent::isLoaded()
88 {
89   if(CORBA::is_nil(_objComponent))
90     return false;
91   else
92     return true;
93 }
94
95 //! Load the component 
96 void CORBAComponent::load()
97 {
98   DEBTRACE( "CORBAComponent::load" );
99   CORBA::ORB_ptr orb;
100   try
101     {
102       DEBTRACE( "+++++++++++++++++" << getCompoName() << " +++++++++++++++++" );
103       orb = getSALOMERuntime()->getOrb();
104       _objComponent= orb->string_to_object(getCompoName().c_str());
105 #ifdef REFCNT
106       std::cerr << "CORBAComponent::load:refCount: " <<_objComponent->_PR_getobj()->pd_refCount << std::endl;
107 #endif
108     }
109   catch(CORBA::COMM_FAILURE& ex) 
110     {
111       cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
112            << "object." << endl;
113       throw Exception("Execution problem");
114     }
115   catch(CORBA::SystemException& ex) 
116     {
117       cerr << "Caught a CORBA::SystemException." ;
118       CORBA::Any tmp;
119       tmp <<= ex;
120       CORBA::TypeCode_var tc = tmp.type();
121       const char *p = tc->name();
122       if ( *p != '\0' ) 
123         cerr <<p;
124       else  
125         cerr  << tc->id();
126       cerr << endl;
127       throw Exception("Execution problem");
128     }
129   catch(CORBA::Exception& ex) 
130     {
131       cerr << "Caught CORBA::Exception. " ;
132       CORBA::Any tmp;
133       tmp <<= ex;
134       CORBA::TypeCode_var tc = tmp.type();
135       const char *p = tc->name();
136       if ( *p != '\0' )
137         cerr <<p;
138       else 
139         cerr  << tc->id();
140       cerr << endl;
141       throw Exception("Execution problem");
142     }
143   catch(omniORB::fatalException& fe) 
144     {
145       cerr << "Caught omniORB::fatalException:" << endl;
146       cerr << "  file: " << fe.file() << endl;
147       cerr << "  line: " << fe.line() << endl;
148       cerr << "  mesg: " << fe.errmsg() << endl;
149       throw Exception("Execution problem");
150     }
151   catch(...) 
152     {
153       cerr << "Caught unknown exception." << endl;
154       throw Exception("Execution problem");
155     }
156   if( CORBA::is_nil(_objComponent) )
157     {
158       cerr << "Can't get reference to object (or it was nil)." << endl;
159       throw Exception("Execution problem");
160     }
161   //TODO: if IOR is valid but the component does not exist, it works (bad)
162 }
163
164 //! Create a ServiceNode with this component instance and no input or output port
165 /*!
166  *   \param name : node name
167  *   \return       a new CORBANode node
168  */
169 ServiceNode* CORBAComponent::createNode(const std::string& name)
170 {
171    CORBANode* node=  new CORBANode(name);
172    node->setComponent(this);
173    return node;
174 }
175
176 //! Clone the component instance 
177 ComponentInstance* CORBAComponent::clone() const
178 {
179   //no real need to clone a CORBA Component : there is no component instance loading
180   incrRef();
181   return (ComponentInstance*)this;
182   //return new CORBAComponent(*this);
183 }
184
185 std::string CORBAComponent::getFileRepr() const
186 {
187   ostringstream stream;
188   stream << "<ref>" << getCompoName() << "</ref>";
189   return stream.str();
190 }