Echo �ͻ��ͷ�����

�����ӵ�Ŀ��������һ���ͻ����ܼ���(typed input)���������Ѵ˼����ڱ�׼����������

echo �Ĺ���������ķdz��̵� IDL ��ʾ��

Example 5-1.

// MyFirstOrbit program - The Echo object
// �ҵĵ�һ��Orbit���� - Echo ����
//
// All this does is pass a string from the client to the server.
// ��������һ��ֻ�Ǵӿͻ������������һ���ַ���
// 

interface Echo {
	void echoString(in string input);
};

�ӿ�(interface)�����Ƕ���Ĺؼ����֡�ÿ���ӿڶ���һ�������䷽������������ӵ������ֻ��һ��������������һ���ַ����������Ҳ������κζ���������ǰ��� in ����ָ���˲���ֻ�����뷽�������ڵ�һ�������ͨ�����в������� in ������

��� idl ���� echo.idl �ļ����ҵ������� idl Ҫ�������в��裺$ orbit-idl echo.idl

Echo �ͻ�

�����ǿͻ�����

Example 5-2.

/*
 * MyFirstOrbit program. Client. Hacked by Ewan Birney
 * [email protected] from echo test suite.
 *
 */

#include "stdio.h"
#include "orb/orbit.h"

/*
 * This header file was generated from the idl
 * ���Ǵ� idl ���ɵ�ͷ�ļ�
 */

#include "echo.h"

/*
 * This is our Echo Object
 * �������ǵ� Echo ����
 */

Echo echo_client;

int
main (int argc, char *argv[])
{
    CORBA_Environment ev;
    CORBA_ORB orb;

    FILE * ifp;
    char * ior;
    char filebuffer[1024];

    /*
     * Standard initalisation of the orb. Notice that
     * ORB_init 'eats' stuff off the command line
     * orb �ı�׼�ij�ʼ����ע�� ORB_init �ᡰ�Ե������������в���
     */

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

    /*
     * Get the IOR (object reference). It should be written out
     * by the echo-server into the file echo.ior. So - if you
     * are running the server in the same place as the client,
     * this should be fine!
     * �õ� IOR (��������)������ echo-������д���ļ� echo.ior��
     * ���������������ͻ���ͬһ�ط����У��⽫�����õġ�
     */

    ifp = fopen("echo.ior","r");
    if( ifp == NULL ) {
      g_error("No echo.ior file!");
      exit(-1);
    }

    fgets(filebuffer,1024,ifp);
    ior = g_strdup(filebuffer);

    fclose(ifp);
    /*
     * Actually get the object. So easy!
     * �����õ�����̫������!
     */

    echo_client = CORBA_ORB_string_to_object(orb, ior, &ev);
    if (!echo_client) {
	printf("Cannot bind to %s\n", ior);
	return 1;
    }

    /*
     * Ok. Now we use the echo object...
     * OK. ����ʹ�� echo ����...
     */

    printf("Type messages to the server\n. as the only thing on the line stops\n");
    while( fgets(filebuffer,1024,stdin) ) {
      if( filebuffer[0] == '.' && filebuffer[1] == '\n' ) 
	break;

      /*
       * chop the newline off
       * �е����з�
       */
       
      filebuffer[strlen(filebuffer)-1] = '\0';
      
      /*
       * using the echoString method in the Echo object
       * this is defined in the echo.h header, compiled from echo.idl
       * �� Echo ������ʹ�� echoString ����
       * ����������� echo.idl �ļ�������� echo.h ͷ�ļ��ж��塣
       */
      
      Echo_echoString(echo_client,filebuffer,&ev);

      /*
       * catch any exceptions (eg, network is down)
       * �����κ�����(�������类�ر�)
       */

      if(ev._major != CORBA_NO_EXCEPTION) {
	printf("we got exception %d from echoString!\n", ev._major);
	return 1;
      }
    }
      
    /*
     * Clean up
     * ���
     */
     
    CORBA_Object_release(echo_client, &ev);
    CORBA_Object_release((CORBA_Object)orb, &ev);

    return 0;
}

�ͻ�����ɱ��ֳ�����������Ƭ�Ρ�

�ͻ�����Ĺؼ������ǵ��÷������ϵ� echoString ������idl ����

     void echoString(in string input);
���ձ������ idl ���ɵ� echo.h ͷ�ļ��е���������
extern void Echo_echoString(Echo _obj, CORBA_char * astring, CORBA_Environment *ev);

�����ǻ��� C ��̵Ķ���ı������˵Ĺ���

��Ȼ������Ĵ����в��DZ���������Щ���򣬵� CORBA C ӳ��������������ģ����Ǹ������Ľ��������

��ע������(1)(2)�����Ƶ� C++ ����(this ָ�룬��������)������(3)��Ŀ�����̰߳�ȫ��

Echo ������

���������������Ҫ�ȿͻ�����һЩ����������ͻ���Щͨ��֮�����������������ս���һ����������ѭ�����ڴ�֮ǰ�����뽨�� orb ���������Ķ���ʵ�ְ�(bind Ҳ��Ϊ����)�� orb �ϡ�

��ʵ�ʵķ������У����ø��Ӹ��ӣ����ڱ������У�һ����ͨ���� orb ��ʼ�����̣�һ�оͼ��ˡ�

/*
 * MyFirstOrbit program - server. Hacked
 * from Echo test suite by [email protected]
 */

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "signal.h"
#include "orb/orbit.h"
#include "echo.h"

/*
 * This is so we can get out a valid IOR later...
 * ��ʹ�����Ժ�ɵõ�һ����Ч�� IOR
 */
Echo echo_client = CORBA_OBJECT_NIL;

/*
 * declaration of the meat of the process
 */
 
static void
do_echoString(PortableServer_Servant servant,
	      CORBA_char *astring,
	      CORBA_Environment *ev);

/*
 * I have **no** idea what this bit does
 * �Ҳ�֪����λ���Ǹ�ʲô��
 */
 
PortableServer_ServantBase__epv base_epv = {
  NULL,
  NULL,
  NULL
};
POA_Echo__epv echo_epv = { NULL, do_echoString };
POA_Echo__vepv poa_echo_vepv = { &base_epv, &echo_epv };
POA_Echo poa_echo_servant = { NULL, &poa_echo_vepv };

int
main (int argc, char *argv[])
{
    PortableServer_ObjectId objid = {0, sizeof("myEchoString"), "myEchoString"};
    PortableServer_POA poa;

    CORBA_Environment ev;
    char *retval;
    CORBA_ORB orb;
    FILE * ofp;

    signal(SIGINT, exit);
    signal(SIGTERM, exit);

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

    POA_Echo__init(&poa_echo_servant, &ev);

    poa = (PortableServer_POA)CORBA_ORB_resolve_initial_references(orb, "RootPOA", &ev);
    PortableServer_POAManager_activate(PortableServer_POA__get_the_POAManager(poa, &ev), &ev);
    PortableServer_POA_activate_object_with_id(poa,
					       &objid, &poa_echo_servant, &ev);

    echo_client = PortableServer_POA_servant_to_reference(poa,
							  &poa_echo_servant,
							  &ev);
    if (!echo_client) {
	printf("Cannot get objref\n");
	return 1;
    }

    retval = CORBA_ORB_object_to_string(orb, echo_client, &ev);

    ofp = fopen("echo.ior","w");

    fprintf(ofp,"%s", retval);
    fclose(ofp);

    CORBA_free(retval);

    fprintf(stdout,"Written the file echo.ior with the IOR of this server.\n Now waiting for requests...\n");
    fflush(stdout);
    CORBA_ORB_run(orb, &ev);

    return 0;
}

static void
do_echoString(PortableServer_Servant servant,
	      CORBA_char *astring,
	      CORBA_Environment *ev)
{
  g_message("[server] %s", astring);
  return;
}
��������������������ڴ˷��ϸ������