Salome HOME
omniORB4 porting
[modules/superv.git] / src / Supervision / Value_Impl.cxx
1 //  SUPERV Supervision : contains the implementation of interfaces of SuperVision described in SUPERV.idl
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : Value_Impl.cxx
25 //  Author : Jean Rahuel
26 //  Module : SUPERV
27 //  $Header: 
28
29 using namespace std;
30 #include <stdio.h>
31 #include <fstream>
32 //#include <strstream>
33 //#include <sstream>
34 #include <string>
35
36 //#include "utilities.h"
37
38 #include "Value_Impl.hxx"
39
40 #include "DataFlowExecutor_DataFlow.hxx"
41
42 Value_Impl::Value_Impl( CORBA::ORB_ptr orb ,
43                         PortableServer::POA_ptr poa ,
44                         PortableServer::ObjectId * contId , 
45                         const char *instanceName ,
46                         const char *interfaceName ,
47                         GraphEditor::DataFlow * DataFlowEditor ,
48                         GraphEditor::InNode * DataFlowNode ,
49                         const char *ParameterName ,
50                         const CORBA::Any * anAny ,
51                         const bool activate ) :
52   Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
53   if ( activate ) {
54 //    MESSAGE("Value_Impl::Value_Impl activate object instanceName("
55 //            << instanceName << ") interfaceName(" << interfaceName << ") --> "
56 //            << hex << (void *) this << dec )
57     _thisObj = this ;
58     _id = _poa->activate_object(_thisObj);
59   }
60   _Orb = CORBA::ORB::_duplicate(orb);
61   _Poa = poa ;
62   _ContId = contId ;
63   _DataFlowEditor = DataFlowEditor ;
64   _DataFlowNode = DataFlowNode ;
65   if ( ParameterName != NULL ) {
66     _ParameterName = new char[ strlen( ParameterName ) + 1 ] ;
67     strcpy( _ParameterName , ParameterName ) ;
68     if ( DataFlowNode ) {
69       _NodeName = new char[ strlen( DataFlowNode->Name() ) + 1 ] ;
70       strcpy( _NodeName , DataFlowNode->Name() ) ;
71     }
72     else {
73     _NodeName = NULL ;
74     }
75   }
76   else {
77     _ParameterName = NULL ;
78     _NodeName = NULL ;
79   }
80   _InputValue = false ;
81   _Any = new CORBA::Any( *anAny ) ;
82 }
83
84 Value_Impl::Value_Impl( CORBA::ORB_ptr orb ,
85                         PortableServer::POA_ptr poa ,
86                         PortableServer::ObjectId * contId , 
87                         const char *instanceName ,
88                         const char *interfaceName ,
89                         GraphEditor::DataFlow * DataFlowEditor ,
90                         GraphEditor::InNode * DataFlowNode ,
91                         const char *ParameterName ,
92                         const bool activate ) :
93   Engines_Component_i(orb, poa, contId, instanceName, interfaceName, false, false) {
94   if ( activate ) {
95 //    MESSAGE("Value_Impl::Value_Impl activate object instanceName("
96 //            << instanceName << ") interfaceName(" << interfaceName << ") --> "
97 //            << hex << (void *) this << dec )
98     _thisObj = this ;
99     _id = _poa->activate_object(_thisObj);
100   }
101   _Orb = CORBA::ORB::_duplicate(orb);
102   _Poa = poa ;
103   _ContId = contId ;
104   _DataFlowEditor = DataFlowEditor ;
105   _DataFlowNode = DataFlowNode ;
106   if ( ParameterName != NULL ) {
107     _ParameterName = new char[ strlen( ParameterName ) + 1 ] ;
108     strcpy( _ParameterName , ParameterName ) ;
109     if ( DataFlowNode ) {
110       _NodeName = new char[ strlen( DataFlowNode->Name() ) + 1 ] ;
111       strcpy( _NodeName , DataFlowNode->Name() ) ;
112     }
113     else {
114     _NodeName = NULL ;
115     }
116   }
117   else {
118     _ParameterName = NULL ;
119     _NodeName = NULL ;
120   }
121   _InputValue = false ;
122   _Any = new CORBA::Any() ;
123 }
124
125 Value_Impl::Value_Impl() {
126 }
127
128 Value_Impl::~Value_Impl() {
129   beginService( "Value_Impl::~Value_Impl" );
130   endService( "Value_Impl::~Value_Impl" );
131 }
132
133 void Value_Impl::destroy() {
134   beginService( "Value_Impl::destroy" );
135   _poa->deactivate_object(*_id) ;
136 //  CORBA::release(_poa) ;
137   delete(_id) ;
138 //  _thisObj->_remove_ref();
139   endService( "Value_Impl::destroy" );
140 }
141
142 void Value_Impl::InPort( bool anInput ) {
143   _InputPort = anInput ;
144 }
145
146 bool Value_Impl::Input( const CORBA::Any * anAny ) {
147 //  _DataFlowEditor->AddInputData( _DataFlowNode->Name() ,
148 //                                 _ParameterName ,
149 //                                 *anAny ) ;
150   delete _Any ;
151   _Any = new CORBA::Any( *anAny ) ;
152   _InputValue = true ;
153   return true ;
154 }
155
156 CORBA::Any * Value_Impl::ToAny() {
157   beginService( "Value_Impl::ToAny" );
158   CORBA::Any * anAny ;
159   if ( _DataFlowEditor ) {
160     char * name ;
161     if ( _DataFlowNode ) {
162       name = _DataFlowNode->Name() ;
163     }
164     else {
165       name = _DataFlowEditor->Graph()->Name() ;
166     }
167     MESSAGE( "ToAny " << name<< "( " << _ParameterName << " )" ) ;
168     if ( _InputPort ) {
169       if ( _DataFlowNode ) {
170         _InputValue = _DataFlowNode->HasInput( _ParameterName ) ;
171       }
172       else {
173         _InputValue = _DataFlowEditor->HasInput( _ParameterName ) ;
174       }
175       GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
176       if ( _DataFlowExecutor ) {
177 //JR 30.03.2005        anAny = new CORBA::Any( * _DataFlowExecutor->GetInData( name ,
178         anAny = new CORBA::Any( _DataFlowExecutor->GetInData( name ,
179                                                               _ParameterName ) ) ;
180       }
181       else if ( _InputValue ) {
182 //JR 30.03.2005        anAny = new CORBA::Any( * _DataFlowEditor->GetInData( name ,
183         anAny = new CORBA::Any( _DataFlowEditor->GetInData( name ,
184                                                             _ParameterName ) ) ;
185       }
186       else {
187         anAny = new CORBA::Any( *_Any ) ;
188       }
189     }
190     else {
191       GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
192       if ( _DataFlowExecutor ) {
193 //JR 30.03.2005        anAny = new CORBA::Any( * _DataFlowExecutor->GetOutData( name ,
194         anAny = new CORBA::Any( _DataFlowExecutor->GetOutData( name ,
195                                                               _ParameterName ) ) ;
196       }
197       else {
198         anAny = new CORBA::Any( *_Any ) ;
199       }
200     }
201   }
202   else {
203     anAny = new CORBA::Any( *_Any ) ;
204   }
205   endService( "Value_Impl::ToAny" );
206   return anAny ;
207 }
208
209 char * Value_Impl::ToString() {
210 //  beginService( "Value_Impl::ToString" );
211 //JR 22.03.2005 Memory Leak  CORBA::Any anAny = *ToAny() ;
212   CORBA::Any anAny ;
213   if ( _DataFlowEditor ) {
214     char * name ;
215     if ( _DataFlowNode ) {
216       name = _DataFlowNode->Name() ;
217     }
218     else {
219       name = _DataFlowEditor->Graph()->Name() ;
220     }
221 //    MESSAGE( "ToString " << name<< "( " << _ParameterName << " )" ) ;
222     if ( _InputPort ) {
223       if ( _DataFlowNode ) {
224         _InputValue = _DataFlowNode->HasInput( _ParameterName ) ;
225       }
226       else {
227         _InputValue = _DataFlowEditor->HasInput( _ParameterName ) ;
228       }
229       GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
230       if ( _DataFlowExecutor ) {
231 //        cout << "-->_DataFlowExecutor->GetInData " << name << " " << _ParameterName << endl ;
232 //JR 30.03.2005        const CORBA::Any * AnyPtr = _DataFlowExecutor->GetInData( name ,
233         const CORBA::Any AnyRef = _DataFlowExecutor->GetInData( name ,
234                                                                 _ParameterName ) ;
235 //JR 30.03.2005        cout << "<--_DataFlowExecutor->GetInData " << AnyPtr << endl ;
236 //JR 30.03.2005        anAny = * AnyPtr ;
237         anAny = AnyRef ;
238       }
239       else if ( _InputValue ) {
240 //        cout << "_DataFlowEditor->GetInData _DataFlowNode " << _DataFlowNode
241 //             << " _InputValue " << _InputValue << endl ;
242 //JR 30.03.2005        anAny = * _DataFlowEditor->GetInData( name ,
243         anAny = _DataFlowEditor->GetInData( name ,
244                                             _ParameterName ) ;
245       }
246       else {
247         anAny = *_Any ;
248       }
249     }
250     else {
251       GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
252       if ( _DataFlowExecutor ) {
253 //        cout << "-->_DataFlowExecutor->GetOutData " << name << " " << _ParameterName << endl ;
254 //JR 30.03.2005        const CORBA::Any * AnyPtr = _DataFlowExecutor->GetOutData( name ,
255         const CORBA::Any AnyRef = _DataFlowExecutor->GetOutData( name ,
256                                                                  _ParameterName ) ;
257 //JR 30.03.2005        cout << "<--_DataFlowExecutor->GetOutData " << AnyPtr << endl ;
258 //JR 30.03.2005        anAny = * AnyPtr ;
259         anAny = AnyRef ;
260       }
261       else {
262         anAny = *_Any ;
263       }
264     }
265   }
266   ostringstream astr ;
267   const char * retstr ;
268   int startstr = 0 ;
269   switch (anAny.type()->kind()) {
270     case CORBA::tk_string: {
271       anAny >>= retstr;
272 //      MESSAGE( "ToString( string ) '" << retstr << "'" );
273       break ;
274     }
275     case CORBA::tk_long: {
276       long l;
277       anAny >>= l;
278       astr << l << ends ;
279       retstr = astr.str().c_str() ;
280 //      MESSAGE( "ToString( long ) '" << l << " " << retstr << "'" );
281       break ;
282     }
283     case CORBA::tk_double: {
284       double d;
285       anAny >>= d;
286       astr << setw(25) << setprecision(18) << d << ends ;
287       retstr = astr.str().c_str() ;
288       int i = 0 ;
289       while ( i < (int ) strlen( retstr ) && retstr[ i++ ] == ' ' ) {
290         startstr = i ;
291       }
292 //      MESSAGE( "ToString( double ) '" << d << "' '" << retstr << "' '" << &retstr[ startstr ] << "'");
293       break ;
294     }
295     case CORBA::tk_objref: {
296       CORBA::Object_ptr obj ;
297       try {
298 #if OMNIORB_VERSION >= 4
299         anAny >>= (CORBA::Any::to_object ) obj ;
300 #else
301         anAny >>= obj ;
302 #endif
303         retstr = _Orb->object_to_string( obj );
304 //        MESSAGE( "ToString( object ) '" << retstr << "'" );
305       }
306       catch ( ... ) {
307         retstr = "object_to_string catched " ;
308       }
309       break ;
310     }
311     default: {
312       retstr = "Unknown CORBA::Any Type" ;
313 //      MESSAGE( retstr );
314       break ;
315     }
316   }
317 //  endService( "Value_Impl::ToString" );
318   return CORBA::string_dup( &retstr[ startstr ] ) ;
319 }
320
321 bool Value_Impl::IsIOR() {
322   CORBA::Any anAny = *ToAny() ;
323   if ( _DataFlowEditor ) {
324     char * name ;
325     if ( _DataFlowNode ) {
326       name = _DataFlowNode->Name() ;
327     }
328     else {
329       name = _DataFlowEditor->Graph()->Name() ;
330     }
331     if ( _InputPort ) {
332       if ( _DataFlowNode ) {
333         _InputValue = _DataFlowNode->HasInput( _ParameterName ) ;
334       }
335       else {
336         _InputValue = _DataFlowEditor->HasInput( _ParameterName ) ;
337       }
338       if ( _InputValue && _DataFlowEditor->IsEditing() ) {
339 //JR 30.03.2005        anAny = * _DataFlowEditor->GetInData( name ,
340         anAny = _DataFlowEditor->GetInData( name ,
341                                             _ParameterName ) ;
342       }
343       else {
344         GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
345         if ( _DataFlowExecutor ) {
346 //JR 30.03.2005          anAny = * _DataFlowExecutor->GetInData( name ,
347           anAny = _DataFlowExecutor->GetInData( name ,
348                                                 _ParameterName ) ;
349         }
350       }
351     }
352     else {
353       if ( _InputValue && _DataFlowEditor->IsEditing() ) {
354 //JR 30.03.2005        anAny = * _DataFlowEditor->GetOutData( name ,
355         anAny = _DataFlowEditor->GetOutData( name ,
356                                              _ParameterName ) ;
357       }
358       else {
359         GraphExecutor::DataFlow * _DataFlowExecutor = _DataFlowEditor->Executor() ;
360         if ( _DataFlowExecutor ) {
361 //JR 30.03.2005          anAny = * _DataFlowExecutor->GetOutData( name ,
362           anAny = _DataFlowExecutor->GetOutData( name ,
363                                                  _ParameterName ) ;
364         }
365       }
366     }
367   }
368   return(anAny.type()->kind() == CORBA::tk_objref);
369 }
370
371 char* Value_Impl::ComponentDataType() {
372   GraphExecutor::DataFlow* _DataFlowExecutor = _DataFlowEditor->Executor();
373   const GraphBase::ComputingNode* node = _DataFlowExecutor->Graph()->GetGraphNode(_DataFlowNode->Name());
374   if ( node->IsFactoryNode() ) {
375     Engines::Component_var compo = ((GraphBase::FactoryNode * ) node )->Component();
376     if ( CORBA::is_nil( compo ) ) {
377       return ( strdup( "UnknownComponent" ) ) ;
378     }
379     else {
380       return ( strdup( compo->instanceName() ) ) ;
381     }
382   }
383   return ( strdup ( "UnknownComponent" ) ) ;
384 }