CPP实现CORBA静态绑定(六)

  • CORBA基本架构
  • IDL文件编写
  • CPP示例实现(上)
  • CPP示例实现(下)
  • C示例实现(IOR+NS上)
  • C示例实现(IOR+NS下)
  • C示例实现(IOR上)
  • C示例实现(IOR下)

首先完成客户端部分:
Hi-client-ns.c

#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <orbit/orbit.h>
#include <ORBitservices/CosNaming.h>
#include <ORBitservices/CosNaming_impl.h>

#include "Hi.h"

/*
Usage: ./hi-client_ns
*/

/**
 * test for exception
 */
static
gboolean
raised_exception(CORBA_Environment *ev)
{
	return ((ev)->_major != CORBA_NO_EXCEPTION);
}

/**
 * in case of any exception this macro will abort the process
 */
static
void
abort_if_exception(CORBA_Environment *ev, const char* mesg)
{
	if (raised_exception (ev)) {
		g_error ("%s %s", mesg, CORBA_exception_id (ev));
		CORBA_exception_free (ev);
		abort();
	}
}


/*
 * main
 */
int main(int argc, char* argv[])
{
	CORBA_ORB orb=CORBA_OBJECT_NIL;
	CORBA_Environment ev;
	CosNaming_NamingContext ns =CORBA_OBJECT_NIL;
	HiCorba_Hi service = CORBA_OBJECT_NIL;
	CORBA_char  name_service[]="Hi";
	CosNaming_NameComponent path[1]={name_service,""};
	CosNaming_Name name={1,1,path,CORBA_FALSE};

	//init orb
	g_print("\nClient>starting client...");
	g_print("\nClient>creating and initializing the ORB");
	CORBA_exception_init(&ev);
	abort_if_exception(&ev, "CORBA_exception_init failed");
	orb=CORBA_ORB_init(&argc,argv,"orbit-local-orb",&ev);
	abort_if_exception(&ev, "CORBA_ORB_init failed");

	// read name_service ior from ns.ior 
	CORBA_char  filename[] = "ns.ior";
 	FILE *file   = NULL;
	g_print("\nClient>reading the file '%s'",filename);
	if ((file=fopen(filename, "r"))==NULL)
                g_error ("could not open '%s'", filename);
	gchar objref[1024];
	fscanf (file, "%s", &objref);
	g_print("\nClient>getting the root naming context 'NameService' - from the file '%s'",filename);
	g_print("\nClient>the root naming context ior is '%s'",objref);
        ns = (CosNaming_NamingContext)CORBA_ORB_string_to_object(orb,objref,&ev);
	//free (objref);
	abort_if_exception(&ev, "CORBA_ORB_string_to_object 'NameService IOR' failed");

        //resolve object reference
	g_print("\nClient>Resolving the object reference in naming '%s'",name_service);
	service=CosNaming_NamingContext_resolve(ns,&name,&ev);
	abort_if_exception(&ev, "resolve failed");

	// invoke service
	g_print("\nClient>calling the Hi service...");
	CORBA_char *msg=HiCorba_Hi_sayHiTo(service, "neohope", &ev);
	abort_if_exception(&ev, "HiCorba_Hi_sayHiTo failed");
        g_print("\nClient>server returned the following message: %s\n", msg);

	CORBA_Object_release(service, &ev);
	abort_if_exception(&ev, "release failed");

        if (orb != CORBA_OBJECT_NIL)
        {
           /* going to destroy orb.. */
           CORBA_ORB_destroy(orb, &ev);
	   abort_if_exception(&ev, "destroy failed");
	}

}

MakeClient

CC       = gcc
CFLAGS   = -c -g -pthread -D_REENTRANT -DORBIT2=1 \
           -I/usr/include/orbit-2.0 \
           -I/usr/include/glib-2.0 \
           -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
LDFLAGS  = -Wl,--export-dynamic -lORBit-2 -lORBitCosNaming-2 -lgmodule-2.0 \
           -ldl -lgobject-2.0 -lgthread-2.0 -lpthread -lglib-2.0 -lm \
           -L/usr/lib
ORBIT_IDL= /usr/bin/orbit-idl-2

all : Hi-client-ns.bin

Hi-client-ns.bin : Hi-common.o Hi-stubs.o Hi-client-ns.o
	$(CC) $(LDFLAGS) Hi-common.o Hi-stubs.o Hi-client-ns.o -o Hi-client-ns.bin

%.o : %.c 
	$(CC) $(CFLAGS) $< -o $@ 

nidl : Hi.idl
	$(ORBIT_IDL) Hi.idl
	$(ORBIT_IDL) --skeleton-impl Hi.idl

clean:
	rm -rf *.bin
	rm -rf *.o

编译

make -f MakeClient

然后是运行

#首先运行orbd
orbd -ORBInitialPort 1900

#然后运行nameservice
orbit-name-server-2 > ns.ior

#然后运行server
./Hi-server-ns.bin
Server>starting server...
Server>creating and initializing the ORB
Server>getting reference to RootPOA
Server>activating the POA Manager
Server>creating the servant
Server>reading the file 'ns.ior'
Server>getting the root naming context 'NameService' - from the file 'ns.ior'
Server>the root naming context ior is 'IOR:010000002b00000049444c3a6f6d672e6f72672f436f734e616d696e672f4e616d696e67436f6e746578744578743a312e300000030000000054424f580000000101020005000000554e4958000000000a0000006c6f63616c686f73740000002d0000002f746d702f6f726269742d6e656f686f70652f6c696e632d313735302d302d346431323731616531616661390000000000000000caaedfba58000000010102002d0000002f746d702f6f726269742d6e656f686f70652f6c696e632d313735302d302d34643132373161653161666139000000001c00000000000000d4d2dcc0a89f68a8c02b28282828282801000000eaaf213d01000000480000000100000002000000050000001c00000000000000d4d2dcc0a89f68a8c02b28282828282801000000eaaf213d01000000140000000100000001000105000000000901010000000000'
Server>binding the object reference in naming with name 'Hi'
Server>running the orb...
Server>server is returning: Hi, neohope !

#然后运行client
./Hi-client-ns.bin
Client>starting client...
Client>creating and initializing the ORB
Client>reading the file 'ns.ior'
Client>getting the root naming context 'NameService' - from the file 'ns.ior'
Client>the root naming context ior is 'IOR:010000002b00000049444c3a6f6d672e6f72672f436f734e616d696e672f4e616d696e67436f6e746578744578743a312e300000030000000054424f580000000101020005000000554e4958000000000a0000006c6f63616c686f73740000002d0000002f746d702f6f726269742d6e656f686f70652f6c696e632d313735302d302d346431323731616531616661390000000000000000caaedfba58000000010102002d0000002f746d702f6f726269742d6e656f686f70652f6c696e632d313735302d302d34643132373161653161666139000000001c00000000000000d4d2dcc0a89f68a8c02b28282828282801000000eaaf213d01000000480000000100000002000000050000001c00000000000000d4d2dcc0a89f68a8c02b28282828282801000000eaaf213d01000000140000000100000001000105000000000901010000000000'
Client>Resolving the object reference in naming 'Hi'
Client>calling the Hi service...
Client>server returned the following message: Hi, neohope !

Leave a Reply

Your email address will not be published. Required fields are marked *

*