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