<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
    數(shù)據(jù)加載中……

    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 閱讀(414) 評論(0)  編輯  收藏 所屬分類: Java

    主站蜘蛛池模板: 国产精品亚洲lv粉色| 亚洲伊人久久大香线蕉综合图片| 亚洲视频在线观看地址| 怡红院免费的全部视频| 亚洲情综合五月天| 伊人久久大香线蕉免费视频| 久久久亚洲精品视频| 久久久久高潮毛片免费全部播放 | 久久国产福利免费| 亚洲国产成人乱码精品女人久久久不卡 | 成年人免费视频观看| 国产精品亚洲片在线va| 成人免费毛片内射美女-百度| 国产人成亚洲第一网站在线播放| 我要看免费的毛片| 免费大片av手机看片高清| 亚洲一区二区三区国产精品| a国产成人免费视频| 亚洲国产精品热久久| 无码少妇一区二区浪潮免费| 久久亚洲AV成人无码国产电影| 免费一看一级毛片全播放| 韩国免费A级毛片久久| 亚洲电影国产一区| 免费av欧美国产在钱| 色吊丝免费观看网站| 亚洲国产婷婷六月丁香| 日本免费网站视频www区| 亚洲av永久无码一区二区三区| 一区国严二区亚洲三区| 在线看片免费人成视频久网下载 | 日本免费精品一区二区三区 | 最近高清国语中文在线观看免费| 国产精品无码亚洲精品2021| 亚洲无码黄色网址| 久久久久久精品成人免费图片| 亚洲av无码偷拍在线观看| 亚洲精品制服丝袜四区| 无码永久免费AV网站| 中国国产高清免费av片| 亚洲粉嫩美白在线|