要求:

1)要求编写实现显示“Hello,World!+班级+中文姓名”字符串

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

一、JAVA版CORBA程序1——HelloWorld

实现详解:

1 编写IDL接口HelloWorld.idl

module sample{
interface HelloWorld{
wstring sayHello();   
};  
};

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

2编译IDL接口:D:\>idlj –fall HelloWorld.idl

JAVA版CORBA程序练习

编译结果生成sample包,生成下述文件,将sample直接复制到了自己新建的工程里。

JAVA版CORBA程序练习

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

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

JAVA版CORBA程序练习

5 运行

要记得修改端口号

JAVA版CORBA程序练习

启动名字服务器:D:\ >tnameserv -ORBInitialPort 1050

JAVA版CORBA程序练习

启动服务端程序:D:\ >java HelloWorldServer -ORBInitialPort 1050

输出:HelloWorld is running

JAVA版CORBA程序练习

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

输出:Hello World!软工1601王予诺!!!

JAVA版CORBA程序练习

二、JAVA版CORBA程序2——Counter

1 编写IDL接口counter.idl:

module CounterApp{   
    interface Counter{   
        readonly attribute long value;   
        void inc();   
        void dec();   
    };   
};

2编译IDL接口:D:\>idlj –fall counter.idl

JAVA版CORBA程序练习

编译结果生成CounterApp包,生成下述文件

JAVA版CORBA程序练习

3 编写并编译对象实现代码: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;   
    }   
}

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);   
}   
}   
}

 

JAVA版CORBA程序练习

5 运行

记得修改端口号

JAVA版CORBA程序练习

启动名字服务器:D:\>tnameserv -ORBInitialPort 1050

JAVA版CORBA程序练习

启动服务端程序:D:\>java Server -ORBInitialPort 1050

输出:Server started. Stop: Ctrl-c

JAVA版CORBA程序练习

启动客户端程序:D:\>java Client -ORBInitialPort 1050

JAVA版CORBA程序练习

相关文章:

  • 2019-10-07
  • 2021-09-02
  • 2021-09-02
  • 2021-08-07
  • 2021-11-03
  • 2021-09-02
  • 2021-09-02
  • 2021-10-13
猜你喜欢
  • 2021-09-02
  • 2021-08-27
  • 2021-07-07
  • 2021-06-21
  • 2021-11-03
  • 2021-10-27
相关资源
相似解决方案