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