JAVA版CORBA程序2——Counter

要求:
编写实现连加、连减和加减混合等数学++/- -运算,并进行测试。
1 .编写IDL接口counter.idl:

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

2.编译IDL接口:X:\corba>idlj –fall counter.idl
编译结果生成CounterApp包,生成下述文件
_CounterStub.java
Counter.java
CounterHelper.java
CounterHolder.java
CounterOperations.java
CounterPOA.java

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

最后一步:运行
启动名字服务器:X:\corba >tnameserv -ORBInitialPort 1050
启动服务端程序:X:\corba >java Server -ORBInitialPort 1050
输出:Server started. Stop: Ctrl-c
启动客户端程序:X:\corba >java Client -ORBInitialPort 1050

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值