0

In the example of TAO/example/Simple/Bank, the two idl methods: open and close are defined in the AccountManager, the former is to generate a new activated Account servant while the latter is to recycle it . The AccountManager_i is like:

Bank::Account_ptr
AccountManager_i::open (const char *name,
                    CORBA::Float initial_balance)
{
     Account_i_var result;
     if (hash_map_.find (name, result) != 0)
     {
         Account_i *tmp = 0;
         ACE_NEW_THROW_EX (tmp,
                    Account_i (name,
                               initial_balance),
                    CORBA::NO_MEMORY ());
         result = tmp;
     }
    // Generate an IOR for the result object and register it with the
   // POA.  In case the object already exists then the previously
   // generated IOR is returned.
   return result->_this ();
 }

// Shutdown.
void
AccountManager_i::close (Bank::Account_ptr account)
{
  try
    {
     CORBA::String_var name = account->name ();
     Account_i_var account;
     ..
     if (account.is_nil ())
     {
      PortableServer::POA_var poa = account->_default_POA ();

      PortableServer::ObjectId_var id = poa->servant_to_id (account.in ());

      poa->deactivate_object (id.in ());
    }
   }
   catch (const CORBA::Exception& ex)
  {
     ex._tao_print_exception ("Unable to close Account\n");
  }
}

The question is 1) Is result(new created account servant)shares same ORB object with AccountManager_i in the open method? How can i reset it with a new duplicated ORB for this servant?

2) When did account(in Bank::Account_ptr account) object is recycled in the close method. In the method, it is only deactivate and detached from POA.

shijie xu
  • 1,975
  • 21
  • 52

1 Answers1

0

A servant is activated under a POA, so if you want to have the account servant be activated under a new ORB, you have to create that ORB somewhere, create a new POA, and override the _default_POA method in the Account servant to return that different POA. The other option is to not use _this, but do a manual activation of the POA.

In the close method, the if (account.is_nil()) should be !account.is_nil() as far as I can determine. The servants are reference counted, when the last reference drops it gets deleted, I can't see any code that it is recycled.

Johnny Willemsen
  • 2,942
  • 1
  • 14
  • 16
  • It should be !account.is_nil() in close. Maybe mistake in example http://www.dre.vanderbilt.edu/~schmidt/DOC_ROOT/TAO/examples/Simple/bank/AccountManager_i.cpp . In open method, the new Account_i is assigned to 'Account_var result' and recycled after result._this() because result is out of scope. The parameter Account_ptr for close method is the one return value of open method. The key problem is that should we recycle Account_ptr which is equal of result->_this() in the open method? – shijie xu Dec 05 '13 at 15:45
  • With _this() the Account_i is activated within the POA and a client object reference is returned. This client object reference is again used in close to deactivate the servant. I don't see anything wrong with that code. – Johnny Willemsen Dec 06 '13 at 13:26
  • 1st,if(account.is_nil ()) should be if(!account.is_nil ()) in close method as you point out; 2nd, I am not sure whether we should to free Accounti_ptr client reference? or when Account_i_ptr will be recycled? – shijie xu Dec 06 '13 at 14:47
  • You don't need to free the _ptr it in Account_i, the Account_var in the calling method will drop the reference count on the object reference, which means it will get destructor. Related to 1, I have adapted the code, will be part of the next TAO release. – Johnny Willemsen Dec 06 '13 at 15:44
  • Also check TAOX11 as CORBA implementation supporting IDL to C++11, that is far easier to use – Johnny Willemsen Dec 06 '13 at 15:44
  • which is the calling method, args1 in BankS.cpp:close_AccountManager::execute()? – shijie xu Dec 06 '13 at 17:33
  • That is all internal, the real caller is Bank_Client_i::test_for_different_name, you shouldn't care about the coding of S.cpp, check only the user code – Johnny Willemsen Dec 06 '13 at 18:25
  • a ha, I got your point now.. In the test_for_name/test_for_different_name, the new created servant reference are assigned to Bank::Account_var. After close corba invocation, the servant refer at client side is freed automatically then the real seravnt object at server side are freed correspondingly. TAO Corba itself would completes all these procedures. – shijie xu Dec 07 '13 at 00:00
  • The client just gets a newly created client object reference, not a servant reference. Try to make a clear distinction between the servant and object. The life time of the servant is independent of the life time of the client object reference. – Johnny Willemsen Dec 07 '13 at 19:07
  • Thanks very much. Therefore, it is CORBA mechanism that manages lifetime of Bank::Account_ptr account. – shijie xu Dec 08 '13 at 03:51
  • The Account servant lifetime is manages by the POA, the client object reference has a different lifetime. Have a look at http://www.ciaranmchale.com/corba-explained-simply/ – Johnny Willemsen Dec 09 '13 at 08:59