00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _CALCIUM_CXXINTERFACE_HXX_
00030 #define _CALCIUM_CXXINTERFACE_HXX_
00031
00032 #include <string>
00033 #include <vector>
00034 #include <iostream>
00035 #include "Superv_Component_i.hxx"
00036 #include "CalciumException.hxx"
00037 #include "CalciumTypes.hxx"
00038 #include "CalciumGenericUsesPort.hxx"
00039 #include "Copy2UserSpace.hxx"
00040 #include "Copy2CorbaSpace.hxx"
00041 #include "CalciumPortTraits.hxx"
00042
00043 #include <stdio.h>
00044
00045 #include <typeinfo>
00046
00047 template <typename T1, typename T2>
00048 struct IsSameType {
00049 static const bool value = false;
00050 };
00051 template <typename T1>
00052 struct IsSameType<T1,T1> {
00053 static const bool value = true;
00054 };
00055
00056 extern const char * CPMESSAGE[];
00057
00058
00059
00060 #include <boost/type_traits/remove_all_extents.hpp>
00061
00062 namespace CalciumInterface {
00063
00064
00065
00066 static inline void
00067 ecp_cd (Superv_Component_i & component, std::string & instanceName)
00068 {
00069
00070 CORBA::String_var componentName=component.instanceName();
00071 std::string containerName=component.getContainerName();
00072 if (instanceName.empty()) instanceName=componentName;
00073 Engines_DSC_interface::writeEvent("CP_CD",containerName,componentName,"","","");
00074 }
00075
00076 static void
00077 ecp_fin (Superv_Component_i & component, bool provideLastGivenValue)
00078 {
00079 CORBA::String_var componentName=component.instanceName();
00080 std::string containerName=component.getContainerName();
00081 Engines_DSC_interface::writeEvent("CP_FIN",containerName,componentName,"","","");
00082
00083 std::vector<std::string> usesPortNames;
00084 std::vector<std::string>::const_iterator it;
00085 component.get_uses_port_names(usesPortNames);
00086
00087
00088
00089
00090
00091 calcium_uses_port* myCalciumUsesPort;
00092
00093 for (it=usesPortNames.begin(); it != usesPortNames.end(); ++it)
00094 {
00095 try
00096 {
00097 myCalciumUsesPort= component.Superv_Component_i::get_port< calcium_uses_port >((*it).c_str());
00098
00099
00100
00101
00102
00103 #ifdef MYDEBUG
00104 std::cerr << "-------- CalciumInterface(ecp_fin) MARK 1 -|"<< *it <<"|----"<<
00105
00106 typeid(myCalciumUsesPort).name() <<"-------------" << std::endl;
00107 #endif
00108
00109
00110
00111
00112
00113 myCalciumUsesPort->disconnect(provideLastGivenValue);
00114 }
00115 catch ( const Superv_Component_i::BadCast & ex)
00116 {
00117 Engines_DSC_interface::writeEvent("CP_FIN",containerName,componentName,"",CPMESSAGE[CalciumTypes::CPTPVR],ex.what());
00118 throw (CalciumException(CalciumTypes::CPTPVR,ex));
00119 }
00120 catch ( const DSC_Exception & ex)
00121 {
00122 Engines_DSC_interface::writeEvent("CP_FIN",containerName,componentName,"",CPMESSAGE[CalciumTypes::CPOK],ex.what());
00123
00124
00125
00126
00127 }
00128 catch (...)
00129 {
00130 Engines_DSC_interface::writeEvent("CP_FIN",containerName,componentName,"",CPMESSAGE[CalciumTypes::CPATAL],"Unexpected exception");
00131 throw (CalciumException(CalciumTypes::CPATAL,"Unexpected exception"));
00132
00133 }
00134 }
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 template <typename T1, typename T2> static void
00146 ecp_free ( T1 * dataPtr )
00147 {
00148 typedef typename ProvidesPortTraits<T2>::PortType PortType;
00149 typedef typename PortType::DataManipulator DataManipulator;
00150 typedef typename DataManipulator::Type DataType;
00151 typedef typename DataManipulator::InnerType InnerType;
00152
00153 DeleteTraits<IsSameType<T1,InnerType>::value, DataManipulator >::apply(dataPtr);
00154 }
00155
00156 template <typename T1> static void
00157 ecp_free ( T1 * dataPtr )
00158 {
00159 ecp_free<T1,T1> ( dataPtr );
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 template <typename T1, typename T2 > static void
00172 ecp_lecture ( Superv_Component_i & component,
00173 int const & dependencyType,
00174 double & ti,
00175 double const & tf,
00176 long & i,
00177 const std::string & nomVar,
00178 size_t bufferLength,
00179 size_t & nRead,
00180 T1 * &data )
00181 {
00182
00183 assert(&component);
00184 CORBA::String_var componentName=component.instanceName();
00185 std::string containerName=component.getContainerName();
00186
00187 typedef typename ProvidesPortTraits<T2>::PortType PortType;
00188 typedef typename PortType::DataManipulator DataManipulator;
00189 typedef typename DataManipulator::Type CorbaDataType;
00190 typedef typename DataManipulator::InnerType InnerType;
00191 CalciumTypes::DependencyType _dependencyType=
00192 static_cast<CalciumTypes::DependencyType>(dependencyType);
00193
00194 CorbaDataType corbaData;
00195
00196 #ifdef MYDEBUG
00197 std::cerr << "-------- CalciumInterface(ecp_lecture) MARK 1 ------------------" << std::endl;
00198 #endif
00199
00200 if (nomVar.empty())
00201 {
00202 Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,"",CPMESSAGE[CalciumTypes::CPNMVR],"");
00203 throw CalciumException(CalciumTypes::CPNMVR, LOC("Empty variable name"));
00204 }
00205 PortType * port;
00206 #ifdef MYDEBUG
00207 std::cout << "-------- CalciumInterface(lecture) MARK 2 --"<<typeid(port).name()<<"----------------" << std::endl;
00208 T1 t1;
00209 T2 t2;
00210 std::cout << "-------- CalciumInterface(lecture) MARK 2b1 -----" << typeid(t1).name() << "-------------" << std::endl;
00211 std::cout << "-------- CalciumInterface(lecture) MARK 2b2 -----" << typeid(t2).name() << "-------------" << std::endl;
00212 #endif
00213
00214 try
00215 {
00216 port = component.Superv_Component_i::get_port< PortType > (nomVar.c_str());
00217 #ifdef MYDEBUG
00218 std::cout << "-------- CalciumInterface(ecp_lecture) MARK 3 ------------------" << std::endl;
00219 #endif
00220 }
00221 catch ( const Superv_Component_i::PortNotDefined & ex)
00222 {
00223 Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPNMVR],ex.what());
00224 throw (CalciumException(CalciumTypes::CPNMVR,ex));
00225 }
00226 catch ( const Superv_Component_i::PortNotConnected & ex)
00227 {
00228 Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPLIEN],ex.what());
00229 throw (CalciumException(CalciumTypes::CPLIEN,ex));
00230
00231 }
00232 catch ( const Superv_Component_i::BadCast & ex)
00233 {
00234 Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPTPVR],ex.what());
00235 throw (CalciumException(CalciumTypes::CPTPVR,ex));
00236 }
00237
00238
00239 CalciumTypes::DependencyType portDependencyType = port->getDependencyType();
00240
00241 if ( portDependencyType == CalciumTypes::UNDEFINED_DEPENDENCY )
00242 {
00243 Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPIT],"Dependency mode is undefined");
00244 throw CalciumException(CalciumTypes::CPIT, LOC(OSS()<<"Dependency mode of variable " << nomVar << " is undefined."));
00245 }
00246
00247 if ( ( portDependencyType != _dependencyType ) && ( _dependencyType != CalciumTypes::SEQUENCE_DEPENDENCY ) )
00248 {
00249 Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPIT],
00250 "Dependency mode is not the same as the required one");
00251 throw CalciumException(CalciumTypes::CPITVR, LOC(OSS()<<"Dependency mode of variable " << nomVar << ": "
00252 << portDependencyType << " is not the same as the required one."));
00253 }
00254
00255
00256 std::stringstream msgout,msg;
00257 if ( _dependencyType == CalciumTypes::TIME_DEPENDENCY )
00258 {
00259 try
00260 {
00261 double tt=ti;
00262 msg << "ti=" << ti << ", tf=" << tf ;
00263 Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),"",msg.str().c_str());
00264 corbaData = port->get(tt,tf, 0);
00265 msgout << "read t=" << tt ;
00266 #ifdef MYDEBUG
00267 std::cout << "-------- CalciumInterface(ecp_lecture) MARK 5 ------------------" << std::endl;
00268 #endif
00269 }
00270 catch ( const DSC_Exception & ex)
00271 {
00272 Engines_DSC_interface::writeEvent("END_READ",containerName,componentName,nomVar.c_str(),"",ex.what());
00273 throw;
00274 }
00275 }
00276 else if ( _dependencyType == CalciumTypes::ITERATION_DEPENDENCY )
00277 {
00278 try
00279 {
00280 msg << "i=" << i ;
00281 Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),"",msg.str().c_str());
00282 corbaData = port->get(0, i);
00283 msgout << "read i=" << i ;
00284 #ifdef MYDEBUG
00285 std::cout << "-------- CalciumInterface(ecp_lecture) MARK 6 ------------------" << std::endl;
00286 #endif
00287 }
00288 catch ( const DSC_Exception & ex)
00289 {
00290 Engines_DSC_interface::writeEvent("END_READ",containerName,componentName,nomVar.c_str(),"",ex.what());
00291 throw;
00292 }
00293 }
00294 else
00295 {
00296
00297 try
00298 {
00299 #ifdef MYDEBUG
00300 std::cout << "-------- CalciumInterface(ecp_lecture) MARK 7 ------------------" << std::endl;
00301 #endif
00302 Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),"","Sequential read");
00303 corbaData = port->next(ti,i);
00304 msgout << "read ";
00305 if(i==0)msgout<< "t=" <<ti;
00306 else msgout<< "i=" <<i;
00307 }
00308 catch ( const DSC_Exception & ex)
00309 {
00310 Engines_DSC_interface::writeEvent("END_READ",containerName,componentName,nomVar.c_str(),"",ex.what());
00311 throw;
00312 }
00313 }
00314
00315 #ifdef MYDEBUG
00316 std::cout << "-------- CalciumInterface(ecp_lecture) MARK 8 ------------------" << std::endl;
00317 #endif
00318 size_t corbaDataSize = DataManipulator::size(corbaData);
00319 #ifdef MYDEBUG
00320 std::cout << "-------- CalciumInterface(ecp_lecture) corbaDataSize : " << corbaDataSize << std::endl;
00321 #endif
00322
00323
00324 if ( data == NULL )
00325 {
00326 if ( bufferLength != 0 )
00327 {
00328 MESSAGE("bufferLength devrait valoir 0 pour l'utilisation du mode sans copie (data==NULL)");
00329 }
00330 nRead = corbaDataSize;
00331
00332 if (!IsSameType<T1,InnerType>::value) data = new T1[nRead];
00333 #ifdef MYDEBUG
00334 std::cout << "-------- CalciumInterface(ecp_lecture) MARK 9 ------------------" << std::endl;
00335 #endif
00336
00337
00338
00339
00340
00341
00342
00343
00344 Copy2UserSpace< IsSameType<T1,InnerType>::value, DataManipulator >::apply(data,corbaData,nRead);
00345 #ifdef MYDEBUG
00346 std::cout << "-------- CalciumInterface(ecp_lecture) MARK 10 ------------------" << std::endl;
00347 #endif
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358 }
00359 else
00360 {
00361 nRead = std::min < size_t > (corbaDataSize,bufferLength);
00362 #ifdef MYDEBUG
00363 std::cout << "-------- CalciumInterface(ecp_lecture) MARK 11 ------------------" << std::endl;
00364 #endif
00365 Copy2UserSpace<false, DataManipulator >::apply(data,corbaData,nRead);
00366
00367
00368 #ifdef MYDEBUG
00369 std::cout << "-------- CalciumInterface(ecp_lecture) MARK 12 ------------------" << std::endl;
00370 #endif
00371 }
00372 #ifdef MYDEBUG
00373 std::cout << "-------- CalciumInterface(ecp_lecture), Valeur de data : " << std::endl;
00374 std::copy(data,data+nRead,std::ostream_iterator<T1>(std::cout," "));
00375 std::cout << "Ptr :" << data << std::endl;
00376 std::cout << "-------- CalciumInterface(ecp_lecture) MARK 13 ------------------" << std::endl;
00377 #endif
00378 Engines_DSC_interface::writeEvent("END_READ",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPOK],msgout.str().c_str());
00379 return;
00380 }
00381
00382
00383 template <typename T1 > static void
00384 ecp_lecture ( Superv_Component_i & component,
00385 int const & dependencyType,
00386 double & ti,
00387 double const & tf,
00388 long & i,
00389 const std::string & nomVar,
00390 size_t bufferLength,
00391 size_t & nRead,
00392 T1 * &data )
00393 {
00394 ecp_lecture<T1,T1> (component,dependencyType,ti,tf,
00395 i,nomVar,bufferLength,nRead,data);
00396
00397 }
00398
00399
00400
00401
00402
00403 template <typename T1, typename T2> static void
00404 ecp_ecriture ( Superv_Component_i & component,
00405 int const & dependencyType,
00406 double const & t,
00407 long const & i,
00408 const std::string & nomVar,
00409 size_t bufferLength,
00410 T1 const & data )
00411 {
00412
00413 assert(&component);
00414 CORBA::String_var componentName=component.instanceName();
00415 std::string containerName=component.getContainerName();
00416
00417
00418 typedef typename boost::remove_all_extents< T1 >::type T1_without_extent;
00419 typedef typename boost::remove_all_extents< T2 >::type T2_without_extent;
00420 typedef typename UsesPortTraits <T2_without_extent>::PortType UsesPortType;
00421 typedef typename ProvidesPortTraits<T2_without_extent>::PortType ProvidesPortType;
00422 typedef typename ProvidesPortType::DataManipulator DataManipulator;
00423
00424
00425 typedef typename DataManipulator::Type CorbaDataType;
00426 typedef typename DataManipulator::InnerType InnerType;
00427
00428 T1_without_extent const & _data = data;
00429
00430 CalciumTypes::DependencyType _dependencyType=
00431 static_cast<CalciumTypes::DependencyType>(dependencyType);
00432
00433 #ifdef MYDEBUG
00434 std::cerr << "-------- CalciumInterface(ecriture) MARK 1 ------------------" << std::endl;
00435 #endif
00436 if ( nomVar.empty() )
00437 {
00438 Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,"",CPMESSAGE[CalciumTypes::CPNMVR],"");
00439 throw CalciumException(CalciumTypes::CPNMVR, LOC("Empty variable name"));
00440 }
00441 UsesPortType * port;
00442 #ifdef MYDEBUG
00443 std::cout << "-------- CalciumInterface(ecriture) MARK 2 ---"<<typeid(port).name()<<"---------------" << std::endl;
00444 T1 t1;
00445 T2 t2;
00446 std::cout << "-------- CalciumInterface(ecriture) MARK 2b1 -----" << typeid(t1).name() << "-------------" << std::endl;
00447 std::cout << "-------- CalciumInterface(ecriture) MARK 2b2 -----" << typeid(t2).name() << "-------------" << std::endl;
00448 #endif
00449
00450 try
00451 {
00452 port = component.Superv_Component_i::get_port< UsesPortType > (nomVar.c_str());
00453 #ifdef MYDEBUG
00454 std::cout << "-------- CalciumInterface(ecriture) MARK 3 ------------------" << std::endl;
00455 #endif
00456 }
00457 catch ( const Superv_Component_i::PortNotDefined & ex)
00458 {
00459 Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPNMVR],ex.what());
00460 throw (CalciumException(CalciumTypes::CPNMVR,ex));
00461 }
00462 catch ( const Superv_Component_i::PortNotConnected & ex)
00463 {
00464 Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPLIEN],ex.what());
00465 throw (CalciumException(CalciumTypes::CPLIEN,ex));
00466
00467 }
00468 catch ( const Superv_Component_i::BadCast & ex)
00469 {
00470 Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPTPVR],ex.what());
00471 throw (CalciumException(CalciumTypes::CPTPVR,ex));
00472 }
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487 if ( _dependencyType == CalciumTypes::UNDEFINED_DEPENDENCY )
00488 {
00489 Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPIT],"Dependency mode is undefined");
00490 throw CalciumException(CalciumTypes::CPIT, LOC(OSS()<<"Dependency mode of variable " << nomVar << " is undefined."));
00491 }
00492
00493 if ( _dependencyType == CalciumTypes::SEQUENCE_DEPENDENCY )
00494 {
00495 Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPIT],
00496 "SEQUENCE_DEPENDENCY mode is not possible when writing");
00497 throw CalciumException(CalciumTypes::CPIT, LOC(OSS()<<"Dependency mode SEQUENCE_DEPENDENCY for variable " << nomVar
00498 << " is not possible when writing."));
00499 }
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512 if ( bufferLength < 1 )
00513 {
00514 Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPNTNULL],"Buffer to send is empty");
00515 throw CalciumException(CalciumTypes::CPNTNULL, LOC(OSS()<<"Buffer to send is empty"));
00516 }
00517
00518 CorbaDataType corbaData;
00519 #ifdef MYDEBUG
00520 std::cout << "-------- CalciumInterface(ecriture) MARK 4 ------------------" << std::endl;
00521 #endif
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 #ifdef MYDEBUG
00544 T1_without_extent t1b;
00545 InnerType t2b;
00546 std::cout << "-------- CalciumInterface(ecriture) MARK 4b1 -----" << typeid(t1b).name() << "-------------" << std::endl;
00547 std::cout << "-------- CalciumInterface(ecriture) MARK 4b2 -----" << typeid(t2b).name() << "-------------" << std::endl;
00548 #endif
00549
00550 Copy2CorbaSpace<IsSameType<T1_without_extent,InnerType>::value, DataManipulator >::apply(corbaData,_data,bufferLength);
00551
00552
00553 if ( _dependencyType == CalciumTypes::TIME_DEPENDENCY )
00554 {
00555 try
00556 {
00557 port->put(*corbaData,t, -1);
00558 std::stringstream msg;
00559 msg << "t=" << t ;
00560 Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPOK],msg.str().c_str());
00561 }
00562 catch ( const DSC_Exception & ex)
00563 {
00564 Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPATAL],ex.what());
00565 throw (CalciumException(CalciumTypes::CPATAL,ex.what()));
00566 }
00567
00568
00569 #ifdef MYDEBUG
00570 std::cout << "-------- CalciumInterface(ecriture) MARK 5 ------------------" << std::endl;
00571 #endif
00572 }
00573 else if ( _dependencyType == CalciumTypes::ITERATION_DEPENDENCY )
00574 {
00575 try
00576 {
00577 port->put(*corbaData,-1, i);
00578 std::stringstream msg;
00579 msg << "i=" << i ;
00580 Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPOK],msg.str().c_str());
00581 }
00582 catch ( const DSC_Exception & ex)
00583 {
00584 Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPATAL],ex.what());
00585 throw (CalciumException(CalciumTypes::CPATAL,ex.what()));
00586 }
00587 #ifdef MYDEBUG
00588 std::cout << "-------- CalciumInterface(ecriture) MARK 6 ------------------" << std::endl;
00589 #endif
00590 }
00591
00592 #ifdef MYDEBUG
00593 std::cout << "-------- CalciumInterface(ecriture), Valeur de corbaData : " << std::endl;
00594 for (int i = 0; i < corbaData->length(); ++i)
00595 std::cout << "-------- CalciumInterface(ecriture), corbaData[" << i << "] = " << (*corbaData)[i] << std::endl;
00596 #endif
00597
00598
00599
00600 delete corbaData;
00601
00602 #ifdef MYDEBUG
00603 std::cout << "-------- CalciumInterface(ecriture) MARK 7 ------------------" << std::endl;
00604 #endif
00605
00606 return;
00607 };
00608
00609 template <typename T1> static void
00610 ecp_ecriture ( Superv_Component_i & component,
00611 int const & dependencyType,
00612 double const & t,
00613 long const & i,
00614 const std::string & nomVar,
00615 size_t bufferLength,
00616 T1 const & data )
00617 {
00618 ecp_ecriture<T1,T1> (component,dependencyType,t,i,nomVar,bufferLength,data);
00619 };
00620
00621 };
00622
00623 #endif