| 1 | /* SocketImpl.java -- Abstract socket implementation class
|
|---|
| 2 | Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 | package java.net;
|
|---|
| 39 |
|
|---|
| 40 | import java.io.*;
|
|---|
| 41 |
|
|---|
| 42 | /* Written using on-line Java Platform 1.2 API Specification.
|
|---|
| 43 | * Believed complete and correct.
|
|---|
| 44 | */
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * This abstract class serves as the parent class for socket implementations.
|
|---|
| 48 | * The implementation class serves an intermediary to native routines that
|
|---|
| 49 | * perform system specific socket operations.
|
|---|
| 50 | * <p>
|
|---|
| 51 | * A default implementation is provided by the system, but this can be
|
|---|
| 52 | * changed via installing a <code>SocketImplFactory</code> (through a call
|
|---|
| 53 | * to the static method <code>Socket.setSocketImplFactory</code>). A
|
|---|
| 54 | * subclass of <code>Socket</code> can also pass in a <code>SocketImpl</code>
|
|---|
| 55 | * to the <code>Socket(SocketImpl)</code> constructor to use an
|
|---|
| 56 | * implementation different from the system default without installing
|
|---|
| 57 | * a factory.
|
|---|
| 58 | *
|
|---|
| 59 | * @author Aaron M. Renn ([email protected])
|
|---|
| 60 | * @author Per Bothner <[email protected]>
|
|---|
| 61 | */
|
|---|
| 62 | public abstract class SocketImpl implements SocketOptions
|
|---|
| 63 | {
|
|---|
| 64 | /**
|
|---|
| 65 | * The address of the remote end of the socket connection
|
|---|
| 66 | */
|
|---|
| 67 | protected InetAddress address;
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * A FileDescriptor object representing this socket connection.
|
|---|
| 71 | */
|
|---|
| 72 | protected FileDescriptor fd;
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * The port number the socket is bound to locally
|
|---|
| 76 | */
|
|---|
| 77 | protected int localport;
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * The port number of the remote end of the socket connection
|
|---|
| 81 | */
|
|---|
| 82 | protected int port;
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * Default, no-argument constructor for use by subclasses.
|
|---|
| 86 | */
|
|---|
| 87 | public SocketImpl()
|
|---|
| 88 | {
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Creates a new socket that is not bound to any local address/port and
|
|---|
| 93 | * is not connected to any remote address/port. This will be created as
|
|---|
| 94 | * a stream socket if the stream parameter is true, or a datagram socket
|
|---|
| 95 | * if the stream parameter is false.
|
|---|
| 96 | *
|
|---|
| 97 | * @param stream true for a stream socket, false for a datagram socket
|
|---|
| 98 | */
|
|---|
| 99 | protected abstract void create(boolean stream) throws IOException;
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * Connects to the remote hostname and port specified as arguments.
|
|---|
| 103 | *
|
|---|
| 104 | * @param host The remote hostname to connect to
|
|---|
| 105 | * @param port The remote port to connect to
|
|---|
| 106 | *
|
|---|
| 107 | * @exception IOException If an error occurs
|
|---|
| 108 | */
|
|---|
| 109 | protected abstract void connect(String host, int port) throws IOException;
|
|---|
| 110 |
|
|---|
| 111 | /**
|
|---|
| 112 | * Connects to the remote address and port specified as arguments.
|
|---|
| 113 | *
|
|---|
| 114 | * @param host The remote address to connect to
|
|---|
| 115 | * @param port The remote port to connect to
|
|---|
| 116 | *
|
|---|
| 117 | * @exception IOException If an error occurs
|
|---|
| 118 | */
|
|---|
| 119 | protected abstract void connect(InetAddress host, int port)
|
|---|
| 120 | throws IOException;
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Binds to the specified port on the specified addr. Note that this addr
|
|---|
| 124 | * must represent a local IP address.
|
|---|
| 125 | * <p>
|
|---|
| 126 | * Note that it is unspecified how to bind to all interfaces on the localhost
|
|---|
| 127 | * (INADDR_ANY).
|
|---|
| 128 | *
|
|---|
| 129 | * @param host The address to bind to
|
|---|
| 130 | * @param port The port number to bind to
|
|---|
| 131 | *
|
|---|
| 132 | * @exception IOException If an error occurs
|
|---|
| 133 | */
|
|---|
| 134 | protected abstract void bind(InetAddress host, int port) throws IOException;
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Starts listening for connections on a socket. The backlog parameter
|
|---|
| 138 | * is how many pending connections will queue up waiting to be serviced
|
|---|
| 139 | * before being accept'ed. If the queue of pending requests exceeds this
|
|---|
| 140 | * number, additional connections will be refused.
|
|---|
| 141 | *
|
|---|
| 142 | * @param backlog The length of the pending connection queue
|
|---|
| 143 | *
|
|---|
| 144 | * @exception IOException If an error occurs
|
|---|
| 145 | */
|
|---|
| 146 | protected abstract void listen(int backlog) throws IOException;
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * Accepts a connection on this socket.
|
|---|
| 150 | *
|
|---|
| 151 | * @param s The implementation object for the accepted connection.
|
|---|
| 152 | *
|
|---|
| 153 | * @exception IOException If an error occurs
|
|---|
| 154 | */
|
|---|
| 155 | protected abstract void accept(SocketImpl s) throws IOException;
|
|---|
| 156 |
|
|---|
| 157 | /**
|
|---|
| 158 | * Returns an <code>InputStream</code> object for reading from this socket.
|
|---|
| 159 | *
|
|---|
| 160 | * @return An <code>InputStream</code> for reading from this socket.
|
|---|
| 161 | *
|
|---|
| 162 | * @exception IOException If an error occurs
|
|---|
| 163 | */
|
|---|
| 164 | protected abstract InputStream getInputStream() throws IOException;
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * Returns an <code>OutputStream</code> object for writing to this socket
|
|---|
| 168 | *
|
|---|
| 169 | * @return An <code>OutputStream</code> for writing to this socket.
|
|---|
| 170 | *
|
|---|
| 171 | * @exception IOException If an error occurs.
|
|---|
| 172 | */
|
|---|
| 173 | protected abstract OutputStream getOutputStream() throws IOException;
|
|---|
| 174 |
|
|---|
| 175 | /**
|
|---|
| 176 | * Returns the number of bytes that the caller can read from this socket
|
|---|
| 177 | * without blocking.
|
|---|
| 178 | *
|
|---|
| 179 | * @return The number of readable bytes before blocking
|
|---|
| 180 | *
|
|---|
| 181 | * @exception IOException If an error occurs
|
|---|
| 182 | */
|
|---|
| 183 | protected abstract int available() throws IOException;
|
|---|
| 184 |
|
|---|
| 185 | /**
|
|---|
| 186 | * Closes the socket. This will normally cause any resources, such as the
|
|---|
| 187 | * InputStream, OutputStream and associated file descriptors to be freed.
|
|---|
| 188 | * <p>
|
|---|
| 189 | * Note that if the SO_LINGER option is set on this socket, then the
|
|---|
| 190 | * operation could block.
|
|---|
| 191 | *
|
|---|
| 192 | * @exception IOException If an error occurs
|
|---|
| 193 | */
|
|---|
| 194 | protected abstract void close() throws IOException;
|
|---|
| 195 |
|
|---|
| 196 | /**
|
|---|
| 197 | * Returns the FileDescriptor objects for this socket.
|
|---|
| 198 | *
|
|---|
| 199 | * @return A FileDescriptor for this socket.
|
|---|
| 200 | */
|
|---|
| 201 | protected FileDescriptor getFileDescriptor() { return fd; }
|
|---|
| 202 |
|
|---|
| 203 | /**
|
|---|
| 204 | * Returns the remote address this socket is connected to
|
|---|
| 205 | *
|
|---|
| 206 | * @return The remote address
|
|---|
| 207 | */
|
|---|
| 208 | protected InetAddress getInetAddress() { return address; }
|
|---|
| 209 |
|
|---|
| 210 | /**
|
|---|
| 211 | * Returns the remote port this socket is connected to
|
|---|
| 212 | *
|
|---|
| 213 | * @return The remote port
|
|---|
| 214 | */
|
|---|
| 215 | protected int getPort() { return port; }
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * Returns the local port this socket is bound to
|
|---|
| 219 | *
|
|---|
| 220 | * @return The local port
|
|---|
| 221 | */
|
|---|
| 222 | protected int getLocalPort() { return localport; }
|
|---|
| 223 |
|
|---|
| 224 | /**
|
|---|
| 225 | * Returns a <code>String</code> representing the remote host and port of
|
|---|
| 226 | * this socket.
|
|---|
| 227 | *
|
|---|
| 228 | * @return A <code>String</code> for this socket.
|
|---|
| 229 | */
|
|---|
| 230 | public String toString()
|
|---|
| 231 | {
|
|---|
| 232 | return "[addr=" + address.toString() + ",port=" + Integer.toString(port)
|
|---|
| 233 | + ",localport=" + Integer.toString(localport) + "]";
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /**
|
|---|
| 237 | * Sets the specified option on a socket to the passed in object. For
|
|---|
| 238 | * options that take an integer argument, the passed in object is an
|
|---|
| 239 | * <code>Integer</code>. For options that are set to on or off, the
|
|---|
| 240 | * value passed will be a <code>Boolean</code>. The <code>option_id</code>
|
|---|
| 241 | * parameter is one of the defined constants in the superinterface.
|
|---|
| 242 | *
|
|---|
| 243 | * @param option_id The identifier of the option
|
|---|
| 244 | * @param val The value to set the option to
|
|---|
| 245 | *
|
|---|
| 246 | * @exception SocketException If an error occurs
|
|---|
| 247 | * @XXX This redeclaration from SocketOptions is a workaround to a gcj bug.
|
|---|
| 248 | */
|
|---|
| 249 | public abstract void setOption(int option_id, Object val)
|
|---|
| 250 | throws SocketException;
|
|---|
| 251 |
|
|---|
| 252 | /**
|
|---|
| 253 | * Returns the current setting of the specified option. The
|
|---|
| 254 | * <code>Object</code> returned will be an <code>Integer</code> for options
|
|---|
| 255 | * that have integer values. For options that are set to on or off, a
|
|---|
| 256 | * <code>Boolean</code> will be returned. The <code>option_id</code>
|
|---|
| 257 | * is one of the defined constants in the superinterface.
|
|---|
| 258 | *
|
|---|
| 259 | * @param option_id The option identifier
|
|---|
| 260 | *
|
|---|
| 261 | * @return The current value of the option
|
|---|
| 262 | *
|
|---|
| 263 | * @exception SocketException If an error occurs
|
|---|
| 264 | * @XXX This redeclaration from SocketOptions is a workaround to a gcj bug.
|
|---|
| 265 | */
|
|---|
| 266 | public abstract Object getOption(int option_id) throws SocketException;
|
|---|
| 267 | }
|
|---|