Salome HOME
Remove of obsolete methods
[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     InitialValue <<= (double ) 0. ;
95   }
96   else if ( !strcmp( Type , "double" ) ) {
97     cdebug << "double" << endl ;
98     InitialValue <<= (double ) 0. ;
99   }
100   else { // Default
101     cdebug << "objref" << endl ;
102 //    InitialValue.replace(CORBA::_tc_Object, NULL);
103     InitialValue <<= CORBA::Object::_nil() ;
104   }
105 //  else {
106 //    cdebug << "InitialValues ERROR (other) " << Type << endl ;
107 //    InitialValue <<= (long ) 0 ;
108 //  }
109   _InitialValue = new CORBA::Any( InitialValue ) ;
110   _Value = &_InitialValue ;
111 }
112
113 void GraphBase::DataPort::SetValue( const CORBA::Any & aDataValue ) {
114   CORBA::Any * aValue = new CORBA::Any( aDataValue ) ;
115   SetValue( aValue ) ;
116 }
117
118 #define ValueTrace 0
119 void GraphBase::DataPort::SetValue( const CORBA::Any * aDataValue ) {
120 //  cdebug << pthread_self() << " SetValue(aDataValue) --> pthread_mutex_lock " << &_MutexWait
121 //         << endl ;
122   if ( pthread_mutex_lock( &_MutexWait ) ) {
123     perror( "lock DataValue" ) ;
124     exit( 0 ) ;
125   }
126 //  cdebug << pthread_self() << " SetValue(aDataValue) pthread_mutex_locked " << &_MutexWait
127 //         << endl ;
128
129   delete _theValue ;
130
131 //JR 21.02.2005 Debug Memory leak :  string _Type = CORBA::string_dup( GetServicesParameter().Parametertype ) ;
132 //  const char * Type = _Type.c_str() ;
133   const char * Type = GetServicesParameter().Parametertype ;
134   _Value = &_theValue ;
135 #if ValueTrace
136   cdebug << "NewValue " << NodeName() << " " << PortName() << " " << PortType()
137          << " : " << aDataValue << " kind " << aDataValue->type()->kind() << " " ;
138 #endif
139   switch (aDataValue->type()->kind()) { // Input Value
140   case CORBA::tk_string: { // Input string Value
141     char * t;
142     *aDataValue >>= t;
143 #if ValueTrace
144     cdebug << t << " (string)" ;
145 #endif
146     if ( !strcmp( Type , "string" ) ) { // SuperVision Value
147       _theValue = aDataValue ;
148       *_theValue >>= t;
149 #if ValueTrace
150       cdebug << " == Value( " << t << ") (string)";
151 #endif
152     }
153     else {
154       CORBA::Any * theValue = new CORBA::Any() ;
155       if ( !strcmp( Type , "boolean" ) || !strcmp( Type , "char" ) ||
156            !strcmp( Type , "short" ) || !strcmp( Type , "int" ) || !strcmp( Type , "long" ) ) {
157         long l ;
158         sscanf( t , "%ld" , &l ) ;
159         *theValue <<= l ;
160         *theValue >>= l;
161 #if ValueTrace
162         cdebug << " --> Value( " << l << ") (long) kind " << theValue->type()->kind() ;
163 #endif
164       }
165       else if ( !strcmp( Type , "float" ) || !strcmp( Type , "double" ) ) {
166         double d ;
167         sscanf( t , "%lf" , &d ) ;
168         *theValue <<= d ;
169         *theValue >>= d;
170 #if ValueTrace
171         cdebug << " --> Value( " << d << ") (double) kind " << theValue->type()->kind() ;
172 #endif
173       }
174       else { // Default
175         CORBA::Object_ptr ObjRef ;
176         try {
177           ObjRef = StringToObject( t ) ;
178           *theValue <<= ObjRef ;
179         }
180         catch( ... ) {
181           *theValue <<= CORBA::Object::_nil() ;
182         }
183         *theValue >>= ObjRef ;
184 #if ValueTrace
185         cdebug << " --> Value( " << ObjectToString( ObjRef ) << ") (object reference) kind "
186                << theValue->type()->kind() ;
187 #endif
188       }
189       _theValue = theValue ;
190       *_Value = theValue ;
191 //JR 21.02.2005 Debug Memory leak : 
192       delete aDataValue ;
193     }
194     break;
195   }
196   case CORBA::tk_long: { // Input long Value
197     long l;
198     *aDataValue >>= l;
199 #if ValueTrace
200     cdebug << "Value( " << l << ") (long)";
201 #endif
202     if ( !strcmp( Type , "boolean" ) || !strcmp( Type , "char" ) ||
203          !strcmp( Type , "short" ) || !strcmp( Type , "int" ) || !strcmp( Type , "long" ) ) { // SuperVision Value
204       _theValue = aDataValue ;
205       *_Value = aDataValue ;
206       *_theValue >>= l;
207 #if ValueTrace
208       cdebug << " == Value( " << l << ") (long)";
209 #endif
210     }
211     else {
212       CORBA::Any * theValue = new CORBA::Any() ;
213       if ( !strcmp( Type , "string" ) ) {
214         char t[40] ;
215         sprintf( t , "%ld" , l ) ;
216         *theValue <<=  t ;
217         char *tt ;
218         *theValue >>= tt ;
219 #if ValueTrace
220         cdebug << " --> Value( " << t << ") (string) kind " << theValue->type()->kind() ;
221 #endif
222       }
223       else if ( !strcmp( Type , "float" ) || !strcmp( Type , "double" ) ) {
224         double d = l ;
225         *theValue <<= d ;
226         *theValue >>= d ;
227 #if ValueTrace
228         cdebug << " --> Value( " << d << ") (double) kind " << theValue->type()->kind() ;
229 #endif
230       }
231       else { // Default
232         CORBA::Object_ptr ObjRef ;
233         *theValue <<= CORBA::Object::_nil() ;
234         *theValue >>= ObjRef ;
235 #if ValueTrace
236         cdebug << " --> Value( " << ObjectToString( ObjRef ) << ") (object reference) kind "
237                << theValue->type()->kind() ;
238 #endif
239       }
240       _theValue = theValue ;
241       *_Value = theValue ;
242 //JR 21.02.2005 Debug Memory leak : 
243       delete aDataValue ;
244     }
245     break;
246   }
247   case CORBA::tk_double: { // Input double Value
248     double d;
249     *aDataValue >>= d;
250 #if ValueTrace
251     cdebug << "Value( " << d << ") (double)";
252 #endif
253     if ( !strcmp( Type , "float" ) || !strcmp( Type , "double" ) ) { // SuperVision Value
254       _theValue = aDataValue ;
255       *_Value = aDataValue ;
256       *_theValue >>= d;
257 #if ValueTrace
258       cdebug << " == Value( " << d << ") (double)";
259 #endif
260     }
261     else {
262       CORBA::Any * theValue = new CORBA::Any() ;
263       if ( !strcmp( Type , "string" ) ) {
264         char t[40] ;
265         sprintf( t , "%lf" , d ) ;
266         *theValue <<=  t ;
267         char *tt ;
268         *theValue >>= tt ;
269 #if ValueTrace
270         cdebug << " --> Value( " << t << ") (string) kind " << theValue->type()->kind() ;
271 #endif
272       }
273       else if ( !strcmp( Type , "boolean" ) || !strcmp( Type , "char" ) ||
274                 !strcmp( Type , "short" ) || !strcmp( Type , "int" ) || !strcmp( Type , "long" ) ) {
275         long l = (long ) d ;
276         *theValue <<= l ;
277         *theValue >>= l;
278 #if ValueTrace
279         cdebug << " --> Value( " << l << ") (long) kind " << theValue->type()->kind() ;
280 #endif
281       }
282       else { // Default
283         CORBA::Object_ptr ObjRef ;
284         *theValue <<= CORBA::Object::_nil() ;
285         *theValue >>= ObjRef ;
286 #if ValueTrace
287         cdebug << " --> Value( " << ObjectToString( ObjRef ) << ") (object reference) kind "
288                << theValue->type()->kind() ;
289 #endif
290       }
291       _theValue = theValue ;
292       *_Value = theValue ;
293 //JR 21.02.2005 Debug Memory leak : 
294       delete aDataValue ;
295     }
296     break;
297   }
298   case CORBA::tk_objref: { // Input objref Value
299     CORBA::Object_ptr obj ;
300     *aDataValue >>= obj;
301 #if ValueTrace
302     cdebug << "Value( " << ObjectToString( obj ) << ") (object reference)";
303 #endif
304     if ( strcmp( Type , "string" ) &&
305          strcmp( Type , "boolean" ) && strcmp( Type , "char" ) &&
306          strcmp( Type , "short" ) && strcmp( Type , "long" ) &&
307          strcmp( Type , "double" ) ) { // SuperVision Default Value
308       _theValue = aDataValue ;
309       *_Value = aDataValue ;
310       *_theValue >>= obj;
311 #if ValueTrace
312       cdebug << " == Value( " << ObjectToString( obj ) << ") (object reference)";
313 #endif
314     }
315     else {
316       CORBA::Any * theValue = new CORBA::Any() ;
317       if ( !strcmp( Type , "string" ) ) {
318         *theValue <<=  ObjectToString( obj ) ;
319         *theValue >>= obj ;
320 #if ValueTrace
321         cdebug << " --> Value( " << ObjectToString( obj ) << ") (string) kind " << theValue->type()->kind() ;
322 #endif
323       }
324       else if ( !strcmp( Type , "boolean" ) || !strcmp( Type , "char" ) ||
325                 !strcmp( Type , "short" ) || !strcmp( Type , "int" ) || !strcmp( Type , "long" ) ) {
326         long l = (long ) obj ;
327         *theValue <<= l ;
328         *theValue >>= l;
329 #if ValueTrace
330         cdebug << " --> Value( " << l << ") (long) kind " << theValue->type()->kind() ;
331 #endif
332       }
333       else if ( !strcmp( Type , "float" ) || !strcmp( Type , "double" ) ) {
334         double d = (double ) 0. ;
335         *theValue <<= d ;
336         *theValue >>= d;
337 #if ValueTrace
338         cdebug << " --> Value( " << d << ") (double) kind " << theValue->type()->kind() ;
339 #endif
340       }
341       _theValue = theValue ;
342       *_Value = theValue ;
343 //JR 21.02.2005 Debug Memory leak : 
344       delete aDataValue ;
345     }
346     break;
347   }
348   default: {
349     cdebug << "Value" << " (other(tk_string,tk_double,tk_long,tk_objref)) ERROR kind "
350            << aDataValue->type()->kind() ;
351     break;
352   }
353   }
354 #if ValueTrace
355   cdebug << endl ;
356 #endif
357   if ( pthread_mutex_unlock( &_MutexWait ) ) {
358     perror( "unlock DataValue" ) ;
359     exit( 0 ) ;
360   }
361 //  cdebug << pthread_self() << " SetValue(aDataValue) --> pthread_mutex_unlocked " << &_MutexWait
362 //         << endl ;
363
364 }
365
366 // PAL8506
367 //JR 30.03.2005 Memory Leak + Debug(crash) CORBA::Any const * GraphBase::DataPort::Value() const {
368 const CORBA::Any GraphBase::DataPort::Value() const {
369   pthread_mutex_t * aMutexWait = (pthread_mutex_t *) &_MutexWait ;
370 //  cdebug << pthread_self() << " Value() --> pthread_mutex_lock " << aMutexWait << endl ;
371   if ( pthread_mutex_lock( aMutexWait ) ) {
372     perror( "lock DataValue" ) ;
373     exit( 0 ) ;
374   }
375 //  cdebug << pthread_self() << " Value() --> pthread_mutex_locked " << aMutexWait << endl ;
376   const CORBA::Any *const Value = *_Value ;
377 //JR 30.03.2005  CORBA::Any * theValue ;
378   CORBA::Any theValue ;
379 //  cdebug  << pthread_self() << " Value " << NodeName() << " " << PortName() << " "
380 //          << PortType() << " _Value " << _Value << " *_Value " << *_Value  << " Value->type "
381 //          << Value->type() ;
382   if ( Value->type() ) {
383 //    cdebug << " kind " << Value->type()->kind() << " :" << endl ;
384   }
385   else {
386 //    cdebug << " Null" << endl ;
387   }
388   if ( PortDone() ) {
389 //JR 21.02.2005 Debug Memory leak :     theValue = new CORBA::Any( *Value ) ;
390 //JR 30.03.2005     theValue = (CORBA::Any * ) Value ;
391     theValue = *Value ;
392 //JR 30.03.2005    switch ( theValue->type()->kind() ) {
393     switch ( theValue.type()->kind() ) {
394     case CORBA::tk_string: {
395       char * t;
396 //JR 30.03.2005      *theValue >>= t;
397       theValue >>= t;
398 //      cdebug << "GraphBase::DataPort::Value() : " << t << " (string) " << endl ;
399       break;
400     }
401     case CORBA::tk_double: {
402       double d;
403 //JR 30.03.2005      *theValue >>= d;
404       theValue >>= d;
405 //      cdebug << "GraphBase::DataPort::Value() : " << d << " (double) " << endl ;
406       break;
407     }
408     case CORBA::tk_long: {
409       long l;
410 //JR 30.03.2005      *theValue >>= l;
411       theValue >>= l;
412 //      cdebug << "GraphBase::DataPort::Value() : " << l << " (long) " << endl ;
413       break;
414     }
415     case CORBA::tk_objref: {
416       CORBA::Object_ptr obj ;
417       char * retstr ;
418       try {
419 //JR 30.03.2005        *theValue >>= obj ;
420         theValue >>= obj ;
421         retstr = ObjectToString( obj );
422 //        cdebug << "GraphBase::DataPort::Value() : " << retstr
423 //               << "(object reference) " << endl;
424       }
425       catch( ... ) {
426         cdebug << "ToString( object ) Catched ERROR" << endl ;
427       }
428       break;
429     }
430     default: {
431       cdebug << "GraphBase::DataPort::Value() : " << NodeName() << "( " << PortName() << " ) " << PortType()
432              << " (other(tk_string,tk_double,tk_long,tk_objref)) ERROR" << endl ;
433       break;
434     }
435     }
436   }
437   else {
438 //    cdebug << "GraphBase::DataPort::InitialValue() " << endl ;
439 //JR 21.02.2005 Debug Memory leak :    theValue = new CORBA::Any( *Value ) ;
440 //JR 30.03.2005    theValue = (CORBA::Any * ) Value ;
441     theValue = *Value ;
442   }
443   if ( pthread_mutex_unlock( aMutexWait ) ) {
444     perror( "unlock DataValue" ) ;
445     exit( 0 ) ;
446   }
447 //  cdebug << pthread_self() << " Value() --> pthread_mutex_unlocked " << aMutexWait << endl ;
448     
449   return theValue ;
450 }
451
452 bool GraphBase::DataPort::BoolValue() const {
453   bool RetVal = false ;
454   pthread_mutex_t * aMutexWait = (pthread_mutex_t *) &_MutexWait ;
455 //  cdebug << pthread_self() << " BoolValue() --> pthread_mutex_lock " << aMutexWait << endl ;
456   if ( pthread_mutex_lock( aMutexWait ) ) {
457     perror( "lock DataValue" ) ;
458     exit( 0 ) ;
459   }
460 //  cdebug << pthread_self() << " BoolValue() --> pthread_mutex_locked " << aMutexWait
461 //         << endl ;
462   if ( (*_Value)->type()->kind() == CORBA::tk_long ) {
463     long val ;
464     **_Value >>= val ;
465     if ( val ) {
466       RetVal = true ;
467     }
468   }
469   else if ( (*_Value)->type()->kind() == CORBA::tk_boolean ) {
470     bool val ;
471     **_Value >>= val ;
472     if ( val ) {
473       RetVal = true ;
474     }
475   }
476   if ( pthread_mutex_unlock( aMutexWait ) ) {
477     perror( "unlock DataValue" ) ;
478     exit( 0 ) ;
479   }
480 //  cdebug << pthread_self() << " " << NodeName() << "( " << PortName() << " ) BoolValue " << RetVal << endl ;
481   return RetVal ;
482 }
483
484 void GraphBase::DataPort::StringValue(ostream & f ) const {
485   pthread_mutex_t * aMutexWait = (pthread_mutex_t *) &_MutexWait ;
486 //  cdebug_in << pthread_self() << " StringValue() --> pthread_mutex_lock " << aMutexWait
487 //            << endl ;
488   if ( pthread_mutex_lock( aMutexWait ) ) {
489     perror( "lock DataValue" ) ;
490     exit( 0 ) ;
491   }
492 //  cdebug << pthread_self() << " StringValue() --> pthread_mutex_locked " << aMutexWait
493 //         << endl ;
494   if ( PortDone() ) {
495 //    cdebug << "StringValue " << NodeName() << " " << PortName() << " " << PortType()
496 //           << " _Value "  << _Value << " *_Value "
497 //           << *_Value  << " " << endl ;
498     const CORBA::Any * theValue = *_Value ;
499     switch (theValue->type()->kind()) {
500     case CORBA::tk_string:
501       char * t;
502       *theValue >>= t;
503       f << t << " (string)" ;
504       break;
505     case CORBA::tk_double:
506       double d;
507       *theValue >>= d;
508       f << d << " (double)" ;
509       break;
510     case CORBA::tk_long:
511       long l;
512       *theValue >>= l;
513       f << l << " (long)" ;
514       break;
515     case CORBA::tk_objref:
516       CORBA::Object_ptr ObjRef ;
517       try {
518         *theValue >>= ObjRef ;
519         f << "(object reference) " << ObjectToString( ObjRef ) ;
520       }
521       catch(...) {
522         f << "(object reference) catched error" ;
523       }
524       break;
525     default:
526       f << "(other ERROR)" ;
527       break;
528     }
529   }
530   else {
531     f << " Default (undefined) DATAPORT NOT DONE"  ;
532   }
533   if ( pthread_mutex_unlock( aMutexWait ) ) {
534     perror( "unlock DataValue" ) ;
535     exit( 0 ) ;
536   }
537 //  cdebug_out << pthread_self() << " StringValue() --> pthread_mutex_unlocked " << aMutexWait
538 //             << endl ;
539     
540 }
541
542