1.题目分析 实验一:CORBA组件编程实现DOS窗口显示“Hello,World!+班级+中文姓名” 实验二:编写实现连加、连减和加减混合等数学++/- -运算。 在CORBA组件编程设计思路下,两个实验大体框架相同,即编写对象接口,编写服务端应用程序,编写客户端操作程序。实验一只有一个功能,打印字符串,所以直接定义接口,实验二是进行数学加减运算,可以封装成对象,包含加,减方法,然后编写程序具体实现,然后是编写客户端和服务器端程序即可。 2.CORBA模型分析 实验一:
实验二:
3.组件实现 程序源代码 实验一: 1接口的定义:HelloWorld.idl
module sample{ interface HelloWorld{ wstring sayHello(); //wstring类型可以插入中文 }; };2服务端程序:HelloWorldServer.java
import sample.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import org.omg.CORBA.portable.*; import org.omg.PortableServer.*; class HelloWorldServant extends HelloWorldPOA{ //对象实现类 public String sayHello(){ return "\nHello World!\n"; } } public class HelloWorldServer{ //服务程序 public static void main(String args[]){ try{ //初始化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(); //创建伺服对象 HelloWorldServant objRef = new HelloWorldServant(); 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); } } }3客户端程序: HelloWorldClient.java
import sample.*; import org.omg.CosNaming.*; import org.omg.CORBA.*; public class HelloWorldClient { public static void main(String args[]) { try{ ORB orb = ORB.init(args, null); org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); NameComponent nc = new NameComponent("Hello",""); NameComponent path[] = {nc}; HelloWorld helloWorld = HelloWorldHelper.narrow(ncRef.resolve(path)); String hello = helloWorld.sayHello(); System.out.println(hello); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } } }实验二: 1 IDL接口counter.idl:
module CounterApp{ interface Counter{ readonly attribute long value; void inc(); void dec(); }; };2 对象实现代码:CounterImpl.java
import CounterApp.*; public class CounterImpl extends CounterPOA { private int count; public CounterImpl(){ count = 0; } public void inc(){ count++; } public void dec(){ count - -; } public int value(){ return count; } }3服务端程序: Server.java
import CounterApp.*; import java.io.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import org.omg.CORBA.portable.*; import org.omg.PortableServer.*; public class Server { public static void main(String[] args){ try{ 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); org.omg.PortableServer.POAManager manager = rootPOA.the_POAManager(); 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); } } }4客户端程序: Client.java
import CounterApp.*; import java.util.*; import java.io.*; import org.omg.CORBA.*; import org.omg.CosNaming.*; public class Client { public static void main(String[] args){ try{ ORB orb = ORB.init(args, null); org.omg.CORBA.Object obj = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(obj); NameComponent nc = new NameComponent("Count",""); NameComponent path[] = {nc}; String ref = null; try{ 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); } } }4.测试、调试及运行结果 实验1: 启动名字服务器:
启动服务器端程序
启动客户端程序:
实验二: 编译CounterImpl.Java出现了问题,count–格式错误,更改后正常编译
启动名字服务器: 启动服务器端程序
启动客户端程序,正常运行:
5.经验归纳 CORBA是为了实现分布式计算而出现,它实现了跨平台,通过IDL接口定义语言,做到与语言无关。书中给出的一种理解方式是CORBA是一种异平台下的与语言无关的对象互操作模型。一开始这个模型给我的感觉就是一个cs模式,但实际变了一点,它是一种多客户/服务器结构,而且二者不能直接通信,而是依靠中间代理ORB实现。这个模型中两个关键词就是IDL和ORB,ORB实现了平台无关,IDL则实现了语言无关。