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 10 * of the test. */ 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
{ 20 if ( mgr.init("example", argc, argv).success() )
{ 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 (...)
{ 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; }
| 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
|
| Next, we must initialize the CORBA Manager using the
method |
| To ensure that no exceptions go uncaught, we enclose the
bulk of |