簡單的JAVA版CORBA程式
阿新 • • 發佈:2018-12-19
為了簡單的學習瞭解CORBA元件,這裡用簡單的java程式作為練習,
作業一: 要求編寫實現顯示“Hello,World!+班級+中文姓名”字串。
作業二:編寫實現連加、連減和加減混合等數學++/- -運算,並進行測試。
作業一:
(1):idl介面定義檔案,HelloWorld.idl:
因為要輸出包含中文字串,將函式宣告為修改為wstring型別,避免亂碼或其他字符集錯誤。
module sample{
interface HelloWorld{
wstring sayHello();
};
};
(2):編譯IDL介面
在剛剛定義的idl介面檔案所在路徑下,執行命令idlj –fall HelloWorld.idl 可以看到自動生成了一個sample資料夾,主要包括以下六個類,方便起見,我建了工程,拷入到工程裡。
(3):編寫並編譯以下三個類。
編寫並編譯服務端程式:HelloWorldServer.java
package szj; import sample.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import org.omg.CORBA.portable.*; import org.omg.PortableServer.*; 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); } } }
編寫並編譯客戶端程式: 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); } } }
編寫並編譯HelloWorldServant類
為了在客戶端得到我想要的輸出結果,比如helloworld我的班級加姓名,在HelloWorldServant類下做如下修改
package szj;
import sample.HelloWorldPOA;
class HelloWorldServant extends HelloWorldPOA { // 物件實現類
public String sayHello() {
return "\nHello World!軟體工程1602班司志傑\n";
}
}
(4):執行
啟動名字伺服器:輸入tnameserv -ORBInitialPort 1050命令 可以看到以下截圖說明啟動成功
啟動伺服器程式:不要忘了修改埠號
如下圖,啟動成功
接著啟動客戶端程式,如下圖執行成功。
作業二:
(1):編寫idl介面定義檔案,counter.idl:
module CounterApp{
interface Counter{
readonly attribute long value;
void inc();
void dec();
};
};
(2):編譯IDL介面
在剛剛定義的idl介面檔案所在路徑下,執行命令idlj –fall counter.idl 可以看到自動生成了一個CounterApp資料夾,主要包括以下幾個類,方便起見,我建了工程,拷入到工程裡。
(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);
}
}
}
編寫並編譯客戶端程式: 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);
}
}
}
(4):執行
同第一個一樣,執行之前不要忘了啟動名字伺服器:輸入tnameserv -ORBInitialPort 1050命令,看到如下說明啟動成功
接著啟動伺服器程式,和第一個一樣不要忘了傳入埠 見下圖執行成功
接著啟動客戶端程式,如下圖,測試成功