Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/superv.git] / src / GraphBase / DataFlowBase_DataPort.cxx
1 //  SUPERV GraphBase : contains fondamental classes for Services, Input Ports, Output Ports Links and Nodes.
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   : DataFlowBase_DataPort.cxx
25 //  Author : Jean Rahuel, CEA
26 //  Module : SUPERV
27 //  $Header:
28
29 using namespace std;
30 #include <stdio.h>
31 #include "DataFlowBase_DataPort.hxx"
32
33 GraphBase::DataPort::DataPort() :
34               Port() {
35   pthread_mutex_init( &_MutexWait , NULL ) ;
36   _PortState = SUPERV::UndefinedState ;
37   _Done = false ;
38   InitialValues( CORBA::Any() ) ;
39 }
40
41 GraphBase::DataPort::DataPort( const char *const * NodeName  ,
42                                const SALOME_ModuleCatalog::ServicesParameter aserviceParameter ,
43                                const SUPERV::KindOfPort aKind ,
44                                const SALOME_ModuleCatalog::DataStreamDependency aDependency ) :
45            Port( NodeName , aserviceParameter , aKind , aDependency ) {
46   pthread_mutex_init( &_MutexWait , NULL ) ;
47   _PortState = SUPERV::UndefinedState ;
48   _Done = false ;
49   InitialValues( CORBA::Any() ) ;
50 }
51
52 GraphBase::DataPort::~DataPort() {
53 }
54
55 void GraphBase::DataPort::InitialValues(CORBA::Any aValue ) {
56   _theValue = new CORBA::Any( aValue ) ;
57 //JR 24.02.2005 Memory Leak  string _Type = CORBA::string_dup(GetServicesParameter().Parametertype) ;
58   string _Type = string( GetServicesParameter().Parametertype ) ;
59   const char * Type = _Type.c_str();
60   CORBA::Any InitialValue ;
61   cdebug << "InitialValues " << NodeName() << " " << PortName() << " " << PortType()
62          << " : " ;
63   if ( !strcmp( Type , "" ) ) {
64     cdebug << "void" << endl ;
65     InitialValue <<= (void *) NULL ;
66   }
67   else if ( !strcmp( Type , "string" ) ) {
68     cdebug << "string" << endl ;
69 //    InitialValue <<= (char *) NULL ;
70     InitialValue <<= "" ;
71   }
72   else if ( !strcmp( Type , "boolean" ) ) {
73     cdebug << "boolean" << endl ;
74     InitialValue <<= (long ) 0 ;
75   }
76   else if ( !strcmp( Type , "char" ) ) {
77     cdebug << "char" << endl ;
78     InitialValue <<= (long ) 0 ;
79   }
80   else if ( !strcmp( Type , "short" ) ) {
81     cdebug << "short" << endl ;
82     InitialValue <<= (long ) 0 ;
83   }
84   else if ( !strcmp( Type , "int" ) ) {
85     cdebug << "long" << endl ;
86     InitialValue <<= (long ) 0 ;
87   }
88   else if ( !strcmp( Type , "long" ) ) {
89     cdebug << "long" << endl ;
90     InitialValue <<= (long ) 0 ;
91   }
92   else if ( !strcmp( Type , "float" ) ) {
93     cdebug << "float" << endl ;
94     double d = 0.;
95     InitialValue.replace(CORBA::TypeCode::PR_double_tc(), (void*)(&d));
96     //InitialValue <<= (double ) 0. ;
97   }
98   else if ( !strcmp( Type , "double" ) ) {
99     cdebug << "double" << endl ;
100     double d = 0.;
101     InitialValue.replace(CORBA::TypeCode::PR_double_tc(), (void*)(&d));
102     //InitialValue <<= (double ) 0. ;
103   }
104   else { // Default
105     cdebug << "objref" << endl ;
106 //    InitialValue.replace(CORBA::_tc_Object, NULL);
107     InitialValue <<= CORBA::Object::_nil() ;
108   }
109 //  else {
110 //    cdebug << "InitialValues ERROR (other) " << Type << endl ;
111 //    InitialValue <<= (long ) 0 ;
112 //  }
113   _InitialValue = new CORBA::Any( InitialValue ) ;
114   _Value = &_InitialValue ;
115 }
116
117 void GraphBase::DataPort::SetValue( const CORBA::Any & aDataValue ) {
118   CORBA::Any * aValue = new CORBA::Any( aDataValue ) ;
119   SetValue( aValue ) ;
120 }
121
122 #define ValueTrace 0
123 void GraphBase::DataPort::SetValue( const CORBA::Any * aDataValue ) {
124 //  cdebug << pthread_self() << " SetValue(aDataValue) --> pthread_mutex_lock " << &_MutexWait
125 //         << endl ;
126   if ( pthread_mutex_lock( &_MutexWait ) ) {
127     perror( "lock DataValue" ) ;
128     exit( 0 ) ;
129   }
130 //  cdebug << pthread_self() << " SetValue(aDataValue) pthread_mutex_locked " << &_MutexWait
131 //         << endl ;
132
133   delete _theValue ;
134
135 //JR 21.02.2005 Debug Memory leak :  string _Type = CORBA::string_dup( GetServicesParameter().Parametertype ) ;
136 //  const char * Type = _Type.c_str() ;
137   const char * Type = GetServicesParameter().Parametertype ;
138   _Value = &_theValue ;
139 #if ValueTrace
140   cdebug << "NewValue " << NodeName() << " " << PortName() << " " << PortType()
141          << " : " << aDataValue << " kind " << aDataValue->type()->kind() << " " ;
142 #endif
143   switch (aDataValue->type()->kind()) { // Input Value
144   case CORBA::tk_string: { // Input string Value
145     char * t;
146     *aDataValue >>= t;
147 #if ValueTrace
148     cdebug << t << " (string)" ;
149 #endif
150     if ( !strcmp( Type , "string" ) ) { // SuperVision Value
151       _theValue = aDataValue ;
152       *_theValue >>= t;
153 #if ValueTrace
154       cdebug << " == Value( " << t << ") (string)";
155 #endif
156     }
157     else {
158       CORBA::Any * theValue = new CORBA::Any() ;
159       if ( !strcmp( Type , "boolean" ) || !strcmp( Type , "char" ) ||
160            !strcmp( Type , "short" ) || !strcmp( Type , "int" ) || !strcmp( Type , "long" ) ) {
161         long l ;
162         sscanf( t , "%ld" , &l ) ;
163         *theValue <<= l ;
164         *theValue >>= l;
165 #if ValueTrace
166         cdebug << " --> Value( " << l << ") (long) kind " << theValue->type()->kind() ;
167 #endif
168       }
169       else if ( !strcmp( Type , "float" ) || !strcmp( Type , "double" ) ) {
170         double d ;
171         sscanf( t , "%lf" , &d ) ;
172         theValue->replace(CORBA::TypeCode::PR_double_tc(), (void*)(&d));
173         //*theValue <<= d ;
174         *theValue >>= d ;
175 #if ValueTrace
176         cdebug << " --> Value( " << d << ") (double) kind " << theValue->type()->kind() ;
177 #endif
178       }
179       else { // Default
180         CORBA::Object_ptr ObjRef ;
181         try {
182           ObjRef = StringToObject( t ) ;
183           *theValue <<= ObjRef ;
184         }
185         catch( ... ) {
186           *theValue <<= CORBA::Object::_nil() ;
187         }
188 #if OMNIORB_VERSION >= 4
189         *theValue >>= (CORBA::Any::to_object ) ObjRef ;
190 #else
191         *theValue >>= ObjRef ;
192 #endif
193 #if ValueTrace
194         cdebug << " --> Value( " << ObjectToString( ObjRef ) << ") (object reference) kind "
195                << theValue->type()->kind() ;
196 #endif
197       }
198       //_theValue = theValue ;
199       _theValue = new CORBA::Any( *theValue ) ;
200
201       //*_Value = theValue ;
202       _Value = &_theValue ;
203
204 //JR 21.02.2005 Debug Memory leak : 
205       delete aDataValue ;
206     }
207     break;
208   }
209   case CORBA::tk_long: { // Input long Value
210     long l;
211     *aDataValue >>= l;
212 #if ValueTrace
213     cdebug << "Value( " << l << ") (long)";
214 #endif
215     if ( !strcmp( Type , "boolean" ) || !strcmp( Type , "char" ) ||
216          !strcmp( Type , "short" ) || !strcmp( Type , "int" ) || !strcmp( Type , "long" ) ) { // SuperVision Value
217       _theValue = aDataValue ;
218       *_Value = aDataValue ;
219       *_theValue >>= l;
220 #if ValueTrace
221       cdebug << " == Value( " << l << ") (long)";
222 #endif
223     }
224     else {
225       CORBA::Any * theValue = new CORBA::Any() ;
226       if ( !strcmp( Type , "string" ) ) {
227         char t[40] ;
228         sprintf( t , "%ld" , l ) ;
229         *theValue <<=  t ;
230         char *tt ;
231         *theValue >>= tt ;
232 #if ValueTrace
233         cdebug << " --> Value( " << t << ") (string) kind " << theValue->type()->kind() ;
234 #endif
235       }
236       else if ( !strcmp( Type , "float" ) || !strcmp( Type , "double" ) ) {
237         double d = l ;
238         theValue->replace(CORBA::TypeCode::PR_double_tc(), (void*)(&d));
239         //*theValue <<= d ;
240         *theValue >>= d ;
241
242 #if ValueTrace
243         cdebug << " --> Value( " << d << ") (double) kind " << theValue->type()->kind() ;
244 #endif
245       }
246       else { // Default
247         CORBA::Object_ptr ObjRef ;
248         *theValue <<= CORBA::Object::_nil() ;
249 #if OMNIORB_VERSION >= 4
250         *theValue >>= (CORBA::Any::to_object ) ObjRef ;
251 #else
252         *theValue >>= ObjRef ;
253 #endif
254 #if ValueTrace
255         cdebug << " --> Value( " << ObjectToString( ObjRef ) << ") (object reference) kind "
256                << theValue->type()->kind() ;
257 #endif
258       }
259
260       //_theValue = theValue ;
261       _theValue = new CORBA::Any( *theValue ) ;
262
263       //*_Value = theValue ;
264       _Value = &_theValue ;
265
266 //JR 21.02.2005 Debug Memory leak : 
267       delete aDataValue ;
268     }
269     break;
270   }
271   case CORBA::tk_double: { // Input double Value
272     double d;
273     *aDataValue >>= d;
274 #if ValueTrace
275     cdebug << "Value( " << d << ") (double)";
276 #endif
277     if ( !strcmp( Type , "float" ) || !strcmp( Type , "double" ) ) { // SuperVision Value
278       //_theValue = aDataValue ;
279       _theValue = new CORBA::Any( *aDataValue ) ;
280
281       //*_Value = aDataValue ;
282       _Value = &_theValue ;
283
284       *_theValue >>= d;
285 #if ValueTrace
286       cdebug << " == Value( " << d << ") (double)";
287 #endif
288     }
289     else {
290       CORBA::Any * theValue = new CORBA::Any() ;
291       if ( !strcmp( Type , "string" ) ) {
292         char t[40] ;
293         sprintf( t , "%lf" , d ) ;
294         *theValue <<=  t ;
295         char *tt ;
296         *theValue >>= tt ;
297 #if ValueTrace
298         cdebug << " --> Value( " << t << ") (string) kind " << theValue->type()->kind() ;
299 #endif
300       }
301       else if ( !strcmp( Type , "boolean" ) || !strcmp( Type , "char" ) ||
302                 !strcmp( Type , "short" ) || !strcmp( Type , "int" ) || !strcmp( Type , "long" ) ) {
303         long l = (long ) d ;
304         *theValue <<= l ;
305         *theValue >>= l;
306 #if ValueTrace
307         cdebug << " --> Value( " << l << ") (long) kind " << theValue->type()->kind() ;
308 #endif
309       }
310       else { // Default
311         CORBA::Object_ptr ObjRef ;
312         *theValue <<= CORBA::Object::_nil() ;
313 #if OMNIORB_VERSION >= 4
314         *theValue >>= (CORBA::Any::to_object ) ObjRef ;
315 #else
316         *theValue >>= ObjRef ;
317 #endif
318 #if ValueTrace
319         cdebug << " --> Value( " << ObjectToString( ObjRef ) << ") (object reference) kind "
320                << theValue->type()->kind() ;
321 #endif
322       }
323       //_theValue = theValue ;
324       _theValue = new CORBA::Any( *theValue ) ;
325
326       //*_Value = theValue ;
327       _Value = &_theValue ;
328
329 //JR 21.02.2005 Debug Memory leak : 
330       delete aDataValue ;
331     }
332     break;
333   }
334   case CORBA::tk_objref: { // Input objref Value
335     CORBA::Object_ptr obj ;
336 #if OMNIORB_VERSION >= 4
337     *aDataValue >>= (CORBA::Any::to_object ) obj;
338 #else
339     *aDataValue >>= obj;
340 #endif
341 #if ValueTrace
342     cdebug << "Value( " << ObjectToString( obj ) << ") (object reference)";
343 #endif
344     if ( strcmp( Type , "string" ) &&
345          strcmp( Type , "boolean" ) && strcmp( Type , "char" ) &&
346          strcmp( Type , "short" ) && strcmp( Type , "long" ) &&
347          strcmp( Type , "double" ) ) { // SuperVision Default Value
348       _theValue = aDataValue ;
349       *_Value = aDataValue ;
350 #if OMNIORB_VERSION >= 4
351       *_theValue >>= (CORBA::Any::to_object ) obj ;
352 #else
353       *_theValue >>= obj;
354 #endif
355 #if ValueTrace
356       cdebug << " == Value( " << ObjectToString( obj ) << ") (object reference)";
357 #endif
358     }
359     else {
360       CORBA::Any * theValue = new CORBA::Any() ;
361       if ( !strcmp( Type , "string" ) ) {
362         *theValue <<=  ObjectToString( obj ) ;
363 #if OMNIORB_VERSION >= 4
364         *theValue >>= (CORBA::Any::to_object ) obj ;
365 #else
366         *theValue >>= obj ;
367 #endif
368 #if ValueTrace
369         cdebug << " --> Value( " << ObjectToString( obj ) << ") (string) kind " << theValue->type()->kind() ;
370 #endif
371       }
372       else if ( !strcmp( Type , "boolean" ) || !strcmp( Type , "char" ) ||
373                 !strcmp( Type , "short" ) || !strcmp( Type , "int" ) || !strcmp( Type , "long" ) ) {
374         long l = (long ) obj ;
375         *theValue <<= l ;
376         *theValue >>= l;
377 #if ValueTrace
378         cdebug << " --> Value( " << l << ") (long) kind " << theValue->type()->kind() ;
379 #endif
380       }
381       else if ( !strcmp( Type , "float" ) || !strcmp( Type , "double" ) ) {
382         double d = (double ) 0. ;
383         theValue->replace(CORBA::TypeCode::PR_double_tc(), (void*)(&d));
384         //*theValue <<= d ;
385         *theValue >>= d;
386 #if ValueTrace
387         cdebug << " --> Value( " << d << ") (double) kind " << theValue->type()->kind() ;
388 #endif
389       }
390       //_theValue = theValue ;
391       _theValue = new CORBA::Any( *theValue ) ;
392
393       //*_Value = theValue ;
394       _Value = &_theValue ;
395
396 //JR 21.02.2005 Debug Memory leak : 
397       delete aDataValue ;
398     }
399     break;
400   }
401   default: {
402     cdebug << "Value" << " (other(tk_string,tk_double,tk_long,tk_objref)) ERROR kind "
403            << aDataValue->type()->kind() ;
404     break;
405   }
406   }
407 #if ValueTrace
408   cdebug << endl ;
409 #endif
410   if ( pthread_mutex_unlock( &_MutexWait ) ) {
411     perror( "unlock DataValue" ) ;
412     exit( 0 ) ;
413   }
414 //  cdebug << pthread_self() << " SetValue(aDataValue) --> pthread_mutex_unlocked " << &_MutexWait
415 //         << endl ;
416
417 }
418
419 // PAL8506
420 //JR 30.03.2005 Memory Leak + Debug(crash) CORBA::Any const * GraphBase::DataPort::Value() const {
421 const CORBA::Any GraphBase::DataPort::Value() const {
422   pthread_mutex_t * aMutexWait = (pthread_mutex_t *) &_MutexWait ;
423 //  cdebug << pthread_self() << " Value() --> pthread_mutex_lock " << aMutexWait << endl ;
424   if ( pthread_mutex_lock( aMutexWait ) ) {
425     perror( "lock DataValue" ) ;
426     exit( 0 ) ;
427   }
428 //  cdebug << pthread_self() << " Value() --> pthread_mutex_locked " << aMutexWait << endl ;
429   const CORBA::Any *const Value = *_Value ;
430 //JR 30.03.2005  CORBA::Any * theValue ;
431   CORBA::Any theValue ;
432 //  cdebug  << pthread_self() << " Value " << NodeName() << " " << PortName() << " "
433 //          << PortType() << " _Value " << _Value << " *_Value " << *_Value  << " Value->type "
434 //          << Value->type() ;
435   if ( Value->type() ) {
436 //    cdebug << " kind " << Value->type()->kind() << " :" << endl ;
437   }
438   else {
439 //    cdebug << " Null" << endl ;
440   }
441   if ( PortDone() ) {
442 //JR 21.02.2005 Debug Memory leak :     theValue = new CORBA::Any( *Value ) ;
443 //JR 30.03.2005     theValue = (CORBA::Any * ) Value ;
444     theValue = *Value ;
445 //JR 30.03.2005    switch ( theValue->type()->kind() ) {
446     switch ( theValue.type()->kind() ) {
447     case CORBA::tk_string: {
448       char * t;
449 //JR 30.03.2005      *theValue >>= t;
450       theValue >>= t;
451 //      cdebug << "GraphBase::DataPort::Value() : " << t << " (string) " << endl ;
452       break;
453     }
454     case CORBA::tk_double: {
455       double d;
456 //JR 30.03.2005      *theValue >>= d;
457       theValue >>= d;
458 //      cdebug << "GraphBase::DataPort::Value() : " << d << " (double) " << endl ;
459       break;
460     }
461     case CORBA::tk_long: {
462       long l;
463 //JR 30.03.2005      *theValue >>= l;
464       theValue >>= l;
465 //      cdebug << "GraphBase::DataPort::Value() : " << l << " (long) " << endl ;
466       break;
467     }
468     case CORBA::tk_objref: {
469       CORBA::Object_ptr obj ;
470       char * retstr ;
471       try {
472 //JR 30.03.2005        *theValue >>= obj ;
473 #if OMNIORB_VERSION >= 4
474         theValue >>= (CORBA::Any::to_object ) obj ;
475 #else
476         theValue >>= obj ;
477 #endif
478         retstr = ObjectToString( obj );
479 //        cdebug << "GraphBase::DataPort::Value() : " << retstr
480 //               << "(object reference) " << endl;
481       }
482       catch( ... ) {
483         cdebug << "ToString( object ) Catched ERROR" << endl ;
484       }
485       break;
486     }
487     default: {
488       cdebug << "GraphBase::DataPort::Value() : " << NodeName() << "( " << PortName() << " ) " << PortType()
489              << " (other(tk_string,tk_double,tk_long,tk_objref)) ERROR" << endl ;
490       break;
491     }
492     }
493   }
494   else {
495 //    cdebug << "GraphBase::DataPort::InitialValue() " << endl ;
496 //JR 21.02.2005 Debug Memory leak :    theValue = new CORBA::Any( *Value ) ;
497 //JR 30.03.2005    theValue = (CORBA::Any * ) Value ;
498     theValue = *Value ;
499   }
500   if ( pthread_mutex_unlock( aMutexWait ) ) {
501     perror( "unlock DataValue" ) ;
502     exit( 0 ) ;
503   }
504 //  cdebug << pthread_self() << " Value() --> pthread_mutex_unlocked " << aMutexWait << endl ;
505     
506   return theValue ;
507 }
508
509 bool GraphBase::DataPort::BoolValue() const {
510   bool RetVal = false ;
511   pthread_mutex_t * aMutexWait = (pthread_mutex_t *) &_MutexWait ;
512 //  cdebug << pthread_self() << " BoolValue() --> pthread_mutex_lock " << aMutexWait << endl ;
513   if ( pthread_mutex_lock( aMutexWait ) ) {
514     perror( "lock DataValue" ) ;
515     exit( 0 ) ;
516   }
517 //  cdebug << pthread_self() << " BoolValue() --> pthread_mutex_locked " << aMutexWait
518 //         << endl ;
519   if ( (*_Value)->type()->kind() == CORBA::tk_long ) {
520     long val ;
521     **_Value >>= val ;
522     if ( val ) {
523       RetVal = true ;
524     }
525   }
526   else if ( (*_Value)->type()->kind() == CORBA::tk_boolean ) {
527     bool val ;
528     **_Value >>= val ;
529     if ( val ) {
530       RetVal = true ;
531     }
532   }
533   if ( pthread_mutex_unlock( aMutexWait ) ) {
534     perror( "unlock DataValue" ) ;
535     exit( 0 ) ;
536   }
537 //  cdebug << pthread_self() << " " << NodeName() << "( " << PortName() << " ) BoolValue " << RetVal << endl ;
538   return RetVal ;
539 }
540
541 void GraphBase::DataPort::StringValue(ostream & f ) const {
542   pthread_mutex_t * aMutexWait = (pthread_mutex_t *) &_MutexWait ;
543 //  cdebug_in << pthread_self() << " StringValue() --> pthread_mutex_lock " << aMutexWait
544 //            << endl ;
545   if ( pthread_mutex_lock( aMutexWait ) ) {
546     perror( "lock DataValue" ) ;
547     exit( 0 ) ;
548   }
549 //  cdebug << pthread_self() << " StringValue() --> pthread_mutex_locked " << aMutexWait
550 //         << endl ;
551   if ( PortDone() ) {
552 //    cdebug << "StringValue " << NodeName() << " " << PortName() << " " << PortType()
553 //           << " _Value "  << _Value << " *_Value "
554 //           << *_Value  << " " << endl ;
555     const CORBA::Any * theValue = *_Value ;
556     switch (theValue->type()->kind()) {
557     case CORBA::tk_string:
558       char * t;
559       *theValue >>= t;
560       f << t << " (string)" ;
561       break;
562     case CORBA::tk_double:
563       double d;
564       *theValue >>= d;
565       f << d << " (double)" ;
566       break;
567     case CORBA::tk_long:
568       long l;
569       *theValue >>= l;
570       f << l << " (long)" ;
571       break;
572     case CORBA::tk_objref:
573       CORBA::Object_ptr ObjRef ;
574       try {
575 #if OMNIORB_VERSION >= 4
576         *theValue >>= (CORBA::Any::to_object ) ObjRef ;
577 #else
578         *theValue >>= ObjRef ;
579 #endif
580         f << "(object reference) " << ObjectToString( ObjRef ) ;
581       }
582       catch(...) {
583         f << "(object reference) catched error" ;
584       }
585       break;
586     default:
587       f << "(other ERROR)" ;
588       break;
589     }
590   }
591   else {
592     f << " Default (undefined) DATAPORT NOT DONE"  ;
593   }
594   if ( pthread_mutex_unlock( aMutexWait ) ) {
595     perror( "unlock DataValue" ) ;
596     exit( 0 ) ;
597   }
598 //  cdebug_out << pthread_self() << " StringValue() --> pthread_mutex_unlocked " << aMutexWait
599 //             << endl ;
600     
601 }
602
603