组件化开发方法
作者:互联网
1、 题目分析
实验目的:
掌握组件化开发的概念,了解CORBA模型及ORB机制;
掌握CORBA组件编程方法。
题目:
题目一:首先编写一个后缀名为idl的接口HelloWorld,用idlj –fall HelloWorld.idl,这句话就能生成一个sample包里面有相应的各种class,在此基础上编译运行HelloWorldClient和HelloWorldServer端,分别实现编译后运行出现hello world,helloworld is running这两个结果,并修改响应消息改为hello world !1702 Huliuqing,再次编译出现这句话。
题目二:定义一个counter.idl接口,里面有加和减等运算方法,编译该接口自动生成CounterApp包,编写对象程序,客户端,服务端程序,启动端口,程序,实现加和减以及混合操作。
2、 CORBA模型分析
对于HelloWorld,CORBA服务器要有一个ORB对象,每个服务器实例化一个ORB,并向其注册服务对象,因此当ORB接收到调用请求时可以寻找到服务器。最后是服务对象的管理,服务器是一个进程,实例化了一个或多个服务对象,服务对象具体实现接口中说明的操作。HelloServer和命名服务一起工作,使得服务对象对于客户机来说是可用的,服务器需要对名字服务的对象引用,可以向名字服务注册,并保证向Hello接口的调用被路由到其服务对象上,最后是等待客户机的调用。
对于Counter,Client调用定义的对象方法 Client通过Name Service查询获得要访问的对象的引用ORB Client通过这个引用调用对象的方法,因为ORB中有足够的信息来定位一个对象 这个调用被传递给ORB (3).调用的完成 Client端的调用请求通过ORB被传递给正确的Server端的ORB,定位是根据ORB实现的。这个ORB把调用请求交给真正的Server进行处理 Server又根据orb定位产生这个ORB的POA,并把请求传给它。 POA又把请求传给最后真正的Servant,完成调用并返回。
2、 源程序
1 编写IDL接口HelloWorld.idl:
module sample{
interface HelloWorld{
string sayHello();
};
};
编译IDL接口:X:\corba>idlj –fall HelloWorld.idl
编译结果生成sample包,生成下述文件
_HelloWorldStub.java
HelloWorld.java
HelloWorldHelper.java
HelloWorldHolder.java
HelloWorldOperations.java
HelloWorldPOA.java
编写并编译服务端程序: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);
}
}
}
编写并编译客户端程序: 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);
}
}
}
启动名字服务器:X:\corba >tnameserv -ORBInitialPort 1050
启动服务端程序:X:\corba >java HelloWorldServer -ORBInitialPort 1050
输出:HelloWorld is running
启动客户端程序:X:\corba >java HelloWorldClient -ORBInitialPort 1050
输出:Hello World!
题目二
编写IDL接口counter.idl:
module CounterApp{
interface Counter{
readonly attribute long value;
void inc();
void dec();
};
};
编译IDL接口:X:\corba>idlj –fall counter.idl
编译结果生成CounterApp包,生成下述文件
_CounterStub.java
Counter.java
CounterHelper.java
CounterHolder.java
CounterOperations.java
CounterPOA.java
编写并编译对象实现代码: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;
}
}
编写并编译服务端程序: 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);
}
}
}
编写并编译客户端程序: 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);
}
}
}
启动名字服务器:X:\corba >tnameserv -ORBInitialPort 1050
启动服务端程序:X:\corba >java Server -ORBInitialPort 1050
输出:Server started. Stop: Ctrl-c
启动客户端程序:X:\corba >java Client -ORBInitialPort 1050
3、 程序测试结果截图:
题目二运行截图
4、 经验归纳
本次程序设计以掌握组件化开发的概念,了解CORBA模型及ORB机制;掌握CORBA组件编程方法为目标。在运行程序过程中遇到了很多问题。主要是因为对CORBA运行步骤不熟悉,通过查课本和上网查询初步了解。希望下次对运行步骤更加熟练。
标签:java,omg,System,开发方法,import,组件,org,ORB 来源: https://blog.csdn.net/qq_44727254/article/details/90483016