Java版CORBA程序

Helloworld程序

1. 步骤

  1. 编写IDL接口 HelloWorld.idl:
 module sample{
interface HelloWorld{ 
wstring sayHello();   
};  
};

将文件拖入jdk所在的bin文件目录下,输入命令:idlj -fall HelloWorld.idl,执行完毕后,在bin目录下生成了名为sample的文件夹,其中有6个类,并将sample导入Eclipse中新建的HelloWorld工程下的src中。
2. 服务端代码

package server;
import sample.HelloWorldPOA;
public class HelloWorldImpl extends HelloWorldPOA {
	/*
	 * 服务器端的实现代码
	 * 
	 * @see sample.HelloWorldOperations#sayHello()
	 */
	public String sayHello() {
		return "helloWorld!,软件工程****班,***,16408070907";
	}
}
package server;
import org.omg.CosNaming.*;
import sample.HelloWorld;
import org.omg.CORBA.*;
public class HelloWorldServer {
	public static void main(String args[]) {
		try {
			args = new String[2];
			args[0] = "-ORBInitialPort";
			args[1] = "1050";
			// 初始化ORB
			ORB orb = ORB.init(args, null);
			// 取根POA的引用
			org.omg.CORBA.Object poaobj = orb.resolve_initial_references("RootPOA");
			org.omg.PortableServer.POA rootPOA = org.omg.PortableServer.POAHelper.narrow(poaobj);
			org.omg.PortableServer.POAManager manager = rootPOA.the_POAManager();
			// 创建伺服对象
			HelloWorldImpl objRef = new HelloWorldImpl();
			HelloWorld obj = objRef._this(orb);
			// 绑定命名服务
			NamingContext ncRef = NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));
			NameComponent nc = new NameComponent("Hello", "");
			NameComponent path[] = { nc };
			ncRef.rebind(path, obj);
			// 激活POA管理器
			manager.activate();
			// 等待处理客户程序的请求
			System.out.println("HelloWorld is running!");
			orb.run();
		} catch (Exception e) {
			System.err.println("ERROR: " + e);
			e.printStackTrace(System.out);
		}
	}
}
///*cd /d  %JAVA_HOME%\bin
//orbd -ORBInitialPort 1050 -ORBInitialHost 127.0.0.1
  1. 客户端代码
package Client;
import sample.HelloWorld;
import sample.HelloWorldHelper;
import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
public class HelloWorldClient {
	static HelloWorld helloWorld;
	 
	static {
		System.out.println("client开始连接server.......");
		String args[] = new String[4];
		args[0] = "-ORBInitialHost";
		args[1] = "127.0.0.1";
		args[2] = "-ORBInitialPort";
		args[3] = "1050";
		ORB orb = ORB.init(args, null);
		org.omg.CORBA.Object objRef = null;
		try {
		objRef = orb.resolve_initial_references("NameService");
		} catch (InvalidName e) {
			e.printStackTrace();
		}
		NamingContextExt neRef = NamingContextExtHelper.narrow(objRef);
		String name = "Hello";
		try {
			helloWorld = HelloWorldHelper.narrow(neRef.resolve_str(name));
		} catch (NotFound e) {
			e.printStackTrace();
		} catch (CannotProceed e) {
			e.printStackTrace();
		} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
			e.printStackTrace();
		}
		 
		System.out.println("client connected server.......");
	}
	 
	public static void main(String args[]) throws Exception {
		sayHello("");
	}
	public static void sayHello(String name) {
		//String str[] = helloWorld.sayHello();
		//String str=helloWorld.sayHello();
		//System.out.print(helloWorld.sayHello());
		String str=helloWorld.sayHello();
		System.out.println(str);
	}
}

2. 运行

运行命令:
cd /d %JAVA_HOME%\bin
orbd -ORBInitialPort 1050 -ORBInitialHost 127.0.0.1
进行服务端和客户端的测试。

Counter程序

1. 步骤

  1. 编写IDL接口
    module CounterApp{
    interface Counter{
    readonly attribute long value;
    void inc();
    void dec();
    };
    };
  2. 服务端代码
package sever;
import CounterApp.*;
public class CounterImpl extends CounterPOA{
	private int count;   
    public CounterImpl(){   
        count = 0;   
    }   
    public void inc(){   
        count++;  
    }   
    public void dec(){   
        count --;  
        System.out.println(count--);
    }   
    public int value(){   
        return count;   
    }   

}
package sever;
import CounterApp.*;
import org.omg.CosNaming.*;
import java.io.*;   
import org.omg.CORBA.*;
public class Server {
	public static void main(String[] args){
		try{
			args = new String[2];
			args[0] = "-ORBInitialPort";
			args[1] = "1050";
		    ORB orb = ORB.init(args, null);
		org.omg.CORBA.Object poaobj = orb.resolve_initial_references ("RootPOA");
		org.omg.PortableServer.POA rootPOA = org.omg.PortableServer.POAHelper.narrow(poaobj);
		CounterImpl c_impl = new CounterImpl();
		Counter c = c_impl._this(orb);
		NamingContext ncRef = NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));
		NameComponent nc = new NameComponent("Count", "");
		NameComponent path[] = {nc}; 
		ncRef.rebind(path, c);
		FileOutputStream file = new FileOutputStream("Counter.ref");
		PrintWriter writer = new PrintWriter(file);
		String ref = orb.object_to_string(c);
		writer.println(ref);
		writer.flush();
		file.close();
		System.out.println("Server started."+" Stop:Ctrl-c");
		rootPOA.the_POAManager().activate();
		orb.run();
		}catch(IOException ex){
		System.out.println("File error:"+ex.getMessage());
		System.exit(2);
		}catch(Exception ex){
		System.out.println("Exception: "+ex.getMessage());
		System.exit(1);
		}
		}

}
  1. 客户端代码
package Client;
import CounterApp.*;  
import java.util.*;   
import java.io.*;   
import org.omg.CORBA.*; 
public class Client {
	public static void main(String[] args){  
		try{   
			 
			System.out.println("client开始连接server.......");
			args[0] = "-ORBInitialHost";
			args[1] = "127.0.0.1";
			args[2] = "-ORBInitialPort";
			args[3] = "1050";
		    ORB orb = ORB.init(args, null);
		    org.omg.CORBA.Object obj = orb.resolve_initial_references("NameService"); 
		   String ref = null; 
		   try{   
		   @SuppressWarnings("resource")
		Scanner reader = new Scanner(new File("Counter.ref"));   
		   ref = reader.nextLine();   
		    }catch(IOException ex){   
		    System.out.println("File error: "+ex.getMessage());   
		    System.exit(2);   
		    }   
		obj = orb.string_to_object(ref);   
		if(obj == null){   
		System.out.println("Invalid IOR");   
		System.exit(4);   
		}   
		Counter c = null;   
		try{   
		c = CounterHelper.narrow(obj);   
		}catch(BAD_PARAM ex){   
		System.out.println("Narrowing failed");   
		System.exit(3);   
		}   
		int inp = -1;   
		do{   
		System.out.print("Counter value: "+c.value()+"\nAction(+/-/e)?");   
		System.out.flush();   
		do{   
		try{   
		inp = System.in.read();   
		}catch(IOException ioe){}   
		}while(inp != '+' && inp != '-' && inp != 'e');   
		if(inp == '+')   
		c.inc();   
		else if(inp == '-')   
		c.dec();   
		}while(inp != 'e');   
		}catch(Exception ex){   
		System.out.println("Exception: "+ex.getMessage()); 
		System.exit(1);   
		}  
		
		}   

}

2. 运行

进行服务端和客户端测试

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值