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