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