Servants
A servant is a mapping between an interface implementation in some language (Python in this case) and a CORBA object. This mapping can be made using one of two approaches: inheritance or delegation. While the Python language mapping specification requires that only the inheritance approach be implemented, ORBit-Python supports both.
In order to create a servant, you first need to import the POA for the module that contains the interface you're implementing. We'll use the following IDL for our examples:
module Bank {
interface Account {
typedef double Amount;
readonly attribute Amount balance;
exception InsufficientFunds { Amount overdraft; };
void withdraw(in Amount x) raises (InsufficientFunds);
};
};
The POA for the Bank module is Bank__POA. (This convention is defined by the Python mapping specification.) Using the inheritance-based approach, your implementation of Bank.Account will derive Bank__POA.Account. The implementation of the Account interface, then, could look like:
import CORBA, Bank, Bank__POA
class Account(Bank__POA.Account):
def __init__(self):
self.balance = 100.0
def withdraw(self, amount):
if self.balance - amount < 0:
d = Bank.Account.InsufficientFunds()
d.overdraft = amount - self.balance
raise Bank.Account.InsufficientFunds, d
else:
self.balance = self.balance - amount
servant = Account()
With delegation, your implementation class needn't derive anything. You use the POA to create a servant that binds an instance of the implementation. Using delegation, the above code looks slightly different:
import CORBA, Bank, Bank__POA
class Account:
def __init__(self):
self.balance = 100.0
def withdraw(self, amount):
if self.balance - amount < 0:
d = Bank.Account.InsufficientFunds()
d.overdraft = amount - self.balance
raise Bank.Account.InsufficientFunds, d
else:
self.balance = self.balance - amount
servant = Bank__POA.Account( Account() )
The fact that the implementation class in the above to fragments is called Account is irrelevant. The actual name of the implementation class is arbitrary. It's a common convention to suffix the class name with _impl.
The inheritance-based approach is the prefered method because the mapping specification requires it be implemented, whereas delegation is optional. Using inhertance will ensure your code will be easily portable between all compliant Python ORBs. Still, delegation is available to you if you find it suits your needs better. You may use delegation in circumstances where you are wrapping the functionality of an existing Python class -- one that you can't modify -- as a CORBA object.
All servants have the following methods and attributes:
_this()
Description:
Returns a reference to the servant, implicitly activating it (by calling POA.activate_object) if necessary.
Example:
import sys, CORBA, Bank__POA
orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
poa = orb.resolve_initial_references("RootPOA")
class Account(Bank__POA.Account):
pass
# Servant through inheritance
ref = Account()._this()
|