<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    weidagang2046的專欄

    物格而后知致
    隨筆 - 8, 文章 - 409, 評論 - 101, 引用 - 0
    數據加載中……

    ZRO Guide

    Overview

      ZRO is a set of api for developing distributed systems.

    Contents

    Architecture

      The architecture is simple. ZRO can also be names "Remote Reflection API".Information about Reflection API can be found is JDK's documentation. The reflection API represents, or reflects, the classes, interfaces, and objects in the current Java Virtual Machine. With the reflection API you can:

    • Create an instance of a class whose name is not known until runtime.
    • Get and set the value of an object's field, even if the field name is unknown to your program until runtime.
    • Invoke a method on an object, even if the method is not known until runtime.
    • Create a new array, whose size and component type are not known until runtime, and then modify the array's components.
      These things are all done at runtime.
      ZRO is a remote version of Reflection API. that means:
    • Create an instance of a class at server side.
    • Get and set the value of an servier-side object's field,.
    • Invoke a method on an server-side object.
    • Create a new array at server-side,and then modify the array's components.

      The server-side object is called Remote Object in ZRO.

      The api is client-server mode. The is a server that open a ServerSocket to allow clients to connect. The client connect to server and send request and then fetch response.

    Contents

    Main Concept

      Remote Object

        I'm sure that you know the reference concept in Java. A reference can be seen as a pointer that points to a object in the jvm.Then I can tell you that a Remote Object in one jvm is a reference or pointer that points to a server-side object in a different jvm. You can invoke methos on this remote object and get or set the field of the remote object just as the object is local.

    Contents

    Guide and Examples

      This guard is by examples mainly.

      The Server Side

      ZRO is so-called zero server-side programing. So the program runs in the serverside is very simple.

    package com.zms.zro.test;
    
    import com.zms.zro.ZROServer;
    
    public class OneServer {
    
    	public static void main(String[] args) throws Exception {
    		ZROServer server=new ZROServer(1234);
    		System.in.read();
    		server.close();
    	}
    }

      This program almost can handle all client side programs.

      This program has a main method.First it create a ZROServer instance. The constructor parameter is a int that tells on which port to open a ServerSocket.The lines else means a enter on the console will end the program.

      Hello World

      Below is the classical Hello world program:

    package com.zms.zro.test;
    public class ClassicHelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } } 
      To understand better,we change the program little:
    package com.zms.zro.test;
    import java.io.PrintStream;
    public class ClassicHelloWorld { public static void main(String[] args) { PrintStream out=System.out; out.println("Hello world!"); } }
      System is a class. The System class has a static field "out" with the type PrintStream.We get the static field and invoke the println method on the object. Than the ZRO client program is just the same:
    package com.zms.zro.test;
    import com.zms.zro.RemoteObject; import com.zms.zro.ZROClient;
    public class ZROHelloWorld { public static void main(String[] args) throws Exception { ZROClient client=ZROClient.fetchClient("localhost",1234,false); RemoteObject out=(RemoteObject)client.getField(null,System.class.getField("out")); out.invoke("println","Hello ZRO world!"); } }

    Note: First you need a ZROClient!

      In the client,you must first have a ZROClient.The ZROClient communicate with the server. The ZROClient use the Multiton design pattern.The Constructor is private. You can only get/create a ZROClient through the method ZROClient.fetchClient(String host,int port,boolean sharable). The String host is the server host.The int port is the server port.

    Note: sharable and non sharable

      The third parameter boolean sharable stands for if the client is sharable. Because actions on one ZROClient is blocked.So in muti-thread programs,if different thread own the same ZROClient,the efficiency is low. If you don't want to share a ZROClient with other threads,you specify the parameter false.
      The same ZROClient is returned If sharable and host and port are the same:

    		ZROClient one=ZROClient.fetchClient("localhost",1234,true);
    		ZROClient two=ZROClient.fetchClient("localhost",1234,true);
    		//one==two
    
    		ZROClient one=ZROClient.fetchClient("localhost",1234,true);
    		ZROClient two=ZROClient.fetchClient("localhost",5678,true);
    		//one!=two
    
    		ZROClient one=ZROClient.fetchClient("localhost",1234,false);
    		ZROClient two=ZROClient.fetchClient("localhost",1234,true);
    		//one!=two
    
    		ZROClient one=ZROClient.fetchClient("localhost",1234,true);
    		ZROClient two=ZROClient.fetchClient("localhost",1234,false);
    		//one!=two
    

      The program first get the Remote Object points to the System.out in server's jvm.

    Note: get a static field

      ZROClient.getField(null,Field)
      Becuase the field is static,so the first parameter is null. The second parameter is of the type java.lang.reflect.Field.

    Note: why the return type is Object other than RemoteObject

      In many methods,the return type is Object other than RemoteObject:
      java.lang.Object ZROClient.getField
      java.lang.Object ZROClient.getField.invoke

      Some Object are transported through the network directly.They are:
      primitive type: int long double float short byte char boolean
      primitive wrap type: Integer Long Double Float Short Byte Character Boolean
      special: String

      If the return type is common Object such as java.util.Vector,then a RemoteObject is returned.If the return type is the types listed,the real object is returned.

    //to get the java.lang.File.pathSeparator
    RemoteObject pathSeparator=(RemoteObject)client.getField(null,java.io.File.class.getField("pathSeparator"));
    //this line will throw a ClassCastException,Because the static field pathSeparator of File is of type String
    //so the String is returned ,not the RemoteObject
    
    String pathSeparator=(String)client.getField(null,java.io.File.class.getField("pathSeparator"));
    //correct

      How to determin which type to cast
        This depends the declare type. If the declare type is the immutable types listed beyond,then the real object is returned. Otherwise the return type is RemoteObject

    Note: How to invoke method

      If the method is static,then ZROClient.invoke(null,Method,args)

    client.invoke(null,System.class.getMethod("exit",new Class[]{int.class}),0);
    //System.exit(0)   will cause the server process exit
      If the method is not static,the RemoteObject.invoke
       RemoteObject.invoke(Method method,args)


       RemoteObject.invoke(String methodName,args)

    Contents

      New Object

        In this example ,we will create a JFrame that runs on server.

    package com.zms.zro.test;
    import javax.swing.JFrame; import javax.swing.WindowConstants; import com.zms.zro.RemoteObject; import com.zms.zro.ZROClient;
    public class JFrameTest { public static void main(String[] args) throws Exception { ZROClient client=ZROClient.fetchClient("localhost",1234,false); //JFrame frame=new JFrame("A Frame"); RemoteObject frame=(RemoteObject)client.newInstance(JFrame.class,"A Frame"); //frame.setBounds(100,100,200,200); frame.invoke("setBounds",100,100,200,200); //frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.invoke("setDefaultCloseOperation",WindowConstants.DISPOSE_ON_CLOSE); //frame.setVisible(true); frame.invoke("setVisible",true); } }

    Note: How to new Object in remote JVM

      ZROClient.newInstance(Constructor,args)
      ZROClient.newInstance(Class,args)

      A frame with a button:

    package com.zms.zro.test;
    import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.WindowConstants; import com.zms.zro.RemoteObject; import com.zms.zro.ZROClient;
    public class JFrameTestA { public static void main(String[] args) throws Exception { ZROClient client=ZROClient.fetchClient("localhost",1234,false); //JFrame frame=new JFrame("A Frame"); RemoteObject frame=(RemoteObject)client.newInstance(JFrame.class,"A Frame"); //frame.setBounds(100,100,200,200); frame.invoke("setBounds",100,100,200,200); //frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.invoke("setDefaultCloseOperation",WindowConstants.DISPOSE_ON_CLOSE); //Container container=frame.getContentPane(); RemoteObject container=(RemoteObject)frame.invoke("getContentPane"); //container.setLayout(null); container.invoke("setLayout",new Object[]{null}); //JButton button=new JButton("Kick me"); RemoteObject button=(RemoteObject)client.newInstance(JButton.class,"Kick me"); //container.add(button); container.invoke("add",button); //button.setBounds(50,50,80,30); button.invoke("setBounds",50,50,80,30); //frame.setVisible(true); frame.invoke("setVisible",true); } }

    Array Opertaions

      ZROClient.newArray(Class,int)
      ZROClient.newArray(Class,int[])
      RemoteObject.arrayLength()
      RemoteObject.arrayGet(int)
      RemoteObject.arraySet(int,Object)

      Here we will create some arrays on server:

    package com.zms.zro.test;
    import com.zms.zro.RemoteObject; import com.zms.zro.ZROClient;
    public class ArrayTest { public static void main(String[] args) { ZROClient client=ZROClient.fetchClient("majl",1234,false); //int[] ints=new int[10240]; RemoteObject ints=(RemoteObject)client.newArray(int.class,10240); for(int i=0;i<100;i++) { //ints[i]=i*i; ints.arraySet(i,i*i); } System.out.println(ints.arrayLength()); System.out.println(ints.arrayGet(50)); } }
      Another:
    package com.zms.zro.test;
    import java.util.Date; import com.zms.zro.RemoteObject; import com.zms.zro.ZROClient;
    public class ArrayTest { public static void main(String[] args) { ZROClient client=ZROClient.fetchClient("majl",1234,false); //Date[] dates=new Date[10]; RemoteObject dates=(RemoteObject)client.newArray(Date.class,10); for(int i=0;i<dates.arrayLength();i++) { //dates[i]=new Date(); dates.arraySet(i,client.newInstance(Date.class)); } RemoteObject one=(RemoteObject)dates.arrayGet(5); //getTime System.out.println(one.invoke("getTime")); } }

    Contents

    Other Topics

      Security

         To avoid baleful clients from connecting. You can set a usename and a password to the ZROServer.When the clients connected,it must first provide the username and the password.

    The Server Side:

    ZROServer server=new ZROServer(1234,"user","pass");

    The Client Side:

    		ZROClient client=ZROClient.fetchClient("host",1234,true);
    		if(client.needAuth()) {
    			client.doAuth("user","pass");
    		}


      SecurityManager

    posted on 2005-09-22 11:32 weidagang2046 閱讀(418) 評論(0)  編輯  收藏 所屬分類: Java

    主站蜘蛛池模板: 国产成人免费a在线资源| 免费网站看v片在线香蕉| 狠狠亚洲婷婷综合色香五月排名 | 一本色道久久88—综合亚洲精品 | 色老头综合免费视频| 免费国产怡红院在线观看| 亚洲AV无码之国产精品| 在线观看免费国产视频| 无遮挡免费一区二区三区| 亚洲黄片毛片在线观看| 国产精品成人69XXX免费视频| 国产亚洲精品观看91在线| 久久免费视频网站| 亚洲精品中文字幕乱码| 久久久久久久久免费看无码| 亚洲国产精品ⅴa在线观看| 国产三级免费电影| 中国一级特黄高清免费的大片中国一级黄色片| 国产成人亚洲影院在线观看| 日韩电影免费在线观看网站 | 免费观看午夜在线欧差毛片| 免费精品国自产拍在线播放| 国产AV无码专区亚洲AWWW| 99视频在线精品免费| 亚洲人成77777在线观看网| 凹凸精品视频分类国产品免费| 中国国语毛片免费观看视频| 亚洲成a人片在线观看中文!!!| 在线播放免费播放av片| 一区二区视频在线免费观看| 亚洲精品免费在线观看| 大学生美女毛片免费视频| 久久久久久久国产免费看| 久久久久久亚洲AV无码专区| 日本一道综合久久aⅴ免费| 中国一级全黄的免费观看| 亚洲不卡中文字幕| 国产亚洲精品线观看动态图| 很黄很黄的网站免费的| 一级特黄aaa大片免费看| 亚洲国产综合专区在线电影|