POA ӳ��

POA �ӿ�

POA �ӿ��е�󵫺ܼ�:

module PortableServer {
//  ����������û�õIJ���
	interface POA {

	// ��������
	ObjectId activate_object (in Servant p_servant)
		raises ServantAlreadyActive, WrongPolicy);
	void activate_object_with_id   (in ObjectId,
					in Servant p_servant)
		raises (ServantAlreadyActive, ObjectAlreadyActive, 
			WrongPolicy);
	void deactivate_object (in ObjectId id) 
		raises (ObjectNotActive, WrongPolicy);

	// ���ý�������
	Object create_reference (in CORBA::RepositoryId repid)
		raises (WrongPolicy);
	Object create_reference_with_id (in ObjectId id,
					in CORBA::RepositoryId repid)
		raises (WrongPolicy);

	// ����ӳ�亯��
	ObjectId servant_to_id (in Servant p_servant)
		raises (ServantNotActive, WrongPolicy);
	Object servant_to_reference (in Servant p_servant)
		raises (ServantNotActive, WrongPolicy);
	Servant reference_to_servant (in Object reference)
		raises (ObjectNotActive, WrongAdapter, WrongPolicy);
	ObjectId reference_to_id (in Object reference)
		raises (WrongAdapter, WrongPolicy);
	Servant id_to_servant (in ObjectId oid)
		raises (ObjectNotActive, WrongPolicy);
	Object id_to_reference (in ObjectId oid)
		raises (ObjectNotActive, WrongPolicy);


	};
};

���������������һ����ʵ�� POA �ӿڵĽ�¼����ɾ�������е����ⶨ��������ﲻ�漰�ĺ�����һЩ��������Ҫ�ڱ��º���IJ������۶����������ˡ�

���ڣ������ǿ�һ����Щ�����Լ����ǵ���;��

�� 4-1. POA �ӿ�

������������������;
activate_object

����������� POA ��ע�����ʹӱ�ʾ�Ķ���ʵ�֣����ص� ObjectId �ɽ��ű� id_to_reference ���������õ����������ʵ��������Ķ������á�

activate_object_with_id����������� POA ��ע�����ʹӱ�ʾ�Ķ���ʵ�֣��������ʵ�ֽ�ͬ��Ϊһ�� ObjectId �� id ������ϵ��һ��ObjectId �����õ� create_reference �ĵ������ɶ������ã�������  reference_to_id ���ط�װ�ڶ��������е�ObjectId ��
deactivate_object

���������ȥ��һ������ ObjectId ָ���Ķ����ע��(ObjectId ͨ�� reference_to_id �� servant_to_id �������)��

create_reference����������Ӷ���ʵ��(�ʹӶ���)����һ���������á�
create_reference_with_id���������ֻ������֪���� ObjectId ��������֪�Ķ���...
servant_to_id��������IJ���˵��ʹ����dz����ԡ�����ֵ��ע���������ʹ�δ�������������һ�� ServantNotActive ���⣬����ζ���������������ʹ��activate_object �� activate_object_with_id ����һ������֮ǰ�����á�
servant_to_reference��������IJ���˵��ʹ����dz����ԡ�����ֵ��ע���������ʹ�δ�������������һ�� ServantNotActive ���⣬����ζ���������������ʹ��activate_object �� activate_object_with_id ����һ������֮ǰ�����á�
id_to_servant��������IJ���˵��ʹ����dz����ԡ�����ֵ��ע���������� ObjectId ������һ��Ķ���δ�������������һ�� ServantNotActive ���⣬����ζ���������������ʹ��activate_object �� activate_object_with_id ����һ������֮ǰ�����á�
id_to_reference��������IJ���˵��ʹ����dz����ԡ�����ֵ��ע���������� ObjectId ������һ��Ķ���δ�������������һ�� ServantNotActive ���⣬����ζ���������������ʹ��activate_object �� activate_object_with_id ����һ������֮ǰ�����á�
reference_to_servant��������IJ���˵��ʹ����dz����ԡ�����ֵ��ע����������������ù�����һ��Ķ���δ�������������һ�� ServantNotActive ���⣬����ζ���������������ʹ��activate_object �� activate_object_with_id ����һ������֮ǰ�����á�
reference_to_id��������IJ���˵��ʹ����dz����ԡ�������������������IJ�ͬ�ǣ����������κ� NotActivated ���⣬��ζ��������������� create_reference ����������ȷ�� ObjectId ֮����ã������صķ�װ�ڶ��������е� ObjectId �ɱ�activate_object_with_id ����ʹ�á�

������ʹ������Щ������һ������ʵ��:

/* ʹ�� activate_object ���������� */
ObjectId obj_id;
CORBA_Object obj;
obj_id = PortableServer_POA_activate_object (poa, servant, &ev);
/* ����, ���ǿ�����������������������ע����� CORBA_Object 
 * ����ת����һ�� ior �ַ���
 */
obj = PortableServer_POA_id_to_reference (poa, obj_id, &ev);

/* ʹ�� activate_object_with_id ���������� */
CORBA_Object obj;
/* ע�� PortableServer_string_to_id ���÷�һ������ϸ��˵����
* ����֪��������һ�� ObjectId ���㹻�ˡ�
*/
PortableServer_POA_activate_object_with_id (poa,
	PortableServer_string_to_id ("my_small_servant", &ev),
	servant, 
	&ev);

obj = Portable_Server_POA_servant_to_reference (poa, servant, &ev);
/* ����, ���ǿ�����������������������ע����� CORBA_Object 
 * ����ת����һ�� ior �ַ���
*/

�ʹ�

������Щ����չʾ������� POA ��ע��һ���ʹӡ��ʹ������������Եġ����ǵĽṹ�����ǵ�ʹ������ȫ������ӳ��ġ���ǰ�����Ѿ����͵����������������������ֻչʾ C ӳ�䣬����ֻ�鿴���� POA �� C ���Եķ�ʽ��

�뷨����һЩ��ʼ����������һ�����ݽṹ��������ݽṹ���е����󷽷�ʵ�ֺ�����ָ����б���������ݽṹ���� EPV (entry point vector����ڵ�����)���ʹ����ݽṹ����������һ�� EPV��

������Ϊ����Ľӿڶ��彨����һ���������ݽṹ��

// idl interface
module FruitsBasket {
	interface Apple {
		void EatMe ();
		void ThrowMe ();
	};
};
typedef struct {
	void *_private; /* ORB specific data */
	void (*EatMe) (PortableServer_Servant servant, CORBA_Environment * ev);
	void (*ThrowMe) (PortableServer_Servant servant, CORBA_Environment * ev);
} POA_FruitsBasket_Apple__epv;

typedef struct {
	PortableServer_ServantBase__epv *_base_epv;
	POA_FruitsBasket_Apple__epv *FruitsBasket_Apple_epv;
} POA_FruitsBasket_Apple__vepv;

typedef struct {
	void *_private; /* ORB specific data */
	POA_FruitsBasket_Apple__vepv *vepv;
} POA_FruitsBasket_Apple;

extern void POA_FruitsBasket_Apple__init(POA_FruitsBasket_Apple servant,
	CORBA_Environment * ev);
extern void POA_FruitsBasket_Apple__fini(POA_FruitsBasket_Apple servant,
	CORBA_Environment * ev);

һ���ʹӾ���һ�����ݽṹ��������һ���ض��ڶ���ʵ�ֵ� EPV ��һ���� _base_epv ��ͨ�� EPV������ض� EPV ������˵���ģ��ȴ����ĺ���ָ����б������� EPV ������δ����ʾ���������ڳ����ض����ʹ�������һЩ��Ϣ(��ʵ�ϣ�һ���ʹӿ�����Ӧ�Ķ���ʵ�ֶ���һ���������ھ͹����������ɹ��ȵ�ɱ��)��

���ڣ�������һ�� PortableServer_ServantBase__epv �ṹ��

typedef struct PortableServer_ServantBase__epv {
void *private;  /* �ض��� ORB ������ */
/* �����õ���ֹ����
* �����������е��ʹ�����
*/
void (*finalize) (PortableServer_Servant, CORBA_Environment *);
/* ����ʹ�ʹ�õ�ȱʡ POA */
PortableServer_POA (*default_POA) (PortableServer_Servant, 
	CORBA_Environment *);
} PortableServant_ServantBase__epv;

��� finalize �� default_poa ��Ա�� NULL�������ʹӴ��ݸ� POA_FruitsBasket_Apple__init ʱ��ORB ���ñ�׼��ȱʡֵ����ʼ�����ǡ� default_POA ȱʡ�� "RootPOA"������CORBA_ORB_resolve_initial_reference (orb, "RootPOA", &ev) ���ص��Ǹ� POA������ ORB �ٶ�Ӧ�ó�����ȷ�ij�ʼ��������ֶΡ�

���͵ģ�Ҫ������������ʼ��һϵ�����ݽṹ�� 

void MyEatMe (PortableServer_Servant servant, 
	CORBA_Environment * ev)
{
/* ���������Լ�����Ӧ�Ķ���ʵ�ֵĴ��� */
}
void MyThrowMe (PortableServer_Servant servant, 
	CORBA_Environment * ev)
{
/* ���������Լ�����Ӧ�Ķ���ʵ�ֵĴ��� */
}

PortableServer_ServantBase__epv base_epv {
	NULL, /* �ض��� ORB ������ */
	NULL, /* ��ֹ���� */
	NULL  /* ȱʡ POA */
}

POA_FruitsBasket_Apple__epv Apple_epv {
	NULL,    /* ORB specific data */
	MyEatMe,
	MyThrowMe
};

POA_FruitsBasket_Apple__vepv Apple_vepv {
	&base_epv,
	&Apple_epv
};

POA_FruitsBasket_Apple Apple_servant = {
	NULL,
	&Apple_vpev
};

������Ҫ����ֻʣ�µ��� POA_FruitsBasket_Apple__init ( Apple_servant, &ev) �ˡ������ʹ��Ѿ��� POA ��ע���ˣ������輤���������ʵ�֣��û�һ����Ч�Ķ������ò������ַ�����ע�ᡣ�����������Ĵ��룺

#include <orb/orbit.h>
#include <ORBitservices/CosNaming.h>

void MyEatMe (PortableServer_Servant servant, 
CORBA_Environment * ev)
{
/* ���������Լ�����Ӧ�Ķ���ʵ�ֵĴ��� */
}
void MyThrowMe (PortableServer_Servant servant, 
CORBA_Environment * ev)
{
/* ���������Լ�����Ӧ�Ķ���ʵ�ֵĴ��� */
}

PortableServer_ServantBase__epv base_epv {
	NULL, /* �ض��� ORB ������ */
	NULL, /* ��ֹ���� */
	NULL  /* ȱʡ POA */
}

POA_FruitsBasket_Apple__epv Apple_epv {
	NULL,    /* �ض��� ORB ������ */
	MyEatMe,
	MyThrowMe
};

POA_FruitsBasket_Apple__vepv Apple_vepv {
	&base_epv,
	&Apple_epv
};

POA_FruitsBasket_Apple Apple_servant = {
	NULL,
	&Apple_vpev
};

int main (int argc, char **argv)
{
CORBA_Environment ev;
CORBA_ORB orb;
PortableServer_POA root_poa;
ObjectId *obj_id;
CORBA_Object obj;
CosNaming_NamingContext *root_name;
CosNaming_NameComponent name_component[1] = { {"MyApple", "server"} };
CosNaming_Name name = {1, 1, name_component, CORBA_FALSE};


CORBA_exception_init (&ev);
orb = CORBA_ORB_init (&argc, argv, 
	"orbit-local-orb", 
	&ev);

POA_FruitsBasket_Apple__init (orb, 
	Apple_servant, 
	&ev);
if (ev->_major != CORBA_NO_EXCEPTION) {
	fprintf (stderr, 
		"Error: exception during Servant initialization: %s\n", 
		CORBA_exception_id(&ev));
	CORBA_exception_free (&ev);
	exit 1;
}

root_poa = CORBA_ORB_resolve_initial_references (orb, 
	"RootPOA", 
	&ev);
if (ev->_major != CORBA_NO_EXCEPTION) {
	fprintf (stderr, 
		"Error: exception during RootPOA initialization: %s\n", 
		CORBA_exception_id(&ev));
	CORBA_exception_free (&ev);
	exit 1;
}

/* ������� */
obj_id = PortableServer_POA_activate_object (root_poa,
	&Apple_servant,
	&ev);
/* �õ�һ���������� */
obj = PortableServer_POA_id_to_reference (poa,
	obj_id,
	&ev);

/* ������������ע����� */
root_name = CORBA_ORB_resolve_initial_service (orb, 
	"NameService", 
	&ev);
if (ev->_major != CORBA_NO_EXCEPTION) {
	fprintf (stderr, 
		"Error: could not get name service: %s\n", 
		CORBA_exception_id(&ev));
	exit 1;
}
CosNaming_NamingContext_bind (root_amp, 
	&name, 
	obj, 
	&ev);
if (ev->_major != CORBA_NO_EXCEPTION) {
	fprintf (stderr, 
		"Error: could not register object: %s\n", 
		CORBA_exception_id(&ev));
	exit 1;
}
CORBA_free (root_poa);
CORBA_exception_free (&ev);
exit 0;
}

pfew !! ��Ҳ̫���������� !! ... ����ֻ�Dzſ�ʼ���� CORBA ����IJ����С������������ʵ������ļ򵥻��� �������Ǽٶ� POA �Ѿ�׼���������ˣ���������Ϊ�˼򵥻���ʹ�� RootPOA���������ϰٸ�����ʱ��Ͳ���һ���������ˣ����Ƕ�ʹ��ͬһ�� POA ���������ص��������⡣

������

�����ǽ�����������IJ���֮ǰ��������һ��С�ĵ������õ��ض��� C ӳ��� API���ڴ��� ObjectId �ṹʱ���á����IJ���˵���Ѿ��㹻���������������ˡ�

PortableServer_ObjectId   * PortableServer_string_to_ObjectId (CORBA_Char *string, 
	CORBA_Environment *ev);

CORBA_Char                * PortableServer_ObjectId_to_string (PortableServer_ObjectId *obj, 
	CORBA_Environment *ev);