From fb567bb3f80fdbed3e4ba45229843475a7a0dff6 Mon Sep 17 00:00:00 2001 From: srn Date: Tue, 26 Dec 2006 11:27:43 +0000 Subject: [PATCH] Added methods getErrorCode and isDone. Changed the error treatment. Now in the case if error appears the corresponding error code is set. --- src/CALCULATOR/CALCULATOR.cxx | 237 ++++++++++++++++++++++++++-------- src/CALCULATOR/CALCULATOR.hxx | 8 +- 2 files changed, 190 insertions(+), 55 deletions(-) diff --git a/src/CALCULATOR/CALCULATOR.cxx b/src/CALCULATOR/CALCULATOR.cxx index d74f4fd..e777196 100644 --- a/src/CALCULATOR/CALCULATOR.cxx +++ b/src/CALCULATOR/CALCULATOR.cxx @@ -43,6 +43,8 @@ CALCULATOR::CALCULATOR(CORBA::ORB_ptr orb, MESSAGE("activate object"); _thisObj = this ; _id = _poa->activate_object(_thisObj); + + _errorCode = CALCULATOR_ORB::NO_ERROR; } CALCULATOR::~CALCULATOR() @@ -52,12 +54,27 @@ CALCULATOR::~CALCULATOR() CORBA::Double CALCULATOR::norm2(SALOME_MED::FIELDDOUBLE_ptr field1) { beginService( "CALCULATOR::norm2"); + _errorCode = CALCULATOR_ORB::NO_ERROR; BEGIN_OF("CALCULATOR::Norm2(SALOME_MED::FIELDDOUBLE_ptr field1)"); - // Create a local field from corba field, apply method normMax on it. - // When exiting the function, f1 is deleted, and with it the remote corba field. - TFieldDouble_c f1(field1); - CORBA::Double norme = f1.norm2(); - END_OF("CALCULATOR::Norm2(SALOME_MED::FIELDDOUBLE_ptr field1)"); + + if(CORBA::is_nil(field1)) { + _errorCode = CALCULATOR_ORB::INVALID_FIELD; + return 0.0; + } + + CORBA::Double norme = 0.0; + // Create a local field from corba field, apply method normMax on it. + // When exiting the function, f1 is deleted, and with it the remote corba field. + TFieldDouble_c f1(field1); + + try { + norme = f1.norm2(); + } + catch(...) { + _errorCode = CALCULATOR_ORB::EXCEPTION_RAISED; + } + + END_OF("CALCULATOR::Norm2(SALOME_MED::FIELDDOUBLE_ptr field1)"); endService( "CALCULATOR::norm2"); return norme; } @@ -65,13 +82,34 @@ CORBA::Double CALCULATOR::norm2(SALOME_MED::FIELDDOUBLE_ptr field1) CORBA::Double CALCULATOR::normL2(SALOME_MED::FIELDDOUBLE_ptr field1) { beginService( "CALCULATOR::normL2"); - BEGIN_OF("CALCULATOR::NormL2(SALOME_MED::FIELDDOUBLE_ptr field1)"); + _errorCode = CALCULATOR_ORB::NO_ERROR; + BEGIN_OF("CALCULATOR::NormL2(SALOME_MED::FIELDDOUBLE_ptr field1)"); + + if(CORBA::is_nil(field1)) { + _errorCode = CALCULATOR_ORB::INVALID_FIELD; + return 0.0; + } + + //Check that the Field is not on the Nodes (a limitation of normL2) + SALOME_MED::SUPPORT_var aSupport = field1->getSupport(); + if(CORBA::is_nil(aSupport) || aSupport->getEntity() == SALOME_MED::MED_NODE) { + _errorCode = CALCULATOR_ORB::NOT_COMPATIBLE; + return 0.0; + } // Create a local field (on the stack) from corba field, apply method normMax on it. // When exiting the function, FIELDClient f1 is deleted, and with it the remote corba field. TFieldDouble_c f1(field1); - CORBA::Double norme = f1.normL2(); - // Send a notification message to supervision + + CORBA::Double norme = 0.0; + try { + norme = f1.normL2(); + } + catch(...) { + _errorCode = CALCULATOR_ORB::EXCEPTION_RAISED; + } + + // Send a notification message to supervision ostringstream message("CALCULATOR::normL2 : "); message << norme; sendMessage("warning",message.str().c_str()); @@ -83,13 +121,28 @@ CORBA::Double CALCULATOR::normL2(SALOME_MED::FIELDDOUBLE_ptr field1) CORBA::Double CALCULATOR::normMax(SALOME_MED::FIELDDOUBLE_ptr field1) { beginService( "CALCULATOR::normMax"); + _errorCode = CALCULATOR_ORB::NO_ERROR; BEGIN_OF("CALCULATOR::NormMax(SALOME_MED::FIELDDOUBLE_ptr field1)"); - // An other way to do it : create an local field on the heap, inside an auto_ptr. - // When exiting the function, auto_ptr is deleted, and with it the local field and - // the remote field if ownership was transferred. - auto_ptr f1 (new TFieldDouble_c(field1) ); - CORBA::Double norme = f1->normMax(); - END_OF("CALCULATOR::NormMax(SALOME_MED::FIELDDOUBLE_ptr field1)"); + + if(CORBA::is_nil(field1)) { + _errorCode = CALCULATOR_ORB::INVALID_FIELD; + return 0.0; + } + + CORBA::Double norme = 0.0; + // An other way to do it : create an local field on the heap, inside an auto_ptr. + // When exiting the function, auto_ptr is deleted, and with it the local field and + // the remote field if ownership was transferred. + auto_ptr f1 (new TFieldDouble_c(field1) ); + + try { + norme = f1->normMax(); + } + catch(...) { + _errorCode = CALCULATOR_ORB::EXCEPTION_RAISED; + } + + END_OF("CALCULATOR::NormMax(SALOME_MED::FIELDDOUBLE_ptr field1)"); endService( "CALCULATOR::normMax"); return norme; } @@ -97,9 +150,32 @@ CORBA::Double CALCULATOR::normMax(SALOME_MED::FIELDDOUBLE_ptr field1) CORBA::Double CALCULATOR::normL1(SALOME_MED::FIELDDOUBLE_ptr field1) { beginService( "CALCULATOR::normL1"); + _errorCode = CALCULATOR_ORB::NO_ERROR; BEGIN_OF("CALCULATOR::NormL1(SALOME_MED::FIELDDOUBLE_ptr field1)"); - auto_ptr f1 (new TFieldDouble_c(field1) ); - CORBA::Double norme = f1->normL1(); + + if(CORBA::is_nil(field1)) { + _errorCode = CALCULATOR_ORB::INVALID_FIELD; + return 0.0; + } + + //Check that the Field is not on the Nodes (a limitation of normL1) + SALOME_MED::SUPPORT_var aSupport = field1->getSupport(); + + if(CORBA::is_nil(aSupport) || aSupport->getEntity() == SALOME_MED::MED_NODE) { + _errorCode = CALCULATOR_ORB::NOT_COMPATIBLE; + return 0.0; + } + + CORBA::Double norme = 0.0; + auto_ptr f1 (new TFieldDouble_c(field1) ); + + try { + norme = f1->normL1(); + } + catch(...) { + _errorCode = CALCULATOR_ORB::EXCEPTION_RAISED; + } + END_OF("CALCULATOR::Norm2(SALOME_MED::FIELDDOUBLE_ptr field1)"); endService( "CALCULATOR::normL1"); return norme; @@ -109,17 +185,31 @@ CORBA::Double CALCULATOR::normL1(SALOME_MED::FIELDDOUBLE_ptr field1) SALOME_MED::FIELDDOUBLE_ptr CALCULATOR::applyLin(SALOME_MED::FIELDDOUBLE_ptr field1,CORBA::Double a,CORBA::Double b) { beginService( "CALCULATOR::applyLin"); + _errorCode = CALCULATOR_ORB::NO_ERROR; BEGIN_OF("applyLin(SALOME_MED::FIELDDOUBLE_ptr field1,CORBA::Double a,CORBA::Double b)"); - // create a local field on the heap, because it has to remain after exiting the function - TFieldDouble * f1 = new TFieldDouble_c(field1); - f1->applyLin(a,b); - // create servant from f1, give it the property of c++ field (parameter true). - // This imply that when the client will release it's field, it will delete NewField, - // and f1. - TFieldDouble_i * NewField = new TFieldDouble_i(f1,true) ; - // activate object - SALOME_MED::FIELDDOUBLE_ptr myFieldIOR = NewField->_this() ; + if(CORBA::is_nil(field1)) { + _errorCode = CALCULATOR_ORB::INVALID_FIELD; + return NULL; + } + + SALOME_MED::FIELDDOUBLE_ptr myFieldIOR = NULL; + // create a local field on the heap, because it has to remain after exiting the function + TFieldDouble * f1 = new TFieldDouble_c(field1); + TFieldDouble_i * NewField = NULL; + + try { + f1->applyLin(a,b); + // create servant from f1, give it the property of c++ field (parameter true). + // This imply that when the client will release it's field, it will delete NewField, + // and f1. + NewField = new TFieldDouble_i(f1,true) ; + // activate object + myFieldIOR = NewField->_this() ; + } + catch(...) { + _errorCode = CALCULATOR_ORB::EXCEPTION_RAISED; + } END_OF("applyLin(SALOME_MED::FIELDDOUBLE_ptr field1,CORBA::Double a,CORBA::Double b)"); endService( "CALCULATOR::applyLin"); @@ -127,25 +217,34 @@ SALOME_MED::FIELDDOUBLE_ptr CALCULATOR::applyLin(SALOME_MED::FIELDDOUBLE_ptr fie } SALOME_MED::FIELDDOUBLE_ptr CALCULATOR::add(SALOME_MED::FIELDDOUBLE_ptr field1, SALOME_MED::FIELDDOUBLE_ptr field2) - throw ( SALOME::SALOME_Exception ) { beginService( "CALCULATOR::add"); + _errorCode = CALCULATOR_ORB::NO_ERROR; BEGIN_OF("CALCULATOR::add(SALOME_MED::FIELDDOUBLE_ptr field1, SALOME_MED::FIELDDOUBLE_ptr field2)"); + + if(CORBA::is_nil(field1) || CORBA::is_nil(field2)) { + _errorCode = CALCULATOR_ORB::INVALID_FIELD; + return NULL; + } + // Create local fields from corba field TFieldDouble_c f1(field1); TFieldDouble_c f2(field2); // Create new c++ field on the heap by copying f1, add f2 to it. TFieldDouble* fres = new TFieldDouble(f1); - // catch exception for non compatible fields + + // catch exception for non compatible fields try { *fres+=f2; } catch(MEDEXCEPTION) { - throw(SALOME_Exception(LOCALIZED("Fields are not compatible"))); + _errorCode = CALCULATOR_ORB::NOT_COMPATIBLE; + return NULL; } + // create CORBA field from c++ toField. give property to servant (true) TFieldDouble_i * myFieldDoubleI = new TFieldDouble_i(fres,true); SALOME_MED::FIELDDOUBLE_ptr myFieldIOR = myFieldDoubleI->_this() ; @@ -160,8 +259,14 @@ void CALCULATOR::cloneField(SALOME_MED::FIELDDOUBLE_ptr field, SALOME_MED::FIELDDOUBLE_out clone3, SALOME_MED::FIELDDOUBLE_out clone4) { beginService( "CALCULATOR::cloneField"); + _errorCode = CALCULATOR_ORB::NO_ERROR; BEGIN_OF("CALCULATOR::cloneField"); + if(CORBA::is_nil(field)) { + _errorCode = CALCULATOR_ORB::INVALID_FIELD; + return; + } + // load local field, using MED ressource file pointe.med TFieldDouble_c f(field); @@ -192,7 +297,13 @@ void CALCULATOR::cloneField(SALOME_MED::FIELDDOUBLE_ptr field, void CALCULATOR::printField(SALOME_MED::FIELDDOUBLE_ptr field) { beginService( "CALCULATOR::printField"); - + _errorCode = CALCULATOR_ORB::NO_ERROR; + + if(CORBA::is_nil(field)) { + _errorCode = CALCULATOR_ORB::INVALID_FIELD; + return; + } + // Create a local field from corba field. // Use auto_ptr to perform automatic deletion after usage. // The deletion of the FIELDClient will delete the remote Corba object. @@ -255,42 +366,62 @@ void CALCULATOR::printField(SALOME_MED::FIELDDOUBLE_ptr field) CORBA::Double CALCULATOR::convergenceCriteria(SALOME_MED::FIELDDOUBLE_ptr field) { beginService( "CALCULATOR::convergenceCriteria"); + _errorCode = CALCULATOR_ORB::NO_ERROR; BEGIN_OF("CALCULATOR::convergenceCriteria(SALOME_MED::FIELDDOUBLE_ptr field)"); + if(CORBA::is_nil(field)) { + _errorCode = CALCULATOR_ORB::INVALID_FIELD; + return 0.0; + } + double criteria=1; - static auto_ptr fold(0); - auto_ptr fnew (new TFieldDouble_c(field) ); - if (fold.get() == NULL) // if old field is not set, set it and return 1 - fold=fnew; - else - { - // if size of fields are not equal, return 1 - const int size=fold->getNumberOfValues()*fold->getNumberOfComponents(); - if ( size == fnew->getNumberOfValues()*fnew->getNumberOfComponents() ) - { - MED_EN::medModeSwitch mode=fold->getInterlacingType(); // storage mode - const double* oldVal= fold->getValue(); // retrieve values - const double* newVal= fnew->getValue(); - criteria=0.0; - double ecart_rel=0.0; - for (unsigned i=0; i!=size; ++i) // compute criteria - { - if ( oldVal[i] != 0.0) + static auto_ptr fold(0); + auto_ptr fnew (new TFieldDouble_c(field) ); + + try { + if (fold.get() == NULL) // if old field is not set, set it and return 1 + fold=fnew; + else { - ecart_rel = std::abs( (oldVal[i]-newVal[i])/oldVal[i] ); - if ( ecart_rel>criteria ) - criteria=ecart_rel; + // if size of fields are not equal, return 1 + const int size=fold->getNumberOfValues()*fold->getNumberOfComponents(); + if ( size == fnew->getNumberOfValues()*fnew->getNumberOfComponents() ) + { + //MED_EN::medModeSwitch mode=fold->getInterlacingType(); // storage mode + const double* oldVal= fold->getValue(); // retrieve values + const double* newVal= fnew->getValue(); + criteria=0.0; + double ecart_rel=0.0; + for (unsigned i=0; i!=size; ++i) // compute criteria + { + if ( oldVal[i] != 0.0) + { + ecart_rel = std::abs( (oldVal[i]-newVal[i])/oldVal[i] ); + if ( ecart_rel>criteria ) + criteria=ecart_rel; + } + } + } } - } - } - } + catch(...) { + _errorCode = CALCULATOR_ORB::EXCEPTION_RAISED; + } endService( "CALCULATOR::convergenceCriteria"); END_OF("CALCULATOR::convergenceCriteria(SALOME_MED::FIELDDOUBLE_ptr field1)"); return criteria; } +CORBA::Boolean CALCULATOR::isDone() +{ + return (_errorCode == CALCULATOR_ORB::NO_ERROR); +} + +CALCULATOR_ORB::ErrorCode CALCULATOR::getErrorCode() +{ + return _errorCode; +} extern "C" diff --git a/src/CALCULATOR/CALCULATOR.hxx b/src/CALCULATOR/CALCULATOR.hxx index 3536850..b714af9 100644 --- a/src/CALCULATOR/CALCULATOR.hxx +++ b/src/CALCULATOR/CALCULATOR.hxx @@ -43,11 +43,15 @@ public: CORBA::Double norm2(SALOME_MED::FIELDDOUBLE_ptr field1); CORBA::Double normL1(SALOME_MED::FIELDDOUBLE_ptr field1); SALOME_MED::FIELDDOUBLE_ptr applyLin(SALOME_MED::FIELDDOUBLE_ptr field1,CORBA::Double a,CORBA::Double b); - SALOME_MED::FIELDDOUBLE_ptr add(SALOME_MED::FIELDDOUBLE_ptr field1, SALOME_MED::FIELDDOUBLE_ptr field2) - throw ( SALOME::SALOME_Exception ); + SALOME_MED::FIELDDOUBLE_ptr add(SALOME_MED::FIELDDOUBLE_ptr field1, SALOME_MED::FIELDDOUBLE_ptr field2); void printField(SALOME_MED::FIELDDOUBLE_ptr field); void cloneField(SALOME_MED::FIELDDOUBLE_ptr field, SALOME_MED::FIELDDOUBLE_out clone1, SALOME_MED::FIELDDOUBLE_out clone2, SALOME_MED::FIELDDOUBLE_out clone3, SALOME_MED::FIELDDOUBLE_out clone4); + CORBA::Boolean isDone(); + CALCULATOR_ORB::ErrorCode getErrorCode(); + +protected: + CALCULATOR_ORB::ErrorCode _errorCode; }; -- 2.30.2