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