CORBA的IDL資料類型定義了一系列的基礎資料型別 (Elementary Data Type),但是如果需要傳遞對象可能就有些問題了。雖然IDL檔案中的interface其實就是傳遞對象的一種方法,我的理解就是把要傳遞的對象的屬性分成基礎資料型別 (Elementary Data Type)傳遞過去,但是如果傳遞的對象結構變化了,還需要修改IDL檔案。所以想找到一個直接傳遞對象的方法,研究了CORBA定義的資料類型,最好的辦法就是像很多串連傳輸對象的方法一樣,傳遞byte數組。
建一個目錄,名為Hello(名字隨意起),然後在這個目錄中建兩個子目錄,分別為server,client
在兩個目錄中各建一個People.java檔案,內容如下:
import java.io.*;
class People implements Serializable
{
private String name;
private int age;
public People()
{
name = "CY";
age = 27;
}
public People(String str, int i)
{
name = str;
age = i;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
在server目錄建一個Hello.idl檔案,內容如下:
module HelloApp
{
interface Hello
{
string sayHello();
oneway void shutdown();
};
};
同樣在server目錄建HelloServer.java檔案,內容如下:
// A server for the Hello object
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import java.util.Properties;
public class HelloServer {
public static void main(String args[]) {
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// get reference to rootpoa & activate the POAManager
POA rootpoa =
(POA)orb.resolve_initial_references("RootPOA");
rootpoa.the_POAManager().activate();
// create servant and register it with the ORB
HelloImpl helloImpl = new HelloImpl();
helloImpl.setORB(orb);
// get object reference from the servant
org.omg.CORBA.Object ref =
rootpoa.servant_to_reference(helloImpl);
// and cast the reference to a CORBA reference
Hello href = HelloHelper.narrow(ref);
// get the root naming context
// NameService invokes the transient name service
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
// Use NamingContextExt, which is part of the
// Interoperable Naming Service (INS) specification.
NamingContextExt ncRef =
NamingContextExtHelper.narrow(objRef);
// bind the Object Reference in Naming
String name = "Hello1";
NameComponent path[] = ncRef.to_name( name );
ncRef.rebind(path, href);
System.out.println
("HelloServer ready and waiting ...");
// wait for invocations from clients
orb.run();
}
catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("HelloServer Exiting ...");
} //end main
} // end class
在server目錄下建立HelloImpl.java檔案,內容如下:
// The servant -- object implementation -- for the Hello
// example. Note that this is a subclass of HelloPOA, whose
// source file is generated from the compilation of
// Hello.idl using j2idl.
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import java.util.Properties;
import java.io.*;
class HelloImpl extends HelloPOA //必須繼承這個類,在helloApp目錄中已自動產生
{
private ORB orb;
private byte[] bytes;
public void setORB(ORB orb_val)
{
orb = orb_val;
}
// implement sayHello() method
public byte[] sayHello()
{
return (ObjectToByte(new People("Chen Yi",27)));
}
// implement shutdown() method
public void shutdown()
{
orb.shutdown(false);
}
public byte[] ObjectToByte(java.lang.Object obj)
{
try {
//object to bytearray
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(obj);
bytes = bo.toByteArray();
bo.close();
oo.close();
}
catch(Exception e) {
System.out.println("translation"+e.getMessage());
e.printStackTrace();
}
return(bytes);
}
} //end class
在client目錄下建立HelloClient.java檔案,內容如下:
// A sample Java IDL object client application.
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;
public class HelloClient
{
static Hello helloImpl;
String [] x=new String[6];
static People people;
static java.lang.Object obj;
public static void main(String args[]){
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
System.out.println("ORB initialised/n");
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
// Use NamingContextExt instead of NamingContext,
// part of the Interoperable naming Service.
NamingContextExt ncRef =
NamingContextExtHelper.narrow(objRef);
// resolve the Object Reference in Naming
String name = "Hello1";
helloImpl =
HelloHelper.narrow(ncRef.resolve_str(name));
people = (People)(ByteToObject(helloImpl.sayHello()));
System.out.println
("Obtained a handle on server object: "
+ helloImpl);
System.out.println(people.getName());
helloImpl.shutdown();
}
catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
} //end main
private static java.lang.Object ByteToObject(byte[] bytes){
try {
//bytearray to object
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(bi);
obj = oi.readObject();
bi.close();
oi.close();
}
catch(Exception e) {
System.out.println("translation"+e.getMessage());
e.printStackTrace();
}
return obj;
}
} // end class
下面是單機版編譯步驟
1. 編譯server中IDL檔案,在命令列執行 idlj -fall HelloApp.idl
2. 編譯server中.java檔案 javac *.java
3. 將server中編譯IDL檔案後產生的檔案目錄HelloApp複製到client目錄中
4. 編譯client中.java檔案 javac *.java
5. 運行orbd start orbd -ORBInitialPort 1050
6. 運行server start java HelloServer -ORBInitialPort 1050 -ORBInitialHost localhost
7. 運行client java HelloClient -ORBInitialPort 1050 -ORBInitialHost localhost
運行結果,會在client命令列中看到列印出人名.如果需要進一步測試,可以在HelloImpl中修改new People(str,i)的參數也可以在client中調用People的其他方法.
多機版運行方法
編譯方法同單機版
1. 運行orbd start orbd -ORBInitialPort 1050 -ORBInitialHost servermachinename
2. 運行server java HelloServer -ORBInitialPort 1050
3. 運行client java HelloClient -ORBInitialHost nameserverhost -ORBInitialPort 1050
注意 nameserverhost 是伺服器的IP地址
另外idlj命令的參數也可以修改, -fall是產生server和client的映射, -fserver是產生server映射, -fclient是產生client映射.如果用後面兩個參數需要將.idl檔案分別放在兩個目錄中