To demonstrate use of the Subject Manager, we begin by revisiting the main() function examined in the previous section. This time, we will focus our attention on the code related to the Subject Manager only. We will also explain how to use the extended API of the Subject Manager implementation.
It is important to know that the Subject Manager is a CORBA object that can be accessed by remote code. In the following example, the methods used are defined in the class tweek::SubjectManagerImpl, the C++ implementation of the tweek::SubjectManager interface. The details of how the Subject Manager is handled through CORBA are largely irrelevant for most users of Tweek. Simply bear in mind that the Subject Manager is accessibly remotely and that it simplifies the use of CORBA in general.
In order to use the Tweek Subject Manager, it must be initialized. Each CORBA Manager should have a single Subject Manager associated with it. If not, use of Tweek will be much more difficult because the CORBA Manager and the Subject Manager together hide most of the details relating to the use of CORBA. The following example shows how to initialize the Subject Manager using the CORBA Manager object created earlier.
Example 4.4. 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("corba_test", 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(custom_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;
} | After the COBRA Manager has been initialized successfully, the Tweek Subject Manager must be created. We do this by invoking the method tweek::CorbaManager::createSubjectManager() on our tweek::CorbaManager instance. |
| Once we have a valid Subject Manager, we must register subjects with it in order for object references to be passed out by CORBA. This creates the servant to which mymod::CustomSubject references will be made. |
| Once the servant is created, it is registered with the Subject Manager. The Subject Manager will activate the servant within the POA so that references to it can be created and returned to clients. We register it with the symbolic name “CustomSubject” that can be referenced later by remote objects. |
| After the subject is registered, all the work is done. This simple application now just waits for clients to request references. It will exit when the user enters 'x'. |
Note that all the code relating to the Subject Manager is enclosed within another try/catch block. This block only catches exceptions of type CORBA::Exception. Anything more general is caught by the larger try/catch block.
The class tweek::SubjectManagerImpl has some methods that are not part of the IDL-specified interface. One such method is registerSubject(), which was used in the preceding example. Some other methods that may be of interest to users are described in the following subsections.
public void setApplicationName(const std::string& appName);This method can be used to set one of the application-specific identifiers within the Subject Manager. Namely, it sets the application name identifier. These identifiers are used to aid users in choosing a Subject Manager instance when making remote connections to applications.
public void setUserName(const std::string& userName);Similar to setApplicationName(), this method allows users to tell the Subject Manager their user name. When multiple users are all running the same application (and are thus using the same parameter to setApplicationName()), this provides another level of uniqueness. If this method is not used, the Subject Manager will try to get the user name through the $USER environment variable.
public void addInfoItem(const std::string& key,
const std::string& value);If the previous two built-in Subject Manager identifiers are not enough, the method addInfoItem() allows users to define their own unique identifiers. The first parameter is the identifier, and the second is its (ideally) unique value. Users are free to add any key/value pairs they need in order to aid in the selection of a Subject Manager at runtime.