- 论坛徽章:
- 0
|
我刚刚学corba,试着用java编写了一点简单的程序,在编写实现DSI时遇见问题,Server段好像没问题,但运行Client端会报错,不知道是那里边错了,请大家帮忙看看,谢谢!!!
- Hello.idl:
- module HelloApp
- {
- interface Hello
- {
- string printHelloArgs(in string arg1, in short arg2);
- };
- };
复制代码
- HelloServer.java
- import HelloApp.*;
- import java.util.*;
- import org.omg.CosNaming.*;
- import org.omg.CosNaming.NamingContextPackage.*;
- import java.io.*;
- import org.omg.CORBA.*;
- // servant 必须扩展 DynamicImplementation
- class HelloImpl extends DynamicImplementation
- {
- // 存储已实现的接口的存储库标识符
- static String[] myIDs = {"IDL:JavaIDL/DSIExample:1.0"};
- ORB orb;
- // 创建对 ORB 的引用
- HelloImpl(ORB orb) {
- this.orb = orb;
- }
- // 必须实现 invoke() 以处理请求
- public void invoke(ServerRequest request) {
- try {
- System.out.println("DSI: invoke called, op = "+request.op_name());
- // 创建 NVList 以保留参数
- NVList nvlist = orb.create_list(0);
- // 每个方法名都需要如下 if 语句
- if (request.op_name().equals("printHelloArgs") == true) {
- // 每个参数都需要 Any
- Any any1 = orb.create_any();
- any1.insert_string("");
- nvlist.add_value("arg1", any1, ARG_IN.value);
- Any any2 = orb.create_any();
- any2.insert_string("");
- nvlist.add_value("arg2", any2, ARG_IN.value);
- // 将 NVList 传递到请求以获取值
- request.params(nvlist);
- System.err.println("Argument 1: In value: " + nvlist.item(0).value().extract_string());
- System.err.println("Argument 2: In value: " + nvlist.item(1).value().extract_short());
- TypeCode result_tc = orb.get_primitive_tc(TCKind.tk_void);
- Any result_any = orb.create_any();
- result_any.type(result_tc);
- request.result(result_any);
- }
- }
- catch (Exception ex) {
- ex.printStackTrace();
- System.out.println("DSIExample: Exception thrown: " + ex);
- }
- }
- // 实现 an _ids 方法以返回接口的存储库标识符
- public String[] _ids() {
- return myIDs;
- }
- }
- // 以常规方式实现的 HelloServer
- public class HelloServer {
- public static void main(String args[])
- {
- try{
- // 创建并初始化 ORB
- ORB orb = ORB.init(args, null);
- // 创建 servant 并注册到 ORB
- HelloImpl helloRef = new HelloImpl(orb);
- orb.connect(helloRef);
- File fi = new File("ior");
- java.io.OutputStream f = new java.io.FileOutputStream(fi);
- java.io.DataOutputStream out = new java.io.DataOutputStream(f);
- String ior = orb.object_to_string(helloRef);
- out.writeBytes(ior);
- out.close();
- System.out.println("IOR is "+ ior);
- // 等待客户机调用
- orb.run();
- } catch (Exception e) {
- System.err.println("ERROR: " + e);
- e.printStackTrace(System.out);
- }
- }
- }
复制代码
- HelloClient.java
- import java.io.*;
- import HelloApp.*;
- import org.omg.CORBA.*;
- import org.omg.CosNaming.*;
- import org.omg.PortableServer.*;
- public class HelloClient{
- public static void main(String[] args){
- try{
- ORB orb = ORB.init(args,null);
- POA rootpoa = (POA)orb.resolve_initial_references("RootPOA");
- rootpoa.the_POAManager().activate();
-
- FileInputStream fis = new FileInputStream("ior");
- java.io.DataInputStream dis = new java.io.DataInputStream(fis);
- String ior = dis.readLine();
- System.out.println(ior);
- org.omg.CORBA.Object obj = orb.string_to_object(ior);
- Hello helloRef = HelloHelper.narrow(obj);
- System.out.println("+"+obj);
- String sin = "string";
- short shin = 3;
- String str = helloRef.printHelloArgs(sin,shin);
- System.out.println(str);
- System.out.println("remote call success");
- }
- catch(Exception e){
- e.printStackTrace(System.out);
-
- }
- }
- }
复制代码 |
|