KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.ubik.net.mplex;
2
3 import java.io.IOException JavaDoc;
4
5 import java.net.Socket JavaDoc;
6 import java.net.SocketException JavaDoc;
7
8 import java.util.Iterator JavaDoc;
9 import java.util.LinkedList JavaDoc;
10
11
12 /**
13  * This class implements a simple asynchronous, thread safe, FIFO queue of
14  * <code>Socket</code> objects. A producer actor can add a new socket to the
15  * queue by calling the <code>add(Socket)</code> method. A consumer actor can
16  * retrieve the next available socket from the queue using the <code>getSocket()</code>
17  * method.<p>
18  *
19  * Upon creation a <code>SocketQueue</code> is considered active or open; therefore
20  * producers and consumers can use the queue. When the queue is no longer necessary
21  * the <code>close()</clode> method will put the queue in a closed state where:
22  * <ul>
23  * <li>All the pending <code>Socket</code> objects in the queue will be removed
24  * from the queue and then explicitly closed.</li>
25  * <li>All the consumers that are blocked on the <code>getSocket()</code> method
26  * will return with a <code>SocketException</code>.</li>
27  * <li>Any future call to the <code>add(Socket)</code> or <code>getSocket()</code>
28  * method will result in a <code>SocketException</code>.</li>
29  * <li></li>
30  * </ul><p>
31  *
32  * @author <a HREF="mailto:jc@sapia-oss.org">Jean-Cedric Desrochers</a>
33  * <dl>
34  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">
35  * Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
36  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
37  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a>
38  * at the Sapia OSS web site</dd></dt>
39  * </dl>
40  */

41 public class SocketQueue {
42   /** The list that act as a queue. */
43   private LinkedList JavaDoc _theSockets;
44
45   /** The close indicator of the queue. */
46   private boolean _isClosed;
47
48   /** The reference over the last exception that occured. */
49   private IOException JavaDoc _theLastException;
50
51   /**
52    * Creates a new SocketQueue instance.
53    */

54   public SocketQueue() {
55     _theSockets = new LinkedList JavaDoc();
56     _isClosed = false;
57   }
58
59   /**
60    * Asynchronously add the socket passed in to the internal queue.
61    *
62    * @param aSocket The socket to add.
63    */

64   public synchronized void add(Socket JavaDoc aSocket) throws SocketException JavaDoc {
65     if (_isClosed) {
66       throw new SocketException JavaDoc(
67         "Could not add socket - the socket queue is closed");
68     }
69
70     _theLastException = null;
71     _theSockets.addLast(aSocket);
72     notify();
73   }
74
75   /**
76    * Returns the next available socket. This method will block until
77    * a socket is available.
78    *
79    * @return The next available socket.
80    */

81   public synchronized Socket JavaDoc getSocket() throws IOException JavaDoc {
82     if (_isClosed) {
83       throw new SocketException JavaDoc(
84         "No socket available - the socket queue is closed");
85     } else if (_theSockets.isEmpty()) {
86       try {
87         wait();
88       } catch (InterruptedException JavaDoc ie) {
89         // noop
90
}
91     }
92
93     if (_isClosed) {
94       throw new SocketException JavaDoc(
95         "No socket available - the socket queue is closed");
96     } else if (_theLastException != null) {
97       IOException JavaDoc ioe = _theLastException;
98       _theLastException = null;
99       throw ioe;
100     } else if (_theSockets.isEmpty()) {
101       return getSocket();
102     } else {
103       return (Socket JavaDoc) _theSockets.removeFirst();
104     }
105   }
106
107   /**
108    * Close this queue.
109    */

110   public synchronized void close() {
111     _isClosed = true;
112
113     for (Iterator JavaDoc it = _theSockets.iterator(); it.hasNext();) {
114       Socket JavaDoc socket = (Socket JavaDoc) it.next();
115
116       try {
117         socket.close();
118       } catch (IOException JavaDoc ioe) {
119         // noop
120
}
121     }
122
123     _theSockets.clear();
124     notifyAll();
125   }
126
127   /**
128    * Sets the exception to this queue.
129    *
130    * @param anException The exception to send to the client.
131    */

132   public synchronized void setException(IOException JavaDoc anException) {
133     _theLastException = anException;
134     notify();
135   }
136
137   /**
138    * Returns the current exception of this queue.
139    *
140    * @return The current exception of this queue.
141    */

142   public IOException JavaDoc getException() {
143     return _theLastException;
144   }
145
146   /**
147    * Returns the number of socket pending in the internal queue.
148    *
149    * @return The number of socket pending in the internal queue.
150    */

151   public int size() {
152     return _theSockets.size();
153   }
154 }
155
Popular Tags