要求:编写实现连加、连减和加减混合等数学++/- -运算,并进行测试。
1. 此程序与CoORBA程序1实现方法相同。生成counter包。
module CounterApp{
interface Counter{
readonly attribute long value;
void inc();
void dec();
};
};
2. server端实现代码
1) CounterImpl类,实现inc(),dec(),value()等方法:
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; //返回值
}
}
2)server类,服务启动的代码,这段代码主要功能是将接口实现注册到ORB中,并启动监听,等待client来调用。
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);
}
}
}
3. 客户端实现
创建CLient类,实现客户端:
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.......");//自己加的连接测试代码
//初始化Ip和端口号,-OR.BInitialHost 127.0.0.1 -ORBInitialPort 1050
args[0] = "-ORBInitialHost";
//server端的IP地址,在HelloServer中定义的
args[1] = "127.0.0.1";
args[2] = "-ORBInitialPort";
//server端的端口,在HelloServer中定义的
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);
}
}
}
服务端与客户端配置好之后,输入以下命令来启动服务:
cd /d %JAVA_HOME%\bin
orbd -ORBInitialPort 1050 -ORBInitialHost 127.0.0.1
服务器端测试:输出Server started.Stop:Ctrl-c
客户端测试
以上程序测试成功