]> SALOME platform Git repositories - modules/superv.git/blob - src/GraphBase/DataFlowBase_DataPort.cxx
Salome HOME
Change configuration
[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 #if OMNIORB_VERSION >= 4
184         *theValue >>= (CORBA::Any::to_object ) ObjRef ;
185 #else
186         *theValue >>= ObjRef ;
187 #endif
188 #if ValueTrace
189         cdebug << " --> Value( " << ObjectToString( ObjRef ) << ") (object reference) kind "
190                << theValue->type()->kind() ;
191 #endif
192       }
193       _theValue = theValue ;
194       *_Value = theValue ;
195 //JR 21.02.2005 Debug Memory leak : 
196       delete aDataValue ;
197     }
198     break;
199   }
200   case CORBA::tk_long: { // Input long Value
201     long l;
202     *aDataValue >>= l;
203 #if ValueTrace
204     cdebug << "Value( " << l << ") (long)";
205 #endif
206     if ( !strcmp( Type , "boolean" ) || !strcmp( Type , "char" ) ||
207          !strcmp( Type , "short" ) || !strcmp( Type , "int" ) || !strcmp( Type , "long" ) ) { // SuperVision Value
208       _theValue = aDataValue ;
209       *_Value = aDataValue ;
210       *_theValue >>= l;
211 #if ValueTrace
212       cdebug << " == Value( " << l << ") (long)";
213 #endif
214     }
215     else {
216       CORBA::Any * theValue = new CORBA::Any() ;
217       if ( !strcmp( Type , "string" ) ) {
218         char t[40] ;
219         sprintf( t , "%ld" , l ) ;
220         *theValue <<=  t ;
221         char *tt ;
222         *theValue >>= tt ;
223 #if ValueTrace
224         cdebug << " --> Value( " << t << ") (string) kind " << theValue->type()->kind() ;
225 #endif
226       }
227       else if ( !strcmp( Type , "float" ) || !strcmp( Type , "double" ) ) {
228         double d = l ;
229         *theValue <<= d ;
230         *theValue >>= d ;
231 #if ValueTrace
232         cdebug << " --> Value( " << d << ") (double) kind " << theValue->type()->kind() ;
233 #endif
234       }
235       else { // Default
236         CORBA::Object_ptr ObjRef ;
237         *theValue <<= CORBA::Object::_nil() ;
238 #if OMNIORB_VERSION >= 4
239         *theValue >>= (CORBA::Any::to_object ) ObjRef ;
240 #else
241         *theValue >>= ObjRef ;
242 #endif
243 #if ValueTrace
244         cdebug << " --> Value( " << ObjectToString( ObjRef ) << ") (object reference) kind "
245                << theValue->type()->kind() ;
246 #endif
247       }
248       _theValue = theValue ;
249       *_Value = theValue ;
250 //JR 21.02.2005 Debug Memory leak : 
251       delete aDataValue ;
252     }
253     break;
254   }
255   case CORBA::tk_double: { // Input double Value
256     double d;
257     *aDataValue >>= d;
258 #if ValueTrace
259     cdebug << "Value( " << d << ") (double)";
260 #endif
261     if ( !strcmp( Type , "float" ) || !strcmp( Type , "double" ) ) { // SuperVision Value
262       _theValue = aDataValue ;
263       *_Value = aDataValue ;
264       *_theValue >>= d;
265 #if ValueTrace
266       cdebug << " == Value( " << d << ") (double)";
267 #endif
268     }
269     else {
270       CORBA::Any * theValue = new CORBA::Any() ;
271       if ( !strcmp( Type , "string" ) ) {
272         char t[40] ;
273         sprintf( t , "%lf" , d ) ;
274         *theValue <<=  t ;
275         char *tt ;
276         *theValue >>= tt ;
277 #if ValueTrace
278         cdebug << " --> Value( " << t << ") (string) kind " << theValue->type()->kind() ;
279 #endif
280       }
281       else if ( !strcmp( Type , "boolean" ) || !strcmp( Type , "char" ) ||
282                 !strcmp( Type , "short" ) || !strcmp( Type , "int" ) || !strcmp( Type , "long" ) ) {
283         long l = (long ) d ;
284         *theValue <<= l ;
285         *theValue >>= l;
286 #if ValueTrace
287         cdebug << " --> Value( " << l << ") (long) kind " << theValue->type()->kind() ;
288 #endif
289       }
290       else { // Default
291         CORBA::Object_ptr ObjRef ;
292         *theValue <<= CORBA::Object::_nil() ;
293 #if OMNIORB_VERSION >= 4
294         *theValue >>= (CORBA::Any::to_object ) ObjRef ;
295 #else
296         *theValue >>= ObjRef ;
297 #endif
298 #if ValueTrace
299         cdebug << " --> Value( " << ObjectToString( ObjRef ) << ") (object reference) kind "
300                << theValue->type()->kind() ;
301 #endif
302       }
303       _theValue = theValue ;
304       *_Value = theValue ;
305 //JR 21.02.2005 Debug Memory leak : 
306       delete aDataValue ;
307     }
308     break;
309   }
310   case CORBA::tk_objref: { // Input objref Value
311     CORBA::Object_ptr obj ;
312 #if OMNIORB_VERSION >= 4
313     *aDataValue >>= (CORBA::Any::to_object ) obj;
314 #else
315     *aDataValue >>= obj;
316 #endif
317 #if ValueTrace
318     cdebug << "Value( " << ObjectToString( obj ) << ") (object reference)";
319 #endif
320     if ( strcmp( Type , "string" ) &&
321          strcmp( Type , "boolean" ) && strcmp( Type , "char" ) &&
322          strcmp( Type , "short" ) && strcmp( Type , "long" ) &&
323          strcmp( Type , "double" ) ) { // SuperVision Default Value
324       _theValue = aDataValue ;
325       *_Value = aDataValue ;
326 #if OMNIORB_VERSION >= 4
327       *_theValue >>= (CORBA::Any::to_object ) obj ;
328 #else
329       *_theValue >>= obj;
330 #endif
331 #if ValueTrace
332       cdebug << " == Value( " << ObjectToString( obj ) << ") (object reference)";
333 #endif
334     }
335     else {
336       CORBA::Any * theValue = new CORBA::Any() ;
337       if ( !strcmp( Type , "string" ) ) {
338         *theValue <<=  ObjectToString( obj ) ;
339 #if OMNIORB_VERSION >= 4
340         *theValue >>= (CORBA::Any::to_object ) obj ;
341 #else
342         *theValue >>= obj ;
343 #endif
344 #if ValueTrace
345         cdebug << " --> Value( " << ObjectToString( obj ) << ") (string) kind " << theValue->type()->kind() ;
346 #endif
347       }
348       else if ( !strcmp( Type , "boolean" ) || !strcmp( Type , "char" ) ||
349                 !strcmp( Type , "short" ) || !strcmp( Type , "int" ) || !strcmp( Type , "long" ) ) {
350         long l = (long ) obj ;
351         *theValue <<= l ;
352         *theValue >>= l;
353 #if ValueTrace
354         cdebug << " --> Value( " << l << ") (long) kind " << theValue->type()->kind() ;
355 #endif
356       }
357       else if ( !strcmp( Type , "float" ) || !strcmp( Type , "double" ) ) {
358         double d = (double ) 0. ;
359         *theValue <<= d ;
360         *theValue >>= d;
361 #if ValueTrace
362         cdebug << " --> Value( " << d << ") (double) kind " << theValue->type()->kind() ;
363 #endif
364       }
365       _theValue = theValue ;
366       *_Value = theValue ;
367 //JR 21.02.2005 Debug Memory leak : 
368       delete aDataValue ;
369     }
370     break;
371   }
372   default: {
373     cdebug << "Value" << " (other(tk_string,tk_double,tk_long,tk_objref)) ERROR kind "
374            << aDataValue->type()->kind() ;
375     break;
376   }
377   }
378 #if ValueTrace
379   cdebug << endl ;
380 #endif
381   if ( pthread_mutex_unlock( &_MutexWait ) ) {
382     perror( "unlock DataValue" ) ;
383     exit( 0 ) ;
384   }
385 //  cdebug << pthread_self() << " SetValue(aDataValue) --> pthread_mutex_unlocked " << &_MutexWait
386 //         << endl ;
387
388 }
389
390 // PAL8506
391 //JR 30.03.2005 Memory Leak + Debug(crash) CORBA::Any const * GraphBase::DataPort::Value() const {
392 const CORBA::Any GraphBase::DataPort::Value() const {
393   pthread_mutex_t * aMutexWait = (pthread_mutex_t *) &_MutexWait ;
394 //  cdebug << pthread_self() << " Value() --> pthread_mutex_lock " << aMutexWait << endl ;
395   if ( pthread_mutex_lock( aMutexWait ) ) {
396     perror( "lock DataValue" ) ;
397     exit( 0 ) ;
398   }
399 //  cdebug << pthread_self() << " Value() --> pthread_mutex_locked " << aMutexWait << endl ;
400   const CORBA::Any *const Value = *_Value ;
401 //JR 30.03.2005  CORBA::Any * theValue ;
402   CORBA::Any theValue ;
403 //  cdebug  << pthread_self() << " Value " << NodeName() << " " << PortName() << " "
404 //          << PortType() << " _Value " << _Value << " *_Value " << *_Value  << " Value->type "
405 //          << Value->type() ;
406   if ( Value->type() ) {
407 //    cdebug << " kind " << Value->type()->kind() << " :" << endl ;
408   }
409   else {
410 //    cdebug << " Null" << endl ;
411   }
412   if ( PortDone() ) {
413 //JR 21.02.2005 Debug Memory leak :     theValue = new CORBA::Any( *Value ) ;
414 //JR 30.03.2005     theValue = (CORBA::Any * ) Value ;
415     theValue = *Value ;
416 //JR 30.03.2005    switch ( theValue->type()->kind() ) {
417     switch ( theValue.type()->kind() ) {
418     case CORBA::tk_string: {
419       char * t;
420 //JR 30.03.2005      *theValue >>= t;
421       theValue >>= t;
422 //      cdebug << "GraphBase::DataPort::Value() : " << t << " (string) " << endl ;
423       break;
424     }
425     case CORBA::tk_double: {
426       double d;
427 //JR 30.03.2005      *theValue >>= d;
428       theValue >>= d;
429 //      cdebug << "GraphBase::DataPort::Value() : " << d << " (double) " << endl ;
430       break;
431     }
432     case CORBA::tk_long: {
433       long l;
434 //JR 30.03.2005      *theValue >>= l;
435       theValue >>= l;
436 //      cdebug << "GraphBase::DataPort::Value() : " << l << " (long) " << endl ;
437       break;
438     }
439     case CORBA::tk_objref: {
440       CORBA::Object_ptr obj ;
441       char * retstr ;
442       try {
443 //JR 30.03.2005        *theValue >>= obj ;
444 #if OMNIORB_VERSION >= 4
445         theValue >>= (CORBA::Any::to_object ) obj ;
446 #else
447         theValue >>= obj ;
448 #endif
449         retstr = ObjectToString( obj );
450 //        cdebug << "GraphBase::DataPort::Value() : " << retstr
451 //               << "(object reference) " << endl;
452       }
453       catch( ... ) {
454         cdebug << "ToString( object ) Catched ERROR" << endl ;
455       }
456       break;
457     }
458     default: {
459       cdebug << "GraphBase::DataPort::Value() : " << NodeName() << "( " << PortName() << " ) " << PortType()
460              << " (other(tk_string,tk_double,tk_long,tk_objref)) ERROR" << endl ;
461       break;
462     }
463     }
464   }
465   else {
466 //    cdebug << "GraphBase::DataPort::InitialValue() " << endl ;
467 //JR 21.02.2005 Debug Memory leak :    theValue = new CORBA::Any( *Value ) ;
468 //JR 30.03.2005    theValue = (CORBA::Any * ) Value ;
469     theValue = *Value ;
470   }
471   if ( pthread_mutex_unlock( aMutexWait ) ) {
472     perror( "unlock DataValue" ) ;
473     exit( 0 ) ;
474   }
475 //  cdebug << pthread_self() << " Value() --> pthread_mutex_unlocked " << aMutexWait << endl ;
476     
477   return theValue ;
478 }
479
480 bool GraphBase::DataPort::BoolValue() const {
481   bool RetVal = false ;
482   pthread_mutex_t * aMutexWait = (pthread_mutex_t *) &_MutexWait ;
483 //  cdebug << pthread_self() << " BoolValue() --> pthread_mutex_lock " << aMutexWait << endl ;
484   if ( pthread_mutex_lock( aMutexWait ) ) {
485     perror( "lock DataValue" ) ;
486     exit( 0 ) ;
487   }
488 //  cdebug << pthread_self() << " BoolValue() --> pthread_mutex_locked " << aMutexWait
489 //         << endl ;
490   if ( (*_Value)->type()->kind() == CORBA::tk_long ) {
491     long val ;
492     **_Value >>= val ;
493     if ( val ) {
494       RetVal = true ;
495     }
496   }
497   else if ( (*_Value)->type()->kind() == CORBA::tk_boolean ) {
498     bool val ;
499     **_Value >>= val ;
500     if ( val ) {
501       RetVal = true ;
502     }
503   }
504   if ( pthread_mutex_unlock( aMutexWait ) ) {
505     perror( "unlock DataValue" ) ;
506     exit( 0 ) ;
507   }
508 //  cdebug << pthread_self() << " " << NodeName() << "( " << PortName() << " ) BoolValue " << RetVal << endl ;
509   return RetVal ;
510 }
511
512 void GraphBase::DataPort::StringValue(ostream & f ) const {
513   pthread_mutex_t * aMutexWait = (pthread_mutex_t *) &_MutexWait ;
514 //  cdebug_in << pthread_self() << " StringValue() --> pthread_mutex_lock " << aMutexWait
515 //            << endl ;
516   if ( pthread_mutex_lock( aMutexWait ) ) {
517     perror( "lock DataValue" ) ;
518     exit( 0 ) ;
519   }
520 //  cdebug << pthread_self() << " StringValue() --> pthread_mutex_locked " << aMutexWait
521 //         << endl ;
522   if ( PortDone() ) {
523 //    cdebug << "StringValue " << NodeName() << " " << PortName() << " " << PortType()
524 //           << " _Value "  << _Value << " *_Value "
525 //           << *_Value  << " " << endl ;
526     const CORBA::Any * theValue = *_Value ;
527     switch (theValue->type()->kind()) {
528     case CORBA::tk_string:
529       char * t;
530       *theValue >>= t;
531       f << t << " (string)" ;
532       break;
533     case CORBA::tk_double:
534       double d;
535       *theValue >>= d;
536       f << d << " (double)" ;
537       break;
538     case CORBA::tk_long:
539       long l;
540       *theValue >>= l;
541       f << l << " (long)" ;
542       break;
543     case CORBA::tk_objref:
544       CORBA::Object_ptr ObjRef ;
545       try {
546 #if OMNIORB_VERSION >= 4
547         *theValue >>= (CORBA::Any::to_object ) ObjRef ;
548 #else
549         *theValue >>= ObjRef ;
550 #endif
551         f << "(object reference) " << ObjectToString( ObjRef ) ;
552       }
553       catch(...) {
554         f << "(object reference) catched error" ;
555       }
556       break;
557     default:
558       f << "(other ERROR)" ;
559       break;
560     }
561   }
562   else {
563     f << " Default (undefined) DATAPORT NOT DONE"  ;
564   }
565   if ( pthread_mutex_unlock( aMutexWait ) ) {
566     perror( "unlock DataValue" ) ;
567     exit( 0 ) ;
568   }
569 //  cdebug_out << pthread_self() << " StringValue() --> pthread_mutex_unlocked " << aMutexWait
570 //             << endl ;
571     
572 }
573
574