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

📄 identitysocket.java

📁 分别介绍RMI,分布式对象与CORBA,动态接口,CORBA客户端开发的课件,以及相关的例子
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package masteringrmi.hellosocket.interfaces;

import java.io.IOException;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 *   Sockets of this type is used to communicate between servers and clients.
 *   It is used by both client and server, so it needs some way to distinguish
 *   what it is being used for: if it is used on client, then send identity, and
 *   if used on the server, then read the identity from the client.
 *
 *   The identity is saved in an attribute that is coupled with the thread, and which
 *   may be queried at any time about the identity of the caller.
 *      
 *   @see IdentityServerSocket
 *   @author Rickard 謆erg
 *   @version $Revision:$
 */
public class IdentitySocket
   extends Socket
{
   // Attributes ----------------------------------------------------
   String client;
   boolean isClient = false;
   
   
   // Constructors --------------------------------------------------
   static ThreadLocal clientIdentity = new ThreadLocal();
   

   /**
    *   Return the identity of the client that called this remote object
    *
    * @return     the name of the client
    */
   public static String getClient()
   {
      return (String)clientIdentity.get();
   }
      
   // Constructors --------------------------------------------------

   /**
    *   Create a new socket. This constructor is used by the server socket
    *
    */
   public IdentitySocket()
   {
      // Do nothing
   }


   /**
    *   Create a new socket. This constructor is used by the client connection
    *   factory.
    *
    * @param   host  
    * @param   port  
    * @exception   UnknownHostException  
    * @exception   IOException  
    */
   public IdentitySocket(String host, int port)
      throws java.net.UnknownHostException, java.io.IOException
   {
      super(host, port);
      isClient = true;
   }

   // Public --------------------------------------------------------
   /**
    *   Get the input stream to this socket. The first time this method
    *   is called we check if we are on the server, in which case we read
    *   the identity of the client.
    *
    * @return     an output stream
    * @exception   IOException  
    */
   public InputStream getInputStream()
      throws IOException
   {
      if (!isClient && client == null)
      {
         // Get the name of the client
         DataInputStream din = new DataInputStream(super.getInputStream());
         client = din.readUTF();
      }
      
      // Associate this thread with the client name
      clientIdentity.set(client);

      // Return the stream so that it may be used by the RMI implementation
      return super.getInputStream();
   }
   
   /**
    *   Get the output stream of this socket. The first time this method
    *   is called we check if we are on the client, in which case we send the
    *   identity of the client.
    *
    * @return     an input stream
    * @exception   IOException  
    */
   public OutputStream getOutputStream()
      throws IOException
   {
      if (isClient && client == null)
      {
         // Send the name of this client
         DataOutputStream dout = new DataOutputStream(super.getOutputStream());
         
         // Get the name from system properties - this is the login name
         client = System.getProperty("user.name");
         dout.writeUTF(client);
      }
      
      // Return the stream so that it may be used by the RMI implementation
      return super.getOutputStream();
   }
   
}

⌨️ 快捷键说明

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