组件化开发方法,CORBA模型,显示字符串,实现连加连减

  • 题目分析

目的:

  1. 掌握组件化开发的概念,了解CORBA模型及ORB机制;
  2. 掌握CORBA组件编程方法。

 步骤:

1.配制环境JDK环境。

2.编写编译IDL接口。

3.编写编译服务端程序。

4.编写编译客户端程序。

5.运行测试与调试。

6.完成报告。

题目一:要求编写实现显示“Hello,World!+班级+中文姓名”字符串。

题目二:编写实现连加、连减和加减混合等数学++/- -运算,并进行测试。

  • CORBA模型分析

IDL是CORBA定义的语言,IDL是一种接口定义语言

  • 组件实现

首先配置环境变量,打开计算机属性—>系统属性—>环境变量,新建或编辑Java_Home变量名,变量值为D:\Java\jdk1.8.0_201,CLASSPATH变量值为.;JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;  ,Path变量值为%JAVA_HOME%\jre\bin  。

题目一:实现显示“Hello,World!+班级+中文姓名”字符串

  1. 建立HelloWorld文件夹,并在文件夹中编写IDL接口HelloWorld.idl先建一个HelloWorld文本文档,编写好后将文档后缀.txt改为.idl,这里注意需打开文件扩展名)将string改成wstring,用于处理多字节的字符串,例如:中文

2.编译IDL接口:C:\Users\Administrator\Desktop\HelloWorld>idlj -fall HelloWorld.idl编译结果生成sample包,生成下述文件

3.编写并编译服务端程序:HelloWorldServer.java在HelloWorld文件夹中新建文本文档编写并修改后缀名为.java,修改sayhello方法中返回字符串为HelloWorld+班级+姓名!

4.编写并编译客户端程序: HelloWorldClient.java

5.在cmd命令提示符中输入javac HelloWorldServer.java和javac HelloWorldClient.java编译class文件

6.编译好后生成下述文件

7.运行

启动名字服务器:C:\Users\Administrator\Desktop\HelloWorld >tnameserv -ORBInitialPort 1050

启动服务端程序:C:\Users\Administrator\Desktop\HelloWorld >java HelloWorldServer -ORBInitialPort 1050

输出:HelloWorld is running

启动客户端程序:X:\corba >java HelloWorldClient -ORBInitialPort 1050

输出:Hello World   软工1704班   陈亚娟!

题目二:实现连加、连减和加减混合等数学++/- -运算,并进行测试

  1. 建立Counter文件夹,并在文件夹中编写IDL接口counter.idl先建一个counter文本文档,编写好后将文档后缀.txt改为.idl

2.编译IDL接口:C:\Users\Administrator\Desktop\Counter>idlj -fall Counter.idl编译结果生成CounterAPP包,生成下述文件

3.编写并编译对象实现代码:CounterImpl.java在Counter文件夹中新建文本文档编写并修改后缀名为.java

4.编写并编译服务端程序: 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);

}

}

}

5.编写并编译客户端程序: 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);   

}   

}   

}

6.在cmd命令提示符中输入javac CounterImpl.java ,javac Server.java和javac Client.java编译class文件

7.编译好后生成以下文件:

8.运行

启动名字服务器:C:\Users\Administrator\Desktop\Counter >tnameserv -ORBInitialPort 1050

启动服务端程序:C:\Users\Administrator\Desktop\Counter >java Server -ORBInitialPort 1050

输出:Server started. Stop: Ctrl-c

启动客户端程序:C:\Users\Administrator\Desktop\Counter  >java Client -ORBInitialPort 1050

四.测试、调试及运行结果

题目一:实现显示“Hello,World!+班级+中文姓名”字符串:

题目二:实现连加连减:

五.经验归纳

在配置环境时遇到问题无法在cmd中打开文件,将JAVA_HOME变量值改为D:\Java\jdk1.8.0_201后成功打开。

CORBA处理字符串有:String和wstring两种类型,string类型主要用于处理ASCII类型的字符串,wstring用于处理多字节的字符串,例如:中文

用javac编译class文档。

COMBA通过IDL接口定义语言,能做到与语言无关,是一种中间件技术,体系结构的核心是ORB,ORB是使得客户应用程序能调用远端对象方法的一种机制。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值