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

📄 simpleregistry.java

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

import java.util.*;
import java.net.*;
import java.io.*;

public class SimpleRegistry 
{ 
  // This object holds the address (host and port) of a SimpleRegistryServer,
  // and provides methods for connecting to that address to send  "lookup"
  // and "rebind" requests to the remote SimpleRegistryServer. 
  // A client invokes LocateSimpleRegistry, which returns a SimpleRegistry
  // object for the SimpleRegistryServer. The client can then send lookup
  // and rebind requests through this object to the SimpleRegistryServer.
    String Host;
    int Port;
    
  // Ultra simple constructor.
    public SimpleRegistry(String IPAdr, int PortNum)
    {
	Host = IPAdr;
	Port = PortNum;
    }

  // Returns the ROR (if found) or null (if else)
    public RemoteObjectRef lookup(String serviceName) 
	throws IOException
    {
	// Open socket. It is assumed that an SimpleRegistryServer is already
	// located by LocateSimpleRegistry.
	// You should usually do try-catch here (and other places).
	Socket soc = new Socket(Host, Port);

	System.out.println("Socket created.");
	    
	// Get TCP I/O streams and wrap them. 
	BufferedReader in = 
	    new BufferedReader(new InputStreamReader (soc.getInputStream()));
	PrintWriter out = 
	    new PrintWriter(soc.getOutputStream(), true);

	System.out.println("Stream created.");

	// Send a lookup request with a service name.
	out.println("lookup");
	out.println(serviceName);

	System.out.println("Request and service name sent.");

	// Branch depending on the reply.
	String res = in.readLine();
	RemoteObjectRef ror;

	if (res.equals("found"))
	    {

		System.out.println("Found!.");

		// Receive ROR data witout checking.
		String ro_IPAdr = in.readLine();

		System.out.println(ro_IPAdr);

		int ro_PortNum = Integer.parseInt(in.readLine());

		System.out.println(ro_PortNum);

		int ro_ObjKey = Integer.parseInt(in.readLine());

		System.out.println(ro_ObjKey);

		String ro_InterfaceName = in.readLine();

		System.out.println(ro_InterfaceName);

		
		// Construct ROR.
		ror = new RemoteObjectRef(ro_IPAdr, ro_PortNum, ro_ObjKey, ro_InterfaceName);
	    }
	else		
	    {
		System.out.println("Not found!.");

		ror = null;
	    }

	// Close the socket.
	soc.close();
		
	// Return ROR.
	return ror;
    }

    // Rebind an ROR. ROR can be null. Again no checking is done.
    public void rebind(String serviceName, RemoteObjectRef ror) 
	throws IOException
    {
	// Open socket, as in lookup.
	Socket soc = new Socket(Host, Port);
	    
	// Get TCP streams and wrap them. 
	BufferedReader in = 
	    new BufferedReader(new InputStreamReader (soc.getInputStream()));
	PrintWriter out = 
	    new PrintWriter(soc.getOutputStream(), true);

	// Send a rebind request with a service name (pick a unique name
	// to identify the service) and ROR.
	out.println("rebind");
	out.println(serviceName);
	out.println(ror.IP_adr);
	out.println(ror.Port); 
	out.println(ror.Obj_Key);
	out.println(ror.Remote_Interface_Name);

	// Get an ack, but it is not used.
	String ack = in.readLine();

	// Close the socket.
	soc.close();
    }
} 
  

⌨️ 快捷键说明

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