Tuesday, July 31, 2007

CORBA Tutorial

1.
Obtaining an Object Reference
template
typename T::_ptr_type /* trait */
resolve_service (const char *n,
CosNaming::NamingContextExt_ptr name_context) {
CosNaming::Name svc_name;
svc_name.length (1); svc_name[0].id = n;
svc_name[0].kind = "object impl";
// Find object reference in the name service.
obj = name_context->resolve (svc_name);
// Can also use
// obj = name_context->resolve_str (n);
// Narrow to the T interface and away we go!

2.
Stock Quoter Client Program (2/3)
try { // Use a factory to resolve any quoter.
Stock::Quoter_Factory_var qf =
resolve_service
("my quoter factory", name_context.in ());
if (CORBA::is_nil (qf.in ())) return 0;
CosLifeCycle::Key key; key.length (1);
key[0].id = "my quoter";
// Find a quoter and invoke the call.
CORBA::Object_var obj = qf->create_object (key);
quoter = Stock::Quoter::_narrow (obj);
stock_name = CORBA::string_dup ("ACME ORB Inc.");
CORBA::Long value = quoter->get_quote (stock_name);

3.
CORBA Quoter Example
int main (void)
{
// Use a factory to bind
// to a Quoter.
Quoter_var quoter =
resolve_quoter_service ();
const char *name =
"ACME ORB Inc.";
CORBA::Long value =
quoter->get_quote (name);
cout << name << " = "
<< value << endl;

4.
New Formats
For example, percentage that stock increased or decreased since start of trading day, volume of trades, etc.
module Stock
{
// ...
interface Quoter
{
long get_quote (in string stock_name,
out double percent_change,
out long trading_volume)
raises (Invalid_Stock);
};
};
Note that even making this simple change would involve a great deal of work for a sockets-based solution..

5.
New Interfaces and Operations
For example, adding a trading interface
module Stock {
// Interface Quoter_Factory and Quoter same as before.
interface Trader {
void buy (in string name,
inout long num_shares,
in long max_value) raises (Invalid_Stock);
// sell() operation is similar...
};
interface Trader_Factory { /* ... */ };
};
Multiple inheritance is also useful to define a full service broker:

6.
Canonical Steps to Obtain the Root POA
// ORB is ‘‘locality constrained’’
CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
// Root POA is the default POA (locality constrained)
CORBA::Object_var obj =
orb->resolve_initial_references ("RootPOA");
// Type-safe downcast.
PortableServer::POA_var root_poa
= PortableServer::POA::_narrow (obj.in ());
// Activate the POA.
PortableServer::POA_Manager_var poa_manager =
root_poa->the_POAManager ();
poa_manager->activate ();
// FMM 2
// root_poa->the_POAManager ()->activate ();
interface Full_Service_Broker : Stat_Quoter, Trader {};
Note that you can’t inherit the same operation from more than one interface

No comments: