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

📄 zipcodeserverimpl_stub.java

📁 一个java的rmi实例
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.util.*;


public class ZipCodeServerImpl_stub 
{   /*	public static final int INITIALIZE = 1;
    	public static final int FIND = 2;
    	public static final int FIND_ALL = 3;
    	public static final int PRINT_ALL = 4;
	public static int nextObjKey = 0;
	private int objKey;*/
	
		public static final int REQUEST = 0;
    	public static final int REPLY = 1;
   public static Hashtable hashtbl;
    
    	/** Creates a new instance of ZipCodeServerImpl_stub */
 /*   	public ZipCodeServerImpl_stub() 
	{	objKey = nextObjKey;
		nextObjKey++;
     	}*/
	
	// returns the key for this object
//	public int getObjectKey()
//	{	return objKey;
//	} 
 
 
  	public Hashtable getStubROR(RemoteObjectRef r, Object stub)
	{
	  hashtbl= Tbl.StubRORMap(r,stub);
	  return hashtbl;
	}
 
 
 	public static String send(Object stub, int type, int RtnType,String msg) 
	{	// get the ROR paired with stub in the RORtbl;
 
		RemoteObjectRef ror = (RemoteObjectRef)hashtbl.get(stub);

		// Marshall ROR
        /*	String marshalledROR = 	ror.getIP() + "," +
                			ror.getPort() + "," +
                			ror.getObj_Key() + "," +
                			ror.getRemote_Interface_Name() + "\n";*/
                			 
            			
        TransToSkel trans=new TransToSkel(type,ror.IP_adr,ror.Port,ror.Obj_Key,ror.Remote_Interface_Name,RtnType,msg);

		String rtn = "";
		try
		{	if(type == REQUEST)
				System.out.println("Sending a request");
			else if(type == REPLY)
				System.out.println("Sending a reply");
      		// Open new socket to server
      		Socket newsoc = new Socket(ror.IP_adr, ror.Port);
      
      	/*	BufferedReader in =  new BufferedReader(new InputStreamReader (newsoc.getInputStream()));
      		PrintWriter out = new PrintWriter(newsoc.getOutputStream(), true);
      		out.println(type);
      		out.println(marshalledROR + msg);
			System.out.println("waiting for reply");*/
				 
		InputStream in = newsoc.getInputStream();
        ObjectInputStream obj_in = new ObjectInputStream(in);
        OutputStream out = newsoc.getOutputStream(); 
        ObjectOutputStream obj_out = new ObjectOutputStream(out);
        obj_out.writeObject(trans);
        TransToSkel getTrans=(TransToSkel)obj_in.readObject();
           
      	TransToStub to=(TransToStub)obj_in.readObject();
      	rtn=to.StrTemp;
      	//	rtn = getTrans.TransType+"\n"+getTrans.TransIP+","+getTrans.TransPort+","
      	//	      +getTrans.TransObjKey+","+getTrans.TransRIM+"\n"+getTrans.TransType+"\n"+getTrans.TransMsg+"\n";
      		
     
      		newsoc.close();  
		}catch(Exception e)
		{	System.out.println(e.getMessage());
			System.exit(0);
		} 
		  
		return rtn;  
		    
    	}
 
 
 
 
  
   
 
    	// When the following method is called, marshalled data
    	// should be sent to this remote object, and reconstructed.
    	
    	
 /*   	public void initialise(ZipCodeList newlist)
    	{	String temp = "";
               int RtnType;
        	// Marshall MethodID
        	//temp += INITIALIZE + "\n";
        	RtnType=INITIALIZE;
        	

        	// Marshall Method Args
        	ZipCodeList iter = newlist;
 		int length = 0;
		while(iter != null)
		{	length++;
			iter = iter.next;
		}
		
		temp += "ZipCodeList," ;
		temp += length + ",";
		temp += "String,city,";
		temp += "String,ZipCode,";
		
		iter = newlist;
		
	 	while(iter != null)
		{	temp += iter.city + "," ;
	   		temp += iter.ZipCode + "," ;
	   		iter = iter.next;
		}
        	String reply = ZipCodeServerImpl_stub .send(this,ZipCodeServerImpl_stub .REQUEST,RtnType,temp);
        	return;
	}

    	// Basic function: look up a city name, and return its zipcode.
    	public String find(String request)
    	{	// Search the list.
        	String temp = "";
                 int RtnType;
        	// Marshall MethodID
        //	temp += FIND + "\n";
        RtnType=FIND;

        	// Marshall Method Args
        	temp += request;
		String reply =ZipCodeServerImpl_stub .send(this, ZipCodeServerImpl_stub .REQUEST,RtnType,temp);
        	return reply;
    	}

    	// The following short method should send the marshalled 
    	// whole list to the client site.  The main challenge is coding
    	// the skeleton.
    	public ZipCodeList findAll()
    	{	String temp = "";
                 int RtnType;
        	// Marshall MethodID
        //	temp += FIND_ALL + "\n";
           RtnType=FIND_ALL;
        	String reply =ZipCodeServerImpl_stub .send(this, ZipCodeServerImpl_stub .REQUEST,RtnType, temp);
        	String[] arr = reply.split(","); 

        	String returnType = arr[0];
        	int length = Integer.parseInt(arr[1]);
        	String fieldType1 = arr[2];
        	String fieldName1 = arr[3];
        
        	String fieldType2 = arr[4];
        	String fieldName2 = arr[5];
        
        
        	ZipCodeList prev = new ZipCodeList(arr[6], arr[7], null);
        	ZipCodeList start = prev;
        
        	// Unmarshall Method Args
        	for(int i = 8; i < arr.length; i+=2)
		{	ZipCodeList n = new ZipCodeList(arr[i], arr[i+1], null);
          		prev.next = n;
          		prev = n;
	  	}	
		return start;
   	}

    	// The following method prints at the server site.
    	public void printAll()
    	{	String temp = "";
                 int RtnType;
        	// Marshall MethodID
        //	temp += PRINT_ALL + "\n";
        RtnType=PRINT_ALL;

        	String reply =ZipCodeServerImpl_stub .send(this, ZipCodeServerImpl_stub .REQUEST,RtnType, temp);
        	return;
    	}*/
   	WorkType work_type;
    		public void initialise(ZipCodeList newlist)
    		{
    			work_type.initialise(newlist);
    		}
    		public String find(String request)
    		{
    		  String str=work_type.find(request);
    		  return str;
    		}
    		public ZipCodeList findAll()
    		{
    		ZipCodeList acl=work_type.findAll();
    		return acl;
    		}
    		public void printAll()
    		{
    	    	work_type.printAll();
    		}
    		 
}

⌨️ 快捷键说明

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