Kafka通讯代码01

  • Producer
  • Consumer
  • GroupConsumer

1、MqProducer.java

package com.neohope.kafka.test;

import java.util.*;

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

public class MqProducer {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("metadata.broker.list", "localhost:9092");
        props.put("serializer.class", "kafka.serializer.StringEncoder");
        props.put("partitioner.class", "com.neohope.kafka.test.SimplePartitioner");
        props.put("request.required.acks", "1");

        ProducerConfig config = new ProducerConfig(props);
        Producer<String, String> producer = new Producer<String, String>(config);
        for(int i=0;i<100;i++) {
            KeyedMessage<String, String> data = new KeyedMessage<String, String>("neoTopic", "key"+i, "value"+i);
            producer.send(data);
        }
        KeyedMessage<String, String> data = new KeyedMessage<String, String>("neoTopic", "-=END=-", "-=END=-");
        producer.send(data);

        producer.close();
    }
}

Continue reading Kafka通讯代码01

ActiveMQ通讯代码04

  • JMS方式调用
  • Queue方式调用
  • Topic方式调用
  • ReqRsp方式调用

1、TestMsg.java

package com.neohope.ActiveMQ.test.beans;

public class TestMsg implements java.io.Serializable{
    private static final long serialVersionUID = 12345678;

    public TestMsg(int taskId, String taskInfo, int taskLevel) {
        this.taskId = taskId;
        this.taskInfo = taskInfo;
        this.taskLevel = taskLevel;
    }

    public int taskId;
    public String taskInfo;
    public int taskLevel;
}

Continue reading ActiveMQ通讯代码04

ActiveMQ通讯代码03

  • JMS方式调用
  • Queue方式调用
  • Topic方式调用
  • ReqRsp方式调用

1、TestMsg.java

package com.neohope.ActiveMQ.test.beans;

public class TestMsg implements java.io.Serializable{
    private static final long serialVersionUID = 12345678;

    public TestMsg(int taskId, String taskInfo, int taskLevel) {
        this.taskId = taskId;
        this.taskInfo = taskInfo;
        this.taskLevel = taskLevel;
    }

    public int taskId;
    public String taskInfo;
    public int taskLevel;
}

Continue reading ActiveMQ通讯代码03

ActiveMQ通讯代码02

  • JMS方式调用
  • Queue方式调用
  • Topic方式调用
  • ReqRsp方式调用

1、TestMsg.java

package com.neohope.ActiveMQ.test.beans;

public class TestMsg implements java.io.Serializable{
    private static final long serialVersionUID = 12345678;

    public TestMsg(int taskId, String taskInfo, int taskLevel) {
        this.taskId = taskId;
        this.taskInfo = taskInfo;
        this.taskLevel = taskLevel;
    }

    public int taskId;
    public String taskInfo;
    public int taskLevel;
}

Continue reading ActiveMQ通讯代码02

ActiveMQ通讯代码01

  • JMS方式调用
  • Queue方式调用
  • Topic方式调用
  • ReqRsp方式调用

1、TestMsg.java

package com.neohope.ActiveMQ.test.beans;

public class TestMsg implements java.io.Serializable{
    private static final long serialVersionUID = 12345678;

    public TestMsg(int taskId, String taskInfo, int taskLevel) {
        this.taskId = taskId;
        this.taskInfo = taskInfo;
        this.taskLevel = taskLevel;
    }

    public int taskId;
    public String taskInfo;
    public int taskLevel;
}

Continue reading ActiveMQ通讯代码01

CPP实现CORBA静态绑定(八)

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

然后完成客户端部分:
Hi-client-ior.c

#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <orbit/orbit.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;
	HiCorba_Hi service = CORBA_OBJECT_NIL;

	//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[] = "service.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 'IOR' - from the file '%s'",filename);
	g_print("\nClient>the IOR is '%s'",objref);
        service = (HiCorba_Hi)CORBA_ORB_string_to_object(orb,objref,&ev);
	abort_if_exception(&ev, "CORBA_ORB_string_to_object 'NameService IOR' 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-ior.bin

Hi-client-ior.bin : Hi-common.o Hi-stubs.o Hi-client-ior.o
	$(CC) $(LDFLAGS) Hi-common.o Hi-stubs.o Hi-client-ior.o -o Hi-client-ior.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
 
#然后运行server
./Hi-server-ior.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>writing the file 'service.ior'
Server>writing the IOR to file 'service.ior'
Server>running the orb...
Server>server is returning: Hi, neohope !
 
#然后运行client
./Hi-client-ior.bin
Client>starting client...
Client>creating and initializing the ORB
Client>reading the file 'service.ior'
Client>getting the 'IOR' - from the file 'service.ior'
Client>the IOR is 'IOR:010000001300000049444c3a4869436f7262612f48693a312e300000030000000054424f580000000101020005000000554e4958000000000a0000006c6f63616c686f73740000002d0000002f746d702f6f726269742d6e656f686f70652f6c696e632d316235362d302d376663653334336261303164360000000000000000caaedfba58000000010102002d0000002f746d702f6f726269742d6e656f686f70652f6c696e632d316235362d302d37666365333433626130316436000000001c00000000000000dbcce0b85e73a828c02b2828282828280100000060aa3a1a01000000480000000100000002000000050000001c00000000000000dbcce0b85e73a828c02b2828282828280100000060aa3a1a01000000140000000100000001000105000000000901010000000000'
Client>calling the Hi service...
Client>server returned the following message: Hi, neohope !

CPP实现CORBA静态绑定(七)

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

在linux系统下,有很多开源的CORBA通讯框架,在C的示例中,使用了orbit的框架。
如我用的是Debian系统,可以直接安装:

apt-get install orbit2
apt-get install liborbit2-dev

安装后,可以通过工具orb-rdl-2从idl文件生成需要的stubs及skeletons接口代码:

#该命令会生成文件:Hi.h、Hi-common.c、Hi-skels.c、Hi-stubs.c
orbit-idl-2 Hi.idl

可以通过工具orb-rdl-2从idl文件生成服务端代码:

#该命令会生成模板文件:Hi-skelimpl.c
orbit-idl-2 --skeleton-impl Hi.idl

首先完成服务端部分:
Hi-server-ior.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <orbit/orbit.h>
#include "Hi.h"
#include "Hi-skelimpl.c"
/*
./Hi-server_ns -ORBInitRef NameService=[string of IOR of NameService]
*/

/**
 * 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[])
{
	PortableServer_POA poa;
	HiCorba_Hi servant = CORBA_OBJECT_NIL;
	CORBA_ORB orb=CORBA_OBJECT_NIL;
	CORBA_Environment ev;

	// init ORB
	g_print("\nServer>starting server...");
	g_print("\nServer>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");

	// activate POA
	g_print("\nServer>getting reference to RootPOA");
	poa= (PortableServer_POA) CORBA_ORB_resolve_initial_references(orb,"RootPOA",&ev);
	abort_if_exception(&ev, "CORBA_ORB_resolve_initial 'RootPOA' failed");
	g_print("\nServer>activating the POA Manager");
	PortableServer_POAManager_activate(PortableServer_POA__get_the_POAManager(poa, &ev),&ev);
	abort_if_exception(&ev, "POA_activate failed");

	// create servant
	g_print("\nServer>creating the servant");
	servant = impl_HiCorba_Hi__create (poa, &ev);
	abort_if_exception(&ev, "impl_HiCorba_Hi__create failed");
	
	// read name_service ior from ns.ior 
	CORBA_char  filename[] = "service.ior";
 	FILE *file   = NULL;
	g_print("\nServer>writing the file '%s'",filename);
	if ((file=fopen(filename, "w"))==NULL)
                g_error ("could not open '%s'\n", filename);
	gchar *objref = CORBA_ORB_object_to_string (orb, servant, &ev);
	abort_if_exception(&ev, "CORBA_ORB_object_to_string' failed");
        fprintf(file,"%s",objref);
        fflush(file);
        fclose(file);
	g_print("\nServer>writing the IOR to file '%s'",filename);
	
	// start 
	g_print("\nServer>running the orb...");
	CORBA_ORB_run(orb,&ev);
	abort_if_exception(&ev, "ORB_run failed");
}

Hi-skelimpl.c

/* This is a template file generated by command */
/* orbit-idl-2 --skeleton-impl Hi.idl */
/* User must edit this file, inserting servant  */
/* specific code between markers. */

#include <stdlib.h>
#include <stdio.h>
#include "Hi.h"

/*** App-specific servant structures ***/

#if !defined(_typedef_impl_POA_HiCorba_Hi_)
#define _typedef_impl_POA_HiCorba_Hi_ 1
typedef struct {
POA_HiCorba_Hi servant;
PortableServer_POA poa;
   /* ------ add private attributes here ------ */
   /* ------ ---------- end ------------ ------ */
} impl_POA_HiCorba_Hi;
#endif

/*** Implementation stub prototypes ***/

#if !defined(_decl_impl_HiCorba_Hi__destroy_)
#define _decl_impl_HiCorba_Hi__destroy_ 1
static void impl_HiCorba_Hi__destroy(impl_POA_HiCorba_Hi *servant,
CORBA_Environment *ev);
#endif

#if !defined(_decl_impl_HiCorba_Hi_sayHiTo_)
#define _decl_impl_HiCorba_Hi_sayHiTo_ 1
static CORBA_string
impl_HiCorba_Hi_sayHiTo(impl_POA_HiCorba_Hi *servant,
const CORBA_char * someone,
CORBA_Environment *ev);
#endif

#if !defined(_decl_impl_HiCorba_Hi_add_)
#define _decl_impl_HiCorba_Hi_add_ 1
static CORBA_long
impl_HiCorba_Hi_add(impl_POA_HiCorba_Hi *servant,
const CORBA_long numa,
const CORBA_long numb,
CORBA_Environment *ev);
#endif

#if !defined(_decl_impl_HiCorba_Hi_shutdown_)
#define _decl_impl_HiCorba_Hi_shutdown_ 1


static void
impl_HiCorba_Hi_shutdown(impl_POA_HiCorba_Hi *servant,
CORBA_Environment *ev);
#endif


/*** epv structures ***/

#if !defined(_impl_HiCorba_Hi_base_epv_)
#define _impl_HiCorba_Hi_base_epv_ 1
static PortableServer_ServantBase__epv impl_HiCorba_Hi_base_epv = {
NULL,             /* _private data */
(gpointer) & impl_HiCorba_Hi__destroy, /* finalize routine */
NULL,             /* default_POA routine */
};
#endif

#if !defined(_impl_HiCorba_Hi_epv_)
#define _impl_HiCorba_Hi_epv_ 1
static POA_HiCorba_Hi__epv impl_HiCorba_Hi_epv = {
NULL, /* _private */
(gpointer)&impl_HiCorba_Hi_sayHiTo,
(gpointer)&impl_HiCorba_Hi_add,
(gpointer)&impl_HiCorba_Hi_shutdown,
};
#endif


/*** vepv structures ***/

#if !defined(_impl_HiCorba_Hi_vepv_)
#define _impl_HiCorba_Hi_vepv_ 1
static POA_HiCorba_Hi__vepv impl_HiCorba_Hi_vepv = {
&impl_HiCorba_Hi_base_epv,
&impl_HiCorba_Hi_epv,
};
#endif


/*** Stub implementations ***/

#if !defined(_impl_HiCorba_Hi__create_)
#define _impl_HiCorba_Hi__create_ 1
static HiCorba_Hi impl_HiCorba_Hi__create(PortableServer_POA poa, CORBA_Environment *ev)
{
HiCorba_Hi retval;
impl_POA_HiCorba_Hi *newservant;
PortableServer_ObjectId *objid;

newservant = g_new0(impl_POA_HiCorba_Hi, 1);
newservant->servant.vepv = &impl_HiCorba_Hi_vepv;
newservant->poa = (PortableServer_POA) CORBA_Object_duplicate((CORBA_Object)poa, ev);
POA_HiCorba_Hi__init((PortableServer_Servant)newservant, ev);
   /* Before servant is going to be activated all
    * private attributes must be initialized.  */

   /* ------ init private attributes here ------ */
   /* ------ ---------- end ------------- ------ */

objid = PortableServer_POA_activate_object(poa, newservant, ev);
CORBA_free(objid);
retval = PortableServer_POA_servant_to_reference(poa, newservant, ev);

return retval;
}
#endif

#if !defined(_impl_HiCorba_Hi__destroy_)
#define _impl_HiCorba_Hi__destroy_ 1
static void
impl_HiCorba_Hi__destroy(impl_POA_HiCorba_Hi *servant, CORBA_Environment *ev)
{
    CORBA_Object_release ((CORBA_Object) servant->poa, ev);

    /* No further remote method calls are delegated to 
    * servant and you may free your private attributes. */
   /* ------ free private attributes here ------ */
   /* ------ ---------- end ------------- ------ */

POA_HiCorba_Hi__fini((PortableServer_Servant)servant, ev);

g_free (servant);
}
#endif

#if !defined(_impl_HiCorba_Hi_sayHiTo_)
#define _impl_HiCorba_Hi_sayHiTo_ 1
static CORBA_string
impl_HiCorba_Hi_sayHiTo(impl_POA_HiCorba_Hi *servant,
const CORBA_char * someone,
CORBA_Environment *ev)
{
 CORBA_string retval = CORBA_string_alloc(512);
 /* ------   insert method code here   ------ */
 sprintf(retval,"Hi, %s !",someone);
 g_print("\nServer>server is returning: %s",retval);

 /* ------ ---------- end ------------ ------ */

 return retval;
}
#endif

#if !defined(_impl_HiCorba_Hi_add_)
#define _impl_HiCorba_Hi_add_ 1
static CORBA_long
impl_HiCorba_Hi_add(impl_POA_HiCorba_Hi *servant,
const CORBA_long numa,
const CORBA_long numb,
CORBA_Environment *ev)
{
 CORBA_long retval;
 /* ------   insert method code here   ------ */
 retval = numa+numb;
 /* ------ ---------- end ------------ ------ */

 return retval;
}
#endif

#if !defined(_impl_HiCorba_Hi_shutdown_)
#define _impl_HiCorba_Hi_shutdown_ 1
static CORBA_ORB _neo_ORB = CORBA_OBJECT_NIL;
static HiCorba_Hi _neo_servant =  CORBA_OBJECT_NIL;
static void set_NEO_ORB(CORBA_ORB theORB, HiCorba_Hi theServant)
{
	_neo_ORB = theORB;
	_neo_servant = theServant;
}

static void
impl_HiCorba_Hi_shutdown(impl_POA_HiCorba_Hi *servant,
CORBA_Environment *ev)
{
 /* ------   insert method code here   ------ */
 if(_neo_ORB==CORBA_OBJECT_NIL || _neo_servant ==  CORBA_OBJECT_NIL)return;
 //
 g_print("\nServer>executing the release");
 CORBA_Object_release(_neo_servant,ev);
 if((ev)->_major != CORBA_NO_EXCEPTION){
   g_error ("%s %s", "Object_release failed", CORBA_exception_id (ev));
   CORBA_exception_free (ev);
   abort();
 }
 //
 g_print("\nServer>executing the shutdown");
 CORBA_ORB_shutdown(_neo_ORB, CORBA_FALSE, ev);
 if((ev)->_major != CORBA_NO_EXCEPTION){
   g_error ("%s %s", "shutdown failed", CORBA_exception_id (ev));
   CORBA_exception_free (ev);
   abort();
 }
 exit(0);
 /* ------ ---------- end ------------ ------ */
}
#endif

MakeServer

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-server-ior.bin

Hi-server-ior.bin : Hi-common.o Hi-skelimpl.o Hi-skels.o Hi-server-ior.o
	$(CC) $(LDFLAGS) Hi-common.o Hi-skelimpl.o Hi-skels.o Hi-server-ior.o -o Hi-server-ior.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 MakeServer

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 !

CPP实现CORBA静态绑定(五)

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

在linux系统下,有很多开源的CORBA通讯框架,在C的示例中,使用了orbit的框架。
如我用的是Debian系统,可以直接安装:

apt-get install orbit2
apt-get install orbit2-nameserver
apt-get install liborbit2-dev

安装后,可以通过工具orb-rdl-2从idl文件生成需要的stubs及skeletons接口代码:

#该命令会生成文件:Hi.h、Hi-common.c、Hi-skels.c、Hi-stubs.c
orbit-idl-2 Hi.idl

可以通过工具orb-rdl-2从idl文件生成服务端代码:

#该命令会生成模板文件:Hi-skelimpl.c
orbit-idl-2 --skeleton-impl Hi.idl

首先完成服务端部分:
Hi-server-ns.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <orbit/orbit.h>
#include <ORBitservices/CosNaming.h>
#include <ORBitservices/CosNaming_impl.h>
#include "Hi.h"
#include "Hi-skelimpl.c"
/*
./Hi-server_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[])
{
	PortableServer_POA poa;
	HiCorba_Hi servant = CORBA_OBJECT_NIL;
	CosNaming_NamingContext ns =CORBA_OBJECT_NIL;
	CORBA_ORB orb=CORBA_OBJECT_NIL;
	CORBA_char name_service[]="Hi";
	CosNaming_NameComponent path[1]={name_service,""};
	CosNaming_Name name={1,1,path,CORBA_FALSE};
	CORBA_Environment ev;

	// init ORB
	g_print("\nServer>starting server...");
	g_print("\nServer>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");

	// activate POA
	g_print("\nServer>getting reference to RootPOA");
	poa= (PortableServer_POA) CORBA_ORB_resolve_initial_references(orb,"RootPOA",&ev);
	abort_if_exception(&ev, "CORBA_ORB_resolve_initial 'RootPOA' failed");
	g_print("\nServer>activating the POA Manager");
	PortableServer_POAManager_activate(PortableServer_POA__get_the_POAManager(poa, &ev),&ev);
	abort_if_exception(&ev, "POA_activate failed");

	// create servant
	g_print("\nServer>creating the servant");
	servant = impl_HiCorba_Hi__create (poa, &ev);
	abort_if_exception(&ev, "impl_HiCorba_Hi__create failed");
	
	// read name_service ior from ns.ior 
	CORBA_char  filename[] = "ns.ior";
 	FILE *file   = NULL;
	g_print("\nServer>reading the file '%s'",filename);
	if ((file=fopen(filename, "r"))==NULL)
                g_error ("could not open '%s'\n", filename);
	gchar objref[1024];
	fscanf (file, "%s", &objref);
	g_print("\nServer>getting the root naming context 'NameService' - from the file '%s'",filename);
	g_print("\nServer>the root naming context ior is '%s'",objref);

	// bind service 
	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");
	g_print("\nServer>binding the object reference in naming with name '%s'",name_service);
	CosNaming_NamingContext_rebind(ns,&name,servant,&ev);
	abort_if_exception(&ev, "rebind failed");
	
	// start 
	g_print("\nServer>running the orb...");
	CORBA_ORB_run(orb,&ev);
	abort_if_exception(&ev, "ORB_run failed");
}

Hi-skelimpl.c

/* This is a template file generated by command */
/* orbit-idl-2 --skeleton-impl Hi.idl */
/* User must edit this file, inserting servant  */
/* specific code between markers. */

#include <stdlib.h>
#include <stdio.h>
#include "Hi.h"

/*** App-specific servant structures ***/

#if !defined(_typedef_impl_POA_HiCorba_Hi_)
#define _typedef_impl_POA_HiCorba_Hi_ 1
typedef struct {
POA_HiCorba_Hi servant;
PortableServer_POA poa;
   /* ------ add private attributes here ------ */
   /* ------ ---------- end ------------ ------ */
} impl_POA_HiCorba_Hi;
#endif

/*** Implementation stub prototypes ***/

#if !defined(_decl_impl_HiCorba_Hi__destroy_)
#define _decl_impl_HiCorba_Hi__destroy_ 1
static void impl_HiCorba_Hi__destroy(impl_POA_HiCorba_Hi *servant,
CORBA_Environment *ev);
#endif

#if !defined(_decl_impl_HiCorba_Hi_sayHiTo_)
#define _decl_impl_HiCorba_Hi_sayHiTo_ 1
static CORBA_string
impl_HiCorba_Hi_sayHiTo(impl_POA_HiCorba_Hi *servant,
const CORBA_char * someone,
CORBA_Environment *ev);
#endif

#if !defined(_decl_impl_HiCorba_Hi_add_)
#define _decl_impl_HiCorba_Hi_add_ 1
static CORBA_long
impl_HiCorba_Hi_add(impl_POA_HiCorba_Hi *servant,
const CORBA_long numa,
const CORBA_long numb,
CORBA_Environment *ev);
#endif

#if !defined(_decl_impl_HiCorba_Hi_shutdown_)
#define _decl_impl_HiCorba_Hi_shutdown_ 1


static void
impl_HiCorba_Hi_shutdown(impl_POA_HiCorba_Hi *servant,
CORBA_Environment *ev);
#endif


/*** epv structures ***/

#if !defined(_impl_HiCorba_Hi_base_epv_)
#define _impl_HiCorba_Hi_base_epv_ 1
static PortableServer_ServantBase__epv impl_HiCorba_Hi_base_epv = {
NULL,             /* _private data */
(gpointer) & impl_HiCorba_Hi__destroy, /* finalize routine */
NULL,             /* default_POA routine */
};
#endif

#if !defined(_impl_HiCorba_Hi_epv_)
#define _impl_HiCorba_Hi_epv_ 1
static POA_HiCorba_Hi__epv impl_HiCorba_Hi_epv = {
NULL, /* _private */
(gpointer)&impl_HiCorba_Hi_sayHiTo,
(gpointer)&impl_HiCorba_Hi_add,
(gpointer)&impl_HiCorba_Hi_shutdown,
};
#endif


/*** vepv structures ***/

#if !defined(_impl_HiCorba_Hi_vepv_)
#define _impl_HiCorba_Hi_vepv_ 1
static POA_HiCorba_Hi__vepv impl_HiCorba_Hi_vepv = {
&impl_HiCorba_Hi_base_epv,
&impl_HiCorba_Hi_epv,
};
#endif


/*** Stub implementations ***/

#if !defined(_impl_HiCorba_Hi__create_)
#define _impl_HiCorba_Hi__create_ 1
static HiCorba_Hi impl_HiCorba_Hi__create(PortableServer_POA poa, CORBA_Environment *ev)
{
HiCorba_Hi retval;
impl_POA_HiCorba_Hi *newservant;
PortableServer_ObjectId *objid;

newservant = g_new0(impl_POA_HiCorba_Hi, 1);
newservant->servant.vepv = &impl_HiCorba_Hi_vepv;
newservant->poa = (PortableServer_POA) CORBA_Object_duplicate((CORBA_Object)poa, ev);
POA_HiCorba_Hi__init((PortableServer_Servant)newservant, ev);
   /* Before servant is going to be activated all
    * private attributes must be initialized.  */

   /* ------ init private attributes here ------ */
   /* ------ ---------- end ------------- ------ */

objid = PortableServer_POA_activate_object(poa, newservant, ev);
CORBA_free(objid);
retval = PortableServer_POA_servant_to_reference(poa, newservant, ev);

return retval;
}
#endif

#if !defined(_impl_HiCorba_Hi__destroy_)
#define _impl_HiCorba_Hi__destroy_ 1
static void
impl_HiCorba_Hi__destroy(impl_POA_HiCorba_Hi *servant, CORBA_Environment *ev)
{
    CORBA_Object_release ((CORBA_Object) servant->poa, ev);

    /* No further remote method calls are delegated to 
    * servant and you may free your private attributes. */
   /* ------ free private attributes here ------ */
   /* ------ ---------- end ------------- ------ */

POA_HiCorba_Hi__fini((PortableServer_Servant)servant, ev);

g_free (servant);
}
#endif

#if !defined(_impl_HiCorba_Hi_sayHiTo_)
#define _impl_HiCorba_Hi_sayHiTo_ 1
static CORBA_string
impl_HiCorba_Hi_sayHiTo(impl_POA_HiCorba_Hi *servant,
const CORBA_char * someone,
CORBA_Environment *ev)
{
 CORBA_string retval = CORBA_string_alloc(512);
 /* ------   insert method code here   ------ */
 sprintf(retval,"Hi, %s !",someone);
 g_print("\nServer>server is returning: %s",retval);

 /* ------ ---------- end ------------ ------ */

 return retval;
}
#endif

#if !defined(_impl_HiCorba_Hi_add_)
#define _impl_HiCorba_Hi_add_ 1
static CORBA_long
impl_HiCorba_Hi_add(impl_POA_HiCorba_Hi *servant,
const CORBA_long numa,
const CORBA_long numb,
CORBA_Environment *ev)
{
 CORBA_long retval;
 /* ------   insert method code here   ------ */
 retval = numa+numb;
 /* ------ ---------- end ------------ ------ */

 return retval;
}
#endif

#if !defined(_impl_HiCorba_Hi_shutdown_)
#define _impl_HiCorba_Hi_shutdown_ 1
static CORBA_ORB _neo_ORB = CORBA_OBJECT_NIL;
static HiCorba_Hi _neo_servant =  CORBA_OBJECT_NIL;
static void set_NEO_ORB(CORBA_ORB theORB, HiCorba_Hi theServant)
{
	_neo_ORB = theORB;
	_neo_servant = theServant;
}

static void
impl_HiCorba_Hi_shutdown(impl_POA_HiCorba_Hi *servant,
CORBA_Environment *ev)
{
 /* ------   insert method code here   ------ */
 if(_neo_ORB==CORBA_OBJECT_NIL || _neo_servant ==  CORBA_OBJECT_NIL)return;
 //
 g_print("\nServer>executing the release");
 CORBA_Object_release(_neo_servant,ev);
 if((ev)->_major != CORBA_NO_EXCEPTION){
   g_error ("%s %s", "Object_release failed", CORBA_exception_id (ev));
   CORBA_exception_free (ev);
   abort();
 }
 //
 g_print("\nServer>executing the shutdown");
 CORBA_ORB_shutdown(_neo_ORB, CORBA_FALSE, ev);
 if((ev)->_major != CORBA_NO_EXCEPTION){
   g_error ("%s %s", "shutdown failed", CORBA_exception_id (ev));
   CORBA_exception_free (ev);
   abort();
 }
 exit(0);
 /* ------ ---------- end ------------ ------ */
}
#endif

MakeServer

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-server-ns.bin

Hi-server-ns.bin : Hi-common.o Hi-skelimpl.o Hi-skels.o Hi-server-ns.o
	$(CC) $(LDFLAGS) Hi-common.o Hi-skelimpl.o Hi-skels.o Hi-server-ns.o -o Hi-server-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 MakeServer

CPP实现CORBA静态绑定(四)

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

然后完成客户端部分,同样分了三个文件:

HiClientImpl.hh

#include <iostream>
#include <fstream>
#include "Hi.hh"
                                                                                
using namespace std;
                                                                                
class HiClientImpl {
public:
   HiClientImpl();
   ~HiClientImpl();
   void TestAdd();
                                                                                
   CosNaming::Name m_corbaCosName;
                                                                                
   // CORBA ORB
   CORBA::ORB_var             m_orb;
   // ORB Object                                                                     
   CORBA::Object_var          m_obj;   
   // Resolved id to object reference   
   CORBA::Object_var          m_obj1;                                                                                  
   // Resolved and narrowed CORBA object for proxy calls
   HiCorba::Hi_var            m_Data;
};
                                                                                
class DS_ServerConnectionException{
public:
   DS_ServerConnectionException() { cerr << "CORBA COMM_FAILURE" << endl; };
};
                                                                                
class DS_SystemException{
public:
   DS_SystemException() { cerr << "CORBA Exception" << endl; };
};
                                                                                
class DS_FatalException{
public:
   DS_FatalException() { cerr << "CORBA Fatal Exception" << endl; };
};
                                                                                
class DS_Exception{
public:
   DS_Exception() { cerr << "Exception" << endl; };
};

HiClientImpl.cc

#include "assert.h"
#include "HiClientImpl.hh"
#include "Hi.hh"
                                                                                
HiClientImpl::HiClientImpl()
{
  try {
    //------------------------------------------------------------------------
    // Initialize ORB object.
    //------------------------------------------------------------------------
    int    argc=0;       // Dummy variables to support following call.
    char** argv=0;
    CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

    //------------------------------------------------------------------------
    // Bind ORB object to name service object.
    // (Reference to Name service root context.)
    //------------------------------------------------------------------------
    //CORBA::Object_var obj = orb->resolve_initial_references("OmniNameService");
    CORBA::Object_var obj = orb->resolve_initial_references("NameService");
    assert (!CORBA::is_nil(obj.in()));
    cerr << "resolve_initial_reference OK" << endl;
                                                                                
    //------------------------------------------------------------------------
    // Narrow this to the naming context (Narrowed reference to root context.)
    //------------------------------------------------------------------------
    CosNaming::NamingContext_var nc = CosNaming::NamingContext::_narrow(obj.in());
    assert (!CORBA::is_nil(nc.in()));
    cerr << "narrow ok" << endl;
                                                                                
    //------------------------------------------------------------------------
    // The "name text" put forth by CORBA server in name service.
    // This same name ("DataServiceName1") is used by the CORBA server when
    // binding to the name server (CosNaming::Name).
    //------------------------------------------------------------------------
    CosNaming::Name _corbaCosName;
    _corbaCosName.length(1);
    _corbaCosName[0].id=CORBA::string_dup("Hi");
                                                                                
    //------------------------------------------------------------------------
    // Resolve "name text" identifier to an object reference.
    //------------------------------------------------------------------------
    CORBA::Object_var obj1 = nc->resolve(_corbaCosName);
    assert(!CORBA::is_nil(obj1.in()));
    cerr << "resolve OK" << endl;
                                                                                
    m_Data = HiCorba::Hi::_narrow(obj1.in());
    if (CORBA::is_nil(m_Data.in()))
    {
       cerr << "IOR is not an SA object reference." << endl;
    }
    else{
        cerr << "narrow OK" << endl;
    }
  }
  catch(CORBA::COMM_FAILURE& ex) {
    cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
         << "object." << endl;
    throw DS_ServerConnectionException();
    return;
  }
  catch(CORBA::SystemException& ) {
    cerr << "Caught a CORBA::SystemException." << endl;
    throw DS_SystemException();
    return;
  }
  catch(CORBA::Exception& ) {
    cerr << "Caught CORBA::Exception." << endl;
    throw DS_Exception();
    return;
  }  catch(omniORB::fatalException& fe) {
    cerr << "Caught omniORB::fatalException:" << endl;
    cerr << "  file: " << fe.file() << endl;
    cerr << "  line: " << fe.line() << endl;
    cerr << "  mesg: " << fe.errmsg() << endl;
    throw DS_FatalException();
    return;
  }
  catch(...) {
    cerr << "Caught unknown exception." << endl;
    throw DS_Exception();
    return;
  }
  return;
}
                                                                                
HiClientImpl::~HiClientImpl()
{
}
                                                                                
void HiClientImpl::TestAdd()
{
   CORBA::Long num1=70;
   CORBA::Long num2=80;
   CORBA::Long retNum;
                                                                                
   cout << "Values input to Server: "
        << num1 << " "
        << num2 << " " << endl;
                                                                                
   if( retNum = m_Data->add( num1, num2)) // This is the CORBA call which is to be executed remotely
   {    // Ok
      cout << "Values returned by Server: "
           << num1 << "+"
           << num2 << "="
           << retNum << endl;
   }
}

HiClient.cc

#include "HiClientImpl.hh"
                                                                                
int main(int argc, char** argv)
{
   HiClientImpl impl;  
   impl.TestAdd();                                                      
   return 0;
}

MakeClient

CC            = /usr/bin/g++
CPPFLAGS      = -g -c -I/usr/include -I/usr/include/omniORB4
LDFLAGS       = -g -L/usr/lib -lomniORB4 -lomnithread -lomniDynamic4
OMNIIDL       = /usr/bin/omniidl
                                                                                
all: HiClient.bin

HiClient.bin: HiClient.o HiClientImpl.o HiSK.o
	$(CC) $(LDFLAGS) -o HiClient.bin HiClient.o HiClientImpl.o HiSK.o
                                                                                
HiSK.o: HiSK.cc Hi.hh
	$(CC) $(CPPFLAGS) $(INCLUDES) HiSK.cc
                                                                                
HiClient.o: HiClient.cc HiClientImpl.hh
	$(CC) $(CPPFLAGS) HiClient.cc
                                                                                
HiClientImpl.o: HiClientImpl.cc HiClientImpl.hh Hi.hh
	$(CC) $(CPPFLAGS) HiClientImpl.cc
                                                                                
nidl: Hi.idl
	$(OMNIIDL) -bcxx Hi.idl
                                                                                
clean:
	rm -f *.o

编译

make -f MakeClient

然后是运行

#首先检查OMNI Naming Service是否在运行,默认端口2809
ps -aux | grep omni
root      1585  0.0  0.2 315608  4912 ?        Sl   12:04   0:00 /usr/bin/omniNames -errlog /var/log/omniorb-nameserver.log
neohope   1943  0.0  0.1  12720  2180 tty2     S+   14:57   0:00 grep omni

#运行服务端
./HiServer

#运行客户端
./HiClient
>>Values input to Server: 70 80
>>Values returned by Server: 70+80=150