�������ͻ��ͷ�����

�����Ӵ�������������һ��ʵ�ʵĿɹ����ķ������Ľ�һ�����⣬ʹ�������� ORBit ��֧�֡�

ϵͳ�������ܶࡣ������ֻ�ṩ�������ܣ�һ�����������ļӷ�һ�����������ļ�������һ��������������Ϊ������д IDL �ļ��������ǵ��������Ƿdz��򵥵ġ�

Example 5-3. ������ IDL �ļ�

interface Calculator
{
      double add(in double number1, in double number2);
      double sub(in double number1, in double number2);
};

������������ɿ��(skeleton)�ʹ��(stub)�ļ������������ļ�֮�� ORBit IDL ������һ�������ļ���һ��ͷ�ļ��������ļ�ʵ���ڴ�������ܺ��������飬�Կͻ��ͷ����������á����� C Դ�ļ����������зdz��򵥡�$ orbit-idl --skeleton-impl calculator.idl ������������Ҫ�õ������ļ���

�򵥵ļ������ͻ�

��һ���������������д�������Ϳͻ��������Ǵӿͻ���ʼ����Ϊ������Щ��

һ���򵥵Ŀͻ�ʵ�ֿ����������ģ�

Example 5-4. �������ͻ� C Դ�ļ�

#include "orb/orbit.h"
#include "calculator.h"
#include <stdio.h>

int
main(int argc, char* argv[])
{
        CORBA_Environment ev;
	CORBA_ORB         orb;
	CORBA_Object      server;
        CORBA_double      res;
        gchar*            dummy_argv[2];
        gint              dummy_argc;

        dummy_argc = 1;
        dummy_argv[0] = argv[0];
        dummy_argv[1] = 0;

	CORBA_exception_init(&ev);
	orb = CORBA_ORB_init(&dummy_argc, dummy_argv, "orbit-local-orb", &ev);
	server = CORBA_ORB_string_to_object(orb, argv[1], &ev);
        res = Calculator_add(server,1.0, 2.0, &ev);
	fprintf(stdout,"1.0 + 2.0 = %2.0f\n", res);
	CORBA_Object_release(server,&ev);
	exit(0);
}

  

�dz��򵥣���ȫ��δ�ӽ��͵IJ��ϡ������ȿ�һ�¶���ı�����

env
����������ڱ����ں��������ڼ���ܷ������������Ϣ���ں���������н��������ʹ�������������⺯���еĴ���
orb
���� ORB ������
server
���ǶԷ������Ķ������á�

�����������ȫ���ܵĿͻ���������ӵ��ؾ��ǰ� argv[1] �������ݸ� CORBA_ORB_string_to_object �������Դ˵Ľ���Ϊ����������Ա�ʾ���������������ַ���Ϊ��һ�������������õġ���λ������ַ�����������һ��������������

�򵥵ļ�����������

����ʵ�ַ�������IDL ������Ϊ�����˴����Ĺ���������Ϊ������ʵ��ɢ�����������ݽṹ�ͺ������ñ���IJ��ϡ����������д����������������е�������Щ���ϣ��Լ������ķ���������ʵ�֡������ҽ��ṩ IDL ���������ɵĺ��������ݽṹ�������ҽ�չʾΪʹ��Щ������ȷ�Ĺ������������õĻ�������Щ��

������ʵ�ֿ��

Ϊ�˼���ʵ�ּ����������� ORBit IDL ���������һ����������ʵ�ֿ�ܡ���ͨ���� IDL ������һ�� --skeleton-impl ����(ѡ��)���������ɵġ� orbit-idl --skeleton-impl calculator.idl ������������������(���ɵ�Դ�ļ���ȱʡ�������� calculator-skelimpl.c ��������������б�������Ϊ calculator-impl.c )��

Example 5-5. ������ʵ�ֿ��

#include "calculator.h"

/***
    App-specific servant structures
    Ӧ�ó���ָ�����ʹӽṹ 
 ***/
 
typedef struct {
   POA_Calculator servant;
   PortableServer_POA poa;

} impl_POA_Calculator;

/*** 
    Implementation stub prototypes 
    ʵ�ִ��ԭ��
 ***/

static void impl_Calculator__destroy(impl_POA_Calculator * servant,
				     CORBA_Environment * ev);

CORBA_double impl_Calculator_add(impl_POA_Calculator * servant,
		    CORBA_double number1,
		    CORBA_double number2,
		    CORBA_Environment * ev);

CORBA_double impl_Calculator_sub(impl_POA_Calculator * servant,
		    CORBA_double number1,
		    CORBA_double number2,
		    CORBA_Environment * ev);

/***
    epv structures
    epv �ṹ
 ***/
 
static PortableServer_ServantBase__epv impl_Calculator_base_epv =
{
   NULL,			/* _private data */
   (gpointer) & impl_Calculator__destroy,	/* finalize routine */
   NULL,			/* default_POA routine */
};
static POA_Calculator__epv impl_Calculator_epv =
{
   NULL,			/* _private */
   (gpointer) & impl_Calculator_add,

   (gpointer) & impl_Calculator_sub,

};

/***
    vepv structures
    vepv �ṹ
 ***/
static POA_Calculator__vepv impl_Calculator_vepv =
{
   &impl_Calculator_base_epv,
   &impl_Calculator_epv,
};

/***
    Stub implementations
    ���ʵ��
 ***/
 
static Calculator 
impl_Calculator__create(PortableServer_POA poa, CORBA_Environment * ev)
{
   Calculator retval;
   impl_POA_Calculator *newservant;
   PortableServer_ObjectId *objid;

   newservant = g_new0(impl_POA_Calculator, 1);
   newservant->servant.vepv = &impl_Calculator_vepv;
   newservant->poa = poa;
   POA_Calculator__init((PortableServer_Servant) newservant, ev);
   objid = PortableServer_POA_activate_object(poa, newservant, ev);
   CORBA_free(objid);
   retval = PortableServer_POA_servant_to_reference(poa, newservant, ev);

   return retval;
}

/*
 * You shouldn't call this routine directly without first deactivating the servant... 
 * ����ֹ�ʹӾͲ���ֱ�ӵ����������
 */
 
static void
impl_Calculator__destroy(impl_POA_Calculator * servant, CORBA_Environment * ev)
{

   POA_Calculator__fini((PortableServer_Servant) servant, ev);
   g_free(servant);
}

CORBA_double
impl_Calculator_add(impl_POA_Calculator * servant,
		    CORBA_double number1,
		    CORBA_double number2,
		    CORBA_Environment * ev)
{
   CORBA_double retval;

   return retval;
}

CORBA_double
impl_Calculator_sub(impl_POA_Calculator * servant,
		    CORBA_double number1,
		    CORBA_double number2,
		    CORBA_Environment * ev)
{
   CORBA_double retval;

   return retval;
}

���Դ�ļ��ṩ����һ����������������֮����ע���������ɵ�����ļ�(���� --skeleton-impl ����)ֻ��һ�Σ����� makefile �����κο��صĵ��� orbit-idl��������� makefile �е��� orbit-idl --skeleton-impl����ǰ���ļ�������д�������ʵ�ִ��뽫��ʧ��һ��д��ʵ�ִ��룬ֻ��Ҫ�� calculator-server.c �ļ��Ŀ�ͷ�������Դ�ļ���

����������У��Ҳ���������ɵ�����ļ������в��ֺ�����λ���⽫���Ժ�ȥ�������ǽ������ڷ����������С�

��������������

CORBA_doubleimpl_Calculator_add(impl_POA_Calculator*servant, CORBA_doublenumber1, CORBA_doublenumber2, CORBA_Environment*ev);

��

CORBA_doubleimpl_Calculator_sub(impl_POA_Calculator*servant, CORBA_doublenumber1, CORBA_doublenumber2, CORBA_Environment*ev);

�������������� IDL �ļ��ж���ĺ�����ʵ�֡���Ϊ IDL ���������ṩ��ʵ��ʵ��(������֪�������Ǹ�ʲô�õ�)��������Լ���չ�����ܡ�

���� impl_Calculator_add() Ӧ�����������������ؽ�������������Ӧ�����Ϊ��

CORBA_double
impl_Calculator_add(impl_POA_Calculator * servant,
		    CORBA_double number1,
		    CORBA_double number2,
		    CORBA_Environment * ev)
{
   CORBA_double retval;

   retval = number1 + number2;
   return retval;
}

������������ʵ��

������Ѿ���С��������������Ҫʹ�����������������������з�ʽʵ�ֵġ�

Example 5-6. ������������ʵ��

#include "calculator-impl.c"
#include <stdio.h>

int
main(int argc, char* argv[])
{
  CORBA_ORB                 orb;
  CORBA_Environment*        ev;
  PortableServer_ObjectId*  oid;
  Calculator                calculator;
  PortableServer_POA        root_poa;
  PortableServer_POAManager pm;
  CORBA_char*               objref;
  
  ev = g_new0(CORBA_Environment,1);

  CORBA_exception_init(ev);

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

  /* 
   * Handle exception somehow 
   * ����ij��ԭ���µ�����
   */
   
  root_poa = (PortableServer_POA)CORBA_ORB_resolve_initial_references(orb, "RootPOA", ev);
 
  /* 
   * ����ij��ԭ���µ�����
   */
   
  calculator = impl_Calculator__create(root_poa, ev);
 
  /* 
   * ����ij��ԭ���µ�����
   */
   
  objref = CORBA_ORB_object_to_string(orb, calculator, ev);
 
  /* 
   * ����ij��ԭ���µ�����
   */
   
  fprintf(stderr, "%s\n", objref);

  pm = PortableServer_POA__get_the_POAManager(root_poa, ev);
 
  /* 
   * ����ij��ԭ���µ�����
   */
   
  PortableServer_POAManager_activate(pm, ev);
 
  /* 
   * ����ij��ԭ���µ�����
   */
   
  CORBA_ORB_run(orb, ev);
  return 0;
}

�Ҳ��������������ӵ�ÿһ�У�����Ҫ�����÷��������в������Ƕ����ĵ�һ�ε��á��� fprintf() ���õ���һ��ֵ�ý��͡�������õ�Ŀ���������ʾ�������õ��ַ���������ַ�������"IOR:"���п�ͷ�ģ��ǿͻ�����IJ���������ʶһ���ض����󡣰������������������ڵ���������������λ�ã�������ض��ķ�������Ķ����ʶ��������ϸ��ԭ���Ǻܿ�����һ���������������ж�����������Ժ������εõ�����ַ�����������ã�������Ҫ����һ��(������)��������������ճ������һ��(�ͻ�)������������ϡ�

���벢���з������Ϳͻ�

����� makefile �����ڱ���ͻ��ͷ��������ߡ�Ҫ֪�� ORBit ��λ�ã����ҵ�ϵͳ�ϱ���װ�� /usr ���棬������Դ�ļ��������������� /usr/local �£����� ORBIT ������·�������Ƕ����ġ��� orbit-docs.tar.gz �е����ӵ����ýű����ҵ���ȷ��·����д�� makefile �ļ���

CC = gcc
ORBIT_IDL = /usr/bin/orbit-idl
ORBIT_CFLAGS = -I/usr/lib/glib/include -I/usr/include
ORBIT_LIBS = -L/usr/lib -lORBit -lIIOP -lORBitutil -lglib -lm
CFLAGS = $(ORBIT_CFLAGS)
LFLAGS = $(ORBIT_LIBS)

all : idltargets calculator-client calculator-server

calculator-client : calculator-client.o calculator-common.o calculator-stubs.o
    $(CC) -o calculator-client calculator-client.o calculator-stubs.o
calculator-common.o  -lIIOP -lORBit -lORBitutil $(LFLAGS)

calculator-server : calculator-server.o calculator-skels.o calculator-common.o
    $(CC) -o calculator-server calculator-server.o calculator-skels.o
calculator-common.o  -lIIOP -lORBit -lORBitutil $(LFLAGS)

�ڵ����� make all ֮����Ҫ���������ڡ��ڵ�һ�����������������calculator-server �����������������������һ���dz��������ĸ��ַ��� IOR: ���п�ͷ���ַ������ڵڶ��������������� calculator-client IOR-string �����ͻ���������ò�Ҫ��ͼ��������ַ�������������� xterm �����������Ǹ����ƵĶ�����ʹ�ü��к�ճ�����ܡ�

���һ�������������㽫���������3.