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

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

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

    weidagang2046的專(zhuān)欄

    物格而后知致
    隨筆 - 8, 文章 - 409, 評(píng)論 - 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) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Java

    主站蜘蛛池模板: 久青草国产免费观看| www国产亚洲精品久久久| www在线观看播放免费视频日本| 亚洲乱码日产精品BD在线观看| 亚洲中文字幕在线观看| 国产男女猛烈无遮挡免费视频网站 | 91在线精品亚洲一区二区| 亚洲国产一级在线观看| 成人黄18免费视频| 国产精品视频免费| 久久香蕉国产线看免费| 在线视频网址免费播放| 一级黄色片免费观看| 国产精品手机在线亚洲| 亚洲人成人网站18禁| 亚洲福利电影一区二区?| 亚洲AV福利天堂一区二区三| 久久精品国产亚洲7777| 亚洲国产成人久久笫一页| 国产精品黄页在线播放免费| 男人的好看免费观看在线视频 | 亚洲AV成人片色在线观看高潮| 国产亚洲精久久久久久无码77777 国产亚洲精品成人AA片新蒲金 | 麻豆视频免费观看| 9420免费高清在线视频| 免费国产污网站在线观看15| 国内精品久久久久影院免费| 中文字幕手机在线免费看电影 | 亚洲精品高清一二区久久| 深夜国产福利99亚洲视频| 国产成人免费高清在线观看| 国产免费131美女视频| 国产jizzjizz免费看jizz| 成人永久免费高清| 免费播放特黄特色毛片| 亚洲AV中文无码乱人伦在线视色 | 精品亚洲视频在线| 老司机午夜性生免费福利| 色多多免费视频观看区一区| 一级中文字幕免费乱码专区 | 国产免费观看a大片的网站|