KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > mplex > SocketConnectorImpl


1 package org.sapia.ubik.net.mplex;
2
3 import java.io.IOException JavaDoc;
4
5 import java.net.InetAddress JavaDoc;
6 import java.net.Socket JavaDoc;
7 import java.net.SocketAddress JavaDoc;
8 import java.net.SocketException JavaDoc;
9
10
11 /**
12  * Class documentation
13  *
14  * @author <a HREF="mailto:jc@sapia-oss.org">Jean-Cedric Desrochers</a>
15  * <dl>
16  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">
17  * Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
18  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
19  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a>
20  * at the Sapia OSS web site</dd></dt>
21  * </dl>
22  */

23 public class SocketConnectorImpl implements MultiplexSocketConnector {
24   /** The multiplex server socket to which this handled is registered. */
25   private MultiplexServerSocket _theServerSocket;
26
27   /** The stream selector that was registered with this handler. */
28   private StreamSelector _theSelector;
29
30   /** The queue of incoming socket connection. */
31   private SocketQueue _theQueue;
32
33   /** Indicates if this socket connector is closed. */
34   private boolean _isClosed;
35
36   /**
37    * Creates a new SocketConnectorImpl instance.
38    */

39   public SocketConnectorImpl(MultiplexServerSocket aServerSocket,
40     StreamSelector aSelector, SocketQueue aQueue) {
41     _theServerSocket = aServerSocket;
42     _theSelector = aSelector;
43     _theQueue = aQueue;
44     _isClosed = false;
45   }
46
47   /**
48    * Returns the selector associated to this handler.
49    *
50    * @return The selector associated to this handler.
51    */

52   public StreamSelector getSelector() {
53     return _theSelector;
54   }
55
56   /**
57    * Returns the socket queue of this handler.
58    *
59    * @return The socket queue of this handler.
60    */

61   public SocketQueue getQueue() {
62     return _theQueue;
63   }
64
65   /**
66    * Returns the port on which this socket is listening.
67    *
68    * @return The port number to which this socket is listening or
69    * -1 if the socket is not bound yet.
70    */

71   public int getLocalPort() {
72     return _theServerSocket.getLocalPort();
73   }
74
75   /**
76    * Returns the local address of this server socket.
77    *
78    * @return The address to which this socket is bound, or
79    * <code>null</code> if the socket is unbound.
80    */

81   public InetAddress JavaDoc getInetAddress() {
82     return _theServerSocket.getInetAddress();
83   }
84
85   /**
86    * Returns the address of the endpoint this socket is bound to, or
87    * <code>null</code> if it is not bound yet.
88    *
89    * @return A <code>SocketAddress</code> representing the local endpoint of this
90    * socket, or <code>null</code> if it is not bound yet.
91    */

92   public SocketAddress JavaDoc getLocalSocketAddress() {
93     return _theServerSocket.getLocalSocketAddress();
94   }
95
96   /**
97    * Gets the value of the SO_RCVBUF option for this socket interceptor
98    * that is the proposed buffer size that will be used for Sockets accepted
99    * from this socket interceptor.
100    *
101    * @return the value of the SO_RCVBUF option for this Socket.
102    * @exception SocketException if there is an error in the underlying protocol.
103    */

104   public int getReceiveBufferSize() throws SocketException JavaDoc {
105     return _theServerSocket.getReceiveBufferSize();
106   }
107
108   /**
109    * Tests if SO_REUSEADDR is enabled.
110    *
111    * @return A <code>boolean</code> indicating whether or not SO_REUSEADDR is enabled.
112    * @exception SocketException if there is an error in the underlying protocol.
113    */

114   public boolean getReuseAddress() throws SocketException JavaDoc {
115     return _theServerSocket.getReuseAddress();
116   }
117
118   /**
119    * (i.e., timeout of infinity).
120    *
121    * @return the SO_TIMEOUT value
122    * @exception IOException if an I/O error occurs
123    */

124   public int getSoTimeout() throws IOException JavaDoc {
125     return _theServerSocket.getSoTimeout();
126   }
127
128   /**
129    * Returns the binding state of the socket.
130    *
131    * @return True if the socket succesfuly bound to an address.
132    */

133   public boolean isBound() {
134     return _theServerSocket.isBound();
135   }
136
137   /**
138    * Returns the closed state of the socket.
139    *
140    * @return True if the socket has been closed.
141    */

142   public boolean isClosed() {
143     return _isClosed || _theServerSocket.isClosed();
144   }
145
146   /**
147    * Listens for a connection to be made to this socket and accepts
148    * it. The method blocks until a connection is made.
149    *
150    * @return The new Socket
151    * @exception IOException If an I/O error occurs when waiting for a connection.
152    */

153   public Socket JavaDoc accept() throws IOException JavaDoc {
154     return _theQueue.getSocket();
155   }
156
157   /**
158    * Closes this multiplex socket handler.
159    *
160    * @exception IOException if an I/O error occurs when closing the socket.
161    */

162   public void close() throws IOException JavaDoc {
163     _theServerSocket.removeSocketConnector(this);
164     _theQueue.close();
165     _isClosed = true;
166   }
167 }
168
Popular Tags