一般的CORBA程序都会需要启动一个命名服务,然后在命名服务进行绑定或者解析工作。至于绑定和解析的内容实际上就是CORBA对象的引用。
不使用命名服务进行通信的原理其实很简单,也就是不需要去命名服务绑定,解析引用,换个地方式去搞定这个交互过程。至于传递的内容有很多种,例如IOR,corbaloc,file等。我现在用的就是IOR值
IOR其实就是从orb.object_to_string(Object)方法获得,然后又用orb.string_to_object(String)将IOR变为CORBA对象。
弄点代码吧
服务端:
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// get reference to rootpoa & activate the POAManager
POA rootpoa =
(POA)orb.resolve_initial_references("RootPOA");
rootpoa.the_POAManager().activate();
// create servant and register it with the ORB
CommImpl helloImpl = new CommImpl();
helloImpl.setORB(orb);
// get object reference from the servant
org.omg.CORBA.Object ref =
rootpoa.servant_to_reference(helloImpl);
System.out.println(orb.object_to_string(ref));//打印IOR字符串
System.out.println
("Test Server ready and waiting ...");
// wait for invocations from clients
orb.run();
}
catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("TestServer Exiting ...");
客户端代码:
try {
// create and initialize the ORB
ORB orb = ORB.init(args, null);
System.out.println("ORB initialised/n");
org.omg.CORBA.Object objRef = orb.string_to_object("IOR:"+ior);ior就是服务端打印出来的字符串
agentImpl = AgentHelper.narrow(objRef);
DataEvent dvin = new DataEvent();
dvin.eventName = "testin";
dvin.eventNo=1;
dvin.protocol="";
dvin.recverName="";
dvin.senderName="";
dvin.data="testin".getBytes();
DataEventHolder holder = new DataEventHolder();
int result = agentImpl.excute(dvin, holder);
DataEvent out = holder.value;
System.out.println("reulst: " + result + " out:" + out.eventName);
} catch (Exception e) {
System.out.println("ERROR : " + e);
e.printStackTrace(System.out);
}
很简单吧,不需要在命名服务寻找“NameService”节点,不需要绑定节点。直接就可以通信咯