⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yourrmi.java

📁 一个java的rmi实例
💻 JAVA
字号:

/* Framework for communication module (CM) for RMI Server: This does not
	offer the code of the whole CM, but it gives some hints about how you
	can make it. I call it simply "yourRMI". 

   For example, it  shows how you can get the host name etc.,
   (well you can hardwire it if you like, I should say),
   or how you can make a class out of classname as a string.

   This just shows one design option. Other options are
   possible. We assume there is a unique skeleton for each
   remote object class (not object) which is called by CM 
   by static methods for unmarshalling etc. We can do without
   it, in which case CM does marshalling/unmarhshalling.
   Which is simpler, I cannot say, since both have their
   own simpleness and complexity.
   It is invoked as follows:

  java yourRMI ZipCodeServerImpl registryhost resigstryport servicename
*/

import java.net.*;
import java.lang.reflect.*;
import java.io.*;

public class yourRMI
{
	static String host;
    	static int port;
   	static final int INITIALIZE = 1;
   	static final int FIND = 2;
    static final int FIND_ALL = 3;
    static final int PRINT_ALL = 4;


    	// It will use a hash table, which contains ROR together with
    	// reference to the remote object.
    	// As you can see, the exception handling is not done at all.
   	public static void main(String args[])throws Exception
    	{	String InitialClassName = args[0];
		String registryHost = args[1];
		int registryPort = Integer.parseInt(args[2]);	
		String serviceName = args[3];

		// it should have its own port. assume you hardwire it.
		host = (InetAddress.getLocalHost()).getHostName();
		port = 12345;

		// it now have two classes from MainClassName: 
		// (1) the class itself (say ZipCpdeServerImpl) and
		// (2) its skeleton.
      	String Remote_Interface_Name = InitialClassName;
		Class initialclass = Class.forName(Remote_Interface_Name);
		Class initialSkeleton = Class.forName(Remote_Interface_Name + "_skel");
	
		// You should also create a remote object table here.
		// It is a table of a ROR and a skeleton.
		// As a hint, I give such a table's interface as RORtbl.java. 
		RORtbl tbl = new RORtbl();
	
		// After that, you create one remote object of initialclass,
		Object o = initialclass.newInstance();
      	Object skel = initialSkeleton.newInstance();
	
		// then register it into the table.
		int Obj_Key = tbl.addObj(host, port, o);
	
		// locate registry server
		SimpleRegistry sr = 
	    		LocateSimpleRegistry.getRegistry(registryHost, registryPort);

		// Construct an ROR to be bound.
		RemoteObjectRef ror = 
			new RemoteObjectRef(host,port,Obj_Key,Remote_Interface_Name);

		// Bind the ror to service name
		sr.rebind(serviceName, ror);	

   		//tiko's note: Access SimpleRegistryServer and rebind (serviceName, ror).
		// A client can then lookup serviceName to get ror. The client then
		// localises ror to ceate a stub. The stub will communicates with
   		// yourRMI to have the invoked method executed via the server_skeleton
   		// object, which can be found in RORtbl.

		// Create a socket.
		ServerSocket serverSoc = new ServerSocket(port);

		// Now we go into a loop.
		// Look at rmiregistry.java for a simple server programming.
		// The code is far from optimal but in any way you can get the
      	// basic idea.
		// Actually you should use multiple threads, or this easily
		// deadlocks. But for your implementation I do not ask it.
		// For design, consider well.
		while (true)
		{	// Create a new socket connection.
			Socket newsoc = serverSoc.accept();
		
			// Create input/output streams on the socket (TCP is bidirectional).
		/*	BufferedReader in = 
		    		new BufferedReader(new InputStreamReader (newsoc.getInputStream()));
			PrintWriter out = 
		    		new PrintWriter(newsoc.getOutputStream(), true);

           		           		           
            	String ro = in.readLine();
            	String[] rorArr = ro.split(",");
            	RemoteObjectRef r = new RemoteObjectRef(rorArr[0], Integer.parseInt(rorArr[1]), 
                                                         Integer.parseInt(rorArr[2]), rorArr[3]);
            	Object localObjectReference = tbl.findObj(r);
                       
            	String methodIDStr = in.readLine();
            	int methodID = Integer.parseInt(methodIDStr);
             
            	String methodArgs = in.readLine();
			String[] arr = methodArgs.split(",");*/
			
		 
		InputStream in = newsoc.getInputStream();
        ObjectInputStream obj_in = new ObjectInputStream(in);
        OutputStream out = newsoc.getOutputStream(); 
        ObjectOutputStream obj_out = new ObjectOutputStream(out);
       	TransToSkel trans=(TransToSkel)obj_in.readObject();
       	
       	
	   	int type = trans.TransType;  
	   	RemoteObjectRef r=new  RemoteObjectRef(trans.TransIP,trans.TransPort,trans.TransObjKey,trans.TransRIM);
	   	Object localObjectReference = tbl.findObj(r);
	   	int methodID = trans.TransRtnType;	
		 String methodArgs=trans.TransMsg;   
		String[] arr = methodArgs.split(","); 	
		     	
		     	                 
            	switch(methodID)
			{	case INITIALIZE : 
                  	{
                  		String argType = arr[0];
                       		int length = Integer.parseInt(arr[1]);
                       		String fieldType1 = arr[2];
					String fieldName1 = arr[3];
                       		String fieldType2 = arr[4];
                       		String fieldName2 = arr[5];
                       		String temp=methodArgs;
                       		
                        //	String temp = "";     
				/*	for(int i = 0; i < arr.length; i++)
					{	temp += arr[i] + ",";	
			     		}   */
			     	
                       		try
					{	Object tst = new Object(); 
			       		Class[] c = {temp.getClass(), tst.getClass()};				
                         		Method m = initialSkeleton.getMethod("initialise", c);
		             		Object[] ob = {temp, localObjectReference};
			       		String reply = (String)m.invoke(skel, ob);
				  TransToStub to= new TransToStub(reply);
                       			obj_out.writeObject(to);
                       		}catch(Exception e)
					{	System.out.println(e.getMessage());
					}

				}break;
                         
                	case FIND: 
                  	{   
                  		String s = arr[0];				
                       		try
					{	Object tst = new Object(); 
  			     			Class[] c = {s.getClass(), tst.getClass()};
                       			Method m = initialSkeleton.getMethod("find", c);
		           			Object[] ob = {s, localObjectReference};
			     			String reply = (String)m.invoke(skel, ob);
                       		 TransToStub to= new TransToStub(reply);
                       			obj_out.writeObject(to);
                       		}catch(Exception e)
					{	System.out.println(e.getMessage());
					}
				}break;
                         
                  	case FIND_ALL: 
                 		{	try
					{	Object tst = new Object(); 
			     			Class[] c = {tst.getClass()};
                       			Method m = initialSkeleton.getMethod("findAll", c);
		           			Object[] ob = {localObjectReference};
			     			String reply = (String)m.invoke(skel, ob);
                        	 TransToStub to= new TransToStub(reply);
                       			obj_out.writeObject(to);
                       		}catch(Exception e)
					{	System.out.println(e.getMessage());
					}

               		}break;
                         
                		case PRINT_ALL : 
                 		{	try
					{	Object tst = new Object(); 
			     			Class[] c = {tst.getClass()};
                       			Method m = initialSkeleton.getMethod("printAll", c);
		           			Object[] ob = {localObjectReference};
			     			String reply = (String)m.invoke(skel, ob);  
                      		 TransToStub to= new TransToStub(reply);
                       			obj_out.writeObject(to);
			   		}catch(Exception e)
					{	System.out.println(e.getMessage());
					}

                		}break;
                     
               		default : break;    
         		                 
			}
			newsoc.close();
		// (1) receives an invocation request from a client.
		// (2) creates a socket and input/output streams.
		// (3) gets the invocation, in marshalled form.
		// (4) gets the real object reference from tbl.
		// (5) Either:
		//      -- using the interface name, asks the skeleton,
		//         together with the object reference, to unmarshall
		//         and invoke the real object.
		//      -- or does unmarshalling directly and invokes that
		//         object directly.
		// (6) receives the return value, which (if not marshalled
		//     you should marshal it here) and send it out to the 
		//     client.
		// (7) closes the socket.
		}
    	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -