home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / PlainDatagramSocketImpl.java < prev    next >
Text File  |  1997-05-20  |  5KB  |  184 lines

  1. /*
  2.  * @(#)PlainDatagramSocketImpl.java    1.9 97/01/21
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.net;
  24.  
  25. import java.io.FileDescriptor;
  26. import java.io.IOException;
  27. import java.io.InterruptedIOException;
  28.  
  29. /**
  30.  * Concrete datagram and multicast socket implementation base class.
  31.  * Note: This is not a public class, so that applets cannot call
  32.  * into the implementation directly and hence cannot bypass the
  33.  * security checks present in the DatagramSocket and MulticastSocket 
  34.  * classes.
  35.  *
  36.  * @author Pavani Diwanji
  37.  */
  38.  
  39. class PlainDatagramSocketImpl extends DatagramSocketImpl
  40. {
  41.  
  42.     /* timeout value for receive() */
  43.     private int timeout = 0;
  44.  
  45.     /**
  46.      * Load net library into runtime.
  47.      */
  48.     static {
  49.     System.loadLibrary("net");
  50.     }
  51.  
  52.     /**
  53.      * Creates a datagram socket
  54.      */
  55.     protected synchronized void create() throws SocketException {
  56.     fd = new FileDescriptor();
  57.     datagramSocketCreate();
  58.     }
  59.  
  60.     /**
  61.      * Binds a datagram socket to a local port.
  62.      */
  63.     protected synchronized native void bind(int lport, InetAddress laddr) throws SocketException;
  64.  
  65.     /**
  66.      * Sends a datagram packet. The packet contains the data and the
  67.      * destination address to send the packet to.
  68.      * @param packet to be sent.
  69.      */
  70.     protected native void send(DatagramPacket p) throws IOException;
  71.  
  72.     /**
  73.      * Peek at the packet to see who it is from.
  74.      * @param return the address which the packet came from.
  75.      */
  76.     protected synchronized native int peek(InetAddress i) throws IOException;
  77.  
  78.     /**
  79.      * Receive the datagram packet.
  80.      * @param Packet Received.
  81.      */
  82.     protected synchronized native void receive(DatagramPacket p) throws IOException;
  83.  
  84.     /**
  85.      * Set the TTL (time-to-live) option.
  86.      * @param TTL to be set.
  87.      */
  88.     protected native void setTTL(byte ttl) throws IOException;
  89.  
  90.     /**
  91.      * Get the TTL (time-to-live) option.
  92.      */
  93.     protected native byte getTTL() throws IOException;
  94.  
  95.     /**
  96.      * Join the multicast group.
  97.      * @param multicast address to join.
  98.      */
  99.     protected native void join(InetAddress inetaddr) throws IOException;
  100.  
  101.     /**
  102.      * Leave the multicast group.
  103.      * @param multicast address to leave.
  104.      */
  105.     protected native void leave(InetAddress inetaddr) throws IOException;
  106.  
  107.     /**
  108.      * Close the socket.
  109.      */
  110.     protected void close() {
  111.     if (fd != null) {
  112.         datagramSocketClose();
  113.         fd = null;
  114.     }
  115.     }
  116.  
  117.     protected synchronized void finalize() {
  118.     close();
  119.     }
  120.  
  121.     /**
  122.      * set a value - since we only support (setting) binary options 
  123.      * here, o must be a Boolean
  124.      */
  125.  
  126.      public void setOption(int optID, Object o) throws SocketException {
  127.      switch (optID) {
  128.         /* check type safety b4 going native.  These should never
  129.          * fail, since only java.Socket* has access to 
  130.          * PlainSocketImpl.setOption().
  131.          */
  132.      case SO_TIMEOUT:
  133.          if (o == null || !(o instanceof Integer)) {
  134.          throw new SocketException("bad argument for SO_TIMEOUT");
  135.          }
  136.          int tmp = ((Integer) o).intValue();
  137.          if (tmp < 0) 
  138.          throw new IllegalArgumentException("timeout < 0");
  139.          timeout = tmp;
  140.          return;
  141.      case SO_BINDADDR:
  142.          throw new SocketException("Cannot re-bind Socket");
  143.      case SO_REUSEADDR:
  144.          if (o == null || !(o instanceof Integer)) {
  145.          throw new SocketException("bad argument for SO_REUSEADDR");
  146.          } 
  147.          break;
  148.      case IP_MULTICAST_IF: 
  149.          if (o == null || !(o instanceof InetAddress))
  150.          throw new SocketException("bad argument for IP_MULTICAST_IF");
  151.          break;
  152.      default:
  153.          throw new SocketException("invalid option: " + optID);
  154.      }
  155.      socketSetOption(optID, o);
  156.      }
  157.  
  158.     /*
  159.      * get option's state - set or not
  160.      */
  161.  
  162.     public Object getOption(int optID) throws SocketException {
  163.     if (optID == SO_TIMEOUT) {
  164.         return new Integer(timeout);
  165.     }
  166.     int ret = socketGetOption(optID);
  167.  
  168.     if (optID == SO_BINDADDR || optID == IP_MULTICAST_IF) {
  169.         InetAddress in = new InetAddress();
  170.         in.address = ret;
  171.         return in;
  172.     } else {
  173.         return null;
  174.     }
  175.     }
  176.     
  177.     private native void datagramSocketCreate() throws SocketException;
  178.     private native void datagramSocketClose();
  179.     private native void socketSetOption(int opt, Object val) throws SocketException;
  180.     private native int socketGetOption(int opt) throws SocketException;
  181.     
  182. }
  183.  
  184.