看一个例子吧:
1.IDL
module BankDemo
{
typedef float CashAmount; // Type for representing cash
typedef string AccountId; // Type for representing account ids
// Forward declaration of Account
//
interface Account;
// Bank interface...used to create Accounts
//
interface Bank
{
exception AccountAlreadyExists { AccountId account_id; };
exception AccountNotFound { AccountId account_id; };
Account find_account(
in AccountId account_id
) raises(AccountNotFound);
Account create_account(
in AccountId account_id,
in CashAmount initial_balance
) raises (AccountAlreadyExists);
void shutdown_bank();
};
// Account interface...used to deposit, withdraw, and query
// available funds.
//
interface Account
{
exception InsufficientFunds {};
readonly attribute AccountId account_id;
readonly attribute CashAmount balance;
void withdraw( in CashAmount amount ) raises (InsufficientFunds);
void deposit( in CashAmount amount );
};
};
2.客户端
1).得到Bank对象(factory and stateless)的引用:
CORBA::Object_var bank_obj = orb->string_to_object(ior);
BankDemo::Bank_var bank;
try
{
bank = BankDemo::Bank::_narrow(bank_obj);
}
2).创建account对象(product and stateful)
BankDemo::Account_var account = bank->create_account(name, amount);
3).查找account对象
BankDemo::Account_var account = m_bank->find_account(name);
3.服务端
1).创建account对象
BankDemo::Account_ptr
BankImpl::create_account(
const char* account_id,
BankDemo::CashAmount initial_balance
) IT_THROW_DECL((
CORBA::SystemException,
BankDemo::Bank::AccountAlreadyExists
))
{
// Create a new account in the database, then return a new
// reference to that account.
//
if (!m_account_db.create_account(account_id, initial_balance))
{
throw BankDemo::Bank::AccountAlreadyExists(account_id);
}
return create_account_ref(account_id);
}
BankDemo::Account_ptr
BankImpl::create_account_ref(
const char* account_id
) IT_THROW_DECL((CORBA::SystemException))
{
// Convert the account id into an object id
//
PortableServer::ObjectId_var oid =
PortableServer::string_to_ObjectId(account_id);
// Create a new untyped reference for that account
//
CORBA::Object_var account_obj = m_account_poa->create_reference_with_id(
oid,
BankDemo::_tc_Account->id()
);
// Return the reference, narrowed to the Account type
//
return BankDemo::Account::_narrow(account_obj);
}
2).需要重点注意的是bank与account的POA采用不同的策略,一般采用servant locator 和efault Servant两 种方式,下面的采用Servant LOcator 方式,看看servantlocator的实现方式:
PortableServer::Servant
AccountServantLocatorImpl::preinvoke(
const PortableServer::ObjectId & oid,
PortableServer:: POA_ptr adapter,
const char* operation,
PortableServer::ServantLocator::Cookie& the_cookie
) IT_THROW_DECL((CORBA::SystemException, PortableServer::ForwardRequest))
{
// Just create a new account implementation
//
CORBA::String_var account_id = PortableServer::ObjectId_to_string(oid);
cout << "calling preinvoke for " << account_id << endl;
return new SingleAccountImpl(account_id, m_account_db);
}
// postinvoke()
//
// A ServantLocator receives a call to postinvoke whenever a request
// is finished. postinvoke is responsible for performing any per-request
// cleanup, such as deleting the servant handling the request, ending
// transactions, flushing database handles, etc.
//
// This implementation simply writes the account information to the database,
// then deletes the servant.
//
//
void
AccountServantLocatorImpl::postinvoke(
const PortableServer::ObjectId & oid,
PortableServer:: POA_ptr adapter,
const char* operation,
PortableServer::ServantLocator::Cookie the_cookie,
PortableServer::Servant the_servant
) IT_THROW_DECL((CORBA::SystemException))
{
// Delete the servant...the servant will take care of writing to disk
//
CORBA::String_var account_id = PortableServer::ObjectId_to_string(oid);
cout << "calling postinvoke for " << account_id << endl;
delete the_servant;
}
这个例子与你的是不是有点相象,关于如何再找到你说的emplyee对象,我想重点需要用到servant locator程序。
|