Java实现CORBA静态绑定(六)

本文主要内容涉及:

  • CORBA基本架构
  • IDL文件编写
  • POA示例实现
  • POA+TIE示例实现
  • ImplBase示例实现
  • ImplBase+TIE示例实现
  • Persistent示例实现

ImplBase主要是为了兼容旧代码所提供的,正常情况下,大家是不需要使用的。
JDK提供了工具,可以直接生成stubs及skeletons接口代码:

idlj -fall -oldImplBase Hi.idl
idlj -fallTie -oldImplBase Hi.idl

编写server端代码:

import HiCorba.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.util.Properties;

class HiImpl extends _HiImplBase{
	private ORB orb;
	public void setORB(ORB orb_val){
		orb = orb_val;
	}
	// implement sayHiTo() method
	public String sayHiTo(String someone) {
		return "\nHi, "+someone+" !"+"\n";
	}
	// implement add() method
	public int add(int numa, int numb) {
		return numa+numb;
	}
	// implement shutdown() method
	public void shutdown() {
		orb.shutdown(false);
	}
}

public class HiServer {
	public static void main(String args[]) {
		try{
			// create and initialize the ORB
			ORB orb = ORB.init(args, null);
			// create servant and register it with the ORB
			HiImpl hiImpl = new HiImpl();
			hiImpl.setORB(orb);
			// create a tie, with servant being the delegate.
			Hi_Tie tie = new Hi_Tie(hiImpl);
			// obtain the objectRef for the tie
			// this step also implicitly activates the object
			Hi href =  HiHelper.narrow(tie);
			// get the root naming context
			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
			NamingContext ncRef = NamingContextHelper.narrow(objRef);
			// bind the Object Reference in Naming
			NameComponent nc = new NameComponent("Hi", "");
			NameComponent path[] = {nc};
			ncRef.rebind(path, href);
			System.out.println("HiServer 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("HiServer Exiting ...");
	}
}

编写client端代码:

import HiCorba.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;

public class HiClient{
	public static void main(String args[]){
		try{
			// create and initialize the ORB
			ORB orb = ORB.init(args, null);
			// get the root naming context
			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
			NamingContext ncRef = NamingContextHelper.narrow(objRef);
			// resolve the Object Reference in Naming
			NameComponent nc = new NameComponent("Hi", "");
			NameComponent path[] = {nc};
			Hi hiImpl = HiHelper.narrow(ncRef.resolve(path));
			System.out.println("Obtained a handle on server object: " + hiImpl);
			System.out.println(hiImpl.sayHiTo("neohope"));
			System.out.println(hiImpl.add(70, 80));
			hiImpl.shutdown();
		}
		catch (Exception e) {
			System.out.println("ERROR : " + e) ;
			e.printStackTrace(System.out);
		}
	}
}

编译代码:

javac *.java HiCorba/*.java

测试,在三个shell或cmd窗口中依次运行:

#shell01
orbd -ORBInitialPort 1900
#shell02
java HiServer -ORBInitialPort 1900
>>HiServer ready and waiting ...
>>HiServer Exiting ...
#shell03
java HiClient -ORBInitialPort 1900 -ORBInitialHost localhost
>>Obtained a handle on server object: IOR:000000000000001349444c3a4869436f7262612f
  48693a312e30000000000001000000000000006e000102000000000d3139322e3136382e35362e31
  00007dfd00000019afabcb000000000276c739500000000800000001000000001400000000000002
  00000001000000200000000000010001000000020501000100010020000101090000000100010100
  00000026000000020002
>>Hi, neohope !
>>150

Leave a Reply

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

*