[omniORB] omniORB 4.3: exceptionIdInAny config parameter

pascal.moucheront at univ-eiffel.fr pascal.moucheront at univ-eiffel.fr
Thu May 19 20:45:18 UTC 2022


Hello,
please find below a more streamlined test showing the problem,
directly derived from omniORB's simplest 'echo' example.

Best Regards

Pascal Moucheront

-----------------------------------------------------------------------
IDL File
-----------------------------------------------------------------------
#ifndef __ECHOEXCEPT1_IDL__
#define __ECHOEXCEPT1_IDL__

exception MyExcept1
{
    string reason;
};

interface EchoE1 {
  string echo(in string mesg) raises(MyExcept1);
};

#endif  // __ECHOEXCEPT1_IDL__

-----------------------------------------------------------------------
Test Example
-----------------------------------------------------------------------
// eg1e.cc - Modified example 1 to show ExceptionIdInAny problem
//
// compiled with
// g++ -o eg1e -O3 -Wall -Wno-unused -fexceptions \
//     -L../../../lib \
//     -L/home/mouchero/branches/tango/tango-cc/omniorb/lib \
//     eg1e.o \
//     ../../../stub/echoSK.o \
//     ../../../stub/echoExceptSK.o \
//     ../../../stub/echoExcept1SK.o \
//     ../../../stub/echoDynSK.o \
//     ../../../stub/echoExceptDynSK.o \
//     ../../../stub/echoExcept1DynSK.o \
//     -lomniORB4 -lomniDynamic4 -lomnithread -lpthread
//
//
// Usage: eg1e
//
// try with ExceptionIdInAny = 0 and ExceptionIdInAny = 1 in config file.

#include <echoExcept1.hh>

#include <iostream>
#include <cstring>
#include <unistd.h>
using namespace std;

// This is the object implementation.

class EchoE1_i : public POA_EchoE1
{
public:
  inline EchoE1_i() {}
  virtual ~EchoE1_i() {}
  virtual char* echo(const char* mesg);
};


char* EchoE1_i::echo(const char* mesg)
{
  int th = strncmp(mesg,"NO",2);
  if( th == 0 )
  {
    // Memory management rules say we must return a newly allocated
    // string.
    return CORBA::string_dup(mesg);
  }
  else
  {
    throw MyExcept1("Server throws");
  }
}


//////////////////////////////////////////////////////////////////////

// This function acts as a client to the object.

static void hello(EchoE1_ptr e)
{
  if( CORBA::is_nil(e) ) {
    cerr << "hello: The object reference is nil!" << endl;
    return;
  }

  CORBA::String_var src = (const char*) "Hello!";
  // String literals are (char*) rather than (const char*) on some
  // old compilers.  Thus it is essential to cast to (const char*)
  // here to ensure that the string is copied, so that the
  // CORBA::String_var does not attempt to 'delete' the string
  // literal.

  // asynchronous request
  CORBA::Request_ptr request = e->_request("echo");
  request->add_in_arg() <<= src;
  request->set_return_type(CORBA::_tc_string);
  request->exceptions()->add(_tc_MyExcept1);
  request->send_deferred();

  sleep(1);

  // get reply
  if( request->poll_response() )
  {
    CORBA::Environment_ptr env = request->env();
    if( CORBA::is_nil(env) ) {
      cerr << "hello: The request environment is nil!" << endl;
      return;
    }
    if( env->exception() == NULL ) {
      CORBA::Any val = request->return_value();
      cout << "I said, \"" << (char*)src << "\"." << endl;
      const char* reply;
      if (val >>= reply) {
        cout << "The Echo object replied, \"" << reply <<"\"." << endl;
      }
      else {
        cout << "The Echo object replied an unknown value with "
             << "TypeCode.kind() = " << val.type()->kind() << endl;
      }
    }
    else {
      CORBA::Exception* ex_ptr = env->exception();
      CORBA::UnknownUserException* unk_ex;
      if ((unk_ex = CORBA::UnknownUserException::_downcast(ex_ptr)) != NULL)
      {
        const MyExcept1* my_ex;
        if( unk_ex->exception() >>= my_ex ) {
          cout << "Operation failed, and raised the expected "
               << "MyExcept1 exception!. Rethrowing." << endl;
          MyExcept1 ex(*my_ex);
          throw ex;
        }
        else {
          cout << "Operation failed, but.did not raise the expected "
               << "MyExcept1 exception!" << endl;
        }
      }
      else {
        ex_ptr->_raise();
      }
    }
  }
  else {
    cout << "Repy not yet arrived" << endl;
  }
}

//////////////////////////////////////////////////////////////////////

int main(int argc, char** argv)
{
  try {
    // Initialise the ORB.
    CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

    // Obtain a reference to the root POA.
    CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
    PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);

    // We allocate the servant (implementation object) on the heap.
    // The servant is reference counted. We start out holding a
    // reference, and when the object is activated, the POA holds
    // another reference. The PortableServer::Servant_var<> template
    // automatically releases our reference when it goes out of scope.
    PortableServer::Servant_var<EchoE1_i> myecho = new EchoE1_i();

    // Activate the object. This tells the POA that this object is
    // ready to accept requests.
    PortableServer::ObjectId_var myechoid = poa->activate_object(myecho);

    // Obtain a reference to the object.
    EchoE1_var myechoref = myecho->_this();

    // Obtain a POAManager, and tell the POA to start accepting
    // requests on its objects.
    PortableServer::POAManager_var pman = poa->the_POAManager();
    pman->activate();

    // Do the client-side call.
    hello(myechoref);

    // Clean up all the resources.
    orb->destroy();
  }
  catch (CORBA::SystemException& ex) {
    cerr << "Caught CORBA::" << ex._name() << endl;
  }
  catch (CORBA::Exception& ex) {
    cerr << "Caught CORBA::Exception: " << ex._name() << endl;
  }
  return 0;
}



More information about the omniORB-list mailing list