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