Using the CORBA Manager

Initializing the CORBA Manager is straightforward, but it does require exception handling. If the exceptions are not handled correctly, applications will abort if an exception is thrown but not caught. Refer to a C++ reference for more information about exceptions and exception handling in C++.

The following example shows a main() function for an application that performs all the Tweek initialization steps. We separate the discussion into two parts: one part for the CORBA Manager and one part for the Subject Manager (discussed in the next section).

Example 4.3. TweekApp.cpp

  1 #include <vpr/vpr.h>
    #include <vpr/Thread/Thread.h>
    #include <vpr/Util/Debug.h>
    #include <tweek/CORBA/CorbaManager.h>                         1
  5 
    #include <CustomSubjectImpl.h>                                2
    
    /**
     * This application starts the CORBA server for the C++ side
 10  * of the test.
     */
    int main(int argc, char* argv[])
    {
       tweek::CorbaManager mgr;                                   3
 15 
       // The first thing we have to do is initialize the Tweek
       // CORBA Manager.  If this fails, we're out of luck.
       try                                                        4
       {
 20       if ( mgr.init("example", argc, argv).success() )        5
          {
             vpr::ReturnStatus status;
    
             // Once the CORBA Manager is initialized, we need
 25          // to create a Subject Manager.  This will hold our
             // CustomSubject object.
             try
             {
                status = mgr.createSubjectManager();
 30 
                // If we were able to create the Subject Manager,
                // now we register our objects with it.
                if ( status.success() )
                {
 35                // First, create real instances of the C++
                   // object that will be the CORBA servant.  This
                   // must be allocated on the heap.
                   mymod::CustomubjectImpl* custom_subj =
                      new mymod::CustomSubjectImpl();
 40 
                   // Now we try to register the subject and give
                   // it a symbolic, easy-to-remember name.
                   try
                   {
 45                   mgr.getSubjectManager()->
                         registerSubject(slider_subj, "CustomSubject");
                   }
                   catch (...)
                   {
 50                   vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
                         << "Failed to register subject\n"
                         << vprDEBUG_FLUSH;
                   }
    
 55                // We are done with our pointer to the servant.
                   slider_subj->_remove_ref();
                }
             }
             catch (CORBA::Exception& ex)
 60          {
                vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
                   << "Caught an unknown CORBA exception when "
                   << "trying to register!\n" << vprDEBUG_FLUSH;
             }
 65 
             if ( ! status.success() )
             {
                vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
                   << "Failed to register Subject Manager instance\n"
 70                << vprDEBUG_FLUSH;
             }
    
             std::cout << "Press 'x' to exit" << std::endl;
             char input;
 75 
             // Loop forever so that we can act sort of like
             // a server.
             while ( 1 )
             {
 80             std::cin >> input;
                if ( input == 'x' )
                {
                   break;
                }
 85             else
                {
                   vpr::System::msleep(100);
                }
             }
 90       }
          else
          {
             vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
                << "CORBA failed to initialize\n"
 95             << vprDEBUG_FLUSH;
          }
       }
       catch (...)                                               6
       {
100       vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
             << "Caught an unknown exception!\n"
             << vprDEBUG_FLUSH;
       }
    
105    vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
          << "Exiting\n" << vprDEBUG_FLUSH;
    
       return 0;
    }
1 2

These two headers are typically needed. The first includes the declaration of the Tweek CORBA Manager, and the second is the subject implementation declaration, shown in Example 4.1, “CustomSubjectImpl.h”.

3

In order to use CORBA through Tweek, the CORBA Manager must be created and initialized. Any number of these may be created, but in general, only one is needed per application. Here, we declare an instance of tweek::CorbaManager on the stack.

5

Next, we must initialize the CORBA Manager using the method tweek::CorbaManager::init(). The first argument provides a unique (ideally) identifier for the local Portable Object Adapter (POA). The second and third arguments are argc and argv respectively. They come in through the argument list of main() and represent the command-line arguments. Any arguments relevant to ORB initialization are removed from argv, and argc is decremented accordingly (it is passed by reference).

4 6

To ensure that no exceptions go uncaught, we enclose the bulk of main() in a try/catch block that catches any exception. This is handled by the argument list passed to the catch block, (...). This is the equivalent of catching java.lang.Exception in Java.