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