在CLIENT端输入圆半径让SERVER传回计算后圆的周长_第1页
在CLIENT端输入圆半径让SERVER传回计算后圆的周长_第2页
在CLIENT端输入圆半径让SERVER传回计算后圆的周长_第3页
在CLIENT端输入圆半径让SERVER传回计算后圆的周长_第4页
在CLIENT端输入圆半径让SERVER传回计算后圆的周长_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、RMI Math範例(JDK 1.2.2)問題: 在CLIENT端輸入圓半徑讓SERVER傳回計算後圓的周長1. 開啟新Project,命名為math.jpr2. INTERFACE (新增Mathe.java檔案到Project)package math;public interface Mathe extends java.rmi.Remotepublic double getRadius(double r) throws java.rmi.RemoteException;3. Implement (新增MatheImpl.java檔案到Project)package math;impor

2、t java.rmi.*;import java.lang.Math;import java.rmi.Naming;import java.rmi.server.UnicastRemoteObject;public class MatheImpl extends UnicastRemoteObject implements Mathepublic MatheImpl() throws RemoteExceptionsuper();public double getRadius(double r) throws RemoteExceptionreturn 2*Math.PI*r;public s

3、tatic void main(String args)System.setSecurityManager(new RMISecurityManager();tryMatheImpl math=new MatheImpl();Naming.rebind(rmi:/45/MathServer,math);System.out.println(server start.);catch(Exception e)System.out.println(Server error:+e);System.exit(0);4. File-New-Applet (產生MatheApplet

4、1.java及MatheApplet1.html)package math;import java.awt.*;import java.awt.event.*;import java.applet.*;import java.rmi.*;public class MatheApplet1 extends Applet boolean isStandalone = false; Label label1 = new Label(); TextField radius = new TextField(); Label label2 = new Label(); TextField result =

5、 new TextField(); Button send = new Button(); Mathe ma; double res; /Get a parameter value public String getParameter(String key, String def) return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); /Construct the applet public MatheApplet1() /Initi

6、alize the applet public void init() try jbInit(); catch(Exception e) e.printStackTrace(); /Component initialization private void jbInit() throws Exception label1.setBounds(new Rectangle(37, 14, 89, 36); label1.setFont(new java.awt.Font(Dialog, 0, 14); label1.setForeground(Color.red); label1.setText(

7、請輸入圓半徑); this.setLayout(null); radius.setBackground(Color.white); radius.setBounds(new Rectangle(157, 17, 114, 31); label2.setBounds(new Rectangle(37, 111, 69, 32); label2.setFont(new java.awt.Font(Dialog, 0, 14); label2.setForeground(Color.blue); label2.setText(圓週長是); result.setBackground(SystemCol

8、); result.setBounds(new Rectangle(108, 110, 230, 43); send.setBackground(Color.pink); send.setBounds(new Rectangle(108, 63, 68, 32); send.setLabel(送出); send.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(ActionEvent e) send_actionPerformed(e); ); this.add(la

9、bel1, null); this.add(radius, null); this.add(label2, null); this.add(result, null); this.add(send, null); try/若尋找遠端主機的物件,就建立物件ma=(Mathe)Naming.lookup(/+ getCodeBase().getHost()+/MathServer); result.setText(連接成功); catch(Exception e) result.setText(連接失敗);System.err.println(無法連接+e); void send_actionPe

10、rformed(ActionEvent e) try /傳出去dluble的參數並取得回傳值res=ma.getRadius(Double.valueOf(radius.getText().doubleValue();result.setText(String.valueOf(res); /將回傳值顯示在周長欄位 catch(RemoteException re) result.setText(無法傳回值);result.setText(無法連接+re); 5. MatheImpl.java檔按下滑鼠右鍵編輯Property,Generate RMI stub/skeleton選項打勾6. 編

11、譯整個Project7. Deployment 整個Project,壓縮成為math.jar檔8. 編輯MatheApplet1.html檔,新增一列ARCHIVE敘述math.MatheApplet1 will appear below in a Java enabled browser.APPLET CODEBASE = .ARCHIVE = 45/MyJava/classes/math.jar CODE = math.MatheApplet1.class 9. 存檔執行時注意事項1. 安裝HTTP WEB SERVER2. Jbuilder 3.5中J

12、AVA RMI程式的Project Properties中的Run Application的VM parameters要設為-Djava.rmi.server.codebase=http:/local host(IP_ADDRESS)/MyJava/classes/ -Djava.security.policy=http:/local host(IP_ADDRESS)/MyJava/src/policy.java上面兩行參數中間要加一個空格其中MyJava為JAVA原始程式所在目錄位置,並於HTTP WEB SERVER中登錄3. 以文書編輯器編輯Policy.Java檔,編輯完後要放於跟RM

13、I原始程式不童子目錄中grant / Allow everything for now permission java.security.AllPermission; permission .SocketPermissionIP_ADDRESS:1024-,listen,accept,connect;4.於WEB SERVER新增目錄,指向RMI程式所在位置的目錄名稱。然後啟動HTTP WEB SERVER.5. 啟動TOOLS-RMIREGISTRY6. Jbuilder3.5中 MatheImpl.java檔讓其執行7. 下面視窗中應該會顯示server start8. 開始-程式集-附屬

14、應用程式-MS-DOS模式,螢幕顯示C:WINDOWS9. C:WINDOWSSET PATH=C:Jbuilder35jdk1.2.2bin10. C:WINDOWSappletviewer http:/IP_ADDRESS/MyJava/src/MatheApplet1.htmlCorba VisiBroker Hello World範例(JDK 1.2.2)問題: 在CLIENT端執行Server之jsp程式,Server端回應”Hello World”字串1. 開啟新Project,命名為hello_corba.jpr,Project的Required Libraries必須包含Vis

15、iBroker9. IDL (新增hello.idl檔案到Project,該IDL檔定義存取介面,定義之後編譯該IDL檔)module helloworld interface say_hello string say(); ;10. Implement (新增say_helloImpl.java檔案到Project)package hello_corba;public class say_helloImpl extends hello_corba.helloworld.say_helloPOA public say_helloImpl() public String say() return

16、 Hello World; 11. Server程式 於JBuilder 選 File-New, 於Object Gallery選Enterprise,然後選CORBA Server Application,按下OK鈕,如下圖:下圖視窗之IDL file指向目前的hello.idl,確認無誤後按下OK完成後之project pane如下所示,新增3個模組12. Client程式於JBuilder 選 File-New, 於Object Gallery選Enterprise,然後選CORBA Server Application,按下OK鈕,如下圖:下圖視窗之IDL file指向目前的hello

17、.idl,確認無誤後按下OK013. 編譯整個Project14. 存檔15. 於Jbuilder3.5中,用滑鼠選擇TOOLS-VisiBroker Smart Agent,按下執行Smart Agent於Jbuilder3.5左上方project pane中,用滑鼠點選helloworldServerApp.java,然後按下滑鼠右鍵,選擇Run,啟動server,如下所示16. 於Jbuilder3.5左上方project pane中,用滑鼠點選Formhelloworld.jsp,然後按下滑鼠右鍵,選擇Run,啟動jsp.17. 開啟IE,輸入位址http:/localhost:808

18、0/src/hello_corba/Formhelloworld.jsp 按下say鈕後,出現Hello World字樣Voyager CORBA應用程式開發步驟1. 定義介面idl2. Compile idl3. 實做idl介面4. 設計Server程式5. 設計Client程式6. Compile Server及Client程式7. 執行Server程式8. 執行Client程式程式範例Voyager client/server application in CORBA modeVoyager安裝完後,於examples目錄中的corba子目錄,有一些程式範例,茲以其中的array範例來說

19、明。Array的程式目的是把client端傳來的陣列,計算陣列中的整數元素總和,另外依據client端所定義的倍率大小,來做簡易陣列乘法。1. idl介面(Imath.idl)module array /模組名稱array typedef sequence LongSequence; / bounded typedef long LongArray 3 ; typedef sequence sequence LongMatrix; / unbounded interface IMath long addLongSequence( in LongSequence numbers ); long

20、addLongArray( in LongArray numbers ); void multiMatrix( inout LongMatrix matrix, in long n ); ; ;2. 使用 cgen d 來compile IMath.idl3. 實做idl定義的函式 (Math.java)package voyager_array;public class Math implements array.IMath /實做array模組的IMath介面 public int addLongSequence( int numbers ) /sequence元素加總 int total

21、 = 0; for( int i = 0; i numbers.length; i+ ) total += numbers i ; return total; public int addLongArray( int numbers ) /array元素加總 int total = 0; for( int i = 0; i numbers.length; i+ ) total += numbers i ; return total; public void multiMatrix( array.LongMatrixHolder matrix, int n ) int x_dim = matri

22、x.value.length; int y_dim = matrix.value 0 .length; System.out.println( multiply + x_dim + by + y_dim + array ); for( int x = 0; x x_dim; x+ ) for( int y = 0; y y_dim; y+ ) matrix.value x y *= n; /將陣列元素乘以n倍 4. Server程式(Server.java)package voyager_array;import java.io.RandomAccessFile;import com.obje

23、ctspace.voyager.*;import com.objectspace.voyager.corba.*;public class Server static array.IMath math; public static void main( String args ) try Voyager.startup(); /啟動Voyager math = new Math(); / create bank String ior = Corba.asIOR( math ); / get IOR for object System.out.println( math IOR = + ior

24、); / display IOR RandomAccessFile file = new RandomAccessFile( Math.IOR, rw ); file.writeUTF( ior ); / write IOR to file,供client來讀取file、存取ior及bind file.close(); System.out.println( CORBA server is ready ); catch( Exception exception ) System.err.println( exception ); 5. Client程式(Client.java)package

25、voyager_array;import java.io.RandomAccessFile;import com.objectspace.voyager.*;import com.objectspace.voyager.corba.*;public class Client public static void main( String args ) try Voyager.startup(); RandomAccessFile file = new RandomAccessFile( Math.IOR, r ); String ior = file.readUTF(); / read IOR

26、 from file file.close(); System.out.println( math IOR = + ior ); / display IOR array.IMath math = (array.IMath) Namespace.lookup( ior ); /尋找ior int total1 = math.addLongArray( new int 2, 6, 8 ); System.out.println( 2 + 6 + 8 = + total1 ); int total2 = math.addLongSequence( new int 1, 5, 6, 8 ); Syst

27、em.out.println( 1 + 5 + 6 + 8 = + total2 ); array.LongMatrixHolder matrix = new array.LongMatrixHolder( new int 2, 3, 1 , 4, 6, 7 ); System.out.println( matrix before = + asString( matrix.value ) ); math.multiMatrix( matrix, 2 ); System.out.println( matrix after = + asString( matrix.value ) ); catch

28、( Exception exception ) System.err.println( exception ); Voyager.shutdown(); private static String asString( int matrix ) /將陣列數值元素轉換為字串方式顯示 int x_dim = matrix.length; int y_dim = matrix 0 .length; StringBuffer buffer = new StringBuffer(); for( int x = 0; x x_dim; x+ ) buffer.append( ); for( int y =

29、0; y y_dim; y+ ) buffer.append( matrix x y + ); buffer.append( ) ); return buffer.toString(); Voyager Mobility能力示範程式開發步驟9. 定義介面idl程式10. 利用cgen編譯idl11. 實做idl介面12. 設計Action程式13. Compile程式14. 執行Action程式程式範例1. (hello.idl) module hello interface IHello void sayHi(in string msg); ; ;2.cgen hello.idl, 會產生IHello.java及IHelloHolder.java3.idl實做(helloImpl.java)package voyager_hello;import java.io.*; public class hel

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论