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